Skip to content

Instantly share code, notes, and snippets.

@michaelansel
Created December 15, 2014 19:48
Show Gist options
  • Save michaelansel/8c32b7427b856d0ff1cc to your computer and use it in GitHub Desktop.
Save michaelansel/8c32b7427b856d0ff1cc to your computer and use it in GitHub Desktop.
Using a PAC file for all HTTP requests in hubot
/* from package.json
"dependencies": {
"pac-proxy-agent": "~0.2.0",
"scoped-http-client": "~0.10.0"
}
*/
var url = require('url');
var http = require('http');
var https = require('https');
var HttpClient = require('scoped-http-client');
var PacProxyAgent = require('pac-proxy-agent');
var agentOpts;
// URI to a PAC proxy file to use (the "pac+" prefix is stripped)
var proxy = 'pac+http://my-proxy-host/proxy.pac';
// create an instance of the `PacProxyAgent` class with the PAC file location
agentOpts = url.parse(proxy);
agentOpts.secureEndpoint = true;
var httpsAgent = new PacProxyAgent(agentOpts);
agentOpts = url.parse(proxy);
agentOpts.secureEndpoint = false;
var httpAgent = new PacProxyAgent(agentOpts);
var scopedHttpClient = function (providedUrl) {
var httpClient;
if (url.parse(providedUrl, true).protocol == 'https:') {
httpClient = HttpClient.create(providedUrl, {agent: httpsAgent});
} else {
httpClient = HttpClient.create(providedUrl, {agent: httpAgent});
}
return httpClient;
};
module.exports = (robot) ->
robot.http = (url) ->
scopedHttpClient(url).header('User-Agent', "Hubot/#{robot.version}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment