-
Notifications
You must be signed in to change notification settings - Fork 237
/
index.ts
207 lines (179 loc) · 4.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
import { SocksClient, SocksProxy, SocksClientOptions } from 'socks';
import { Agent, AgentConnectOpts } from 'agent-base';
import createDebug from 'debug';
import * as dns from 'dns';
import * as net from 'net';
import * as tls from 'tls';
import * as http from 'http';
import { URL } from 'url';
const debug = createDebug('socks-proxy-agent');
function parseSocksURL(url: URL): { lookup: boolean; proxy: SocksProxy } {
let lookup = false;
let type: SocksProxy['type'] = 5;
const host = url.hostname;
// From RFC 1928, Section 3: https://tools.ietf.org/html/rfc1928#section-3
// "The SOCKS service is conventionally located on TCP port 1080"
const port = parseInt(url.port, 10) || 1080;
// figure out if we want socks v4 or v5, based on the "protocol" used.
// Defaults to 5.
switch (url.protocol.replace(':', '')) {
case 'socks4':
lookup = true;
type = 4;
break;
// pass through
case 'socks4a':
type = 4;
break;
case 'socks5':
lookup = true;
type = 5;
break;
// pass through
case 'socks': // no version specified, default to 5h
type = 5;
break;
case 'socks5h':
type = 5;
break;
default:
throw new TypeError(
`A "socks" protocol must be specified! Got: ${String(
url.protocol
)}`
);
}
const proxy: SocksProxy = {
host,
port,
type,
};
if (url.username) {
Object.defineProperty(proxy, 'userId', {
value: decodeURIComponent(url.username),
enumerable: false,
});
}
if (url.password != null) {
Object.defineProperty(proxy, 'password', {
value: decodeURIComponent(url.password),
enumerable: false,
});
}
return { lookup, proxy };
}
type SocksSocketOptions = Omit<net.TcpNetConnectOpts, 'port' | 'host'>;
export type SocksProxyAgentOptions = Omit<
SocksProxy,
// These come from the parsed URL
'ipaddress' | 'host' | 'port' | 'type' | 'userId' | 'password'
> & {
socketOptions?: SocksSocketOptions;
} &
http.AgentOptions;
export class SocksProxyAgent extends Agent {
static protocols = [
'socks',
'socks4',
'socks4a',
'socks5',
'socks5h',
] as const;
readonly shouldLookup: boolean;
readonly proxy: SocksProxy;
timeout: number | null;
socketOptions: SocksSocketOptions | null;
constructor(uri: string | URL, opts?: SocksProxyAgentOptions) {
super(opts);
const url = typeof uri === 'string' ? new URL(uri) : uri;
const { proxy, lookup } = parseSocksURL(url);
this.shouldLookup = lookup;
this.proxy = proxy;
this.timeout = opts?.timeout ?? null;
this.socketOptions = opts?.socketOptions ?? null;
}
/**
* Initiates a SOCKS connection to the specified SOCKS proxy server,
* which in turn connects to the specified remote host and port.
*/
async connect(
req: http.ClientRequest,
opts: AgentConnectOpts
): Promise<net.Socket> {
const { shouldLookup, proxy, timeout } = this;
if (!opts.host) {
throw new Error('No `host` defined!');
}
let { host } = opts;
const { port, lookup: lookupFn = dns.lookup } = opts;
if (shouldLookup) {
// Client-side DNS resolution for "4" and "5" socks proxy versions.
host = await new Promise<string>((resolve, reject) => {
// Use the request's custom lookup, if one was configured:
lookupFn(host, {}, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
const socksOpts: SocksClientOptions = {
proxy,
destination: {
host,
port: typeof port === 'number' ? port : parseInt(port, 10),
},
command: 'connect',
timeout: timeout ?? undefined,
// @ts-expect-error the type supplied by socks for socket_options is wider
// than necessary since socks will always override the host and port
socket_options: this.socketOptions ?? undefined,
};
const cleanup = (tlsSocket?: tls.TLSSocket) => {
req.destroy();
socket.destroy();
if (tlsSocket) tlsSocket.destroy();
};
debug('Creating socks proxy connection: %o', socksOpts);
const { socket } = await SocksClient.createConnection(socksOpts);
debug('Successfully created socks proxy connection');
if (timeout !== null) {
socket.setTimeout(timeout);
socket.on('timeout', () => cleanup());
}
if (opts.secureEndpoint) {
// The proxy is connecting to a TLS server, so upgrade
// this socket connection to a TLS connection.
debug('Upgrading socket connection to TLS');
const servername = opts.servername || opts.host;
const tlsSocket = tls.connect({
...omit(opts, 'host', 'path', 'port'),
socket,
servername: net.isIP(servername) ? undefined : servername,
});
tlsSocket.once('error', (error) => {
debug('Socket TLS error', error.message);
cleanup(tlsSocket);
});
return tlsSocket;
}
return socket;
}
}
function omit<T extends object, K extends [...Array<keyof T>]>(
obj: T,
...keys: K
): {
[K2 in Exclude<keyof T, K[number]>]: T[K2];
} {
const ret = {} as { [K in keyof typeof obj]: (typeof obj)[K] };
let key: keyof typeof obj;
for (key in obj) {
if (!keys.includes(key)) {
ret[key] = obj[key];
}
}
return ret;
}