Created
January 13, 2018 17:17
-
-
Save 0xgeert/57689768eceaec43ae0ddd17949d7503 to your computer and use it in GitHub Desktop.
x-ray driver extended with proxy
This file contains hidden or 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 Request = require('request') | |
var r = request.defaults({'proxy':'http://localproxy.com'}) | |
function makeDriver(opts) { | |
if (typeof opts === "function") { | |
var request = opts | |
} else { | |
var request = Request.defaults(opts) | |
} | |
return function driver(context, callback) { | |
var url = context.url | |
request(url, function(err, response, body) { | |
return callback(err, body) | |
}) | |
} | |
} | |
module.exports = makeDriver |
Or use the below if you want to have a list of proxies under your own control
var Request = require('request')
var r = request.defaults({'proxy':'http://localproxy.com'})
let counter = 0;
//list with own proxies.
//A simple way is to iterate through them in a circular fashion. This is called 'round-robin'
let proxies = [
url1,
url2,
url3,
]
function makeDriver(opts) {
if (typeof opts === "function") {
var request = opts
} else {
var request = Request.defaults(opts)
}
return function driver(context, callback) {
var url = context.url
request({
url: url,
proxy: proxies[counter++ % proxies.length] // % is the modulo operation. Look it up if you're unsure how that works
}, function(err, response, body) {
return callback(err, body)
})
}
}
module.exports = makeDriver
You call the above driver as specified here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UNTESTED.
This above setup is useful if you've got a fixed proxy url.
NOTE: adapted from x-ray request driver and added 1 line of code