forked from tailsjs/nodebrawl-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (77 loc) · 2.19 KB
/
index.js
File metadata and controls
99 lines (77 loc) · 2.19 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
const net = require('net')
const { execSync } = require('child_process')
const os = require('os')
const server = new net.Server()
const config = require('./config.json')
const PORT = config.ProxyPort
server.on('connection', async (client) => {
client.setNoDelay(true)
client.log = function (text) {
return console.log(`[LOG] >> ${text}`)
}
client.log('Client connected.')
const remote = net.createConnection({
host: config.ServerIP,
port: config.ServerPort
})
remote.setNoDelay(true)
client.on('data', async (packet) => {
try {
if (packet.length >= 7) {
const len = packet.readUIntBE(2, 3)
if (packet.length >= 7 + len) {
const message = {
id: packet.readUInt16BE(0),
len: len,
version: packet.readUInt16BE(5),
payload: packet.slice(7, len),
client,
}
client.log(`Gotcha ${message.id} client packet!`)
}
}
// forward client to server
remote.write(packet)
} catch (err) {
console.log(err)
}
})
// forward server to client
remote.on('data', (packet) => {
const len = packet.readUIntBE(2, 3)
if (packet.length >= 7 + len) {
const message = {
id: packet.readUInt16BE(0),
len: len,
version: packet.readUInt16BE(5),
payload: packet.slice(7, len),
client,
}
client.log(`Gotcha ${message.id} server packet!`)
}
client.write(packet)
})
client.on('end', async () => {
client.log('Client disconnected.')
remote.destroy()
})
remote.on('end', () => {
client.destroy()
})
client.on('error', async error => {
try {
client.log('A wild error!')
console.log(error)
client.destroy()
remote.destroy()
} catch (e) { }
})
remote.on('error', (err) => {
console.log(err)
client.destroy()
})
})
server.once('listening', () => console.log(`[SERVER] >> Proxy started on ${PORT} port!`))
server.listen(PORT)
process.on("uncaughtException", e => console.log(e))
process.on("unhandledRejection", e => console.log(e))