Created
December 15, 2014 19:48
-
-
Save michaelansel/8c32b7427b856d0ff1cc to your computer and use it in GitHub Desktop.
Using a PAC file for all HTTP requests in hubot
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
/* 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