forked from raywave/PixelX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
205 lines (171 loc) · 5.33 KB
/
core.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
const WebSocket = require('ws')
const axios = require('axios')
module.exports = class PixelBot {
constructor (wsslink, store) {
this.wsslink = wsslink
this.MAX_WIDTH = 1590
this.MAX_HEIGHT = 400
this.MAX_COLOR_ID = 25
this.MIN_COLOR_ID = 0
this.SIZE = this.MAX_WIDTH * this.MAX_HEIGHT
this.SEND_PIXEL = 0
this.ws = null
this.wsloaded = false
this.busy = false
this.isStartedWork = false
this.rCode = null
this.load(store).catch(console.error)
}
async load (store) {
this.startWork(store)
}
async resolveCode (store) {
try {
const url = new URL(this.wsslink)
const result = await axios.get('https://pixel2019.vkforms.ru/api/start', {
headers: {
'X-vk-sign': url.search,
},
})
let code = result.data.response.code
// eslint-disable-next-line no-eval
code = eval(store.replaceAll(code, 'window.', ''))
this.wsslink = this.wsslink.replace(/&c=.*/g, `&c=${code}`)
console.log(`> Код решён: ${code}`)
} catch (e) {
console.log('> Произошла ошибка при решении кода.', e)
}
}
async initWs (store) {
await this.resolveCode(store)
this.ws = new WebSocket(this.wsslink)
this.ws.on('open', async () => {
console.log('> Подключение к WebSocket было успешным.')
})
this.ws.on('message', async (event) => {
while (this.busy) {
await this.sleep(500)
}
try {
this.busy = true
if (typeof event === 'string') {
try {
const a = JSON.parse(event)
if (a.v) {
const codeRaw = a.v.code
let code = codeRaw
const funnyReplacesHs = {
'window.': '',
global: 'undefined',
"=== 'object'": "!== 'object'",
}
for (const replace of Object.keys(funnyReplacesHs)) {
code = store.replaceAll(code, replace, funnyReplacesHs[replace])
}
// eslint-disable-next-line no-eval
this.rCode = eval(code)
this.ws.send('R' + this.rCode)
this.wsloaded = true
console.log(`> Код-R решён: R${this.rCode}`)
}
} catch (e) {
}
} else {
const c = this.toArrayBuffer(event)
for (let d = c.byteLength / 4, e = new Int32Array(c, 0, d), f = Math.floor(d / 3), g = 0; g < f; g++) {
const h = e[3 * g], k = this.unpack(h), l = k.x, m = k.y, n = k.color
store.data[[l, m]] = n
}
}
if (!this.isStartedWork) {
this.startWork(store)
}
this.busy = false
} catch (e) {
this.busy = false
}
})
this.ws.on('close', () => {
console.log('> Exit')
this.ws = null
this.wsloaded = false
})
}
async sendPixel (store) {
const keys = Object.keys(store.pixelDataToDraw)
const ind = keys[Math.floor(Math.random() * keys.length)] // Рандомный элемент
const color = store.pixelDataToDraw[ind]
const coords = ind.split(',')
if (!store.data || !store.data[ind] || !store.data[ind] === color) {
await this.send(color, this.SEND_PIXEL, coords[0], coords[1], store)
if (store.data) {
store.data[ind] = color
}
if (keys.length < 1) {
console.error('! Проблемы с защитой ВКонтакте.')
}
setTimeout(() => {
this.sendPixel(store)
}, 60000)
} else {
await this.sendPixel(store)
}
}
async startWork (store) {
console.log('> Производится запуск скрипта.')
this.isStartedWork = true
await store.load()
await this.sendPixel(store)
}
async send (colorId, flag, x, y, store) {
const c = new ArrayBuffer(4)
new Int32Array(c, 0, 1)[0] = this.pack(colorId, flag, x, y)
if (!this.ws) {
await this.initWs(store)
}
while (!this.wsloaded) {
await this.sleep(500)
}
this.ws.send(c)
console.log(`> Был раскрашен пиксель [${x}, ${y}] (${colorId})`)
}
pack (colorId, flag, x, y) {
const b = parseInt(colorId, 10) + parseInt(flag, 10) * this.MAX_COLOR_ID
return parseInt(x, 10) + parseInt(y, 10) * this.MAX_WIDTH + this.SIZE * b
}
unpack (b) {
const c = Math.floor(b / this.SIZE)
const d = (b -= c * this.SIZE) % this.MAX_WIDTH
return {
x: d,
y: (b - d) / this.MAX_WIDTH,
color: c % this.MAX_COLOR_ID,
flag: Math.floor(c / this.MAX_COLOR_ID),
}
}
sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time))
}
toArrayBuffer (buf) {
const ab = new ArrayBuffer(buf.length)
const view = new Uint8Array(ab)
for (let i = 0; i < buf.length; ++i) {
view[i] = buf[i]
}
return ab
}
chunkString (str, length) {
return str.match(new RegExp('.{1,' + length + '}', 'g'))
}
shuffle (array) {
let currentIndex = array.length, temporaryValue, randomIndex
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
}
return array
}
}