@eum-agent/network-proxy 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +459 -0
- package/package.json +49 -3
- package/ui/ui.js +528 -0
- package/README.md +0 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
5
|
+
import { BlockList, isIP } from "node:net";
|
|
6
|
+
//#region src/bypass-schema.ts
|
|
7
|
+
/** Supported bypass syntax, shared by persisted and discovered routes. */
|
|
8
|
+
const ProxyBypassSchema = z.string().max(4096).refine((value) => value.split(",").every((raw) => {
|
|
9
|
+
const rule = raw.trim().toLowerCase();
|
|
10
|
+
if (!rule || rule === "*" || rule === "<local>") return true;
|
|
11
|
+
if (rule.includes("/")) {
|
|
12
|
+
const parts = rule.split("/");
|
|
13
|
+
if (parts.length !== 2 || !/^\d+$/.test(parts[1] ?? "")) return false;
|
|
14
|
+
let address = parts[0] ?? "";
|
|
15
|
+
if (/^(?:\d{1,3}\.){1,2}\d{1,3}$/.test(address)) address = address.split(".").concat(Array(4 - address.split(".").length).fill("0")).join(".");
|
|
16
|
+
const bits = Number(parts[1]);
|
|
17
|
+
return z.ipv4().safeParse(address).success && bits <= 32 || z.ipv6().safeParse(address).success && bits <= 128;
|
|
18
|
+
}
|
|
19
|
+
if (z.ipv6().safeParse(rule).success) return true;
|
|
20
|
+
const parsed = rule.match(/^(\[[^\]]+\]|(?:\*\.)?\.?[^\s:/?@#*<>]+)(?::(\d+))?$/);
|
|
21
|
+
if (!parsed || parsed[2] && (Number(parsed[2]) < 1 || Number(parsed[2]) > 65535)) return false;
|
|
22
|
+
try {
|
|
23
|
+
return !!new URL(`http://${parsed[1].replace(/^\*?\./, "")}`).hostname;
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}), "proxy-bypass-unsupported");
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/windows-proxy.ts
|
|
30
|
+
/** Converts the supported fixed Windows grammar without widening bypass matching. */
|
|
31
|
+
function windowsProxy(snapshot) {
|
|
32
|
+
if (snapshot.autoDetect || snapshot.autoConfigUrl.trim()) throw new Error("proxy-pac-unsupported");
|
|
33
|
+
const endpoint = (value) => {
|
|
34
|
+
if (!value || /[\s;,]/.test(value)) throw new Error("invalid-proxy");
|
|
35
|
+
let url;
|
|
36
|
+
try {
|
|
37
|
+
url = new URL(value.includes("://") ? value : `http://${value}`);
|
|
38
|
+
} catch {
|
|
39
|
+
throw new Error("invalid-proxy");
|
|
40
|
+
}
|
|
41
|
+
if (url.username || url.password) throw new Error("proxy-credentials-unsupported");
|
|
42
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") throw new Error("proxy-protocol-unsupported");
|
|
43
|
+
if (url.pathname !== "/" || url.search || url.hash || url.port === "0") throw new Error("invalid-proxy");
|
|
44
|
+
return url.href;
|
|
45
|
+
};
|
|
46
|
+
const value = snapshot.proxy.trim();
|
|
47
|
+
let httpProxy;
|
|
48
|
+
let httpsProxy;
|
|
49
|
+
if (value.includes("=")) {
|
|
50
|
+
const seen = /* @__PURE__ */ new Set();
|
|
51
|
+
for (const item of value.split(/[;\s]+/)) {
|
|
52
|
+
if (!item) throw new Error("invalid-proxy");
|
|
53
|
+
const parts = item.split("=");
|
|
54
|
+
if (parts.length !== 2) throw new Error("invalid-proxy");
|
|
55
|
+
const key = parts[0].toLowerCase();
|
|
56
|
+
if (seen.has(key)) throw new Error("invalid-proxy");
|
|
57
|
+
seen.add(key);
|
|
58
|
+
if (key !== "http" && key !== "https") throw new Error("proxy-protocol-unsupported");
|
|
59
|
+
if (key === "http") httpProxy = endpoint(parts[1]);
|
|
60
|
+
else httpsProxy = endpoint(parts[1]);
|
|
61
|
+
}
|
|
62
|
+
} else if (value) httpProxy = httpsProxy = endpoint(value);
|
|
63
|
+
const exceptions = snapshot.bypass.trim().split(/[;\s]+/).filter(Boolean);
|
|
64
|
+
for (const rule of exceptions) {
|
|
65
|
+
if (rule === "*" || rule.toLowerCase() === "<local>") continue;
|
|
66
|
+
if (rule.includes("*") || rule.startsWith(".") || rule.includes("/")) throw new Error("proxy-bypass-unsupported");
|
|
67
|
+
if (!ProxyBypassSchema.safeParse(rule).success) throw new Error("proxy-bypass-unsupported");
|
|
68
|
+
if (rule.includes(":") && !isIP(rule) && !/^\[[^\]]+\](?::\d+)?$/.test(rule)) {
|
|
69
|
+
if (!/^[^:]+:\d+$/.test(rule)) throw new Error("proxy-bypass-unsupported");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
source: "system",
|
|
74
|
+
...httpProxy ? { httpProxy } : {},
|
|
75
|
+
...httpsProxy ? { httpsProxy } : {},
|
|
76
|
+
noProxy: exceptions.join(",")
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/resolve.ts
|
|
81
|
+
/** Validates a fixed, unauthenticated HTTP proxy without exposing the input in errors. */
|
|
82
|
+
function proxyUrl(value) {
|
|
83
|
+
let url;
|
|
84
|
+
try {
|
|
85
|
+
url = new URL(value);
|
|
86
|
+
} catch {
|
|
87
|
+
throw new Error("invalid-proxy");
|
|
88
|
+
}
|
|
89
|
+
if (url.username || url.password) throw new Error("proxy-credentials-unsupported");
|
|
90
|
+
if (!["http:", "https:"].includes(url.protocol)) throw new Error("proxy-protocol-unsupported");
|
|
91
|
+
if (url.search || url.hash || url.pathname !== "/" || url.port === "0") throw new Error("invalid-proxy");
|
|
92
|
+
return url.href;
|
|
93
|
+
}
|
|
94
|
+
function bypassRules(value) {
|
|
95
|
+
if (!ProxyBypassSchema.safeParse(value).success) throw new Error("proxy-bypass-unsupported");
|
|
96
|
+
return value;
|
|
97
|
+
}
|
|
98
|
+
/** Resolves fixed proxy settings without contacting any endpoint. */
|
|
99
|
+
function resolveProxy(settings, env, system, platform = process.platform) {
|
|
100
|
+
if (settings.mode === "direct") return {
|
|
101
|
+
source: "direct",
|
|
102
|
+
noProxy: ""
|
|
103
|
+
};
|
|
104
|
+
if (settings.mode === "manual") {
|
|
105
|
+
const url = proxyUrl(settings.url);
|
|
106
|
+
return {
|
|
107
|
+
source: "manual",
|
|
108
|
+
httpProxy: url,
|
|
109
|
+
httpsProxy: url,
|
|
110
|
+
noProxy: bypassRules(settings.noProxy)
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const value = (name) => {
|
|
114
|
+
if (platform !== "win32") return env[name.toLowerCase()]?.trim() || env[name]?.trim() || void 0;
|
|
115
|
+
const matches = Object.entries(env).filter(([key]) => key.toUpperCase() === name).map(([, entry]) => entry?.trim()).filter((entry) => !!entry);
|
|
116
|
+
if (new Set(matches).size > 1) throw new Error("invalid-proxy");
|
|
117
|
+
return matches[0];
|
|
118
|
+
};
|
|
119
|
+
const http = value("HTTP_PROXY"), https = value("HTTPS_PROXY"), all = value("ALL_PROXY");
|
|
120
|
+
if (http || https || all) {
|
|
121
|
+
const httpProxy = http || all, httpsProxy = https || all;
|
|
122
|
+
return {
|
|
123
|
+
source: "environment",
|
|
124
|
+
...httpProxy ? { httpProxy: proxyUrl(httpProxy) } : {},
|
|
125
|
+
...httpsProxy ? { httpsProxy: proxyUrl(httpsProxy) } : {},
|
|
126
|
+
noProxy: bypassRules(value("NO_PROXY") ?? "")
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (system === void 0) throw new Error("system-proxy-unavailable");
|
|
130
|
+
if (typeof system !== "string") return windowsProxy(system);
|
|
131
|
+
if (!system) return {
|
|
132
|
+
source: "system",
|
|
133
|
+
noProxy: ""
|
|
134
|
+
};
|
|
135
|
+
const field = (name) => system.match(new RegExp(`^\\s*${name}\\s*:\\s*(.*?)\\s*$`, "m"))?.[1];
|
|
136
|
+
if (!system.startsWith("<dictionary>")) throw new Error("system-proxy-unavailable");
|
|
137
|
+
if (field("ProxyAutoConfigEnable") === "1" || field("ProxyAutoDiscoveryEnable") === "1") throw new Error("proxy-pac-unsupported");
|
|
138
|
+
const endpoint = (name) => {
|
|
139
|
+
if (field(`${name}Enable`) !== "1") return void 0;
|
|
140
|
+
const host = field(`${name}Proxy`), port = Number(field(`${name}Port`));
|
|
141
|
+
if (!host || !Number.isInteger(port) || port < 1 || port > 65535) throw new Error("invalid-proxy");
|
|
142
|
+
return proxyUrl(`http://${isIP(host) === 6 ? `[${host}]` : host}:${port}`);
|
|
143
|
+
};
|
|
144
|
+
const httpProxy = endpoint("HTTP"), httpsProxy = endpoint("HTTPS");
|
|
145
|
+
if (!httpProxy && !httpsProxy && field("SOCKSEnable") === "1") throw new Error("proxy-protocol-unsupported");
|
|
146
|
+
const noProxy = [...(system.match(/ExceptionsList\s*:\s*<array>\s*\{([^}]*)\}/)?.[1] ?? "").matchAll(/^\s*\d+\s*:\s*(.*?)\s*$/gm)].map((row) => row[1]);
|
|
147
|
+
if (field("ExcludeSimpleHostnames") === "1") noProxy.push("<local>");
|
|
148
|
+
return {
|
|
149
|
+
source: "system",
|
|
150
|
+
...httpProxy ? { httpProxy } : {},
|
|
151
|
+
...httpsProxy ? { httpsProxy } : {},
|
|
152
|
+
noProxy: bypassRules(noProxy.join(","))
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/** Produces stable identities for approval and connection snapshots, never raw environment. */
|
|
156
|
+
function proxyRevision(value) {
|
|
157
|
+
return createHash("sha256").update(JSON.stringify(value)).digest("hex").slice(0, 24);
|
|
158
|
+
}
|
|
159
|
+
/** Tests a URL against explicit bypass patterns, without DNS lookup or authorization. */
|
|
160
|
+
function bypassesProxy(url, patterns) {
|
|
161
|
+
const host = url.hostname.toLowerCase().replace(/^\[|\]$/g, "").replace(/\.$/, "");
|
|
162
|
+
const port = url.port || (url.protocol === "https:" ? "443" : "80");
|
|
163
|
+
return patterns.split(",").some((raw) => {
|
|
164
|
+
const rule = raw.trim().toLowerCase();
|
|
165
|
+
if (!rule) return false;
|
|
166
|
+
if (rule === "*") return true;
|
|
167
|
+
if (rule === "<local>") return !host.includes(".") && !host.includes(":");
|
|
168
|
+
if (rule.includes("/")) {
|
|
169
|
+
let [address, bits] = rule.split("/");
|
|
170
|
+
if (address && /^(?:\d{1,3}\.){1,2}\d{1,3}$/.test(address)) address = address.split(".").concat(Array(4 - address.split(".").length).fill("0")).join(".");
|
|
171
|
+
if (!address || !bits || !isIP(address) || !isIP(host)) return false;
|
|
172
|
+
try {
|
|
173
|
+
const block = new BlockList();
|
|
174
|
+
block.addSubnet(address, Number(bits), isIP(address) === 6 ? "ipv6" : "ipv4");
|
|
175
|
+
return block.check(host, isIP(host) === 6 ? "ipv6" : "ipv4");
|
|
176
|
+
} catch {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (isIP(rule)) return host === (isIP(rule) === 6 ? new URL(`http://[${rule}]`).hostname.slice(1, -1) : rule);
|
|
181
|
+
const parsed = rule.match(/^(\[[^\]]+\]|[^:]+)(?::(\d+))?$/);
|
|
182
|
+
if (!parsed || parsed[2] && parsed[2] !== port) return false;
|
|
183
|
+
const name = parsed[1].replace(/^\[|\]$/g, "").replace(/\.$/, "");
|
|
184
|
+
if (name.startsWith(".") || name.startsWith("*.")) {
|
|
185
|
+
const suffix = name.replace(/^\*?\./, "");
|
|
186
|
+
return host === suffix || host.endsWith(`.${suffix}`);
|
|
187
|
+
}
|
|
188
|
+
return host === name;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
/** Creates consistent upper/lowercase child variables, or no variables for direct routes. */
|
|
192
|
+
function proxyEnvironment(proxy, platform = process.platform) {
|
|
193
|
+
if (proxy.error) throw new Error(proxy.error);
|
|
194
|
+
const env = {};
|
|
195
|
+
const put = (name, value) => {
|
|
196
|
+
if (value !== void 0) {
|
|
197
|
+
env[name] = value;
|
|
198
|
+
if (platform !== "win32") env[name.toLowerCase()] = value;
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
put("HTTP_PROXY", proxy.httpProxy);
|
|
202
|
+
put("HTTPS_PROXY", proxy.httpsProxy);
|
|
203
|
+
if (proxy.httpProxy && proxy.httpProxy === proxy.httpsProxy) put("ALL_PROXY", proxy.httpProxy);
|
|
204
|
+
if (proxy.httpProxy || proxy.httpsProxy) put("NO_PROXY", proxy.noProxy);
|
|
205
|
+
return env;
|
|
206
|
+
}
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/system.ts
|
|
209
|
+
/** GNOME fixed proxy settings; absence of a desktop settings reader means no OS proxy. */
|
|
210
|
+
async function readLinuxProxy(host) {
|
|
211
|
+
const execute = async (command, args) => {
|
|
212
|
+
const result = await host.execute(command, args);
|
|
213
|
+
if (!result.ok) throw Object.assign(/* @__PURE__ */ new Error("system-proxy-unavailable"), { code: result.missing ? "ENOENT" : void 0 });
|
|
214
|
+
return result;
|
|
215
|
+
};
|
|
216
|
+
const get = async (schema, key) => (await execute("/usr/bin/gsettings", [
|
|
217
|
+
"get",
|
|
218
|
+
schema,
|
|
219
|
+
key
|
|
220
|
+
])).stdout.trim();
|
|
221
|
+
let mode;
|
|
222
|
+
try {
|
|
223
|
+
if (!(await execute("/usr/bin/gsettings", ["list-schemas"])).stdout.split(/\s+/).includes("org.gnome.system.proxy")) return "";
|
|
224
|
+
mode = await get("org.gnome.system.proxy", "mode");
|
|
225
|
+
} catch (error) {
|
|
226
|
+
if (error.code === "ENOENT") return "";
|
|
227
|
+
throw new Error("system-proxy-unavailable");
|
|
228
|
+
}
|
|
229
|
+
if (mode === "'none'") return "";
|
|
230
|
+
if (mode === "'auto'") throw new Error("proxy-pac-unsupported");
|
|
231
|
+
if (mode !== "'manual'") throw new Error("system-proxy-unavailable");
|
|
232
|
+
const [http, httpPort, https, httpsPort, socks, same, bypass, authentication] = await Promise.all([
|
|
233
|
+
get("org.gnome.system.proxy.http", "host"),
|
|
234
|
+
get("org.gnome.system.proxy.http", "port"),
|
|
235
|
+
get("org.gnome.system.proxy.https", "host"),
|
|
236
|
+
get("org.gnome.system.proxy.https", "port"),
|
|
237
|
+
get("org.gnome.system.proxy.socks", "host"),
|
|
238
|
+
get("org.gnome.system.proxy", "use-same-proxy"),
|
|
239
|
+
get("org.gnome.system.proxy", "ignore-hosts"),
|
|
240
|
+
get("org.gnome.system.proxy.http", "use-authentication")
|
|
241
|
+
]);
|
|
242
|
+
const string = (value) => {
|
|
243
|
+
if (!/^(['"])[^'"\\\n]*\1$/.test(value)) throw new Error("system-proxy-unavailable");
|
|
244
|
+
return value.slice(1, -1);
|
|
245
|
+
};
|
|
246
|
+
if (![same, authentication].every((value) => value === "true" || value === "false")) throw new Error("system-proxy-unavailable");
|
|
247
|
+
const httpHost = string(http);
|
|
248
|
+
const httpsHost = same === "true" ? httpHost : string(https);
|
|
249
|
+
if (httpHost && authentication === "true") throw new Error("proxy-credentials-unsupported");
|
|
250
|
+
if (!httpHost && !httpsHost && string(socks)) throw new Error("proxy-protocol-unsupported");
|
|
251
|
+
const array = bypass.replace(/^@as /, "");
|
|
252
|
+
if (!array.startsWith("[") || !array.endsWith("]")) throw new Error("system-proxy-unavailable");
|
|
253
|
+
const entries = array.slice(1, -1).trim();
|
|
254
|
+
const exceptions = entries ? entries.split(",").map((entry) => string(entry.trim())) : [];
|
|
255
|
+
return `<dictionary> {\n HTTPEnable : ${httpHost ? 1 : 0}\n HTTPProxy : ${httpHost}\n HTTPPort : ${httpPort}\n HTTPSEnable : ${httpsHost ? 1 : 0}\n HTTPSProxy : ${httpsHost}\n HTTPSPort : ${same === "true" ? httpPort : httpsPort}\n ExceptionsList : <array> {\n${exceptions.map((host, index) => `${index} : ${host}`).join("\n")}\n }\n}`;
|
|
256
|
+
}
|
|
257
|
+
/** Reads the machine's fixed proxy settings with bounded output and duration. */
|
|
258
|
+
async function readSystemProxy(host) {
|
|
259
|
+
if (host.platform === "win32") return host.windows();
|
|
260
|
+
if (host.platform === "linux") return readLinuxProxy(host);
|
|
261
|
+
if (host.platform !== "darwin") throw new Error("system-proxy-unsupported");
|
|
262
|
+
const result = await host.execute("/usr/sbin/scutil", ["--proxy"]);
|
|
263
|
+
if (!result.ok) throw new Error("system-proxy-unavailable");
|
|
264
|
+
return result.stdout;
|
|
265
|
+
}
|
|
266
|
+
//#endregion
|
|
267
|
+
//#region src/service.ts
|
|
268
|
+
/** Resolves this activation's policy without owning transport or connection pools. */
|
|
269
|
+
function createRoutingPolicy(settings, host, signal, options = {}) {
|
|
270
|
+
const env = { ...host.environment };
|
|
271
|
+
const epoch = randomUUID();
|
|
272
|
+
let generation = 0;
|
|
273
|
+
let observed;
|
|
274
|
+
let cached;
|
|
275
|
+
let reading;
|
|
276
|
+
const errors = /^(invalid-proxy|proxy-credentials-unsupported|proxy-protocol-unsupported|proxy-pac-unsupported|proxy-bypass-unsupported|system-proxy-unavailable|system-proxy-unsupported)$/;
|
|
277
|
+
const revision = () => `${epoch}:${generation}`;
|
|
278
|
+
async function system(refresh) {
|
|
279
|
+
if (refresh || !cached || cached.until < Date.now()) {
|
|
280
|
+
reading ??= (options.readSystem ?? (() => readSystemProxy(host)))().then((output) => {
|
|
281
|
+
cached = {
|
|
282
|
+
output,
|
|
283
|
+
until: Date.now() + 5e3
|
|
284
|
+
};
|
|
285
|
+
}, (error) => {
|
|
286
|
+
cached = {
|
|
287
|
+
error: error instanceof Error && errors.test(error.message) ? error.message : "system-proxy-unavailable",
|
|
288
|
+
until: Date.now() + 5e3
|
|
289
|
+
};
|
|
290
|
+
}).finally(() => {
|
|
291
|
+
reading = void 0;
|
|
292
|
+
});
|
|
293
|
+
await reading;
|
|
294
|
+
}
|
|
295
|
+
if (cached?.error) throw new Error(cached.error);
|
|
296
|
+
return cached?.output ?? "";
|
|
297
|
+
}
|
|
298
|
+
async function status(refresh = false) {
|
|
299
|
+
signal.throwIfAborted();
|
|
300
|
+
if (refresh) generation++;
|
|
301
|
+
const started = generation;
|
|
302
|
+
const saved = settings.read();
|
|
303
|
+
let resolved;
|
|
304
|
+
try {
|
|
305
|
+
try {
|
|
306
|
+
resolved = resolveProxy(saved, env, void 0, host.platform);
|
|
307
|
+
} catch (error) {
|
|
308
|
+
if (saved.mode !== "system" || !(error instanceof Error) || error.message !== "system-proxy-unavailable") throw error;
|
|
309
|
+
resolved = resolveProxy(saved, env, await system(refresh), host.platform);
|
|
310
|
+
}
|
|
311
|
+
} catch (error) {
|
|
312
|
+
resolved = {
|
|
313
|
+
source: saved.mode === "system" ? Object.entries(env).some(([key, value]) => /^(?:https?|all)_proxy$/i.test(key) && value?.trim()) ? "environment" : "system" : saved.mode,
|
|
314
|
+
noProxy: "",
|
|
315
|
+
error: error instanceof Error && errors.test(error.message) ? error.message : "invalid-proxy"
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
signal.throwIfAborted();
|
|
319
|
+
if (started !== generation) throw new Error("network-routing-changed");
|
|
320
|
+
const identity = proxyRevision(resolved);
|
|
321
|
+
if (observed !== void 0 && observed !== identity) generation++;
|
|
322
|
+
observed = identity;
|
|
323
|
+
return {
|
|
324
|
+
settings: saved,
|
|
325
|
+
effective: {
|
|
326
|
+
...resolved,
|
|
327
|
+
revision: revision()
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
signal,
|
|
333
|
+
get revision() {
|
|
334
|
+
return revision();
|
|
335
|
+
},
|
|
336
|
+
status,
|
|
337
|
+
async set(input) {
|
|
338
|
+
signal.throwIfAborted();
|
|
339
|
+
settings.write(input);
|
|
340
|
+
generation++;
|
|
341
|
+
observed = void 0;
|
|
342
|
+
return status();
|
|
343
|
+
},
|
|
344
|
+
async snapshot() {
|
|
345
|
+
const effective = (await status()).effective;
|
|
346
|
+
if (effective.error) throw new Error(effective.error);
|
|
347
|
+
return {
|
|
348
|
+
revision: effective.revision,
|
|
349
|
+
proxyFor(input) {
|
|
350
|
+
const target = new URL(input);
|
|
351
|
+
return bypassesProxy(target, effective.noProxy) ? void 0 : target.protocol === "https:" ? effective.httpsProxy : effective.httpProxy;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
},
|
|
355
|
+
async environment() {
|
|
356
|
+
const effective = (await status()).effective;
|
|
357
|
+
return {
|
|
358
|
+
revision: effective.revision,
|
|
359
|
+
env: proxyEnvironment(effective, host.platform)
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
//#endregion
|
|
365
|
+
//#region src/settings-schema.ts
|
|
366
|
+
/** Connection routing only; changing it never grants network access. */
|
|
367
|
+
const ProxySettingsSchema = z.discriminatedUnion("mode", [
|
|
368
|
+
z.object({ mode: z.literal("system") }).strict(),
|
|
369
|
+
z.object({ mode: z.literal("direct") }).strict(),
|
|
370
|
+
z.object({
|
|
371
|
+
mode: z.literal("manual"),
|
|
372
|
+
url: z.string().trim().min(1).max(2048).refine((value) => {
|
|
373
|
+
try {
|
|
374
|
+
const url = new URL(value);
|
|
375
|
+
return ["http:", "https:"].includes(url.protocol) && !url.username && !url.password && !url.search && !url.hash && url.pathname === "/" && url.port !== "0";
|
|
376
|
+
} catch {
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
}, "invalid-proxy"),
|
|
380
|
+
noProxy: ProxyBypassSchema.default("")
|
|
381
|
+
}).strict()
|
|
382
|
+
]);
|
|
383
|
+
/** Resolved routing without credentials or raw OS output. */
|
|
384
|
+
const EffectiveProxySchema = z.object({
|
|
385
|
+
source: z.enum([
|
|
386
|
+
"manual",
|
|
387
|
+
"direct",
|
|
388
|
+
"environment",
|
|
389
|
+
"system"
|
|
390
|
+
]),
|
|
391
|
+
httpProxy: z.string().optional(),
|
|
392
|
+
httpsProxy: z.string().optional(),
|
|
393
|
+
noProxy: z.string(),
|
|
394
|
+
revision: z.string(),
|
|
395
|
+
error: z.string().optional()
|
|
396
|
+
}).strict();
|
|
397
|
+
/** Authenticated settings response, including failures resolving system settings. */
|
|
398
|
+
const ProxyStatusSchema = z.object({
|
|
399
|
+
settings: ProxySettingsSchema,
|
|
400
|
+
effective: EffectiveProxySchema
|
|
401
|
+
}).strict();
|
|
402
|
+
//#endregion
|
|
403
|
+
//#region src/settings.ts
|
|
404
|
+
/** Persists validated routing preferences in the producing owner's cooperative storage. */
|
|
405
|
+
function createNetworkSettings(storage) {
|
|
406
|
+
const name = storage.tableName("routing_settings");
|
|
407
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) throw new Error("invalid routing table identity");
|
|
408
|
+
const table = `"${name}"`;
|
|
409
|
+
storage.db.exec(`CREATE TABLE IF NOT EXISTS ${table} (id INTEGER NOT NULL PRIMARY KEY CHECK(id=1), settings_json TEXT NOT NULL)`);
|
|
410
|
+
storage.db.prepare(`INSERT OR IGNORE INTO ${table}(id,settings_json) VALUES(1,?)`).run("{\"mode\":\"system\"}");
|
|
411
|
+
/** Reads validated preferences from owner storage; damaged stored data refuses activation. */
|
|
412
|
+
function read() {
|
|
413
|
+
const row = storage.db.prepare(`SELECT settings_json FROM ${table} WHERE id=1`).get();
|
|
414
|
+
return ProxySettingsSchema.parse(JSON.parse(row.settings_json));
|
|
415
|
+
}
|
|
416
|
+
read();
|
|
417
|
+
return {
|
|
418
|
+
read,
|
|
419
|
+
/** Commits validated preferences; any failure leaves the previous value intact. */
|
|
420
|
+
write(input) {
|
|
421
|
+
const next = ProxySettingsSchema.parse(input);
|
|
422
|
+
storage.db.prepare(`UPDATE ${table} SET settings_json=? WHERE id=1`).run(JSON.stringify(next));
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
//#endregion
|
|
427
|
+
//#region src/index.ts
|
|
428
|
+
/** Owns proxy preferences, routing strategy and browser management for one activation. */
|
|
429
|
+
const plugin = {
|
|
430
|
+
name: "@eum-agent/network-proxy",
|
|
431
|
+
requires: ["network-proxy-host"],
|
|
432
|
+
provides: ["network-routing"],
|
|
433
|
+
apply(ctx) {
|
|
434
|
+
const routing = createRoutingPolicy(createNetworkSettings(ctx.storage.open()), ctx.services.get("network-proxy-host"), ctx.signal);
|
|
435
|
+
ctx.services.provide("network-routing", routing);
|
|
436
|
+
ctx.client.registerMethod({
|
|
437
|
+
name: "proxyGet",
|
|
438
|
+
params: z.object({}).strict(),
|
|
439
|
+
result: ProxyStatusSchema,
|
|
440
|
+
handle: () => routing.status()
|
|
441
|
+
});
|
|
442
|
+
ctx.client.registerMethod({
|
|
443
|
+
name: "proxyRefresh",
|
|
444
|
+
params: z.object({}).strict(),
|
|
445
|
+
result: ProxyStatusSchema,
|
|
446
|
+
handle: () => routing.status(true)
|
|
447
|
+
});
|
|
448
|
+
ctx.client.registerMethod({
|
|
449
|
+
name: "proxySet",
|
|
450
|
+
params: ProxySettingsSchema,
|
|
451
|
+
result: ProxyStatusSchema,
|
|
452
|
+
handle: (input) => routing.set(input)
|
|
453
|
+
});
|
|
454
|
+
const module = fileURLToPath(new URL("../ui/ui.js", import.meta.url));
|
|
455
|
+
if (existsSync(module)) ctx.client.registerUi({ module });
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
//#endregion
|
|
459
|
+
export { plugin as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eum-agent/network-proxy",
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
"displayName": {
|
|
4
|
+
"en-US": "Network Proxy",
|
|
5
|
+
"zh-CN": "网络代理"
|
|
6
|
+
},
|
|
7
|
+
"version": "1.0.0",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"description": "Outbound proxy routing and preferences",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"ui"
|
|
19
|
+
],
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"zod": "^4.4.3"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@eum-agent/plugin": "1.0.0",
|
|
25
|
+
"@types/react": "^19.2.18",
|
|
26
|
+
"@types/react-dom": "^19.2.5",
|
|
27
|
+
"react": "^19.2.0",
|
|
28
|
+
"react-dom": "^19.2.0",
|
|
29
|
+
"vite": "^8.2.2",
|
|
30
|
+
"@testing-library/react": "^16.3.3",
|
|
31
|
+
"@testing-library/user-event": "^14.6.6",
|
|
32
|
+
"lucide-react": "^1.40.0",
|
|
33
|
+
"i18next": "^26.4.1"
|
|
34
|
+
},
|
|
35
|
+
"yaoguang": {
|
|
36
|
+
"kind": "extension",
|
|
37
|
+
"description": {
|
|
38
|
+
"en-US": "Outbound proxy routing and preferences",
|
|
39
|
+
"zh-CN": "外部连接与子进程的代理设置"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {},
|
|
42
|
+
"foundationDependencies": {
|
|
43
|
+
"@eum-agent/infra-local": "1.0.0",
|
|
44
|
+
"@eum-agent/storage-sqlite": "1.0.0"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsdown && vite build",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.ui.json"
|
|
51
|
+
}
|
|
6
52
|
}
|
package/ui/ui.js
ADDED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
import { Button, Field, FormFailure, Input, Panel, Segmented, Tooltip, TooltipContent, TooltipTrigger, failureText, useGateway, useTranslation } from "@eum-agent/host/framework";
|
|
2
|
+
import { createContext, createElement, forwardRef, useContext, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
//#region ../../../node_modules/.pnpm/lucide-react@1.40.0_react@19.2.8/node_modules/lucide-react/dist/esm/shared/src/utils/mergeClasses.mjs
|
|
5
|
+
/**
|
|
6
|
+
* @license lucide-react v1.40.0 - ISC
|
|
7
|
+
*
|
|
8
|
+
* This source code is licensed under the ISC license.
|
|
9
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
10
|
+
*/
|
|
11
|
+
var mergeClasses = (...classes) => classes.filter((className, index, array) => {
|
|
12
|
+
return Boolean(className) && className.trim() !== "" && array.indexOf(className) === index;
|
|
13
|
+
}).join(" ").trim();
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region ../../../node_modules/.pnpm/lucide-react@1.40.0_react@19.2.8/node_modules/lucide-react/dist/esm/shared/src/utils/toKebabCase.mjs
|
|
16
|
+
/**
|
|
17
|
+
* @license lucide-react v1.40.0 - ISC
|
|
18
|
+
*
|
|
19
|
+
* This source code is licensed under the ISC license.
|
|
20
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
21
|
+
*/
|
|
22
|
+
var toKebabCase = (string) => string.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region ../../../node_modules/.pnpm/lucide-react@1.40.0_react@19.2.8/node_modules/lucide-react/dist/esm/shared/src/utils/toCamelCase.mjs
|
|
25
|
+
/**
|
|
26
|
+
* @license lucide-react v1.40.0 - ISC
|
|
27
|
+
*
|
|
28
|
+
* This source code is licensed under the ISC license.
|
|
29
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
30
|
+
*/
|
|
31
|
+
var toCamelCase = (string) => string.replace(/^([A-Z])|[\s-_]+(\w)/g, (match, p1, p2) => p2 ? p2.toUpperCase() : p1.toLowerCase());
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region ../../../node_modules/.pnpm/lucide-react@1.40.0_react@19.2.8/node_modules/lucide-react/dist/esm/shared/src/utils/toPascalCase.mjs
|
|
34
|
+
/**
|
|
35
|
+
* @license lucide-react v1.40.0 - ISC
|
|
36
|
+
*
|
|
37
|
+
* This source code is licensed under the ISC license.
|
|
38
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
39
|
+
*/
|
|
40
|
+
var toPascalCase = (string) => {
|
|
41
|
+
const camelCase = toCamelCase(string);
|
|
42
|
+
return camelCase.charAt(0).toUpperCase() + camelCase.slice(1);
|
|
43
|
+
};
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region ../../../node_modules/.pnpm/lucide-react@1.40.0_react@19.2.8/node_modules/lucide-react/dist/esm/defaultAttributes.mjs
|
|
46
|
+
/**
|
|
47
|
+
* @license lucide-react v1.40.0 - ISC
|
|
48
|
+
*
|
|
49
|
+
* This source code is licensed under the ISC license.
|
|
50
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
51
|
+
*/
|
|
52
|
+
var defaultAttributes = {
|
|
53
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
54
|
+
width: 24,
|
|
55
|
+
height: 24,
|
|
56
|
+
viewBox: "0 0 24 24",
|
|
57
|
+
fill: "none",
|
|
58
|
+
stroke: "currentColor",
|
|
59
|
+
strokeWidth: 2,
|
|
60
|
+
strokeLinecap: "round",
|
|
61
|
+
strokeLinejoin: "round"
|
|
62
|
+
};
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region ../../../node_modules/.pnpm/lucide-react@1.40.0_react@19.2.8/node_modules/lucide-react/dist/esm/shared/src/utils/hasA11yProp.mjs
|
|
65
|
+
/**
|
|
66
|
+
* @license lucide-react v1.40.0 - ISC
|
|
67
|
+
*
|
|
68
|
+
* This source code is licensed under the ISC license.
|
|
69
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
70
|
+
*/
|
|
71
|
+
var hasA11yProp = (props) => {
|
|
72
|
+
for (const prop in props) if (prop.startsWith("aria-") || prop === "role" || prop === "title") return true;
|
|
73
|
+
return false;
|
|
74
|
+
};
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region ../../../node_modules/.pnpm/lucide-react@1.40.0_react@19.2.8/node_modules/lucide-react/dist/esm/context.mjs
|
|
77
|
+
/**
|
|
78
|
+
* @license lucide-react v1.40.0 - ISC
|
|
79
|
+
*
|
|
80
|
+
* This source code is licensed under the ISC license.
|
|
81
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
82
|
+
*/
|
|
83
|
+
var LucideContext = createContext({});
|
|
84
|
+
var useLucideContext = () => useContext(LucideContext);
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region ../../../node_modules/.pnpm/lucide-react@1.40.0_react@19.2.8/node_modules/lucide-react/dist/esm/Icon.mjs
|
|
87
|
+
/**
|
|
88
|
+
* @license lucide-react v1.40.0 - ISC
|
|
89
|
+
*
|
|
90
|
+
* This source code is licensed under the ISC license.
|
|
91
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
92
|
+
*/
|
|
93
|
+
var Icon = forwardRef(({ color, size, strokeWidth, absoluteStrokeWidth, className = "", children, iconNode, ...rest }, ref) => {
|
|
94
|
+
const { size: contextSize = 24, strokeWidth: contextStrokeWidth = 2, absoluteStrokeWidth: contextAbsoluteStrokeWidth = false, color: contextColor = "currentColor", className: contextClass = "" } = useLucideContext() ?? {};
|
|
95
|
+
const calculatedStrokeWidth = absoluteStrokeWidth ?? contextAbsoluteStrokeWidth ? Number(strokeWidth ?? contextStrokeWidth) * 24 / Number(size ?? contextSize) : strokeWidth ?? contextStrokeWidth;
|
|
96
|
+
return createElement("svg", {
|
|
97
|
+
ref,
|
|
98
|
+
...defaultAttributes,
|
|
99
|
+
width: size ?? contextSize ?? defaultAttributes.width,
|
|
100
|
+
height: size ?? contextSize ?? defaultAttributes.height,
|
|
101
|
+
stroke: color ?? contextColor,
|
|
102
|
+
strokeWidth: calculatedStrokeWidth,
|
|
103
|
+
className: mergeClasses("lucide", contextClass, className),
|
|
104
|
+
...!children && !hasA11yProp(rest) && { "aria-hidden": "true" },
|
|
105
|
+
...rest
|
|
106
|
+
}, [...iconNode.map(([tag, attrs]) => createElement(tag, attrs)), ...Array.isArray(children) ? children : [children]]);
|
|
107
|
+
});
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region ../../../node_modules/.pnpm/lucide-react@1.40.0_react@19.2.8/node_modules/lucide-react/dist/esm/createLucideIcon.mjs
|
|
110
|
+
/**
|
|
111
|
+
* @license lucide-react v1.40.0 - ISC
|
|
112
|
+
*
|
|
113
|
+
* This source code is licensed under the ISC license.
|
|
114
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
115
|
+
*/
|
|
116
|
+
var createLucideIcon = (iconName, iconNode) => {
|
|
117
|
+
const Component = forwardRef(({ className, ...props }, ref) => createElement(Icon, {
|
|
118
|
+
ref,
|
|
119
|
+
iconNode,
|
|
120
|
+
className: mergeClasses(`lucide-${toKebabCase(toPascalCase(iconName))}`, `lucide-${iconName}`, className),
|
|
121
|
+
...props
|
|
122
|
+
}));
|
|
123
|
+
Component.displayName = toPascalCase(iconName);
|
|
124
|
+
return Component;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* @license lucide-react v1.40.0 - ISC
|
|
128
|
+
*
|
|
129
|
+
* This source code is licensed under the ISC license.
|
|
130
|
+
* See the LICENSE file in the root directory of this source tree.
|
|
131
|
+
*/
|
|
132
|
+
var RefreshCw = createLucideIcon("refresh-cw", [
|
|
133
|
+
["path", {
|
|
134
|
+
d: "M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8",
|
|
135
|
+
key: "v9h5vc"
|
|
136
|
+
}],
|
|
137
|
+
["path", {
|
|
138
|
+
d: "M21 3v5h-5",
|
|
139
|
+
key: "1q7to0"
|
|
140
|
+
}],
|
|
141
|
+
["path", {
|
|
142
|
+
d: "M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16",
|
|
143
|
+
key: "3uifl3"
|
|
144
|
+
}],
|
|
145
|
+
["path", {
|
|
146
|
+
d: "M8 16H3v5",
|
|
147
|
+
key: "1cv678"
|
|
148
|
+
}]
|
|
149
|
+
]);
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region ui-src/i18n.ts
|
|
152
|
+
/** Proxy browser strings share the host's language and React translation runtime. */
|
|
153
|
+
var translations;
|
|
154
|
+
/** Binds this activation's registered namespace without reconstructing the owner's namespace. */
|
|
155
|
+
function configureTranslations(ctx) {
|
|
156
|
+
translations = ctx.i18n;
|
|
157
|
+
}
|
|
158
|
+
/** Observes the shared language while resolving proxy-owned copy. */
|
|
159
|
+
function useTranslation$1() {
|
|
160
|
+
const shared = useTranslation();
|
|
161
|
+
if (!translations) throw new Error("proxy browser translations are not registered");
|
|
162
|
+
return {
|
|
163
|
+
...shared,
|
|
164
|
+
t: translations.t
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region ui-src/components/settings/network.tsx
|
|
169
|
+
/** Applies routing preferences on mode selection, field blur and explicit status refresh. */
|
|
170
|
+
function NetworkPanel({ api }) {
|
|
171
|
+
const client = useGateway(), { t } = useTranslation$1();
|
|
172
|
+
const [status, setStatus] = useState();
|
|
173
|
+
const [mode, setMode] = useState("system");
|
|
174
|
+
const [url, setUrl] = useState(""), [noProxy, setNoProxy] = useState("");
|
|
175
|
+
const [error, setError] = useState(""), [busy, setBusy] = useState(true);
|
|
176
|
+
const [online, setOnline] = useState(client.connection.status === "online");
|
|
177
|
+
const generation = useRef(0);
|
|
178
|
+
const refreshing = useRef(false);
|
|
179
|
+
const writes = useRef(Promise.resolve());
|
|
180
|
+
const submitted = useRef(null);
|
|
181
|
+
function accept(value) {
|
|
182
|
+
setStatus(value);
|
|
183
|
+
setMode(value.settings.mode);
|
|
184
|
+
if (value.settings.mode === "manual") {
|
|
185
|
+
setUrl(value.settings.url);
|
|
186
|
+
setNoProxy(value.settings.noProxy);
|
|
187
|
+
}
|
|
188
|
+
submitted.current = JSON.stringify(value.settings);
|
|
189
|
+
setError("");
|
|
190
|
+
}
|
|
191
|
+
useEffect(() => {
|
|
192
|
+
const read = () => {
|
|
193
|
+
const request = ++generation.current;
|
|
194
|
+
setBusy(true);
|
|
195
|
+
api.get().then((value) => {
|
|
196
|
+
if (request === generation.current) accept(value);
|
|
197
|
+
}, (failure) => {
|
|
198
|
+
if (request === generation.current) setError(failureText(failure, () => "") ?? "");
|
|
199
|
+
}).finally(() => {
|
|
200
|
+
if (request === generation.current) setBusy(false);
|
|
201
|
+
});
|
|
202
|
+
};
|
|
203
|
+
let previous = client.connection.status;
|
|
204
|
+
if (previous === "online") read();
|
|
205
|
+
const off = client.watchConnection((state) => {
|
|
206
|
+
setOnline(state.status === "online");
|
|
207
|
+
if (state.status === "online" && previous !== "online") read();
|
|
208
|
+
previous = state.status;
|
|
209
|
+
});
|
|
210
|
+
return () => {
|
|
211
|
+
generation.current++;
|
|
212
|
+
off();
|
|
213
|
+
};
|
|
214
|
+
}, [client, api]);
|
|
215
|
+
function apply(next) {
|
|
216
|
+
if (!online || !status) return;
|
|
217
|
+
const key = JSON.stringify(next);
|
|
218
|
+
if (submitted.current === key) return;
|
|
219
|
+
submitted.current = key;
|
|
220
|
+
const request = ++generation.current;
|
|
221
|
+
setBusy(true);
|
|
222
|
+
setError("");
|
|
223
|
+
writes.current = writes.current.then(async () => {
|
|
224
|
+
try {
|
|
225
|
+
const value = await api.set(next);
|
|
226
|
+
if (request === generation.current) setStatus(value);
|
|
227
|
+
} catch (failure) {
|
|
228
|
+
if (request === generation.current) {
|
|
229
|
+
submitted.current = null;
|
|
230
|
+
setError(failureText(failure, () => "") ?? "");
|
|
231
|
+
}
|
|
232
|
+
} finally {
|
|
233
|
+
if (request === generation.current) setBusy(false);
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
function applyManual() {
|
|
238
|
+
if (mode !== "manual") return;
|
|
239
|
+
const address = url.trim();
|
|
240
|
+
if (!address) {
|
|
241
|
+
setError(t("network.addressRequired"));
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
apply({
|
|
245
|
+
mode: "manual",
|
|
246
|
+
url: address,
|
|
247
|
+
noProxy: noProxy.trim()
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
function selectMode(next) {
|
|
251
|
+
setMode(next);
|
|
252
|
+
setError("");
|
|
253
|
+
if (next !== "manual") apply({ mode: next });
|
|
254
|
+
else if (url.trim()) apply({
|
|
255
|
+
mode: next,
|
|
256
|
+
url: url.trim(),
|
|
257
|
+
noProxy: noProxy.trim()
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
async function refresh() {
|
|
261
|
+
if (refreshing.current) return;
|
|
262
|
+
refreshing.current = true;
|
|
263
|
+
const request = ++generation.current;
|
|
264
|
+
setBusy(true);
|
|
265
|
+
setError("");
|
|
266
|
+
try {
|
|
267
|
+
const value = await api.refresh();
|
|
268
|
+
if (request === generation.current) {
|
|
269
|
+
if (status) {
|
|
270
|
+
setStatus(value);
|
|
271
|
+
submitted.current = JSON.stringify(value.settings);
|
|
272
|
+
} else accept(value);
|
|
273
|
+
}
|
|
274
|
+
} catch (failure) {
|
|
275
|
+
if (request === generation.current) setError(failureText(failure, () => "") ?? "");
|
|
276
|
+
} finally {
|
|
277
|
+
refreshing.current = false;
|
|
278
|
+
if (request === generation.current) setBusy(false);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const disabled = !status || !online;
|
|
282
|
+
const bypass = status?.effective.noProxy ? status.effective.noProxy.split(",").map((one) => one.trim()).filter(Boolean) : [];
|
|
283
|
+
return /* @__PURE__ */ jsxs(Panel, {
|
|
284
|
+
title: t("network.proxySettings"),
|
|
285
|
+
children: [/* @__PURE__ */ jsxs("form", {
|
|
286
|
+
className: "flex min-w-0 flex-col gap-5",
|
|
287
|
+
onSubmit: (event) => {
|
|
288
|
+
event.preventDefault();
|
|
289
|
+
applyManual();
|
|
290
|
+
},
|
|
291
|
+
children: [
|
|
292
|
+
/* @__PURE__ */ jsx(Panel, {
|
|
293
|
+
title: t("network.mode"),
|
|
294
|
+
level: "field",
|
|
295
|
+
inline: true,
|
|
296
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
297
|
+
className: "flex min-w-0 justify-end",
|
|
298
|
+
children: /* @__PURE__ */ jsx(Segmented, {
|
|
299
|
+
label: t("network.mode"),
|
|
300
|
+
value: mode,
|
|
301
|
+
disabled,
|
|
302
|
+
options: [
|
|
303
|
+
"system",
|
|
304
|
+
"manual",
|
|
305
|
+
"direct"
|
|
306
|
+
].map((value) => ({
|
|
307
|
+
value,
|
|
308
|
+
label: t(`network.${value}`)
|
|
309
|
+
})),
|
|
310
|
+
onSelect: selectMode
|
|
311
|
+
})
|
|
312
|
+
})
|
|
313
|
+
}),
|
|
314
|
+
mode === "manual" && /* @__PURE__ */ jsxs("div", {
|
|
315
|
+
className: "flex min-w-0 flex-col gap-5",
|
|
316
|
+
children: [/* @__PURE__ */ jsx(Field, {
|
|
317
|
+
label: t("network.url"),
|
|
318
|
+
help: t("network.support"),
|
|
319
|
+
required: true,
|
|
320
|
+
disabled,
|
|
321
|
+
children: (binding) => /* @__PURE__ */ jsx(Input, {
|
|
322
|
+
...binding,
|
|
323
|
+
value: url,
|
|
324
|
+
required: true,
|
|
325
|
+
maxLength: 2048,
|
|
326
|
+
placeholder: "http://127.0.0.1:10808",
|
|
327
|
+
disabled,
|
|
328
|
+
onChange: (event) => setUrl(event.target.value),
|
|
329
|
+
onBlur: applyManual,
|
|
330
|
+
onKeyDown: (event) => {
|
|
331
|
+
if (event.key === "Enter") {
|
|
332
|
+
event.preventDefault();
|
|
333
|
+
applyManual();
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
})
|
|
337
|
+
}), /* @__PURE__ */ jsx(Field, {
|
|
338
|
+
label: t("network.noProxy"),
|
|
339
|
+
disabled,
|
|
340
|
+
children: (binding) => /* @__PURE__ */ jsx(Input, {
|
|
341
|
+
...binding,
|
|
342
|
+
value: noProxy,
|
|
343
|
+
maxLength: 4096,
|
|
344
|
+
placeholder: "localhost,127.0.0.1",
|
|
345
|
+
disabled,
|
|
346
|
+
onChange: (event) => setNoProxy(event.target.value),
|
|
347
|
+
onBlur: applyManual,
|
|
348
|
+
onKeyDown: (event) => {
|
|
349
|
+
if (event.key === "Enter") {
|
|
350
|
+
event.preventDefault();
|
|
351
|
+
applyManual();
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
})
|
|
355
|
+
})]
|
|
356
|
+
}),
|
|
357
|
+
online && /* @__PURE__ */ jsx(FormFailure, { reason: error || null }),
|
|
358
|
+
error && online && status && /* @__PURE__ */ jsx(Button, {
|
|
359
|
+
variant: "link",
|
|
360
|
+
size: "sm",
|
|
361
|
+
type: "button",
|
|
362
|
+
className: "self-start px-0",
|
|
363
|
+
disabled: busy,
|
|
364
|
+
onClick: () => mode === "manual" ? applyManual() : apply({ mode }),
|
|
365
|
+
children: t("common.retry")
|
|
366
|
+
})
|
|
367
|
+
]
|
|
368
|
+
}), /* @__PURE__ */ jsxs("section", {
|
|
369
|
+
"aria-label": t("network.effective"),
|
|
370
|
+
"data-slot": "proxy-effective",
|
|
371
|
+
className: "flex min-w-0 flex-col gap-2 rounded-control bg-muted/60 px-3 py-2.5 text-xs leading-relaxed",
|
|
372
|
+
children: [/* @__PURE__ */ jsxs("header", {
|
|
373
|
+
className: "flex min-w-0 items-center gap-2",
|
|
374
|
+
children: [
|
|
375
|
+
/* @__PURE__ */ jsx("h4", {
|
|
376
|
+
className: "text-xs font-medium text-foreground",
|
|
377
|
+
children: t("network.effective")
|
|
378
|
+
}),
|
|
379
|
+
status && /* @__PURE__ */ jsxs("span", {
|
|
380
|
+
className: "text-xs text-muted-foreground",
|
|
381
|
+
children: ["· ", t(`network.${status.effective.source}`)]
|
|
382
|
+
}),
|
|
383
|
+
/* @__PURE__ */ jsx("span", {
|
|
384
|
+
role: "status",
|
|
385
|
+
className: "ms-auto text-2xs text-muted-foreground",
|
|
386
|
+
children: busy ? t("network.applying") : ""
|
|
387
|
+
}),
|
|
388
|
+
/* @__PURE__ */ jsxs(Tooltip, { children: [/* @__PURE__ */ jsx(TooltipTrigger, {
|
|
389
|
+
"aria-label": t("network.refresh"),
|
|
390
|
+
render: /* @__PURE__ */ jsx(Button, {
|
|
391
|
+
variant: "ghost",
|
|
392
|
+
size: "sm",
|
|
393
|
+
type: "button",
|
|
394
|
+
disabled: busy || !online,
|
|
395
|
+
onClick: () => void refresh(),
|
|
396
|
+
className: "aspect-square px-0 text-muted-foreground hover:text-foreground"
|
|
397
|
+
}),
|
|
398
|
+
children: /* @__PURE__ */ jsx(RefreshCw, {
|
|
399
|
+
"aria-hidden": "true",
|
|
400
|
+
className: "size-3.5",
|
|
401
|
+
strokeWidth: 1.75
|
|
402
|
+
})
|
|
403
|
+
}), /* @__PURE__ */ jsx(TooltipContent, {
|
|
404
|
+
side: "top",
|
|
405
|
+
sideOffset: 6,
|
|
406
|
+
children: t("network.refresh")
|
|
407
|
+
})] })
|
|
408
|
+
]
|
|
409
|
+
}), status ? /* @__PURE__ */ jsxs(Fragment, { children: [!status.effective.error && /* @__PURE__ */ jsxs("dl", {
|
|
410
|
+
className: "grid grid-cols-[4rem_minmax(0,1fr)] items-baseline gap-x-3 gap-y-1",
|
|
411
|
+
children: [
|
|
412
|
+
/* @__PURE__ */ jsx("dt", {
|
|
413
|
+
className: "text-muted-foreground",
|
|
414
|
+
children: "HTTP"
|
|
415
|
+
}),
|
|
416
|
+
/* @__PURE__ */ jsx("dd", {
|
|
417
|
+
className: "break-all font-mono",
|
|
418
|
+
children: status.effective.httpProxy ?? t("network.directValue")
|
|
419
|
+
}),
|
|
420
|
+
/* @__PURE__ */ jsx("dt", {
|
|
421
|
+
className: "text-muted-foreground",
|
|
422
|
+
children: "HTTPS"
|
|
423
|
+
}),
|
|
424
|
+
/* @__PURE__ */ jsx("dd", {
|
|
425
|
+
className: "break-all font-mono",
|
|
426
|
+
children: status.effective.httpsProxy ?? t("network.directValue")
|
|
427
|
+
}),
|
|
428
|
+
bypass.length > 0 && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("dt", {
|
|
429
|
+
className: "text-muted-foreground",
|
|
430
|
+
children: t("network.bypass")
|
|
431
|
+
}), /* @__PURE__ */ jsx("dd", { children: /* @__PURE__ */ jsx("ul", {
|
|
432
|
+
"aria-label": t("network.bypassDetails"),
|
|
433
|
+
className: "flex flex-col gap-1 font-mono",
|
|
434
|
+
children: bypass.map((host) => /* @__PURE__ */ jsx("li", {
|
|
435
|
+
className: "break-all",
|
|
436
|
+
children: host
|
|
437
|
+
}, host))
|
|
438
|
+
}) })] })
|
|
439
|
+
]
|
|
440
|
+
}), /* @__PURE__ */ jsx(FormFailure, { reason: status.effective.error ? `${t("network.unavailable")}: ${status.effective.error}` : null })] }) : /* @__PURE__ */ jsx("p", {
|
|
441
|
+
className: "text-muted-foreground",
|
|
442
|
+
children: t("network.notLoaded")
|
|
443
|
+
})]
|
|
444
|
+
})]
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
var en_US_default = {
|
|
448
|
+
network: {
|
|
449
|
+
"title": "Network",
|
|
450
|
+
"proxySettings": "Proxy settings",
|
|
451
|
+
"mode": "Proxy mode",
|
|
452
|
+
"system": "System proxy",
|
|
453
|
+
"manual": "Manual",
|
|
454
|
+
"direct": "Direct",
|
|
455
|
+
"environment": "Environment variables",
|
|
456
|
+
"url": "Proxy URL",
|
|
457
|
+
"noProxy": "Bypass proxy (comma-separated)",
|
|
458
|
+
"effective": "In effect",
|
|
459
|
+
"bypass": "Bypass",
|
|
460
|
+
"source": "Source",
|
|
461
|
+
"directValue": "Direct",
|
|
462
|
+
"bypassDetails": "Proxy bypass addresses",
|
|
463
|
+
"notLoaded": "No configuration available",
|
|
464
|
+
"unavailable": "Proxy configuration unavailable",
|
|
465
|
+
"addressRequired": "Enter a proxy URL.",
|
|
466
|
+
"support": "HTTP/HTTPS proxies without authentication are supported.",
|
|
467
|
+
"applying": "Updating…",
|
|
468
|
+
"refresh": "Refresh"
|
|
469
|
+
},
|
|
470
|
+
common: { "retry": "Retry" }
|
|
471
|
+
};
|
|
472
|
+
var zh_CN_default = {
|
|
473
|
+
network: {
|
|
474
|
+
"title": "网络",
|
|
475
|
+
"proxySettings": "代理设置",
|
|
476
|
+
"mode": "代理模式",
|
|
477
|
+
"system": "系统代理",
|
|
478
|
+
"manual": "手动设置",
|
|
479
|
+
"direct": "直连",
|
|
480
|
+
"environment": "环境变量",
|
|
481
|
+
"url": "代理地址",
|
|
482
|
+
"noProxy": "绕过代理(逗号分隔)",
|
|
483
|
+
"effective": "当前生效",
|
|
484
|
+
"bypass": "绕过",
|
|
485
|
+
"source": "配置来源",
|
|
486
|
+
"directValue": "直连",
|
|
487
|
+
"bypassDetails": "绕过代理的地址",
|
|
488
|
+
"notLoaded": "暂无配置",
|
|
489
|
+
"unavailable": "代理配置不可用",
|
|
490
|
+
"addressRequired": "请输入代理地址。",
|
|
491
|
+
"support": "支持 HTTP/HTTPS 代理,不支持身份认证。",
|
|
492
|
+
"applying": "正在更新…",
|
|
493
|
+
"refresh": "刷新"
|
|
494
|
+
},
|
|
495
|
+
common: { "retry": "重试" }
|
|
496
|
+
};
|
|
497
|
+
//#endregion
|
|
498
|
+
//#region ui-src/index.tsx
|
|
499
|
+
function flatten(value, prefix = "") {
|
|
500
|
+
return Object.fromEntries(Object.entries(value).flatMap(([key, entry]) => typeof entry === "string" ? [[prefix + key, entry]] : Object.entries(flatten(entry, `${prefix}${key}.`))));
|
|
501
|
+
}
|
|
502
|
+
/** Browser contract version understood by the host's module loader. */
|
|
503
|
+
var runtimeVersion = 5;
|
|
504
|
+
/** Browser policy contributes a category without replacing shared host services. */
|
|
505
|
+
var provides = [];
|
|
506
|
+
/** Contributes proxy management with the producing owner's RPC and translation namespace. */
|
|
507
|
+
function register(ctx) {
|
|
508
|
+
configureTranslations(ctx);
|
|
509
|
+
ctx.i18n.register({
|
|
510
|
+
"en-US": flatten(en_US_default),
|
|
511
|
+
"zh-CN": flatten(zh_CN_default)
|
|
512
|
+
}, "zh-CN");
|
|
513
|
+
const api = {
|
|
514
|
+
get: () => ctx.invoke("proxyGet", {}),
|
|
515
|
+
set: (input) => ctx.invoke("proxySet", input),
|
|
516
|
+
refresh: () => ctx.invoke("proxyRefresh", {})
|
|
517
|
+
};
|
|
518
|
+
ctx.slots.register("shell.settings.category", {
|
|
519
|
+
id: "network",
|
|
520
|
+
label: () => ctx.i18n.t("network.title"),
|
|
521
|
+
icon: "globe",
|
|
522
|
+
region: "preset",
|
|
523
|
+
order: 40,
|
|
524
|
+
component: () => /* @__PURE__ */ jsx(NetworkPanel, { api })
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
//#endregion
|
|
528
|
+
export { provides, register, runtimeVersion };
|
package/README.md
DELETED