Update proxy bridge setup
This commit is contained in:
+22
-22
@@ -12,23 +12,22 @@ const PROFILE_FILE = '/opt/proxy-bridge/profile.json';
|
||||
const LISTEN_HOST = process.env.PROXY_BRIDGE_LISTEN_HOST || '127.0.0.1';
|
||||
const BASE_PORT = Number(process.env.PROXY_BRIDGE_BASE_PORT || 8888);
|
||||
|
||||
let INTERNET_PROXY_HOST, INTERNET_PROXY_PORT;
|
||||
let INTERNET_PROXY_HOST, INTERNET_PROXY_PORT, UPSTREAM_ENABLED = true;
|
||||
try {
|
||||
const config = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
|
||||
INTERNET_PROXY_HOST = config.host;
|
||||
INTERNET_PROXY_PORT = Number(config.port);
|
||||
UPSTREAM_ENABLED = (config.enabled !== false);
|
||||
} catch (e) {
|
||||
INTERNET_PROXY_HOST = 'PROXY_HOST_PLACEHOLDER';
|
||||
INTERNET_PROXY_HOST = 'your-proxy.example.com';
|
||||
INTERNET_PROXY_PORT = 8080;
|
||||
}
|
||||
|
||||
let requestCounter = 0;
|
||||
let dynamicProfile = 'internet';
|
||||
const pacContexts = new Map(); // profileName -> vmContext
|
||||
let dynamicProfile = UPSTREAM_ENABLED ? 'internet' : 'direct';
|
||||
const pacContexts = new Map();
|
||||
|
||||
function now() {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
function now() { return new Date().toISOString(); }
|
||||
|
||||
function log(level, id, message, fields = {}) {
|
||||
const suffix = Object.entries(fields)
|
||||
@@ -38,12 +37,10 @@ function log(level, id, message, fields = {}) {
|
||||
console.log(`${now()} ${level} [${id}] ${message}${suffix ? ` ${suffix}` : ''}`);
|
||||
}
|
||||
|
||||
function nextId(prefix) {
|
||||
requestCounter += 1;
|
||||
return `${prefix}-${requestCounter}`;
|
||||
}
|
||||
function nextId(prefix) { requestCounter += 1; return `${prefix}-${requestCounter}`; }
|
||||
|
||||
function loadCredentials() {
|
||||
if (!UPSTREAM_ENABLED) return null;
|
||||
try {
|
||||
const user = JSON.parse(fs.readFileSync(USER_FILE, 'utf8')).username;
|
||||
const pass = execFileSync('secret-tool', [
|
||||
@@ -54,9 +51,7 @@ function loadCredentials() {
|
||||
user,
|
||||
], { encoding: 'utf8' }).trim();
|
||||
return pass ? { user, pass } : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
let AUTH_HEADER;
|
||||
@@ -111,7 +106,10 @@ function loadPacFile(profileName) {
|
||||
}
|
||||
|
||||
function getAvailableProfiles() {
|
||||
const profiles = ['internet', 'direct', 'off'];
|
||||
const profiles = [];
|
||||
if (UPSTREAM_ENABLED) profiles.push('internet');
|
||||
profiles.push('direct', 'off');
|
||||
|
||||
const pacDir = path.join(os.homedir(), '.mozilla');
|
||||
if (fs.existsSync(pacDir)) {
|
||||
fs.readdirSync(pacDir).forEach(f => {
|
||||
@@ -122,8 +120,7 @@ function getAvailableProfiles() {
|
||||
}
|
||||
|
||||
function refreshPacContexts() {
|
||||
const all = getAvailableProfiles();
|
||||
all.forEach(name => {
|
||||
getAvailableProfiles().forEach(name => {
|
||||
if (!['internet', 'direct', 'off'].includes(name)) {
|
||||
pacContexts.set(name, loadPacFile(name));
|
||||
}
|
||||
@@ -134,10 +131,14 @@ function updateDynamicProfile() {
|
||||
try {
|
||||
if (fs.existsSync(PROFILE_FILE)) {
|
||||
const data = JSON.parse(fs.readFileSync(PROFILE_FILE, 'utf8'));
|
||||
const newProfile = data.profile || 'internet';
|
||||
if (newProfile !== dynamicProfile) {
|
||||
const newProfile = data.profile;
|
||||
const valid = getAvailableProfiles();
|
||||
if (newProfile && valid.includes(newProfile) && newProfile !== dynamicProfile) {
|
||||
log('INFO', 'profile', 'dynamic profile switch', { old: dynamicProfile, new: newProfile });
|
||||
dynamicProfile = newProfile;
|
||||
} else if (newProfile && !valid.includes(newProfile)) {
|
||||
log('WARN', 'profile', 'saved profile is invalid/disabled', { profile: newProfile });
|
||||
dynamicProfile = UPSTREAM_ENABLED ? 'internet' : 'direct';
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
@@ -152,7 +153,7 @@ function getUpstream(profileName, url, host) {
|
||||
|
||||
if (profile === 'off') return 'OFF';
|
||||
if (profile === 'direct') return 'DIRECT';
|
||||
if (profile === 'internet') return `PROXY ${INTERNET_PROXY_HOST}:${INTERNET_PROXY_PORT}`;
|
||||
if (profile === 'internet' && UPSTREAM_ENABLED) return `PROXY ${INTERNET_PROXY_HOST}:${INTERNET_PROXY_PORT}`;
|
||||
|
||||
const ctx = pacContexts.get(profile);
|
||||
if (ctx) {
|
||||
@@ -289,7 +290,6 @@ function createBridge(port, profileName) {
|
||||
createBridge(BASE_PORT, 'dynamic');
|
||||
|
||||
// 2. Static bridges on 8889+
|
||||
const allProfiles = getAvailableProfiles();
|
||||
allProfiles.forEach((name, i) => {
|
||||
getAvailableProfiles().forEach((name, i) => {
|
||||
createBridge(BASE_PORT + 1 + i, name);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user