Repository navigation
Expand file tree
/
Copy pathproxyService.ts
More file actions
163 lines (147 loc) · 5.01 KB
/
Copy pathproxyService.ts
File metadata and controls
163 lines (147 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { invoke } from "@tauri-apps/api/core";
import logger from "@/utils/logger";
/** 代理协议类型 */
export type ProxyType = "http" | "socks5";
/** 代理配置:`enabled` 关闭时其余字段仅作草稿保留。 */
export interface ProxyConfig {
enabled: boolean;
type: ProxyType;
host: string;
port: number;
/** HTTP 与 SOCKS5 都可选(SOCKS5 认证用)。 */
username?: string;
/** SOCKS5 认证密码;HTTP 代理的密码同样可选。 */
password?: string;
/** 仅 SOCKS5:把域名交给代理解析(`socks5h://`),避免 DNS 污染。 */
remoteDns?: boolean;
/** 代理排除列表:逗号分隔的主机名,匹配这些主机的请求直连不走代理。 */
exclude?: string;
}
/** 系统代理读取结果 */
export interface SystemProxyInfo {
enabled: boolean;
proxy_type: ProxyType;
host: string;
port: number;
username: string;
password: string;
/** Windows PAC / 自动配置地址,可能为空。 */
pac_url: string;
/** 是否来自环境变量而非系统设置。 */
from_env: boolean;
}
/** 代理连通性测试结果 */
export interface ProxyTestResult {
status: number;
duration_ms: number;
}
export const DEFAULT_PROXY_CONFIG: ProxyConfig = {
enabled: false,
type: "http",
host: "",
port: 8080,
};
export const DEFAULT_PROXY_PORT: Record<ProxyType, number> = {
http: 8080,
socks5: 1080,
};
/** 代理配置的显式失败返回:`ok` 为 false 时 `error` 一定有值。 */
export type ProxyResult<T> =
| { ok: true; value: T }
| { ok: false; error: string };
function fail<T>(error: unknown): ProxyResult<T> {
const message = error instanceof Error ? error.message : String(error);
logger.warn(`[proxyService] ${message}`, "ProxyService");
return { ok: false, error: message };
}
/** 把任意输入收敛成合法的 [`ProxyConfig`]。 */
export function normalizeProxyConfig(input: unknown): ProxyConfig {
const raw = (input ?? {}) as Partial<ProxyConfig> & Record<string, unknown>;
const type: ProxyType = raw.type === "socks5" ? "socks5" : "http";
const port = Number(raw.port);
return {
enabled: Boolean(raw.enabled),
type,
host: typeof raw.host === "string" ? raw.host.trim() : "",
port: Number.isFinite(port) && port > 0 && port <= 65535 ? Math.trunc(port) : DEFAULT_PROXY_PORT[type],
username: typeof raw.username === "string" ? raw.username : "",
password: typeof raw.password === "string" ? raw.password : "",
// HTTP 代理没有这个概念,统一收敛成 false,避免切回 SOCKS5 时意外继承。
remoteDns: type === "socks5" && Boolean(raw.remoteDns),
/** 排除列表:逗号分隔,空值兜底为空字符串。 */
exclude: typeof raw.exclude === "string" ? raw.exclude.trim() : "",
};
}
/** 读取已保存的代理配置。 */
export async function getProxySettings(): Promise<ProxyResult<ProxyConfig>> {
try {
const value = await invoke<unknown>("get_proxy_settings");
return { ok: true, value: normalizeProxyConfig(value) };
} catch (error) {
return fail(error);
}
}
/** 保存代理配置(后端会立即刷新全局代理)。 */
export async function saveProxySettings(
config: ProxyConfig,
): Promise<ProxyResult<ProxyConfig>> {
try {
const value = await invoke<unknown>("save_proxy_settings", {
config,
});
return { ok: true, value: normalizeProxyConfig(value) };
} catch (error) {
return fail(error);
}
}
/** 清除代理配置,恢复「不使用代理」。 */
export async function clearProxySettings(): Promise<ProxyResult<boolean>> {
try {
const removed = await invoke<boolean>("clear_proxy_settings");
return { ok: true, value: removed };
} catch (error) {
return fail(error);
}
}
/** 读取系统代理(Windows 注册表优先,其次环境变量)。 */
export async function readSystemProxy(): Promise<ProxyResult<SystemProxyInfo>> {
try {
const info = await invoke<SystemProxyInfo>("read_system_proxy");
return {
ok: true,
value: {
...info,
proxy_type: info.proxy_type === "socks5" ? "socks5" : "http",
username: info.username ?? "",
password: info.password ?? "",
pac_url: info.pac_url ?? "",
},
};
} catch (error) {
return fail(error);
}
}
/** 测试代理连通性;不传 `config` 时后端使用已保存的配置。 */
export async function testProxyConnection(
config?: ProxyConfig,
url?: string,
): Promise<ProxyResult<ProxyTestResult>> {
try {
const value = await invoke<ProxyTestResult>("test_proxy_connection", {
config: config ?? null,
url: url ?? null,
});
return { ok: true, value };
} catch (error) {
return fail(error);
}
}
/** 自动嗅探本地常用代理端口,返回可用的配置(包括协议与端口)。 */
export async function autoDetectProxy(): Promise<ProxyResult<ProxyConfig>> {
try {
const value = await invoke<ProxyConfig>("auto_detect_proxy");
return { ok: true, value: normalizeProxyConfig(value) };
} catch (error) {
return fail(error);
}
}