/
index.ts
300 lines (263 loc) · 7.95 KB
/
index.ts
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import * as net from 'net';
import * as tls from 'tls';
import * as http from 'http';
import * as crypto from 'crypto';
import { once } from 'events';
import createDebug from 'debug';
import { Readable } from 'stream';
import { format } from 'url';
import { Agent, AgentConnectOpts, toBuffer } from 'agent-base';
import { HttpProxyAgent, HttpProxyAgentOptions } from 'http-proxy-agent';
import { HttpsProxyAgent, HttpsProxyAgentOptions } from 'https-proxy-agent';
import { SocksProxyAgent, SocksProxyAgentOptions } from 'socks-proxy-agent';
import {
getUri,
protocols as gProtocols,
ProtocolOpts as GetUriOptions,
} from 'get-uri';
import {
createPacResolver,
FindProxyForURL,
PacResolverOptions,
} from 'pac-resolver';
const debug = createDebug('pac-proxy-agent');
type Protocols = keyof typeof gProtocols;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Protocol<T> = T extends `pac+${infer P}:${infer _}`
? P
: // eslint-disable-next-line @typescript-eslint/no-unused-vars
T extends `${infer P}:${infer _}`
? P
: never;
export type PacProxyAgentOptions<T> = http.AgentOptions &
PacResolverOptions &
GetUriOptions<`${Protocol<T>}:`> &
HttpProxyAgentOptions<''> &
HttpsProxyAgentOptions<''> &
SocksProxyAgentOptions & {
fallbackToDirect?: boolean;
};
/**
* The `PacProxyAgent` class.
*
* A few different "protocol" modes are supported (supported protocols are
* backed by the `get-uri` module):
*
* - "pac+data", "data" - refers to an embedded "data:" URI
* - "pac+file", "file" - refers to a local file
* - "pac+ftp", "ftp" - refers to a file located on an FTP server
* - "pac+http", "http" - refers to an HTTP endpoint
* - "pac+https", "https" - refers to an HTTPS endpoint
*/
export class PacProxyAgent<Uri extends string> extends Agent {
static readonly protocols: `pac+${Protocols}`[] = [
'pac+data',
'pac+file',
'pac+ftp',
'pac+http',
'pac+https',
];
uri: URL;
opts: PacProxyAgentOptions<Uri>;
cache?: Readable;
resolver?: FindProxyForURL;
resolverHash: string;
resolverPromise?: Promise<FindProxyForURL>;
constructor(uri: Uri | URL, opts?: PacProxyAgentOptions<Uri>) {
super(opts);
// Strip the "pac+" prefix
const uriStr = typeof uri === 'string' ? uri : uri.href;
this.uri = new URL(uriStr.replace(/^pac\+/i, ''));
debug('Creating PacProxyAgent with URI %o', this.uri.href);
// @ts-expect-error Not sure why TS is complaining here…
this.opts = { ...opts };
this.cache = undefined;
this.resolver = undefined;
this.resolverHash = '';
this.resolverPromise = undefined;
// For `PacResolver`
if (!this.opts.filename) {
this.opts.filename = this.uri.href;
}
}
private clearResolverPromise = (): void => {
this.resolverPromise = undefined;
};
/**
* Loads the PAC proxy file from the source if necessary, and returns
* a generated `FindProxyForURL()` resolver function to use.
*
* @api private
*/
private getResolver(): Promise<FindProxyForURL> {
if (!this.resolverPromise) {
this.resolverPromise = this.loadResolver();
this.resolverPromise.then(
this.clearResolverPromise,
this.clearResolverPromise
);
}
return this.resolverPromise;
}
private async loadResolver(): Promise<FindProxyForURL> {
try {
// (Re)load the contents of the PAC file URI
const code = await this.loadPacFile();
// Create a sha1 hash of the JS code
const hash = crypto.createHash('sha1').update(code).digest('hex');
if (this.resolver && this.resolverHash === hash) {
debug(
'Same sha1 hash for code - contents have not changed, reusing previous proxy resolver'
);
return this.resolver;
}
// Cache the resolver
debug('Creating new proxy resolver instance');
this.resolver = createPacResolver(code, this.opts);
// Store that sha1 hash for future comparison purposes
this.resolverHash = hash;
return this.resolver;
} catch (err: unknown) {
if (
this.resolver &&
(err as NodeJS.ErrnoException).code === 'ENOTMODIFIED'
) {
debug(
'Got ENOTMODIFIED response, reusing previous proxy resolver'
);
return this.resolver;
}
throw err;
}
}
/**
* Loads the contents of the PAC proxy file.
*
* @api private
*/
private async loadPacFile(): Promise<string> {
debug('Loading PAC file: %o', this.uri);
const rs = await getUri(this.uri, { ...this.opts, cache: this.cache });
debug('Got `Readable` instance for URI');
this.cache = rs;
const buf = await toBuffer(rs);
debug('Read %o byte PAC file from URI', buf.length);
return buf.toString('utf8');
}
/**
* Called when the node-core HTTP client library is creating a new HTTP request.
*/
async connect(
req: http.ClientRequest,
opts: AgentConnectOpts
): Promise<http.Agent | net.Socket> {
const { secureEndpoint } = opts;
// First, get a generated `FindProxyForURL()` function,
// either cached or retrieved from the source
const resolver = await this.getResolver();
// Calculate the `url` parameter
const defaultPort = secureEndpoint ? 443 : 80;
let path = req.path;
let search: string | null = null;
const firstQuestion = path.indexOf('?');
if (firstQuestion !== -1) {
search = path.substring(firstQuestion);
path = path.substring(0, firstQuestion);
}
const urlOpts = {
...opts,
protocol: secureEndpoint ? 'https:' : 'http:',
pathname: path,
search,
// need to use `hostname` instead of `host` otherwise `port` is ignored
hostname: opts.host,
host: null,
href: null,
// set `port` to null when it is the protocol default port (80 / 443)
port: defaultPort === opts.port ? null : opts.port,
};
const url = format(urlOpts);
debug('url: %o', url);
let result = await resolver(url);
// Default to "DIRECT" if a falsey value was returned (or nothing)
if (!result) {
result = 'DIRECT';
}
const proxies = String(result)
.trim()
.split(/\s*;\s*/g)
.filter(Boolean);
if (this.opts.fallbackToDirect && !proxies.includes('DIRECT')) {
proxies.push('DIRECT');
}
for (const proxy of proxies) {
let agent: Agent | null = null;
let socket: net.Socket | null = null;
const [type, target] = proxy.split(/\s+/);
debug('Attempting to use proxy: %o', proxy);
if (type === 'DIRECT') {
// Direct connection to the destination endpoint
if (secureEndpoint) {
const servername = opts.servername || opts.host;
socket = tls.connect({
...opts,
servername:
!servername || net.isIP(servername)
? undefined
: servername,
});
} else {
socket = net.connect(opts);
}
} else if (type === 'SOCKS' || type === 'SOCKS5') {
// Use a SOCKSv5h proxy
agent = new SocksProxyAgent(`socks://${target}`, this.opts);
} else if (type === 'SOCKS4') {
// Use a SOCKSv4a proxy
agent = new SocksProxyAgent(`socks4a://${target}`, this.opts);
} else if (
type === 'PROXY' ||
type === 'HTTP' ||
type === 'HTTPS'
) {
// Use an HTTP or HTTPS proxy
// http://dev.chromium.org/developers/design-documents/secure-web-proxy
const proxyURL = `${
type === 'HTTPS' ? 'https' : 'http'
}://${target}`;
if (secureEndpoint) {
agent = new HttpsProxyAgent(proxyURL, this.opts);
} else {
agent = new HttpProxyAgent(proxyURL, this.opts);
}
}
try {
if (socket) {
// "DIRECT" connection, wait for connection confirmation
await once(socket, 'connect');
req.emit('proxy', { proxy, socket });
return socket;
}
if (agent) {
const s = await agent.connect(req, opts);
if (!(s instanceof net.Socket)) {
throw new Error(
'Expected a `net.Socket` to be returned from agent'
);
}
req.emit('proxy', { proxy, socket: s });
return s;
}
throw new Error(`Could not determine proxy type for: ${proxy}`);
} catch (err) {
debug('Got error for proxy %o: %o', proxy, err);
req.emit('proxy', { proxy, error: err });
}
}
throw new Error(
`Failed to establish a socket connection to proxies: ${JSON.stringify(
proxies
)}`
);
}
}