proxy-chain
proxy-chain copied to clipboard
Switching upstream proxy without restarting server
Is it possible to switch the upstream proxy host without restarting the server? I'm currently using the anonymizeProxy
helper like so:
const proxyChain = require('proxy-chain');
const user = '...';
const pass = '...';
const vpnHost = process.argv[2];
const oldProxyUrl = `http://${user}:${pass}@${vpnHost}:80`;
const newProxyUrl = proxyChain.anonymizeProxy(oldProxyUrl);
function cleanup(url) {
proxyChain.closeAnonymizedProxy(url);
}
newProxyUrl.then(url => {
console.log(url);
process.on('SIGINT', () => cleanup(url));
process.on('SIGTERM', () => cleanup(url));
});
But I have to restart the process to change the host. It would be nice if I could send a signal to the process and it would switch the upstream proxy without changing its own port.
I think one way to solve this might be to allow passing a desired port to anonymizeProxy
.
Edit: I hacked around this by modifying PORT_SELECTION_CONFIG
:
const proxyChainTools = require('proxy-chain/build/tools');
...
const vpnPort = process.argv[3];
...
// HACK: Force the proxy to choose a port by narrowing the min/max in config.
if (vpnPort) {
proxyChainTools.PORT_SELECTION_CONFIG.FROM = parseInt(vpnPort, 10);
proxyChainTools.PORT_SELECTION_CONFIG.TO = parseInt(vpnPort, 10);
}
Now I can quickly restart the node process to switch the host and keep the same port 🤷♂️
Adding an optional port
argument to anonymizeProxy()
is a good idea. PR is welcome :)
Done in https://github.com/apify/proxy-chain/pull/214