Skip to content

Commit 8ff4169

Browse files
committed
initial commit
0 parents  commit 8ff4169

4 files changed

Lines changed: 179 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
https-proxy-agent
2+
================
3+
### An HTTP(s) proxy `http.Agent` implementation for HTTPS
4+
5+
This module provides an `http.Agent` implementation that connects to a specified
6+
HTTP or HTTPS proxy server, and can be used with the built-in `https` module.
7+
8+
9+
Installation
10+
------------
11+
12+
Install with `npm`:
13+
14+
``` bash
15+
$ npm install https-proxy-agent
16+
```
17+
18+
19+
Example
20+
-------
21+
22+
``` js
23+
var url = require('url');
24+
var https = require('https');
25+
var HttpsProxyAgent = require('https-proxy-agent');
26+
27+
// HTTP/HTTPS proxy to connect to
28+
var proxy = process.env.http_proxy || 'http://168.63.76.32:3128';
29+
console.log('using proxy server %j', proxy);
30+
31+
// HTTPS endpoint for the proxy to connect to
32+
var endpoint = process.argv[2] || 'https://gist.github.com/TooTallNate/5952254/raw/07972db49b5b70a212c39dfee56ed3ab82f8179c/agent.js';
33+
console.log('attempting to GET %j', endpoint);
34+
var opts = url.parse(endpoint);
35+
36+
// create an instance of the `HttpsProxyAgent` class with the proxy server information
37+
var agent = new HttpProxyAgent(proxy);
38+
opts.agent = agent;
39+
40+
https.get(opts, function (res) {
41+
console.log('"response" event!', res.headers);
42+
res.pipe(process.stdout);
43+
});
44+
```
45+
46+
47+
License
48+
-------
49+
50+
(The MIT License)
51+
52+
Copyright (c) 2013 Nathan Rajlich <nathan@tootallnate.net>
53+
54+
Permission is hereby granted, free of charge, to any person obtaining
55+
a copy of this software and associated documentation files (the
56+
'Software'), to deal in the Software without restriction, including
57+
without limitation the rights to use, copy, modify, merge, publish,
58+
distribute, sublicense, and/or sell copies of the Software, and to
59+
permit persons to whom the Software is furnished to do so, subject to
60+
the following conditions:
61+
62+
The above copyright notice and this permission notice shall be
63+
included in all copies or substantial portions of the Software.
64+
65+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
66+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
67+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
68+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
69+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
70+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
71+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

https-proxy-agent.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var net = require('net');
7+
var tls = require('tls');
8+
var url = require('url');
9+
var Agent = require('./agent');
10+
var inherits = require('util').inherits;
11+
12+
/**
13+
* Module exports.
14+
*/
15+
16+
module.exports = HttpsProxyAgent;
17+
18+
/**
19+
* The `HttpsProxyAgent` implements an HTTP Agent subclass that connects to the
20+
* specified "HTTP(s) proxy server" in order to proxy HTTPS requests.
21+
*
22+
* @api public
23+
*/
24+
25+
function HttpsProxyAgent (opts) {
26+
if (!(this instanceof HttpsProxyAgent)) return new HttpsProxyAgent(opts);
27+
if ('string' == typeof opts) opts = url.parse(opts);
28+
Agent.call(this);
29+
this.proxy = opts;
30+
this.secure = this.proxy.protocol && this.proxy.protocol == 'https:';
31+
}
32+
inherits(HttpsProxyAgent, Agent);
33+
34+
/**
35+
* Default port to connect to.
36+
*/
37+
38+
Agent.prototype.defaultPort = 443;
39+
40+
/**
41+
* Initiates a TCP connection to the specified HTTP proxy server.
42+
*
43+
* @api public
44+
*/
45+
46+
HttpsProxyAgent.prototype.createConnection = function (opts, fn) {
47+
var socket;
48+
var info = {
49+
host: this.proxy.hostname || this.proxy.host,
50+
port: +this.proxy.port || (this.secure ? 443 : 80)
51+
};
52+
if (this.secure) {
53+
socket = tls.connect(info);
54+
} else {
55+
socket = net.connect(info);
56+
}
57+
58+
var msg = 'CONNECT ' + opts.host + ':' + opts.port + ' HTTP/1.1\r\n' +
59+
'Host: ' + opts.host + ':' + opts.port + '\r\n' +
60+
'\r\n';
61+
socket.write(msg);
62+
63+
socket.ondata = function (b, offset, length) {
64+
var buf = b.slice(offset, length);
65+
// TODO: verify that the socket is properly connected, check response...
66+
67+
socket.ondata = null;
68+
69+
// since the proxy is connecting to an SSL server, we have
70+
// to upgrade this socket connection to an SSL connection
71+
socket = tls.connect({
72+
socket: socket,
73+
servername: opts.host
74+
});
75+
fn(null, socket);
76+
};
77+
};

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "https-proxy-agent",
3+
"version": "0.0.1",
4+
"description": "An HTTP(s) proxy `http.Agent` implementation for HTTPS",
5+
"main": "https-proxy-agent.js",
6+
"scripts": {
7+
"test": "mocha --reporter spec"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git://github.com/TooTallNate/node-https-proxy-agent.git"
12+
},
13+
"keywords": [
14+
"https",
15+
"proxy",
16+
"endpoint",
17+
"agent"
18+
],
19+
"author": "Nathan Rajlich <nathan@tootallnate.net> (http://n8.io/)",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/TooTallNate/node-https-proxy-agent/issues"
23+
},
24+
"dependencies": {
25+
"agent-base": "~0.0.1"
26+
},
27+
"devDependencies": {
28+
"mocha": "~1.12.0"
29+
}
30+
}

0 commit comments

Comments
 (0)