Skip to content

Instantly share code, notes, and snippets.

@petersirka petersirka/proxy.js
Created May 14, 2018

Embed
What would you like to do?
HTTP agent as a proxy tunel
function request_proxy(options, callback) {
var proxy = options.proxy;
proxy.path = options.uri.hostname;
proxy.headers = { host: options.uri.hostname };
if (proxy.auth)
proxy.headers['Proxy-Authorization'] = 'Basic ' + U.createBuffer(proxy.auth).toString('base64');
var req = Https.request(proxy);
req.on('error', function(e) {
options.callback(new Error('Proxy error: ' + e.toString()), '', 0, EMPTYOBJECT, proxy.hostname, EMPTYOBJECT);
options.callback = null;
});
req.on('connect', function(res, socket) {
if (res.statusCode === 200) {
options.uri.agent = options.uri.protocol === 'http:' ? new Http.Agent() : new Https.Agent();
options.uri.agent.reuseSocket(socket, req);
options.socket = socket;
callback(options.uri, options);
} else {
options.callback(new Error((res.statusMessage || 'Proxy error') + ': ' + res.statusCode), '', res.statusCode, res.headers, proxy.hostname, EMPTYOBJECT);
options.callback = null;
}
});
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.