-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPClient.ts
801 lines (647 loc) · 18.6 KB
/
HTTPClient.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
// Copyright (c) 2022 DBotThePony
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the
// following conditions:
// The above copyright notice and this permission notice shall be
// included in all copiesor substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
import http = require('http')
import https = require('https')
import urlModule = require('url')
import {HeaderGenerator} from 'header-generator'
import {unzip, brotliDecompress, inflate} from 'zlib'
import {promisify} from 'util'
import {RatelimitBucket} from './RatelimitBucket'
const punzip = promisify(unzip)
const pbrotliDecompress = promisify(brotliDecompress)
const pinflate = promisify(inflate)
export interface Headers {
[key: string]: string
}
export interface BakedRequest {
onStart?: (response: http.IncomingMessage) => void
url: urlModule.URL
body?: string | Buffer
headers?: Headers
method: 'POST' | 'GET'
https: boolean
followRedirects: boolean
agent: http.Agent | https.Agent
reject: (err?: any) => void
resolve: (value: Buffer) => void
traceback?: string
requestFailures: number
}
export interface RequestConfig {
headers?: Headers
onStart?: (response: http.IncomingMessage) => void
body?: string | Buffer
followRedirects?: boolean
}
export interface HTTPCookieData {
name: string
value: string
path: string | null
domain: string | null
expires: Date | null
secure: boolean
}
export class HTTPCookie implements HTTPCookieData {
public path: string | null = null
public domain: string | null = null
public expires: Date | null = null
public secure: boolean = false
constructor(
public name: string,
public value: string
) {
}
public save(): HTTPCookieData {
return {
value: this.value,
name: this.name,
path: this.path,
domain: this.domain,
expires: this.expires,
secure: this.secure,
}
}
public load(value: HTTPCookieData) {
this.value = value.value
this.name = value.name
this.path = value.path
this.domain = value.domain
this.expires = value.expires ? new Date(value.expires) : null
this.secure = value.secure
}
}
export class HTTPError {
constructor(
public response: number | undefined,
public body: string | Buffer | null,
public message: string | null
) {
}
public toString() {
return `HTTPError[response: ${this.response}; body: ${this.body}; message: ${this.message}]`
}
}
export class CookieJar {
public cookies: HTTPCookie[] = []
public onCookieAdded?: () => void
constructor() {
}
public save() {
const listing = []
for (const cookie of this.cookies) {
listing.push(cookie.save())
}
return listing
}
public load(values: HTTPCookieData[]) {
this.cookies = []
for (const cookie of values) {
const construct = new HTTPCookie('', '')
construct.load(cookie)
this.cookies.push(construct)
}
}
public static domainMatcher = /https?:\/\/(.*?)(\/|$)/i
public get(url: urlModule.URL): HTTPCookie[] {
const result: HTTPCookie[] = []
const secure = url.protocol == 'https:'
const path = url.pathname
const domainName = url.host
for (const cookie of this.cookies) {
if (
(!cookie.secure || secure) &&
(cookie.path == null || path.startsWith(cookie.path)) &&
(cookie.domain == null || domainName.endsWith(cookie.domain)) &&
(cookie.expires == null || cookie.expires.getTime() >= Date.now())
) {
result.push(cookie)
}
}
return result
}
public getSpecific(url: urlModule.URL, name: string) {
for (const cookie of this.get(url)) {
if (cookie.name == name) {
return cookie
}
}
return null
}
public build(url: urlModule.URL): string {
const list = this.get(url)
const result: string[] = []
for (const cookie of list) {
result.push(`${cookie.name}=${cookie.value}`)
}
return result.join('; ')
}
public removeSpecific(url: urlModule.URL, name: string) {
for (const cookie of this.get(url)) {
if (cookie.name == name) {
const i = this.cookies.indexOf(cookie)
if (i == -1) {
throw new Error('HOW')
}
this.cookies.splice(i, 1)
return cookie
}
}
return null
}
public put(cookie: string, domain: string): boolean {
const split = cookie.split(';')
let secure = false
let firstPair = true
let key: string | undefined
let value: string | undefined
let expires: Date | null = null
let path: string | null = null
for (const token of split) {
const trim = token.trim()
const lower = trim.toLowerCase()
if (lower == 'secure') {
secure = true
} else if (lower == 'httponly') {
// no meaningful input
} else if (lower.indexOf('=') != -1) {
const [_key, _value] = trim.split('=')
if (firstPair) {
firstPair = false
key = _key
value = _value
} else {
const lowerKey = _key.toLowerCase()
if (lowerKey == 'expires') {
expires = new Date(_value)
} else if (lowerKey == 'domain') {
domain = _value
} else if (lowerKey == 'path') {
path = _value
} else if (lowerKey == 'max-age') {
const digit = parseInt(_value)
if (digit == 0) {
expires = new Date(0)
} else {
expires = new Date(Date.now() + digit)
}
}
}
}
}
if (key == undefined || value == undefined) {
return false
}
const cookieConstruct = new HTTPCookie(key, value)
cookieConstruct.expires = expires
cookieConstruct.domain = domain
cookieConstruct.path = path
cookieConstruct.secure = secure
for (const i in this.cookies) {
if (
this.cookies[i].domain == cookieConstruct.domain &&
this.cookies[i].name == cookieConstruct.name &&
this.cookies[i].path == cookieConstruct.path
) {
this.cookies.splice(parseInt(i), 1)
}
}
this.cookies.push(cookieConstruct)
if (this.onCookieAdded != undefined) {
this.onCookieAdded()
}
return true
}
}
import { SocksProxyAgent } from 'socks-proxy-agent';
class ConnectionLock {
constructor(private slot: ConnectionSlot, private token: number) {
}
public unlock() {
return this.slot.unlock(this.token)
}
public heartbeat() {
return this.slot.heartbeat(this.token)
}
}
class ConnectionSlot {
private timer?: NodeJS.Timer
private token = -1
private lastActivity = Date.now()
private lockups = 0
constructor(private callback: (slot: ConnectionSlot) => any, private slotID: number) {
}
public heartbeat(token: number) {
if (this.token == token) {
this.lastActivity = Date.now()
}
}
public lock() {
if (this.timer !== undefined) {
return false
}
this.timer = setInterval(() => {
if (this.lastActivity + 10_000 < Date.now()) {
this._unlock(true)
}
}, 1_000)
return new ConnectionLock(this, ++this.token)
}
private _unlock(force = false) {
clearInterval(this.timer!)
this.lastActivity = Date.now()
this.timer = undefined
if (force)
process.stderr.write(`[HTTP Client] Waiting for request to finish for way too long, freeing up connection slot! This happened ${++this.lockups} times on slot ${this.slotID}\n`)
this.callback(this)
}
public unlock(token: number) {
if (this.timer === undefined || this.token != token) {
return false
}
this._unlock()
return true
}
}
export class HTTPClient {
public cookies = new CookieJar()
private socksagent?: http.Agent
public ratelimit?: RatelimitBucket
// watchdog, lastActivity, token
private connectionSlotsActivity: ConnectionSlot[] = []
private headerGenerator = new HeaderGenerator({
browserListQuery: 'last 10 versions',
devices: ['desktop', 'mobile'],
operatingSystems: ['windows', 'macos', 'linux', 'android'],
})
constructor(
private connections = 8,
private httpsagent: https.Agent = new https.Agent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: connections
}),
private httpagent: http.Agent = new http.Agent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: connections
}),
private proxyAddress?: string,
private proxyPort?: number,
proxySocksAddress?: string,
proxyPortSocks?: number,
) {
if (proxySocksAddress != undefined && proxyPortSocks != undefined) {
const agent = new SocksProxyAgent({
hostname: proxySocksAddress,
port: proxyPortSocks,
})
agent.maxSockets = this.connections
agent.options.maxSockets = this.connections
agent.options.maxFreeSockets = this.connections
agent.options.maxTotalSockets = this.connections
agent.options.keepAlive = true
agent.options.keepAliveMsecs = 10000
this.socksagent = agent
}
for (let i = 0; i < connections; i++) {
this.connectionSlotsActivity.push(new ConnectionSlot((slot) => this.onFree(slot), i))
}
}
private waiters: ((slot: ConnectionLock) => any)[] = []
private alloc(): Promise<ConnectionLock> {
return new Promise((resolve) => {
for (const slot of this.connectionSlotsActivity) {
const result = slot.lock()
if (result !== false) {
resolve(result)
return
}
}
this.waiters.push(resolve)
})
}
private onFree(slot: ConnectionSlot) {
if (this.waiters.length != 0) {
const resolve = this.waiters.splice(0, 1)[0]
const result = slot.lock()
if (result === false) {
throw new Error('HOW')
}
resolve(result)
}
}
private async handleRequest(value: BakedRequest) {
if (this.ratelimit != undefined) {
await this.ratelimit.wait()
}
const buildCookie = this.cookies.build(value.url)
const fingerprintHeaders = this.headerGenerator.getHeaders()
const params: http.RequestOptions = {
hostname: value.url.host,
port: value.url.port,
path: value.url.pathname + value.url.search,
agent: value.agent,
method: value.method,
headers: {
...fingerprintHeaders, // generates User-Agent and other headers to look more realistic
'Connection': 'keep-alive',
'Accept': '*/*',
'Accept-Encoding': 'br, gzip, deflate'
}
}
if (!value.https && this.proxyAddress != undefined && this.proxyPort != undefined) {
params.hostname = this.proxyAddress
params.port = this.proxyPort
params.path = value.url.href
params.agent = this.httpagent
params.headers!['Host'] = value.url.hostname
} else if (value.https && this.socksagent != undefined) {
params.agent = this.socksagent
}
if (buildCookie != '') {
params.headers!['Cookie'] = buildCookie
}
if (value.headers != undefined) {
for (const key in value.headers) {
params.headers![key] = value.headers[key]
}
}
if (value.body != undefined) {
params.headers!['content-length'] = value.body.length
}
const lock = await this.alloc()
let finished = false
let lastActivity = Date.now()
let stream: http.IncomingMessage | undefined = undefined
let validateCompressedInput: (() => Promise<false | Buffer>) | undefined = undefined
let endCallback: ((buffer?: Buffer) => void) | undefined = undefined
const timeoutID = setInterval(async () => {
if (finished) {
clearInterval(timeoutID)
return
}
if (lastActivity + 20_000 < Date.now()) {
if (validateCompressedInput !== undefined) {
const bufferOutput = await validateCompressedInput()
if (bufferOutput) {
process.stderr.write(`[HTTP Client] 'end' event was never fired, yet HTTP request was completed! This is a node.js bug!\n`)
// wtfffffff???
// hey, node.js, what the fuck
// where is my `end` event
clearInterval(timeoutID)
endCallback!(bufferOutput)
stream!.destroy()
}
return
}
// damn SLOW
// better to reject
clearInterval(timeoutID)
finished = true
stream?.destroy()
if (stream === undefined) {
lock.unlock()
}
value.reject('Too slow download stream')
}
}, 1000)
const callback = (response: http.IncomingMessage) => {
stream = response
lastActivity = Date.now()
if (response.headers['set-cookie']) {
for (const cookie of response.headers['set-cookie']) {
this.cookies.put(cookie, value.url.host)
}
}
if (response.statusCode == 301 || response.statusCode == 302) {
response.on('data', (chunk: Buffer) => {})
response.on('end', () => {})
if (response.headers.location && value.followRedirects) {
// redirect, it might also switch protocols
value.onStart = () => {}
try {
if (response.headers.location.startsWith('//')) {
// same protocol, different hostname and path
value.url = new urlModule.URL(value.url.protocol + response.headers.location)
} else if (response.headers.location.startsWith('/')) {
// same protocol and hostname, different path
value.url = new urlModule.URL(value.url.protocol + '//' + value.url.hostname + response.headers.location)
} else {
value.url = new urlModule.URL(response.headers.location)
}
} catch(err) {
lock.unlock()
value.reject(new HTTPError(response.statusCode, String(err), 'Location URL is invalid: ' + response.headers.location))
return
}
lock.unlock()
value.https = value.url.protocol == 'https:'
value.agent = value.url.protocol == 'https:' ? this.httpsagent : this.httpagent
this.handleRequest(value)
finished = true
clearInterval(timeoutID)
// response.destroy()
} else {
lock.unlock()
clearInterval(timeoutID)
finished = true
// response.destroy()
value.reject(new HTTPError(response.statusCode, null, 'Server returned ' + response.statusCode))
}
return
}
if (value.onStart) {
value.onStart(response)
}
let memcache: Buffer[] = []
validateCompressedInput = async () => {
let can = false
switch (response.headers['content-encoding']) {
case 'br':
can = true
break
case 'gzip':
can = true
break
case 'deflate':
can = true
break
}
if (!can) {
return false
}
let size = 0
for (const buff of memcache) {
size += buff.length
}
let newbuff = Buffer.allocUnsafe(size)
let offset = 0
for (const buff of memcache) {
for (let i = 0; i < buff.length; i++) {
newbuff[offset + i] = buff[i]
}
offset += buff.length
}
let decompressed: Buffer
try {
switch (response.headers['content-encoding']) {
case 'br':
decompressed = await pbrotliDecompress(newbuff)
break
case 'gzip':
decompressed = await punzip(newbuff)
break
case 'deflate':
decompressed = await pinflate(newbuff)
break
}
} catch(err) {
return false
}
return decompressed!
}
response.on('data', (chunk: Buffer) => {
lastActivity = Date.now()
lock.heartbeat()
memcache.push(chunk)
})
response.on('error', (err) => {
if (finished) {
return
}
console.error(`Throw INNER ${err} on ${value.traceback}`)
lock.unlock()
clearInterval(timeoutID)
value.reject(err)
})
endCallback = async (newbuff?: Buffer) => {
if (finished) {
return
}
clearInterval(timeoutID)
lock.unlock()
finished = true
if (newbuff === undefined) {
let size = 0
for (const buff of memcache) {
size += buff.length
}
newbuff = Buffer.allocUnsafe(size)
let offset = 0
for (const buff of memcache) {
for (let i = 0; i < buff.length; i++) {
newbuff[offset + i] = buff[i]
}
offset += buff.length
}
// mark buffers as dead for gc
memcache = []
try {
switch (response.headers['content-encoding']) {
case 'br':
newbuff = await pbrotliDecompress(newbuff)
break
case 'gzip':
newbuff = await punzip(newbuff)
break
case 'deflate':
newbuff = await pinflate(newbuff)
break
}
} catch(err) {
value.reject(err)
return
}
}
if (response.statusCode == 200 || response.statusCode == 206) {
value.resolve(newbuff)
} else {
value.reject(new HTTPError(response.statusCode, newbuff, 'Server returned ' + response.statusCode))
}
}
response.on('end', endCallback)
}
const request = value.https ? https.request(params, callback) : http.request(params, callback)
if (value.body != undefined) {
request.write(value.body)
}
request.once('error', (err) => {
if (finished) {
return
}
finished = true
clearInterval(timeoutID)
lock.unlock()
// accept two failures
if (value.requestFailures < 2) {
value.requestFailures++
this.handleRequest(value)
} else {
value.reject(err)
}
})
request.end()
}
public get(url: string, config: RequestConfig = {}): Promise<Buffer> {
return new Promise((resolve, reject) => {
const urlobj = new urlModule.URL(url)
if (urlobj.protocol != 'https:' && urlobj.protocol != 'http:') {
throw new TypeError('Protocol is not supported: ' + urlobj.protocol)
} else {
this.handleRequest({
url: urlobj,
method: 'GET',
https: urlobj.protocol == 'https:',
headers: config.headers,
onStart: config.onStart,
reject: reject,
resolve: resolve,
body: config.body,
followRedirects: config.followRedirects != undefined ? config.followRedirects : true,
agent: urlobj.protocol == 'https:' ? this.httpsagent : this.httpagent,
traceback: new Error().stack,
requestFailures: 0
})
}
})
}
public post(url: string, config: RequestConfig = {}): Promise<Buffer> {
return new Promise((resolve, reject) => {
const urlobj = new urlModule.URL(url)
if (urlobj.protocol != 'https:' && urlobj.protocol != 'http:') {
throw new TypeError('Protocol is not supported: ' + urlobj.protocol)
} else {
this.handleRequest({
url: urlobj,
method: 'POST',
https: urlobj.protocol == 'https:',
headers: config.headers,
onStart: config.onStart,
followRedirects: config.followRedirects != undefined ? config.followRedirects : true,
reject: reject,
resolve: resolve,
body: config.body,
agent: urlobj.protocol == 'https:' ? this.httpsagent : this.httpagent,
traceback: new Error().stack,
requestFailures: 0
})
}
})
}
}