-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (51 loc) · 1.53 KB
/
index.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
import WebSocket from 'ws'
import { readFile } from 'node:fs/promises'
const img = await readFile('./captcha.jpeg', { encoding: 'base64' })
const ws = new WebSocket('wss://s.2captcha.com')
const authData = {
method: 'auth',
requestId: `auth_${Date.now()}`,
key: process.env.APIKEY || '',
options: {
allSessions: true,
suppressSuccess: false
}
}
const captchData = {
method: 'normal',
body: img,
requestId: `captcha_${Date.now()}`,
options: {
minLen: 4,
maxLen: 7,
lang: 'ru'
}
}
ws.on('open', () => {
console.log(`connection opened`)
ws.send(JSON.stringify(authData))
})
ws.on('close', () => {
console.log(`Connection closed.`);
})
ws.on('message', (incomingMsg) => {
const data = JSON.parse(incomingMsg)
switch (data.method) {
case 'auth':
if (!data.success) return console.log(`Auth failed: ${data.error}`)
console.log(`Authenticated, sending captcha...`)
ws.send(JSON.stringify(captchData))
break
case 'normal':
if (!data.success) return console.log(`Captcha not accepted: ${data.error}`)
console.log(`Captcha accepted. Id is ${data.captchaId}`)
break
case 'solution':
if (!data.success) return console.log(`Solution failed: ${data.error}`)
console.log(`Captcha with id ${data.captchaId} is solved. The answer is: ${data.code}`)
ws.close()
break
default:
console.log(data)
}
})