Skip to content

Commit 78cd7dc

Browse files
committed
first commit
1 parent 6bbbef7 commit 78cd7dc

2 files changed

Lines changed: 82 additions & 10 deletions

File tree

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
1+
This is a fork of the original https-proxy-agent library with the following additions:
2+
3+
- You can set http headers to your agent/proxy
4+
- You will receive an event from the agent object when the response is ready
5+
6+
I hacked the source so I could use the library to use with ProxyMesh's https integration:
7+
http://proxymesh.com/blog/pages/proxy-server-headers.html#https-connect-method
8+
9+
It should work with other proxies too.
10+
11+
See example using request library:
12+
13+
```javascript
14+
var url = require('url'),
15+
request = require('request'),
16+
HttpsProxyAgent = require('https-proxy-agent');
17+
18+
var u = url.parse('http://username:password@us-ca.proxymesh.com:31280');
19+
20+
var proxy = new HttpsProxyAgent(u);
21+
22+
var previousIp;
23+
proxy.on('response', function(resp){
24+
previousIp = resp.headers['x-proxymesh-ip'];
25+
// get the proxy response
26+
// { statusCode: 200,
27+
// headers: { 'x-proxymesh-ip': '123.123.123' } }
28+
})
29+
30+
// make request
31+
request({url: 'https://www.google.com', agent: proxy}, function(err, res){
32+
console.log(res.headers)
33+
34+
// make another request with the same proxy mesh ip:
35+
u.headers['X-ProxyMesh-IP'] = previousIp;
36+
var proxy = new HttpsProxyAgent(u);
37+
request({url: 'https://www.google.com', agent: proxy}, function(err, res){ // get the website response
38+
...
39+
})
40+
})
41+
```
42+
143
https-proxy-agent
244
================
345
### An HTTP(s) proxy `http.Agent` implementation for HTTPS

https-proxy-agent.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ inherits(HttpsProxyAgent, Agent);
6060

6161
function connect (req, opts, fn) {
6262

63-
var proxy = this.proxy;
63+
var proxy = this.proxy,
64+
self = this;
6465

6566
// create a socket connection to the proxy server
6667
var socket;
@@ -124,6 +125,31 @@ function connect (req, opts, fn) {
124125
var firstLine = str.substring(0, str.indexOf('\r\n'));
125126
var statusCode = +firstLine.split(' ')[1];
126127
debug('got proxy server response: %o', firstLine);
128+
self.emit('raw_response', buffered);
129+
130+
var resp = {
131+
statusCode: statusCode,
132+
headers: {}
133+
}
134+
135+
// simple http header parser
136+
var lines = buffered.toString('ascii').split('\r\n');
137+
for(var i in lines){
138+
var line = lines[i];
139+
140+
if(i == 0){
141+
continue; // first line is: HTTP/1.1 200 Connection established
142+
}else{
143+
var sep = line.indexOf(':');
144+
if(sep !== -1){
145+
var headerName = line.slice(0, sep),
146+
headerVal = line.slice(sep+1).trim();
147+
148+
resp.headers[headerName.toLowerCase()] = headerVal;
149+
}
150+
}
151+
}
152+
self.emit('response', resp);
127153

128154
if (200 == statusCode) {
129155
// 200 Connected status code!
@@ -191,16 +217,20 @@ function connect (req, opts, fn) {
191217

192218
var hostname = opts.host + ':' + opts.port;
193219
var msg = 'CONNECT ' + hostname + ' HTTP/1.1\r\n';
220+
var auth = proxy.auth;
194221

195-
var headers = extend({}, proxy.headers);
196-
if (proxy.auth) {
197-
headers['Proxy-Authorization'] = 'Basic ' + new Buffer(proxy.auth).toString('base64');
222+
// send extra headers to proxy
223+
if(proxy.headers){
224+
for(var headerName in proxy.headers){
225+
msg += headerName + ': ' + proxy.headers[headerName] + '\r\n';
226+
}
198227
}
199-
headers['Host'] = hostname;
200-
headers['Connection'] = 'close';
201-
Object.keys(headers).forEach(function (name) {
202-
msg += name + ': ' + headers[name] + '\r\n';
203-
});
204228

205-
socket.write(msg + '\r\n');
229+
if (auth) {
230+
msg += 'Proxy-Authorization: Basic ' + new Buffer(auth).toString('base64') + '\r\n';
231+
}
232+
msg += 'Host: ' + hostname + '\r\n' +
233+
'Connection: close\r\n' +
234+
'\r\n';
235+
socket.write(msg);
206236
};

0 commit comments

Comments
 (0)