-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocks-http2-agent.js
219 lines (200 loc) · 6.56 KB
/
socks-http2-agent.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
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
/* Socks Proxy Http2 Agent */
/* jshint esversion: 6 */
/* jshint node: true */
"use strict";
const tls = require('tls'),
http2 = require('http2'),
socks_proxy = require('./socks-proxy');
module.exports = SocksHttp2Agent;
/* --- Http2 Agent Creation --- */
function SocksHttp2Agent(options, selectProxyEndpoint) {
this.pool = [];
if (options.proxy) {
this.proxy = options.proxy;
} else if (selectProxyEndpoint) {
this.selectProxyEndpoint = selectProxyEndpoint;
this.proxy = null;
} else {
throw new Error('Proxy option is required');
}
this.bridge = options.bridge ? options.bridge : null;
this.credentials = options.credentials ? options.credentials : null;
if (isNaN(options.connectTimeout)) {
this.connectTimeout = 10000;
} else {
this.connectTimeout = options.connectTimeout;
}
if (Number.isInteger(options.socketTimeout) && options.socketTimeout >= 0) {
this.socketTimeout = options.socketTimeout;
} else {
this.socketTimeout = 60000;
}
if (options.rejectUnauthorized === false) {
this.rejectUnauthorized = false;
} else {
this.rejectUnauthorized = true;
}
}
/* --- Http2 Agent Clients Managment --- */
SocksHttp2Agent.prototype.findClient = function(options, callback) {
if (!options.endpoint) {
callback(new Error('Endpoint option is required'));
return;
}
if (typeof options.endpoint.host !== 'string' ||
!Number.isInteger(options.endpoint.port)) {
callback(new Error('Endpoint option is invalid'));
return;
}
if (typeof options.scheme !== 'string') {
callback(new Error('Http2 :scheme must be a string'));
return;
}
if (typeof options.authority !== 'string') {
callback(new Error('Http2 :authority must be a string'));
return;
}
if (this.proxy === null) {
this.selectProxyEndpoint((err, proxy) => {
if (err) {
callback(err);
} else {
this.proxy = proxy;
this.findClientInternal(options, callback);
}
});
} else {
this.findClientInternal(options, callback);
}
};
SocksHttp2Agent.prototype.findClientInternal = function(options, callback) {
for (let i = 0; i < this.pool.length; i++) {
const client = this.pool[i];
if (client.ready &&
client.scheme === options.scheme &&
client.authority === options.authority) {
callback(null, client.session);
return;
}
}
const client = {
done: false,
ready: false,
scheme: options.scheme,
authority: options.authority,
session: null,
tlssocket: null,
netsocket: null
};
this.createNewClient(client, options, (err, session) => {
const endflag = !client.done;
client.done = true;
if (err) {
this.destroyClient(client);
}
if (endflag) {
callback(err, session);
}
});
};
/* --- Http2 Agent Clients Creation --- */
SocksHttp2Agent.prototype.createNewClient = function(client, options, callback) {
socks_proxy.connect({
proxy: this.proxy,
endpoint: options.endpoint,
timeout: this.connectTimeout,
bridge: this.bridge,
credentials: this.credentials
}, (err, socket, timedout) => {
if (err) {
if (timedout && this.selectProxyEndpoint) {
this.proxy = null;
}
callback(err);
} else {
client.netsocket = socket;
client.netsocket.on('error', (err) => {
callback(err);
}).on('close', () => {
callback(new Error('Socket closed'));
}).on('timeout', () => {
callback(new Error('Socket timed out'));
});
client.netsocket.setTimeout(this.socketTimeout);
if (options.scheme === 'https') {
client.tlssocket = tls.connect({
servername: options.authority,
socket: client.netsocket,
rejectUnauthorized: this.rejectUnauthorized,
ALPNProtocols: ['h2'],
});
client.tlssocket.on('error', (err) => {
callback(err);
}).on('close', () => {
callback(new Error('TLS Socket closed'));
}).on('timeout', () => {
callback(new Error('TLS Socket timed out'));
}).once('secureConnect', () => {
if (client.tlssocket.authorized || this.rejectUnauthorized === false) {
client.socket = client.tlssocket;
this.setupHttp2Session(client, options, callback);
} else {
callback(new Error('TLS Socket unauthorized'));
}
});
} else {
client.socket = client.netsocket;
this.setupHttp2Session(client, options, callback);
}
}
});
};
SocksHttp2Agent.prototype.setupHttp2Session = function(client, options, callback) {
if (!client.done) {
client.session = http2.connect(options.scheme + '://' + options.authority, {
createConnection: () => {
return client.socket;
}
});
client.session.on('error', (err) => {
callback(err);
}).on('close', (err) => {
callback(new Error('Http2 Session closed'));
}).on('timeout', (err) => {
callback(new Error('Http2 Session timed out'));
}).once('connect', () => {
if (!client.done) {
client.ready = true;
this.pool.push(client);
callback(null, client.session);
}
});
}
};
/* --- Http2 Agent Cleanup --- */
SocksHttp2Agent.prototype.destroyClient = function(client) {
client.ready = false;
if (client.session) {
try {
client.session.destroy();
} catch (unused) {}
}
if (client.tlssocket) {
try {
client.tlssocket.destroy();
} catch (unused) {}
}
if (client.netsocket) {
try {
client.netsocket.destroy();
} catch (unused) {}
}
};
SocksHttp2Agent.prototype.destroy = function() {
for (let i = 0; i < this.pool.length; i++) {
const client = this.pool[i];
if (client.ready) {
this.destroyClient(client);
}
}
};