Spaces:
Sleeping
Sleeping
File size: 1,822 Bytes
f6266b9 | 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 | /**
* Outbound proxy configuration manager.
* Supports per-account and global HTTP proxy settings.
*/
import { readFileSync, writeFileSync, existsSync } from 'fs';
import { join } from 'path';
const PROXY_FILE = join(process.cwd(), 'proxy.json');
const _config = {
global: null, // { type, host, port, username, password }
perAccount: {}, // { accountId: { type, host, port, username, password } }
};
// Load
try {
if (existsSync(PROXY_FILE)) {
Object.assign(_config, JSON.parse(readFileSync(PROXY_FILE, 'utf-8')));
}
} catch {}
function save() {
try {
writeFileSync(PROXY_FILE, JSON.stringify(_config, null, 2));
} catch {}
}
export function getProxyConfig() {
return { ..._config };
}
export function setGlobalProxy(cfg) {
_config.global = cfg && cfg.host ? {
type: cfg.type || 'http',
host: cfg.host,
port: parseInt(cfg.port, 10) || 8080,
username: cfg.username || '',
password: cfg.password || '',
} : null;
save();
}
export function setAccountProxy(accountId, cfg) {
if (cfg && cfg.host) {
_config.perAccount[accountId] = {
type: cfg.type || 'http',
host: cfg.host,
port: parseInt(cfg.port, 10) || 8080,
username: cfg.username || '',
password: cfg.password || '',
};
} else {
delete _config.perAccount[accountId];
}
save();
}
export function removeProxy(scope, accountId) {
if (scope === 'global') {
_config.global = null;
} else if (scope === 'account' && accountId) {
delete _config.perAccount[accountId];
}
save();
}
/**
* Get effective proxy for an account (per-account takes priority over global).
*/
export function getEffectiveProxy(accountId) {
if (accountId && _config.perAccount[accountId]) {
return _config.perAccount[accountId];
}
return _config.global;
}
|