Skip to content

Instantly share code, notes, and snippets.

@lessmind
Last active May 15, 2018 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lessmind/5704519 to your computer and use it in GitHub Desktop.
Save lessmind/5704519 to your computer and use it in GitHub Desktop.
proxy testing
var http = require('http'),
https = require('https'),
URL = require('url');
var request = function(name, url, proxy) {
// options
var options = URL.parse(url, false);
// headers
options.headers = {
accept: '*/*',
'content-length': 0
};
var body = '';
// proxy set
if(proxy) {
var proxy = URL.parse(proxy, false);
options.path = options.protocol+ '//'+ options.host+ options.path;
options.headers.host = options.host;
options.protocol = proxy.protocol;
options.host = proxy.host;
options.port = proxy.port;
}
console.log(name, 'request options:', options);
var r = (options.protocol == 'http:'?http:https).request(options, function(res) {
res.on('end', function() {
// just print ip, instead of whole body
console.log(name, body.match(/check_ip" value="([^"]*)"/)[1]);
// console.log(name, body);
});
res.on('readable', function() {
body += this.read().toString();
});
});
r.end();
};
request('with proxy', 'http://showip.net/', 'http://proxy.com:8080');
request('no proxy', 'http://showip.net/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment