Last active
May 15, 2018 19:25
-
-
Save lessmind/5704519 to your computer and use it in GitHub Desktop.
proxy testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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