-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
222 lines (200 loc) · 5.73 KB
/
Copy pathindex.js
File metadata and controls
222 lines (200 loc) · 5.73 KB
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
const udp = require('dgram')
const crc32 = require('buffer-crc32')
const util = require('util')
module.exports = NodeBe
function NodeBe (ip, port, password) {
this.socket = udp.createSocket('udp4')
this.ip = ip
this.port = port
this.password = password
this.sequenceNumber = 0
this.loggedIn = false
this.lastResponse = 0
this.lastCommand = 0
this.error = false
this.interval = undefined
this.multipacket = undefined
}
// inherit eventemitter
util.inherits(NodeBe, require('events').EventEmitter)
// login
NodeBe.prototype.connect = function () {
let self = this
this.socket.parent = this
this.socket.bind()
this.socket.on('error', function (err) {
console.log('Socket error: ' + err)
})
this.socket.on('message', function (message, requestInfo) {
self.lastResponse = new Date().getTime()
let buffer = Buffer.from(message)
if (buffer.toString('utf8', 0, 2) === 'BE') {
let payload = buffer.slice(6, buffer.length)
switch (payload[1].toString()) {
case '0':
//login-stuff
self.loggedIn = payload[2].toString()
if (self.loggedIn) {
self.emit('listening')
} else {
self.emit('error', 'Login failed')
self.loggedIn = false
self.error = true
self.close()
}
break
case '1':
//ack-package
if (payload.length === 3) return
if (payload[3].toString() > 0) {
//message single packet
self.acknowledge(payload[2])
self.sequenceNumber = payload[2]
self.emit('message', self.stripHeaderServerMessage(buffer).toString())
} else {
//message multi-packet
if (payload[5] === 0) {
self.multipacket = new Array(payload[4])
}
if (self.multipacket && self.multipacket.length === payload[4]) {
self.multipacket[payload[5]] = self.stripHeaderMultipacket(buffer)
}
if (payload[5] + 1 === payload[4]) {
let total = ''
for (let msg in self.multipacket) {
total += self.multipacket[msg].toString()
}
self.emit('message', total)
self.multipacket = undefined
}
}
break
case '2':
//message
self.acknowledge(payload[2])
self.sequenceNumber = payload[2]
self.emit('message', self.stripHeaderServerMessage(buffer).toString())
break
}
}
})
this.login()
this.interval = setInterval(function (client) {
client.keepAlive()
}, 25000, this)
}
// build command packet
NodeBe.prototype.sendCommand = function (command) {
if (this.loggedIn && !this.error) {
let buffer = Buffer.alloc(command.length + 2)
buffer[0] = 0x01
buffer[1] = 0
for (let i = 0; i < command.length; i++) {
buffer[2 + i] = command.charCodeAt(i)
}
let packet = this.buildPacket(buffer)
setTimeout(this.timeout, 3000, this)
this.send(packet)
}
}
// send prepared packet
NodeBe.prototype.send = function (data) {
if (this.error) { return 1 }
this.lastCommand = new Date().getTime()
this.socket.send(data, 0, data.length, this.port, this.ip)
}
// build ack package
NodeBe.prototype.acknowledge = function (sequenceNumber) {
let buffer = Buffer.alloc(2)
buffer[0] = 0x02
buffer[1] = sequenceNumber
let packet = this.buildPacket(buffer)
this.send(packet)
}
// build keepAlive packet
NodeBe.prototype.keepAlive = function () {
if (!this.loggedIn) { return }
let buffer = Buffer.alloc(2)
buffer[0] = 0x01
buffer[1] = 0
buffer[2] = 0
let packet = this.buildPacket(buffer)
setTimeout(this.timeout, 3000, this)
this.send(packet)
}
// add nessesary headers
NodeBe.prototype.buildPacket = function (command) {
let buffer = Buffer.alloc(7 + command.length)
buffer[0] = 0x42
buffer[1] = 0x45
let nbuffer = Buffer.alloc(1 + command.length)
nbuffer[0] = 0xFF
command.forEach(function (cur, i) {
nbuffer[1 + i] = cur
})
let crc = crc32(nbuffer)
for (let i = 0; i < 4; i++) {
buffer[5 - i] = crc[i]
}
nbuffer.forEach(function (cur, i) {
buffer[i + 6] = cur
})
return buffer
}
// build login packet
NodeBe.prototype.buildLoginPacket = function () {
let buffer = Buffer.alloc(this.password.length + 1)
buffer[0] = 0x00
for (let i = 0; i < this.password.length; i++) {
buffer[1 + i] = this.password.charCodeAt(i)
}
return this.buildPacket(buffer)
}
// send login packet
NodeBe.prototype.login = function () {
let packet = this.buildLoginPacket()
setTimeout(this.timeout, 3000, this)
this.send(packet)
}
// timeout to check for server shutdown/connection loss
NodeBe.prototype.timeout = function (client) {
if ((new Date().getTime() - client.lastResponse) >= 5000) {
try {
client.close()
} catch (e) {
console.log(e)
}
}
}
// strip BE header
NodeBe.prototype.stripHeader = function (message) {
let buffer = Buffer.alloc(message.length - 7)
for (let i = 0; i < buffer.length; i++) {
buffer[i] = message[i + 7]
}
return buffer
}
// strip server message header
NodeBe.prototype.stripHeaderServerMessage = function (message) {
let buffer = Buffer.alloc(message.length - 9)
buffer.forEach(function (cur, i) {
buffer[i] = message[i + 9]
})
return buffer
}
// strip multipacket header
NodeBe.prototype.stripHeaderMultipacket = function (message) {
let buffer = Buffer.alloc(message.length - 12)
buffer.forEach(function (cur, i) {
buffer[i] = message[i + 12]
})
return buffer
}
// close socket and shutdown
NodeBe.prototype.close = function () {
this.loggedIn = false
clearInterval(this.interval)
this.socket.unref()
this.socket.close()
this.emit('close')
}