forked from apify/proxy-chain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain.ts
207 lines (172 loc) · 6.54 KB
/
chain.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
import type { Buffer } from 'buffer';
import type dns from 'dns';
import type { EventEmitter } from 'events';
import http from 'http';
import https from 'https';
import type { URL } from 'url';
import type { Socket } from './socket';
import { badGatewayStatusCodes, createCustomStatusHttpResponse, errorCodeToStatusCode } from './statuses';
import type { SocketPreviousStats } from './utils/count_target_bytes';
import { countTargetBytes } from './utils/count_target_bytes';
import { getBasicAuthorizationHeader } from './utils/get_basic';
interface Options {
method: string;
headers: string[];
path?: string;
localAddress?: string;
family?: number;
lookup?: typeof dns['lookup'];
}
export interface HandlerOpts {
upstreamProxyUrlParsed: URL;
localAddress?: string;
ipFamily?: number;
dnsLookup?: typeof dns['lookup'];
customTag?: unknown;
}
interface ChainOpts {
request: { url?: string };
sourceSocket: Socket;
head?: Buffer;
handlerOpts: HandlerOpts;
server: EventEmitter & { log: (connectionId: unknown, str: string) => void };
isPlain: boolean;
}
/**
* Passes the traffic to upstream HTTP proxy server.
* Client -> Apify -> Upstream -> Web
* Client <- Apify <- Upstream <- Web
*/
export const chain = (
{
request,
sourceSocket,
head,
handlerOpts,
server,
isPlain,
}: ChainOpts,
): void => {
if (head && head.length > 0) {
// HTTP/1.1 has no defined semantics when sending payload along with CONNECT and servers can reject the request.
// HTTP/2 only says that subsequent DATA frames must be transferred after HEADERS has been sent.
// HTTP/3 says that all DATA frames should be transferred (implies pre-HEADERS data).
//
// Let's go with the HTTP/3 behavior.
// There are also clients that send payload along with CONNECT to save milliseconds apparently.
// Beware of upstream proxy servers that send out valid CONNECT responses with diagnostic data such as IPs!
sourceSocket.unshift(head);
}
const { proxyChainId } = sourceSocket;
const { upstreamProxyUrlParsed: proxy, customTag } = handlerOpts;
const options: Options = {
method: 'CONNECT',
path: request.url,
headers: [
'host',
request.url!,
],
localAddress: handlerOpts.localAddress,
family: handlerOpts.ipFamily,
lookup: handlerOpts.dnsLookup,
};
if (proxy.username || proxy.password) {
options.headers.push('proxy-authorization', getBasicAuthorizationHeader(proxy));
}
const fn = proxy.protocol === 'https:' ? https.request : http.request;
const client = fn(proxy.origin, options as unknown as http.ClientRequestArgs);
client.once('socket', (targetSocket: Socket & SocketPreviousStats) => {
// socket can be re-used by multiple requests (HTTP keep alive)
// (even in multiple Server objects)
targetSocket.previousBytesRead = targetSocket.bytesRead;
targetSocket.previousBytesWritten = targetSocket.bytesWritten;
countTargetBytes(sourceSocket, targetSocket);
});
client.on('connect', (response, targetSocket, clientHead) => {
if (sourceSocket.readyState !== 'open') {
// Sanity check, should never reach.
targetSocket.destroy();
return;
}
targetSocket.on('error', (error) => {
server.log(proxyChainId, `Chain Destination Socket Error: ${error.stack}`);
sourceSocket.destroy();
});
sourceSocket.on('error', (error) => {
server.log(proxyChainId, `Chain Source Socket Error: ${error.stack}`);
targetSocket.destroy();
});
if (response.statusCode !== 200) {
server.log(proxyChainId, `Failed to authenticate upstream proxy: ${response.statusCode}`);
if (isPlain) {
sourceSocket.end();
} else {
const { statusCode } = response;
const status = statusCode === 401 || statusCode === 407
? badGatewayStatusCodes.AUTH_FAILED
: badGatewayStatusCodes.NON_200;
sourceSocket.end(createCustomStatusHttpResponse(status, `UPSTREAM${statusCode}`));
}
targetSocket.end();
server.emit('tunnelConnectFailed', {
proxyChainId,
response,
customTag,
socket: targetSocket,
head: clientHead,
});
return;
}
if (clientHead.length > 0) {
// See comment above
targetSocket.unshift(clientHead);
}
server.emit('tunnelConnectResponded', {
proxyChainId,
response,
customTag,
socket: targetSocket,
head: clientHead,
});
sourceSocket.write(isPlain ? '' : `HTTP/1.1 200 Connection Established\r\n\r\n`);
sourceSocket.pipe(targetSocket);
targetSocket.pipe(sourceSocket);
// Once target socket closes forcibly, the source socket gets paused.
// We need to enable flowing, otherwise the socket would remain open indefinitely.
// Nothing would consume the data, we just want to close the socket.
targetSocket.on('close', () => {
sourceSocket.resume();
if (sourceSocket.writable) {
sourceSocket.end();
}
});
// Same here.
sourceSocket.on('close', () => {
targetSocket.resume();
if (targetSocket.writable) {
targetSocket.end();
}
});
});
client.on('error', (error: NodeJS.ErrnoException) => {
server.log(proxyChainId, `Failed to connect to upstream proxy: ${error.stack}`);
// The end socket may get connected after the client to proxy one gets disconnected.
if (sourceSocket.readyState === 'open') {
if (isPlain) {
sourceSocket.end();
} else {
const statusCode = errorCodeToStatusCode[error.code!] ?? badGatewayStatusCodes.GENERIC_ERROR;
const response = createCustomStatusHttpResponse(statusCode, error.code ?? 'Upstream Closed Early');
sourceSocket.end(response);
}
}
});
sourceSocket.on('error', () => {
client.destroy();
});
// In case the client ends the socket too early
sourceSocket.on('close', () => {
client.destroy();
});
client.end();
};