Files
Netcatty/domain/vaultImport/csvProxy.ts
T
00c4095e59 feat: global default proxy with explicit direct override (#3774)
* feat: add a global default proxy with an explicit direct override

Resolve the proxy as the host setting, then the nearest group default,
then one optional saved profile from synced settings, then a direct
connection. A host can choose direct and skip both inherited defaults.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: keep global default proxy persistence across settings instances

Unregister only the setter owned by the unmounting useSettingsState instance so closing Settings still writes the cleared default through the remaining AppLockGate setter.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: round-trip explicit direct mode in vault CSV

Write the Proxy cell as direct when a host sets directProxy, and import that literal back as an explicit direct connection. An empty Proxy cell still inherits.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: keep CSV proxy export from embedding the global default

Grouped hosts ran through applyGroupDefaults and picked up the ambient
global proxy, while ungrouped hosts exported an empty inheriting cell.
Both paths now opt out of that default so inheritance stays inheritance.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix: follow global default proxy changes in peer session windows

Session windows disable settings IPC sync, which also skipped the
storage listener and left the resolver singleton at its mount-time id.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* refactor: clear a deleted global default proxy at the call site

Drop the pass-through field on removeProxyProfileReferences. The profile
manager already knows the active id and clears it directly.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* test: share the global default proxy settings harness

The peer-window case and the multi-instance cleanup case were mounting
the same JSDOM fixture twice.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-10-20 13:52:32 +08:00

69 lines
2.6 KiB
TypeScript

import type { ProxyConfig } from "../models";
import { decodeCsvProxy } from "./csvCredentialFields";
// CSV representation of a per-host proxy configuration:
// direct
// http://[user[:pass]@]host:port
// socks5://[user[:pass]@]host:port
// command://<command text, verbatim after the scheme>
// `direct` is an explicit per-host direct connection. An empty cell still means inherit.
export const CSV_DIRECT_PROXY_VALUE = "direct";
export const isCsvDirectProxyValue = (raw: string): boolean =>
decodeCsvProxy(raw.trim()) === CSV_DIRECT_PROXY_VALUE;
const PROXY_SCHEME_PATTERN = /^(http|socks5|command):\/\/(.*)$/is;
const PROXY_AUTHORITY_PATTERN = /^(?:([^@:/?#\s]+)(?::([^@/?#]*))?@)?(\[[^\[\]@/?#\s]+\]|[^\[\]@:/?#\s]+):(\d+)$/u;
const decodeAuthorityPart = (value: string | undefined): string | undefined => {
if (value === undefined) return undefined;
try {
return decodeURIComponent(value);
} catch {
return value;
}
};
export const formatCsvProxy = (config: ProxyConfig): string => {
if (config.type === "command") {
return `command://${config.command ?? ""}`;
}
const scheme = config.type === "socks5" ? "socks5" : "http";
const username = config.username?.trim();
const auth = username
? `${encodeURIComponent(username)}${config.password ? `:${encodeURIComponent(config.password)}` : ""}@`
: "";
const host = config.host.trim();
const authorityHost = host.includes(":") && !host.startsWith("[") ? `[${host}]` : host;
return `${scheme}://${auth}${authorityHost}:${config.port}`;
};
export const parseCsvProxy = (raw: string): ProxyConfig | undefined => {
const value = decodeCsvProxy(raw.trim());
if (!value) return undefined;
const match = value.match(PROXY_SCHEME_PATTERN);
if (!match) return undefined;
const scheme = match[1].toLowerCase();
if (scheme === "command") {
const command = match[2].trim();
if (!command) return undefined;
return { type: "command", host: "", port: 0, command };
}
if (/[\s\0]/.test(match[2])) return undefined;
const authority = match[2].match(PROXY_AUTHORITY_PATTERN);
if (!authority) return undefined;
const host = (authority[3] ?? "").trim().replace(/^\[(.*)\]$/u, "$1");
const port = Number(authority[4]);
if (!host || !Number.isInteger(port) || port < 1 || port > 65535) return undefined;
const config: ProxyConfig = {
type: scheme === "socks5" ? "socks5" : "http",
host,
port,
};
const username = decodeAuthorityPart(authority[1]);
const password = decodeAuthorityPart(authority[2]);
if (username) config.username = username;
if (password) config.password = password;
return config;
};