106 lines
3.4 KiB
JavaScript
106 lines
3.4 KiB
JavaScript
const readline = require('readline');
|
|
const { execFileSync } = require('child_process');
|
|
const fs = require('fs');
|
|
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
});
|
|
|
|
const CONFIG_DIR = '/opt/proxy-bridge';
|
|
const CONFIG_FILE = `${CONFIG_DIR}/config.json`;
|
|
const USER_FILE = `${CONFIG_DIR}/user.json`;
|
|
|
|
console.log("=== Proxy Bridge Configuration Setup ===");
|
|
|
|
function ask(question, defaultValue = "") {
|
|
return new Promise((resolve) => {
|
|
const prompt = defaultValue ? `${question} [${defaultValue}]: ` : `${question}: `;
|
|
rl.question(prompt, (answer) => {
|
|
resolve(answer.trim() || defaultValue);
|
|
});
|
|
});
|
|
}
|
|
|
|
async function run() {
|
|
try {
|
|
// 1. Upstream Proxy Configuration
|
|
console.log("\n--- Upstream Proxy (Corporate) ---");
|
|
const enableUpstream = (await ask("Enable Corporate Upstream Proxy? (y/n)", "y")).toLowerCase() === 'y';
|
|
|
|
let host = "";
|
|
let port = "8080";
|
|
let user = "";
|
|
let pass = "";
|
|
|
|
if (enableUpstream) {
|
|
host = await ask("Corporate Proxy Hostname (e.g. proxy.company.com)");
|
|
port = await ask("Corporate Proxy Port", "8080");
|
|
|
|
if (!host) {
|
|
console.error("❌ Hostname is required if upstream is enabled.");
|
|
process.exit(1);
|
|
}
|
|
|
|
// 2. Credentials
|
|
console.log("\n--- Credentials ---");
|
|
user = await ask("Corporate Username");
|
|
|
|
// Setup password mask (temporary swap)
|
|
const oldWrite = rl._writeToOutput;
|
|
rl._writeToOutput = function _writeToOutput(stringToWrite) {
|
|
// If it's a newline or specific strings, let it through
|
|
if (stringToWrite === "\n" || stringToWrite === "\r" || stringToWrite === "\r\n") {
|
|
rl.output.write(stringToWrite);
|
|
} else {
|
|
rl.output.write("*");
|
|
}
|
|
};
|
|
pass = await ask("Corporate Password");
|
|
rl._writeToOutput = oldWrite;
|
|
}
|
|
|
|
// Save Config
|
|
if (!fs.existsSync(CONFIG_DIR)) {
|
|
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
}
|
|
|
|
fs.writeFileSync(CONFIG_FILE, JSON.stringify({
|
|
enabled: enableUpstream,
|
|
host: host,
|
|
port: port
|
|
}, null, 2));
|
|
|
|
if (enableUpstream) {
|
|
// Save Username
|
|
fs.writeFileSync(USER_FILE, JSON.stringify({ username: user }));
|
|
|
|
// Store Password in Keyring
|
|
console.log("\n--> Storing password in system keyring...");
|
|
try {
|
|
execFileSync('secret-tool', [
|
|
'store',
|
|
'--label=Proxy Bridge Credentials',
|
|
'service',
|
|
'proxy-bridge',
|
|
'account',
|
|
user,
|
|
], {
|
|
input: pass
|
|
});
|
|
} catch (e) {
|
|
console.warn("⚠️ Warning: Failed to store password in keyring. Ensure libsecret-tools is installed.");
|
|
}
|
|
}
|
|
|
|
console.log("\n✅ Configuration successfully updated.");
|
|
} catch (error) {
|
|
console.error("\n❌ Setup failed:", error.message);
|
|
process.exit(1);
|
|
} finally {
|
|
rl.close();
|
|
}
|
|
}
|
|
|
|
run();
|