-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
relay.js
292 lines (259 loc) · 9.93 KB
/
relay.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
const { Client } = require('./client')
const { Server } = require('./server')
const { Player } = require('./serverPlayer')
const { realmAuthenticate } = require('./client/auth')
const debug = globalThis.isElectron ? console.debug : require('debug')('minecraft-protocol')
const debugging = false // Do re-encoding tests
class RelayPlayer extends Player {
constructor (server, conn) {
super(server, conn)
this.startRelaying = false
this.once('join', () => { // The client has joined our proxy
this.flushDownQueue() // Send queued packets from the upstream backend
this.startRelaying = true
})
this.downQ = []
this.upQ = []
this.upInLog = (...msg) => console.debug('* Backend -> Proxy', ...msg)
this.upOutLog = (...msg) => console.debug('* Proxy -> Backend', ...msg)
this.downInLog = (...msg) => console.debug('* Client -> Proxy', ...msg)
this.downOutLog = (...msg) => console.debug('* Proxy -> Client', ...msg)
if (!server.options.logging) {
this.upInLog = () => { }
this.upOutLog = () => { }
this.downInLog = () => { }
this.downOutLog = () => { }
}
this.outLog = this.downOutLog
this.inLog = this.downInLog
this.chunkSendCache = []
this.sentStartGame = false
this.respawnPacket = []
}
// Called when we get a packet from backend server (Backend -> PROXY -> Client)
readUpstream (packet) {
if (!this.startRelaying) {
this.upInLog('Client not ready, queueing packet until join')
this.downQ.push(packet)
return
}
let des
try {
des = this.server.deserializer.parsePacketBuffer(packet)
} catch (e) {
this.server.deserializer.dumpFailedBuffer(packet, this.connection.address)
console.error(this.connection.address, e)
if (!this.options.omitParseErrors) {
this.disconnect('Server packet parse error')
}
return
}
const name = des.data.name
const params = des.data.params
this.upInLog('->', name, params)
if (name === 'play_status' && params.status === 'login_success') return // Already sent this, this needs to be sent ASAP or client will disconnect
if (debugging) { // some packet encode/decode testing stuff
this.server.deserializer.verify(des, this.server.serializer)
}
this.emit('clientbound', des.data, des)
if (!des.canceled) {
if (name === 'start_game') {
setTimeout(() => {
this.sentStartGame = true
}, 500)
} else if (name === 'level_chunk' && !this.sentStartGame) {
this.chunkSendCache.push(params)
return
}
this.queue(name, params)
}
if (this.chunkSendCache.length > 0 && this.sentStartGame) {
for (const entry of this.chunkSendCache) {
this.queue('level_chunk', entry)
}
this.chunkSendCache = []
}
}
// Send queued packets to the connected client
flushDownQueue () {
this.downOutLog('Flushing downstream queue')
for (const packet of this.downQ) {
const des = this.server.deserializer.parsePacketBuffer(packet)
this.write(des.data.name, des.data.params)
}
this.downQ = []
}
// Send queued packets to the backend upstream server from the client
flushUpQueue () {
this.upOutLog('Flushing upstream queue')
for (const e of this.upQ) { // Send the queue
const des = this.server.deserializer.parsePacketBuffer(e)
if (des.data.name === 'client_cache_status') {
// Currently not working, force off the chunk cache
} else {
this.upstream.write(des.data.name, des.data.params)
}
}
this.upQ = []
}
// Called when the server gets a packet from the downstream player (Client -> PROXY -> Backend)
readPacket (packet) {
// The downstream client conn is established & we got a packet to send to upstream server
if (this.startRelaying) {
// Upstream is still connecting/handshaking
if (!this.upstream) {
const des = this.server.deserializer.parsePacketBuffer(packet)
this.downInLog('Got downstream connected packet but upstream is not connected yet, added to q', des)
this.upQ.push(packet) // Put into a queue
return
}
// Send queued packets
this.flushUpQueue()
this.downInLog('recv', packet)
// TODO: If we fail to parse a packet, proxy it raw and log an error
const des = this.server.deserializer.parsePacketBuffer(packet)
if (debugging) { // some packet encode/decode testing stuff
this.server.deserializer.verify(des, this.server.serializer)
}
this.emit('serverbound', des.data, des)
if (des.canceled) return
switch (des.data.name) {
case 'client_cache_status':
// Force the chunk cache off.
this.upstream.queue('client_cache_status', { enabled: this.enableChunkCaching })
break
case 'set_local_player_as_initialized':
this.status = 3
// falls through
default:
// Emit the packet as-is back to the upstream server
this.downInLog('Relaying', des.data)
this.upstream.queue(des.data.name, des.data.params)
}
} else {
super.readPacket(packet)
}
}
close (reason) {
this.upstream?.close(reason)
super.close(reason)
}
}
class Relay extends Server {
/**
* Creates a new non-transparent proxy connection to a destination server
* @param {Options} options
*/
constructor (options) {
super(options)
this.RelayPlayer = options.relayPlayer || RelayPlayer
this.forceSingle = options.forceSingle
this.upstreams = new Map()
this.conLog = debug
this.enableChunkCaching = options.enableChunkCaching
}
// Called after a new player joins our proxy. We first create a new Client to connect to
// the remote server. Then we listen to some events and proxy them over. The queue and
// flushing logic is more of an accessory to make sure the server or client recieves
// a packet, no matter what state it's in. For example, if the client wants to send a
// packet to the server but it's not connected, it will add to the queue and send as soon
// as a connection with the server is established.
async openUpstreamConnection (ds, clientAddr) {
const options = {
authTitle: this.options.authTitle,
flow: this.options.flow,
deviceType: this.options.deviceType,
offline: this.options.destination.offline ?? this.options.offline,
username: this.options.offline ? ds.profile.name : ds.profile.xuid,
version: this.options.version,
realms: this.options.destination.realms,
host: this.options.destination.host,
port: this.options.destination.port,
batchingInterval: this.options.batchingInterval,
onMsaCode: (code) => {
if (this.options.onMsaCode) {
this.options.onMsaCode(code, ds)
} else {
ds.disconnect("It's your first time joining. Please sign in and reconnect to join this server:\n\n" + code.message)
}
},
profilesFolder: this.options.profilesFolder,
backend: this.options.backend,
autoInitPlayer: false
}
if (this.options.destination.realms) {
await realmAuthenticate(options)
}
const client = new Client(options)
// Set the login payload unless `noLoginForward` option
if (!client.noLoginForward) client.options.skinData = ds.skinData
client.ping().then(pongData => {
client.connect()
}).catch(err => {
this.emit('error', err)
})
this.conLog('Connecting to', options.host, options.port)
client.outLog = ds.upOutLog
client.inLog = ds.upInLog
client.once('join', () => {
// Tell the server to disable chunk cache for this connection as a client.
// Wait a bit for the server to ack and process, the continue with proxying
// otherwise the player can get stuck in an empty world.
client.write('client_cache_status', { enabled: this.enableChunkCaching })
ds.upstream = client
ds.flushUpQueue()
this.conLog('Connected to upstream server')
client.readPacket = (packet) => ds.readUpstream(packet)
this.emit('join', /* client connected to proxy */ ds, /* backend server */ client)
})
client.on('error', (err) => {
ds.disconnect('Server error: ' + err.message)
debug(clientAddr, 'was disconnected because of error', err)
this.upstreams.delete(clientAddr.hash)
})
client.on('close', (reason) => {
ds.disconnect('Backend server closed connection')
this.upstreams.delete(clientAddr.hash)
})
this.upstreams.set(clientAddr.hash, client)
}
// Close a connection to a remote backend server.
closeUpstreamConnection (clientAddr) {
const up = this.upstreams.get(clientAddr.hash)
if (!up) throw Error(`unable to close non-open connection ${clientAddr.hash}`)
up.close()
this.upstreams.delete(clientAddr.hash)
this.conLog('closed upstream connection', clientAddr)
}
// Called when a new player connects to our proxy server. Once the player has authenticated,
// we can open an upstream connection to the backend server.
onOpenConnection = (conn) => {
if (this.forceSingle && this.clientCount > 0) {
this.conLog('dropping connection as single client relay', conn)
conn.close()
} else {
this.clientCount++
const player = new this.RelayPlayer(this, conn)
this.conLog('New connection from', conn.address)
this.clients[conn.address] = player
this.emit('connect', player)
player.on('login', () => {
this.openUpstreamConnection(player, conn.address)
})
player.on('close', (reason) => {
this.conLog('player disconnected', conn.address, reason)
this.clientCount--
delete this.clients[conn.address]
})
}
}
// When our server is closed, make sure to kick all of the connected clients and run emitters.
close (...a) {
for (const [, v] of this.upstreams) {
v.close(...a)
}
super.close(...a)
}
}
// Too many things called 'Proxy' ;)
module.exports = { Relay }