Skip to content

Instantly share code, notes, and snippets.

@0xgeert
Created January 13, 2018 17:17
Show Gist options
  • Save 0xgeert/57689768eceaec43ae0ddd17949d7503 to your computer and use it in GitHub Desktop.
Save 0xgeert/57689768eceaec43ae0ddd17949d7503 to your computer and use it in GitHub Desktop.
x-ray driver extended with proxy
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
@0xgeert
Copy link
Author

0xgeert commented Jan 13, 2018

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

@0xgeert
Copy link
Author

0xgeert commented Jan 13, 2018

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

@0xgeert
Copy link
Author

0xgeert commented Jan 13, 2018

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