-
Notifications
You must be signed in to change notification settings - Fork 46
/
h1-proxy-agent.ts
146 lines (111 loc) · 3.98 KB
/
h1-proxy-agent.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
/* eslint-disable max-classes-per-file */
import { URL } from 'url';
import tls, { ConnectionOptions } from 'tls';
import http, { ClientRequest, ClientRequestArgs } from 'http';
import https from 'https';
interface AgentOptions extends http.AgentOptions {
proxy: string | URL;
disableConnect?: boolean;
}
const initialize = (self: http.Agent & { proxy: URL }, options: AgentOptions) => {
self.proxy = typeof options.proxy === 'string' ? new URL(options.proxy) : options.proxy;
};
const getBasic = (url: URL): string => {
let basic = '';
if (url.username || url.password) {
const username = decodeURIComponent(url.username);
const password = decodeURIComponent(url.password);
basic = Buffer.from(`${username}:${password}`).toString('base64');
return `Basic ${basic}`;
}
return basic;
};
export class HttpRegularProxyAgent extends http.Agent {
proxy!: URL;
constructor(options: AgentOptions) {
super(options);
initialize(this, options);
}
addRequest(request: ClientRequest, options: ClientRequestArgs): void {
if (options.socketPath) {
// @ts-expect-error @types/node is missing types
super.addRequest(request, options);
return;
}
const hostport = `${options.host}:${options.port}`;
const url = new URL(`${request.protocol}//${hostport}${request.path}`);
options = {
...options,
host: this.proxy.hostname,
port: this.proxy.port,
};
request.path = url.href;
const basic = getBasic(this.proxy);
if (basic) {
request.setHeader('proxy-authorization', basic);
}
// @ts-expect-error @types/node is missing types
super.addRequest(request, options);
}
}
export class HttpProxyAgent extends http.Agent {
proxy!: URL;
constructor(options: AgentOptions) {
super(options);
initialize(this, options);
}
createConnection(options: ConnectionOptions, callback: (error: Error | undefined, socket?: unknown) => void): void {
if (options.path) {
// @ts-expect-error @types/node is missing types
super.createConnection(options, callback);
return;
}
const fn = this.proxy.protocol === 'https:' ? https.request : http.request;
const hostport = `${options.host}:${options.port}`;
const headers: Record<string, string> = {
host: hostport,
};
const basic = getBasic(this.proxy);
if (basic) {
headers['proxy-authorization'] = basic;
headers.authorization = basic;
}
const connectRequest = fn(this.proxy, {
method: 'CONNECT',
headers,
path: hostport,
agent: false,
rejectUnauthorized: false,
});
connectRequest.once('connect', (response, socket, head) => {
if (head.length > 0 || response.statusCode !== 200) {
socket.destroy();
const error = new Error(`The proxy responded with ${response.statusCode}: ${head.toString()}`);
callback(error);
return;
}
if ((options as any).protocol === 'https:') {
callback(undefined, tls.connect({
...options,
socket,
}));
return;
}
callback(undefined, socket);
});
connectRequest.once('error', (error) => {
callback(error);
});
connectRequest.end();
}
}
export class HttpsProxyAgent extends https.Agent {
proxy!: URL;
constructor(options: AgentOptions) {
super(options);
initialize(this, options);
}
createConnection(options: ConnectionOptions, callback: (error: Error | undefined, socket?: unknown) => void): void {
HttpProxyAgent.prototype.createConnection.call(this, options, callback);
}
}