Skip to content

Instantly share code, notes, and snippets.

@geertbrit
Created January 13, 2018 17:17
Show Gist options
  • Select an option

  • Save geertbrit/57689768eceaec43ae0ddd17949d7503 to your computer and use it in GitHub Desktop.

Select an option

Save geertbrit/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
@geertbrit
Copy link
Copy Markdown
Author

geertbrit 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

@geertbrit
Copy link
Copy Markdown
Author

geertbrit 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

@geertbrit
Copy link
Copy Markdown
Author

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