-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (73 loc) · 1.9 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
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
const WebSocket = require('ws')
const socket = 'ws://192.168.1.3:19131'
class WSSocket {
constructor() {
this.wssocket = new WebSocket.Server({ port: 5000 })
this.server = undefined
this.codemaker = undefined
this.listener()
}
listener() {
console.log('[INFO] Do /connect 127.0.0.1:5000')
this.wssocket.on('connection', (server) => {
this.server = server
this.codemaker = new CodeMaker(this)
this.socketListener()
})
}
socketListener() {
this.server.on('message', (data) => {
console.log(data)
this.codemaker.sendPacket(data)
})
}
sendCommand(command, requestId) {
this.server.send(JSON.stringify(
{
body: {
commandLine: command,
version: 1
},
header: {
requestId: requestId,
messagePurpose: "commandRequest",
version: 1
}
}
))
}
sendBuffer(buffer) {
this.server.send(buffer)
}
}
class CodeMaker {
constructor(wssocket) {
this.socket = new WebSocket(socket, {
perMessageDeflate: false
})
this.wssocket = wssocket
this.publicKey = undefined
this.salt = undefined
this.listener()
}
listener() {
this.socket.on('open', () => {})
this.socket.on('error', () => {})
this.socket.on('message', (data) => {
console.log(data)
if (data == undefined) return
if (!data.includes('header')) return this.wssocket.sendBuffer(data)
const parsedPacket = JSON.parse(data)
if (parsedPacket.body.commandLine.startsWith('enableencryption')) {
const token = parsedPacket.body.commandLine.replace('enableencryption ', '').split(' ')
this.publicKey = token[0]
this.salt = token[1]
}
this.wssocket.sendCommand(parsedPacket.body.commandLine, parsedPacket.header.requestId)
})
}
sendPacket(packet) {
this.socket.send(packet)
}
}
new WSSocket()