This repository has been archived by the owner on Sep 24, 2022. It is now read-only.
forked from felicienfrancois/node-electron-proxy-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
186 lines (155 loc) · 5.11 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* 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, username, password) {
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;
}
}
instance = Agent.call(this, connect);
instance.session = session;
instance.cache = instance._resolver = null;
instance.credentials = {
username: username,
password: password
};
return instance;
}
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
}));
if(!secure && self.credentials.username) {
req.setHeader('Proxy-Authorization', 'Basic ' + new Buffer(self.credentials.username + ":" + self.credentials.password).toString('base64'));
}
debug('url: %o', url);
self.session.resolveProxy(url).then(onproxy);
// `resolveProxy()` callback function
function onproxy (proxy) {
// 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;
opts.checkServerIdentity = googleApisCertBypass;
try {
if (secure) {
socket = tls.connect(opts);
} else {
socket = net.connect(opts);
}
return fn(null, socket);
} catch (e) {
debug("Error making tls connection", e);
return;
}
} else if ('SOCKS' == 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) {
if(self.credentials.username) {
proxy.auth = self.credentials.username + ":" + self.credentials.password;
}
agent = new HttpsProxyAgent(proxy);
} else {
agent = new HttpProxyAgent(proxy);
}
} else {
throw new Error('Unknown proxy type: ' + type);
}
try {
if (agent) agent.callback(req, opts, fn);
}
catch (err) {
debug("Catched an exception while requesting: " + url, err);
throw err;
}
function googleApisCertBypass(servername, cert) {
let domain = servername.split(".").slice(-2).join(".");
if (cert.subjectaltname.includes(domain)) {return undefined;}
if (domain == "googleapis.com" && cert.subjectaltname.includes("google.com,")) {
console.log("accepting google.com for googleapis");
return undefined;
}
throw new Error("Certificate\n" + JSON.stringify(cert, null, 2) + "\nis not valid for " + servername + "/" + domain);
}
}
}