Skip to content

Commit d21b24d

Browse files
committed
Get rid of leftover constant packet IDs
1 parent 09959f8 commit d21b24d

File tree

5 files changed

+73
-39
lines changed

5 files changed

+73
-39
lines changed

android/app/src/main/java/com/enderchat/modules/connection/ConnectionModule.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
9292
aesDecipher = Cipher.getInstance("AES/CFB8/NoPadding").apply {
9393
init(Cipher.DECRYPT_MODE, secretKey, iv)
9494
}
95-
val result = directlyWritePacket(0x01, packetBytes)
95+
val result = directlyWritePacket(PacketIds(-1).encryptionResponse, packetBytes)
9696
aesCipher = Cipher.getInstance("AES/CFB8/NoPadding").apply {
9797
init(Cipher.ENCRYPT_MODE, secretKey, iv)
9898
}
@@ -127,6 +127,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
127127
}
128128
hashSet
129129
}
130+
val ids = PacketIds(protocolVersion) // TODO: Receive packet IDs from JavaScript.
130131

131132
// Start thread which handles creating the connection and then reads packets from it.
132133
// This avoids blocking the main thread on writeLock and keeps the UI thread responsive.
@@ -176,7 +177,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
176177

177178
// Send Login Start packet.
178179
val loginPacketData = Base64.decode(loginPacket, Base64.DEFAULT)
179-
socket.getOutputStream().write(Packet(0x00, loginPacketData).writePacket())
180+
socket.getOutputStream().write(Packet(ids.loginStart, loginPacketData).writePacket())
180181
} catch (e: Exception) {
181182
lock.write {
182183
if (this@ConnectionModule.socket == socket) {
@@ -187,8 +188,6 @@ class ConnectionModule(reactContext: ReactApplicationContext)
187188
return@launch
188189
}
189190

190-
val ids = PacketIds(protocolVersion)
191-
192191
// Re-use the current thread, start reading from the socket.
193192
val buffer = ByteArrayOutputStream()
194193
val buf = ByteArray(4096)

android/app/src/main/java/com/enderchat/modules/connection/PacketIds.kt

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,48 @@ const val PROTOCOL_VERSION_1203 = 765
1111
const val PROTOCOL_VERSION_1205 = 766
1212
const val PROTOCOL_VERSION_1212 = 768
1313

14-
class PacketIds(protocolVersion: Int) {
14+
data class PacketIds(
15+
val protocolVersion: Int,
16+
1517
// Login state packet IDs
16-
val loginSuccess = 0x02
17-
val loginAcknowledged = 0x03
18-
val setCompression = 0x03
18+
var loginStart: Int = 0x00,
19+
var encryptionResponse: Int = 0x01,
20+
var loginSuccess: Int = 0x02,
21+
var loginAcknowledged: Int = 0x03,
22+
var setCompression: Int = 0x03,
1923

2024
// Configuration state packet IDs
21-
val configurationKeepAliveClientBound =
25+
var configurationKeepAliveClientBound: Int =
2226
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x04
23-
else 0x03
27+
else 0x03,
2428

25-
val configurationKeepAliveServerBound =
29+
var configurationKeepAliveServerBound: Int =
2630
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x04
27-
else 0x03
31+
else 0x03,
2832

29-
val finishConfigurationClientBound =
33+
var finishConfigurationClientBound: Int =
3034
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x03
31-
else 0x02
35+
else 0x02,
3236

33-
val finishConfigurationServerBound =
37+
var finishConfigurationServerBound: Int =
3438
if (protocolVersion >= PROTOCOL_VERSION_1205) 0x03
35-
else 0x02
39+
else 0x02,
3640

3741
// Play state packet IDs
38-
val startConfigurationClientBound =
42+
var startConfigurationClientBound: Int =
3943
if (protocolVersion >= PROTOCOL_VERSION_1212) 0x70
4044
else if (protocolVersion >= PROTOCOL_VERSION_1205) 0x69
4145
else if (protocolVersion >= PROTOCOL_VERSION_1203) 0x67
4246
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x65
43-
else -1
47+
else -1,
4448

45-
val acknowledgeConfigurationServerBound =
49+
var acknowledgeConfigurationServerBound: Int =
4650
if (protocolVersion >= PROTOCOL_VERSION_1212) 0x0e
4751
else if (protocolVersion >= PROTOCOL_VERSION_1205) 0x0c
4852
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x0b
49-
else -1
53+
else -1,
5054

51-
val playKeepAliveClientBound =
55+
var playKeepAliveClientBound: Int =
5256
if (protocolVersion >= PROTOCOL_VERSION_1212) 0x27
5357
else if (protocolVersion >= PROTOCOL_VERSION_1205) 0x26
5458
else if (protocolVersion >= PROTOCOL_VERSION_1202) 0x24
@@ -58,9 +62,9 @@ class PacketIds(protocolVersion: Int) {
5862
else if (protocolVersion >= PROTOCOL_VERSION_119) 0x1e
5963
else if (protocolVersion >= PROTOCOL_VERSION_117) 0x21
6064
else if (protocolVersion >= PROTOCOL_VERSION_1164) 0x1f
61-
else -1
65+
else -1,
6266

63-
val playKeepAliveServerBound =
67+
var playKeepAliveServerBound: Int =
6468
if (protocolVersion >= PROTOCOL_VERSION_1212) 0x1a
6569
else if (protocolVersion >= PROTOCOL_VERSION_1205) 0x18
6670
else if (protocolVersion >= PROTOCOL_VERSION_1203) 0x15
@@ -71,5 +75,5 @@ class PacketIds(protocolVersion: Int) {
7175
else if (protocolVersion >= PROTOCOL_VERSION_119) 0x11
7276
else if (protocolVersion >= PROTOCOL_VERSION_117) 0x0f
7377
else if (protocolVersion >= PROTOCOL_VERSION_1164) 0x10
74-
else -1
75-
}
78+
else -1,
79+
)

src/minecraft/connection/javascript.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ const initiateJavaScriptConnection = async (
8585
// Initialise Handshake with server.
8686
socket.write(makeBasePacket(0x00, concatPacketData(handshakeData)), () =>
8787
// Send Login Start packet.
88-
socket.write(makeBasePacket(0x00, getLoginPacket(opts))),
88+
socket.write(
89+
makeBasePacket(
90+
packetIds.SERVERBOUND_LOGIN_START(opts.protocolVersion) ?? 0,
91+
getLoginPacket(opts),
92+
),
93+
),
8994
)
9095
})
9196
socket.on('close', () => {
@@ -138,7 +143,10 @@ const initiateJavaScriptConnection = async (
138143
) {
139144
if (version >= protocolMap['1.20.2']) {
140145
conn
141-
.writePacket(0x03 /* Login Acknowledged */, Buffer.from([]))
146+
.writePacket(
147+
packetIds.SERVERBOUND_LOGIN_ACKNOWLEDGED(version) ?? 0,
148+
Buffer.from([]),
149+
)
142150
.catch((err: unknown) => conn.emit('error', err))
143151
conn.state = ConnectionState.CONFIGURATION
144152
} else conn.state = ConnectionState.PLAY
@@ -191,7 +199,9 @@ const initiateJavaScriptConnection = async (
191199
) {
192200
const [msgId] = readVarInt(packet.data)
193201
const rs = concatPacketData([writeVarInt(msgId), false])
194-
conn.writePacket(0x02, rs).catch((err: unknown) => conn.emit('error', err))
202+
conn
203+
.writePacket(packetIds.SERVERBOUND_LOGIN_PLUGIN_RESPONSE(version) ?? 0, rs)
204+
.catch((err: unknown) => conn.emit('error', err))
195205
} else if (
196206
packet.id === packetIds.CLIENTBOUND_ENCRYPTION_REQUEST(version) &&
197207
conn.state === ConnectionState.LOGIN
@@ -210,7 +220,10 @@ const initiateJavaScriptConnection = async (
210220
async (secret: Buffer, response: Buffer) => {
211221
const AES_ALG = 'aes-128-cfb8'
212222
conn.aesDecipher = createDecipheriv(AES_ALG, secret, secret)
213-
await conn.writePacket(0x01, response)
223+
await conn.writePacket(
224+
packetIds.SERVERBOUND_ENCRYPTION_RESPONSE(version) ?? 0,
225+
response,
226+
)
214227
conn.aesCipher = createCipheriv(AES_ALG, secret, secret)
215228
},
216229
)

src/minecraft/connection/native.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ export class NativeServerConnection extends events.EventEmitter implements Serve
9797
const { protocolVersion: version } = options
9898
// Set Compression and Keep Alive are handled in native for now.
9999
// When modifying this code, apply the same changes to the JavaScript back-end.
100-
if (packet.id === 0x02 /* Login Success */ && this.state === ConnectionState.LOGIN) {
100+
if (
101+
packet.id === packetIds.CLIENTBOUND_LOGIN_SUCCESS(version) &&
102+
this.state === ConnectionState.LOGIN
103+
) {
101104
this.state =
102105
version >= protocolMap['1.20.2']
103106
? ConnectionState.CONFIGURATION // Ack sent by native code
@@ -113,8 +116,8 @@ export class NativeServerConnection extends events.EventEmitter implements Serve
113116
) {
114117
this.state = ConnectionState.CONFIGURATION // Ack sent by native code
115118
} else if (
116-
// Disconnect (login), Disconnect (configuration) or Disconnect (play)
117-
(packet.id === 0x00 && this.state === ConnectionState.LOGIN) ||
119+
(packet.id === packetIds.CLIENTBOUND_DISCONNECT_LOGIN(version) &&
120+
this.state === ConnectionState.LOGIN) ||
118121
(packet.id === packetIds.CLIENTBOUND_DISCONNECT_CONFIGURATION(version) &&
119122
this.state === ConnectionState.CONFIGURATION) ||
120123
(packet.id === packetIds.CLIENTBOUND_DISCONNECT_PLAY(version) &&
@@ -124,17 +127,24 @@ export class NativeServerConnection extends events.EventEmitter implements Serve
124127
packet.data, // The Disconnect (login) packet always returns JSON.
125128
this.state === ConnectionState.LOGIN ? undefined : version,
126129
)[0]
127-
} else if (packet.id === 0x04 && this.state === ConnectionState.LOGIN) {
128-
/* Login Plugin Request */
130+
} else if (
131+
packet.id === packetIds.CLIENTBOUND_LOGIN_PLUGIN_REQUEST(version) &&
132+
this.state === ConnectionState.LOGIN
133+
) {
129134
const [msgId] = readVarInt(packet.data)
130135
const rs = concatPacketData([writeVarInt(msgId), false])
131-
this.writePacket(0x02, rs).catch((err: unknown) => this.emit('error', err))
132-
} else if (packet.id === 0x01 && this.state === ConnectionState.LOGIN) {
133-
/* Encryption Request */
136+
this.writePacket(packetIds.SERVERBOUND_LOGIN_PLUGIN_RESPONSE(version) ?? 0, rs).catch(
137+
(err: unknown) => this.emit('error', err),
138+
)
139+
} else if (
140+
packet.id === packetIds.CLIENTBOUND_ENCRYPTION_REQUEST(version) &&
141+
this.state === ConnectionState.LOGIN
142+
) {
134143
const { accessToken, selectedProfile } = options
135144
if (!accessToken || !selectedProfile) {
136-
this.disconnectReason =
137-
'{"text":"This server requires a premium account to be logged in!"}'
145+
this.disconnectReason = {
146+
text: 'This server requires a premium account to be logged in!',
147+
}
138148
this.close()
139149
return
140150
}

src/minecraft/packets/ids.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ const packetIds = {
1717
CLIENTBOUND_SET_COMPRESSION: generateIdFunction([[protocolMap['1.16.4'], 0x03]]),
1818
CLIENTBOUND_LOGIN_PLUGIN_REQUEST: generateIdFunction([[protocolMap['1.16.4'], 0x04]]),
1919

20+
// ===================
21+
// Serverbound (login)
22+
// ===================
23+
SERVERBOUND_LOGIN_START: generateIdFunction([[protocolMap['1.16.4'], 0x00]]),
24+
SERVERBOUND_ENCRYPTION_RESPONSE: generateIdFunction([[protocolMap['1.16.4'], 0x01]]),
25+
SERVERBOUND_LOGIN_PLUGIN_RESPONSE: generateIdFunction([[protocolMap['1.16.4'], 0x02]]),
26+
SERVERBOUND_LOGIN_ACKNOWLEDGED: generateIdFunction([[protocolMap['1.16.4'], 0x03]]),
27+
2028
// ===========================
2129
// Clientbound (configuration)
2230
// ===========================

0 commit comments

Comments
 (0)