-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
350 lines (332 loc) · 12.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
const UI = require('../ui'),
fs = require('fs'),
path = require('path'),
ws = require('ws'),
http = require('http'),
bodyParser = require('../ui/node_modules/body-parser'),
defParse = require('./lib/defParser'),
LogSave = require('./lib/logSave').LogSave
module.exports = function packetLogger(mod) {
//For standalone env install express and replace these vars
//const UI = require('express')
//const ui = UI()
//ui.listen(port, () => console.log(`Express listening on port ${port}`));
const ui = new UI(mod),
{ command } = mod.require
const tbox = typeof mod.compileProto === 'undefined';
let packetBatchCache = [], protocolData
if (tbox) protocolData = require('../../data/data.json')
/*Setup WS and api server*/
if(!fs.existsSync(path.join(__dirname, "savedData.json")))
fs.writeFileSync(path.join(__dirname, "savedData.json"), JSON.stringify({savedFilters: {}}, null, 1))
let savedData = require('./savedData.json'),
packetCache = [],
filters = {
allowlist: [],
blocklist: []
},
paused = false,
logGroup = {
logServer: true,
logClient: true
},
maxLogSize = 200 //default size of 200 TODO: add static to settings object later
const server = http.createServer().listen(0)
const wsServer = new ws.Server({ server: server })
// Websocket keep alive
function heartbeat() {
this.isAlive = true;
}
wsServer.on('connection', socket => {
socket.isAlive = true;
socket.on('pong', heartbeat)
socket.send(JSON.stringify({
syncState: {
packets: packetCache.map((packet) => packet.name),
filters,
paused,
logServer: logGroup.logServer,
logClient: logGroup.logClient,
maxLogSize
}
}))
})
const wsPing = setInterval(() => { //keepalive ping interval
wsServer.clients.forEach((client) => {
if (!client.isAlive) return client.terminate();
client.isAlive = false;
client.ping(() => { })
});
}, 30000)
wsServer.on('close', () => {
clearInterval(wsPing)
})
ui.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
ui.use(bodyParser.json({ limit: '50mb' }));
let savedDataFile = path.join(__dirname, 'savedData.json')
ui.use(UI.static(path.join(__dirname, 'build')))
const batchPacketUpdates = setInterval(() => { //sending packets in batches as to not flood the UI's listener
if (packetBatchCache.length > 0) {
if (maxLogSize) { //false if no limit
if (packetCache.length + packetBatchCache.length > maxLogSize) packetCache.splice(0, packetCache.length + packetBatchCache.length - maxLogSize) // max size 200
}
wsServer.clients.forEach((client) => {
client.send(JSON.stringify({ packets: packetBatchCache.map((packet) => packet.name) }))
})
packetCache.push(...packetBatchCache)
packetBatchCache = []
}
}, 100)
function syncState() { //Probably a better way of handleing UI desyncronization
wsServer.clients.forEach((client) => {
client.send(JSON.stringify({
syncState: {
packets: packetCache.map((packet) => packet.name),
filters,
paused,
logServer: logGroup.logServer,
logClient: logGroup.logClient,
maxLogSize
}
}))
})
}
ui.post('/changeFilters', (req, res) => { //post add/remove filter
if (req.body.entry) { //add filter
filters[req.body.type].push(req.body.entry)
filters[req.body.type].sort()
} else { //remove filter
filters[req.body.type].splice(req.body.index, 1)
}
res.json({})
syncState();
})
ui.post('/clearFilters', (req, res) => {
filters[req.body.type] = []
res.json({})
syncState();
})
ui.get('/pause', (req, res) => { //pause packet cap
paused = !paused
res.json({})
syncState();
})
ui.post('/saveFilters', (req, res) => { //post save filters
Object.assign(savedData.savedFilters, {
[req.body.name]: {
allowlist: [...filters.allowlist],
blocklist: [...filters.blocklist]
}
})
fs.writeFileSync(savedDataFile, JSON.stringify(savedData, (key, value) => typeof value === 'bigint' ? value.toString() + 'n' : value, '\t'))
res.json({})
})
ui.get('/savedFilters', (req, res) => { //get saved filters list
res.json(Object.keys(savedData.savedFilters))
})
ui.post('/loadFilters', (req, res) => { //get load filter
filters = { //what a mutable nightmare
allowlist: [...savedData.savedFilters[req.body.selectedTemplate].allowlist],
blocklist: [...savedData.savedFilters[req.body.selectedTemplate].blocklist]
}
res.json({});
syncState();
})
ui.post('/saveLogs', (req, res) => { //post save logs
let serializedData = LogSave.serialize(packetCache)
fs.writeFileSync(path.join(__dirname, 'logs', `${req.body.name}.bin`), serializedData)
res.json({});
})
ui.get('/savedLogs', (req, res) => { //get saved logs list
res.json(getLogs())
})
ui.post('/loadLogs', (req, res) => { //get load log
paused = true //auto pause log when you load
let logRead = new Uint8Array(fs.readFileSync(path.join(__dirname, 'logs', `${req.body.logname}`)))
packetCache = LogSave.parse(logRead)
res.json({});
syncState();
})
ui.post('/deleteFromSaved', (req, res) => { //post delete from saved data add new delete for bin logs
if (req.body.type === "savedLogs") {
fs.unlinkSync(path.join(__dirname, 'logs', req.body.name))
res.json(getLogs())
} else if (req.body.type === "savedFilters") {
delete savedData.savedFilters[req.body.name]
fs.writeFileSync(savedDataFile, JSON.stringify(savedData, (key, value) => typeof value === 'bigint' ? value.toString() + 'n' : value, '\t'))
res.json(Object.keys(savedData[req.body.type]))
}
})
ui.post('/getHex', (req, res) => { //Hex Tool Call
res.json(readHex(req.body.hexx.split(/\s|\n/g).join("")))
})
ui.post('/filterGroup', (req, res) => {
logGroup = Object.assign(logGroup, req.body)
res.json({})
syncState();
})
ui.post('/getPacketData', (req, res) => {
//parse def here
const { name, code, version, data } = packetCache[req.body.index]
let parsed = null,
badDef = false,
packetData, def
if (tbox) {
try {
parsed = mod.dispatch.fromRaw(name, version, data)
} catch (e) { badDef = true }
//get def string
try {
def = Buffer.from(protocolData.protocol[`${name}.${version}.def`], 'base64').toString()
} catch (e) { def = `No defintion found for packet: ${name}` }
} else {
try {
parsed = mod.parse(code, version, data)
badDef = mod.packetLength(code, version, parsed) !== data.length
} catch (e) { badDef = true }
//get def string
const protocolPath = path.join(__dirname, '..', '..', 'node_modules', 'tera-data', 'protocol'),
defPath = path.join(protocolPath, `${name}.${version}.def`)
try {
def = defParse(fs.readFileSync(defPath, 'utf-8'), protocolPath).join('\n')
} catch (e) { def = `No defintion found for packet: ${name}` }
}
//build and send
packetData = {
...packetCache[req.body.index], ...{
data: parsed ? parsed : { Error: `Could not parse ${name}: No definition found.` },
hex: data.toString('hex'),
def,
badDef
}
}
let packetDataString = JSON.stringify(packetData, (key, value) =>
typeof value === 'bigint' ? value.toString() + 'n' : value // serialize bigint as string
)
res.json(packetDataString)
})
ui.get('/clearLog', (req, res) => {
packetCache = [];
packetBatchCache = [];
syncState();
res.json({});
})
ui.get('/wsPort', (req, res) => {
res.json({ port: server.address().port })
})
ui.post('/setMaxLogSize', (req, res) => {
maxLogSize = req.body.size;
res.json({});
syncState();
})
//https://github.com/tera-mods/debug credit to Pinkie for original hook implementation
const cache = []
//hook original packets
mod.hook('*', 'raw', { order: -Infinity }, (code, data) => {
cache.push({ code, data: Buffer.from(data) })
})
//hook all packets after mods
mod.hook('*', 'raw', {
order: Infinity, filter: {
// These need to go through so we can clean up from our previous hook, even if we're not logging them
fake: null,
modified: null,
silenced: null
}
}, (code, data) => {
if (data.$fake) {
if (!data.$silenced) //don't need to log silenced packets
writePacket({ code, data }, {
incoming: data.$incoming,
fake: true,
silenced: false
})
return
}
const origPacket = cache.pop()
if (origPacket)
writePacket(origPacket, {
incoming: data.$incoming,
fake: false,
silenced: data.$modified
})
if (data.$modified && !data.$silenced) //don't need to log silenced packets
writePacket({ code, data }, {
incoming: data.$incoming,
fake: true,
silenced: false
})
})
//apply filters write packet
function writePacket(pkt, flags) {
let name, defVersion
if (tbox) {
try {
let packetInfo = mod.dispatch.resolve(pkt.code)
name = packetInfo.name
defVersion = packetInfo.version
} catch {
name = `*${flags.incoming ? "SU" : "CU"}_NEEDS_REMAP_${pkt.code.toString(16).toString().toUpperCase()}`
defVersion = 0
}
} else {
name = mod.dispatch.protocol.packetEnum.code.get(pkt.code)
let defs = name && mod.dispatch.protocol.constructor.defs.get(name)
defVersion = defs && Math.max(...defs.keys())
name = name ? name : `*${flags.incoming ? "SU" : "CU"}_NEEDS_REMAP_${pkt.code.toString(16).toString().toUpperCase()}`
}
//apply filters
if (filters.allowlist.length > 0 && !filters.allowlist.includes(name)) return;
if (filters.blocklist.includes(name)) return;
if (!logGroup.logServer && flags.incoming) return;
if (!logGroup.logClient && !flags.incoming) return;
let packet = {
code: pkt.code,
name: name,
version: defVersion,
fake: flags.fake,
data: pkt.data,
timestamp: Math.round(Date.now() / 1000)
}
if (!paused) {
packetBatchCache.push(packet)
}
}
function readHex(hexx) {
let hex = hexx.length % 2 > 0 ? "0" + hexx : hexx //byte pad
let len = hex.length / 2 //len = byte length
let buf = Buffer.alloc(0xFF) //initialize buffer
try {
buf.write(hex, 0, len, 'hex') //write hex
if (len > 0xFF) throw "hex too large"
if (hex.match(/[0-9a-f]+/i)[0] !== hex) throw "invalid hex"
}
catch (e) {
if (e === "too long") return e
return "invalid hex"
}
return {
int16: len > 2 ? "overflow" : buf.readInt16LE().toString(),
int32: len > 4 ? "overflow" : buf.readInt32LE().toString(),
int64: len > 8 ? "overflow" : buf.readBigInt64LE().toString(),
float: len > 4 ? "overflow" : buf.readFloatLE().toString(),
double: len > 8 ? "overflow" : buf.readDoubleLE().toString(),
string: buf.toString('ucs2', 0, len)
}
}
function getLogs() {
let logs = fs.readdirSync(path.join(__dirname, 'logs'))
logs.splice(logs.indexOf(".gitignore"), 1)
return logs
}
command.add('logger', {
$default() {
ui.open()
}
})
this.destructor = () => { command.remove(['logger']) }
}