Files
Netcatty/domain/globalDefaultProxy.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

63 lines
2.0 KiB
TypeScript

/**
* App-wide default proxy profile. Settings publish the saved id here so
* `applyGroupDefaults` can apply it for every connection path, including
* callers that pass empty group defaults.
*/
let activeId: string | undefined;
const listeners = new Set<() => void>();
let registrationSeq = 0;
// Several settings hooks can be mounted at once (AppLockGate plus a settings
// tab). Each registration owns its cleanup so unmounting one instance falls
// back to the setter that is still mounted.
const persistSetters: Array<{
id: number;
setter: (id: string | undefined) => void;
}> = [];
export const normalizeGlobalDefaultProxyProfileId = (
value: unknown,
): string | undefined => {
if (typeof value !== 'string') return undefined;
const trimmed = value.trim();
return trimmed || undefined;
};
export const getActiveGlobalDefaultProxyProfileId = (): string | undefined => activeId;
export const publishActiveGlobalDefaultProxyProfileId = (value: unknown): void => {
const next = normalizeGlobalDefaultProxyProfileId(value);
if (next === activeId) return;
activeId = next;
for (const listener of listeners) listener();
};
export const subscribeActiveGlobalDefaultProxyProfileId = (
listener: () => void,
): (() => void) => {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
};
export const registerGlobalDefaultProxyProfileSetter = (
setter: (id: string | undefined) => void,
): (() => void) => {
const id = ++registrationSeq;
persistSetters.push({ id, setter });
return () => {
const index = persistSetters.findIndex((entry) => entry.id === id);
if (index >= 0) persistSetters.splice(index, 1);
};
};
/** Clear the saved global default and persist that choice when settings are mounted. */
export const clearActiveGlobalDefaultProxyProfile = (): void => {
const persistSetter = persistSetters[persistSetters.length - 1]?.setter;
if (persistSetter) {
persistSetter(undefined);
return;
}
publishActiveGlobalDefaultProxyProfileId(undefined);
};