forked from TooTallNate/node-pac-proxy-agent
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
153 lines (129 loc) · 4.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Module exports.
*/
module.exports = exports = ElectronProxyAgent;
/**
* Supported "protocols". Delegates out to the `get-uri` module.
*/
var getUri = require('get-uri');
Object.defineProperty(exports, 'protocols', {
enumerable: true,
configurable: true,
get: function () { return Object.keys(getUri.protocols); }
});
/**
* Module dependencies.
*/
var net = require('net');
var tls = require('tls');
var parse = require('url').parse;
var format = require('url').format;
var extend = require('extend');
var Agent = require('agent-base');
var HttpProxyAgent = require('http-proxy-agent');
var HttpsProxyAgent = require('https-proxy-agent');
var SocksProxyAgent = require('socks-proxy-agent');
var inherits = require('util').inherits;
var debug = require('debug')('electron-proxy-agent');
/**
* The `ElectronProxyAgent` class.
*
* session : {
* resolveProxy(url, callback)
* }
*
* See https://github.com/atom/electron/blob/master/docs/api/session.md#sesresolveproxyurl-callback
*
* @api public
*/
function ElectronProxyAgent(session) {
if (!(this instanceof ElectronProxyAgent)) return new ElectronProxyAgent(session);
if (!session || typeof(session.resolveProxy) !== 'function') {
debug('no valid session found, trying to initialize ElectronProxyAgent with defaultSession');
if (typeof(window) === 'undefined') {
session = require('session').defaultSession;
} else {
session = require('remote').getCurrentWindow().webContents.session;
}
}
Agent.call(this, connect);
this.session = session;
this.cache = this._resolver = null;
}
inherits(ElectronProxyAgent, Agent);
/**
* Called when the node-core HTTP client library is creating a new HTTP request.
*
* @api public
*/
function connect (req, opts, fn) {
var url;
var host;
var self = this;
var secure = Boolean(opts.secureEndpoint);
// calculate the `url` parameter
var defaultPort = secure ? 443 : 80;
var path = req.path;
var firstQuestion = path.indexOf('?');
var search;
if (-1 != firstQuestion) {
search = path.substring(firstQuestion);
path = path.substring(0, firstQuestion);
}
url = format(extend({}, opts, {
protocol: secure ? 'https:' : 'http:',
pathname: path,
search: search,
// need to use `hostname` instead of `host` otherwise `port` is ignored
hostname: opts.host,
host: null,
// set `port` to null when it is the protocol default port (80 / 443)
port: defaultPort == opts.port ? null : opts.port
}));
debug('url: %o', url);
var handled = false;
var promise = self.session.resolveProxy(url, onproxy);
if(promise && promise.then) {
promise.then(onproxy);
}
// `resolveProxy()` callback function
function onproxy (proxy) {
if(handled) return;
handled = true
// default to "DIRECT" if a falsey value was returned (or nothing)
if (!proxy) proxy = 'DIRECT';
var proxies = String(proxy).trim().split(/\s*;\s*/g).filter(Boolean);
// XXX: right now, only the first proxy specified will be used
var first = proxies[0];
debug('using proxy: %o', first);
var agent;
var parts = first.split(/\s+/);
var type = parts[0];
if ('DIRECT' == type) {
// direct connection to the destination endpoint
var socket;
if (secure) {
socket = tls.connect(opts);
} else {
socket = net.connect(opts);
}
return fn(null, socket);
} else if ('SOCKS' == type || 'SOCKS5' == type) {
// use a SOCKS proxy
agent = new SocksProxyAgent('socks://' + parts[1]);
} else if ('PROXY' == type || 'HTTPS' == type) {
// use an HTTP or HTTPS proxy
// http://dev.chromium.org/developers/design-documents/secure-web-proxy
var proxyURL = ('HTTPS' === type ? 'https' : 'http') + '://' + parts[1];
var proxy = parse(proxyURL);
if (secure) {
agent = new HttpsProxyAgent(proxy);
} else {
agent = new HttpProxyAgent(proxy);
}
} else {
throw new Error('Unknown proxy type: ' + type);
}
if (agent) agent.callback(req, opts, fn);
}
}