Refactor ProxyWarden routing and settings flow

This commit is contained in:
2026-07-09 11:51:16 +03:00
parent db0c1dede9
commit 1bb795a532
18 changed files with 1018 additions and 210 deletions

View File

@@ -39,6 +39,7 @@ import {
} from '../api/tauriCommands';
import type { ComponentStatus, Profile, ProfileItemInput, ProfileItemType, SubscriptionServer, Target } from '../domain/types';
import { BusyRing, Button, DetailsPopover, IconButton, LogDock, ServiceControlRow, Tabs } from '../ui';
import { parseProxy, type ParsedProxy } from './lib/parseProxy';
import { getApplyReadiness } from './readiness';
import { serviceControlState } from './viewModel';
@@ -1641,12 +1642,6 @@ export function App() {
);
}
interface ParsedProxy {
protocol: 'socks5';
host: string;
port: number;
}
interface SummaryStateInput {
isLoading: boolean;
isDetectingComponents: boolean;
@@ -2337,35 +2332,6 @@ function changesApplyButtonLabel(
return 'Применить изменения';
}
function parseProxy(rawValue: string): ParsedProxy {
const value = rawValue.trim();
if (!value) throw new Error('Введи адрес прокси.');
const withProtocol = /^[a-z][a-z0-9+.-]*:\/\//i.test(value) ? value : `socks5://${value}`;
let parsed: URL;
try {
parsed = new URL(withProtocol);
} catch {
throw new Error('Формат: socks5://host:port или host:port.');
}
const protocol = parsed.protocol.replace(':', '').toLowerCase();
if (protocol !== 'socks5') {
throw new Error('Сейчас поддерживается только SOCKS5.');
}
if (parsed.username || parsed.password) {
throw new Error('Прокси с логином и паролем пока не поддерживаются.');
}
const host = parsed.hostname.replace(/^\[|\]$/g, '');
const port = Number(parsed.port);
if (!host || !Number.isInteger(port) || port < 1 || port > 65535) {
throw new Error('Укажи хост и порт прокси.');
}
return { protocol: 'socks5', host, port };
}
function routeProxyCheckTarget(
routeMode: RouteMode,
proxyInput: string,

View File

@@ -0,0 +1,43 @@
import { describe, expect, it } from 'vitest';
import { parseProxy } from './parseProxy';
describe('parseProxy', () => {
it('parses host and port without explicit protocol', () => {
expect(parseProxy('proxy.example.test:1080')).toEqual({
protocol: 'socks5',
host: 'proxy.example.test',
port: 1080,
});
});
it('parses socks5 URLs', () => {
expect(parseProxy('socks5://127.0.0.1:1080')).toEqual({
protocol: 'socks5',
host: '127.0.0.1',
port: 1080,
});
});
it('parses bracketed IPv6 hosts', () => {
expect(parseProxy('socks5://[::1]:1080')).toEqual({
protocol: 'socks5',
host: '::1',
port: 1080,
});
});
it('rejects unsupported schemes', () => {
expect(() => parseProxy('http://proxy.example.test:8080')).toThrow('SOCKS5');
});
it('rejects missing or invalid ports', () => {
expect(() => parseProxy('proxy.example.test')).toThrow('хост и порт');
expect(() => parseProxy('proxy.example.test:70000')).toThrow('Формат');
});
it('rejects userinfo credentials', () => {
expect(() => parseProxy('socks5://user:password@proxy.example.test:1080')).toThrow(
'логином и паролем',
);
});
});

34
src/app/lib/parseProxy.ts Normal file
View File

@@ -0,0 +1,34 @@
export interface ParsedProxy {
protocol: 'socks5';
host: string;
port: number;
}
export function parseProxy(rawValue: string): ParsedProxy {
const value = rawValue.trim();
if (!value) throw new Error('Введи адрес прокси.');
const withProtocol = /^[a-z][a-z0-9+.-]*:\/\//i.test(value) ? value : `socks5://${value}`;
let parsed: URL;
try {
parsed = new URL(withProtocol);
} catch {
throw new Error('Формат: socks5://host:port или host:port.');
}
const protocol = parsed.protocol.replace(':', '').toLowerCase();
if (protocol !== 'socks5') {
throw new Error('Сейчас поддерживается только SOCKS5.');
}
if (parsed.username || parsed.password) {
throw new Error('Прокси с логином и паролем пока не поддерживаются.');
}
const host = parsed.hostname.replace(/^\[|\]$/g, '');
const port = Number(parsed.port);
if (!host || !Number.isInteger(port) || port < 1 || port > 65535) {
throw new Error('Укажи хост и порт прокси.');
}
return { protocol: 'socks5', host, port };
}