proxy-bridge: add keyring-based HTTP CONNECT proxy bridge
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
const http = require('http');
|
||||
const net = require('net');
|
||||
const { execSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
|
||||
// 1. CONFIGURATION
|
||||
const PROXY_HOST = 'PROXY_HOST_PLACEHOLDER';
|
||||
const PROXY_PORT = 8080;
|
||||
|
||||
// 2. FETCH SECRETS FROM KEYRING
|
||||
let USER, PASS;
|
||||
try {
|
||||
// Read the username we saved during setup
|
||||
USER = JSON.parse(fs.readFileSync('/opt/proxy-bridge/user.json')).username;
|
||||
// Query the Ubuntu Keyring for the password associated with this user/service
|
||||
PASS = execSync(`secret-tool lookup service proxy-bridge account ${USER}`).toString().trim();
|
||||
|
||||
if (!PASS) throw new Error("Password returned empty.");
|
||||
} catch (e) {
|
||||
console.error("CRITICAL: Could not retrieve credentials from keyring. Did you run setup.js?");
|
||||
console.error(e.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// 3. GENERATE AUTH
|
||||
const AUTH_HEADER = 'Basic ' + Buffer.from(`${USER}:${PASS}`).toString('base64');
|
||||
|
||||
const server = http.createServer();
|
||||
|
||||
server.on('connect', (req, clientSocket, head) => {
|
||||
console.log(`--> Connecting to ${req.url}`);
|
||||
|
||||
const serverSocket = net.connect(PROXY_PORT, PROXY_HOST, () => {
|
||||
serverSocket.write(`CONNECT ${req.url} HTTP/1.1\r\n` +
|
||||
`Host: ${req.url}\r\n` +
|
||||
`Proxy-Authorization: ${AUTH_HEADER}\r\n` +
|
||||
`Proxy-Connection: Keep-Alive\r\n\r\n`);
|
||||
serverSocket.write(head);
|
||||
});
|
||||
|
||||
serverSocket.once('data', (data) => {
|
||||
if (data.toString().includes('200')) {
|
||||
clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
|
||||
serverSocket.pipe(clientSocket);
|
||||
clientSocket.pipe(serverSocket);
|
||||
} else {
|
||||
clientSocket.write(data);
|
||||
}
|
||||
});
|
||||
|
||||
serverSocket.on('error', () => clientSocket.end());
|
||||
clientSocket.on('error', () => serverSocket.end());
|
||||
});
|
||||
|
||||
server.listen(8888, '127.0.0.1', () => {
|
||||
console.log('Bridge active on http://127.0.0.1:8888 (Auth via Keyring)');
|
||||
});
|
||||
Reference in New Issue
Block a user