This repository has been archived by the owner on Jan 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridgesDB.js
55 lines (48 loc) · 1.87 KB
/
bridgesDB.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
import fetch from 'node-fetch'
import { parse } from 'node-html-parser'
import { AbortController } from 'node-abort-controller'
const bridgesDBAPIURL = 'https://bridges.torproject.org/bridges?transport=obfs4'
export async function getBridges(captchaID, captchaResponse) {
const controller = new AbortController()
const signal = controller.signal
const proxyTimeout = setTimeout(() => controller.abort(), 15000)
try {
const responseRaw = await fetch(bridgesDBAPIURL, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
captcha_challenge_field: captchaID,
captcha_response_field: captchaResponse,
submit: 'submit'
}),
method: 'POST',
...(global.USE_PROXY && { agent: global.proxyAgent }),
signal
})
} catch(e) {
if(e?.message === 'The operation was aborted.') throw 'Timeout'
else throw e
}
if(responseRaw.status !== 200) throw 'Error'
clearTimeout(proxyTimeout)
const response = await responseRaw.text()
const root = parse(response)
const bridges = root.querySelector('#bridgelines')?.innerText
if(!bridges) throw 'Error'
return bridges
}
export async function requestBridges() {
const responseRaw = await fetch(bridgesDBAPIURL, global.USE_PROXY && { agent: global.proxyAgent })
const response = await responseRaw.text()
const root = parse(response)
try {
const captchaImage = root.querySelector('#captcha-box > img').getAttribute('src').split('data:image/jpeg;base64,', 2)[1]
const captchaChallengeID = root.querySelector('#captcha_challenge_field').getAttribute('value')
return { captchaImage, captchaChallengeID }
} catch(e) {
if(e?.message === 'Cannot read properties of undefined (reading \'split\')') {
throw `Cannot get captcha box. HTTP-Status: ${responseRaw.status}, HTML: ${response}`
} else {
throw e
}
}
}