kojacoord-protocol 0.1.0

Minecraft protocol types, packet (de)serialization and version registry for the Kojacoord proxy.
Documentation
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use crate::codec::{Decode, Encode, PacketId};
use crate::error::ProtocolError;
use crate::types::VarInt;
use bytes::{Buf, BufMut, Bytes, BytesMut};

fn encode_string(s: &str, dst: &mut BytesMut) -> Result<(), ProtocolError> {
    let bytes = s.as_bytes();
    VarInt(bytes.len() as i32).encode(dst)?;
    dst.put_slice(bytes);
    Ok(())
}

fn decode_string(src: &mut Bytes, context: &'static str) -> Result<String, ProtocolError> {
    let len = VarInt::decode(src)?.0 as usize;
    if src.remaining() < len {
        return Err(ProtocolError::Io(std::io::Error::new(
            std::io::ErrorKind::UnexpectedEof,
            format!("Missing bytes for {context}"),
        )));
    }
    let mut b = vec![0u8; len];
    src.copy_to_slice(&mut b);
    String::from_utf8(b).map_err(|_| {
        ProtocolError::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            format!("Invalid UTF-8 in {context}"),
        ))
    })
}

fn encode_byte_array(data: &[u8], dst: &mut BytesMut) -> Result<(), ProtocolError> {
    VarInt(data.len() as i32).encode(dst)?;
    dst.put_slice(data);
    Ok(())
}

fn decode_byte_array(src: &mut Bytes, context: &'static str) -> Result<Vec<u8>, ProtocolError> {
    let len = VarInt::decode(src)?.0 as usize;
    if src.remaining() < len {
        return Err(ProtocolError::Io(std::io::Error::new(
            std::io::ErrorKind::UnexpectedEof,
            format!("Missing bytes for {context}"),
        )));
    }
    let mut b = vec![0u8; len];
    src.copy_to_slice(&mut b);
    Ok(b)
}

#[derive(Debug, Clone, PartialEq)]
pub struct ServerboundLoginStart {
    pub username: String,
}

impl PacketId for ServerboundLoginStart {
    fn packet_id(_ver: u32) -> u8 {
        0x00
    }
}

impl Encode for ServerboundLoginStart {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        encode_string(&self.username, dst)
    }
}

impl Decode for ServerboundLoginStart {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let username = decode_string(src, "ServerboundLoginStart username")?;
        Ok(Self { username })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundLoginDisconnect {
    pub reason: String,
}

impl PacketId for ClientboundLoginDisconnect {
    fn packet_id(_ver: u32) -> u8 {
        0x00
    }
}

impl Encode for ClientboundLoginDisconnect {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        encode_string(&self.reason, dst)
    }
}

impl Decode for ClientboundLoginDisconnect {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let reason = decode_string(src, "ClientboundLoginDisconnect reason")?;
        Ok(Self { reason })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundEncryptionRequest {
    pub server_id: String,
    pub public_key: Vec<u8>,
    pub verify_token: Vec<u8>,
}

impl PacketId for ClientboundEncryptionRequest {
    fn packet_id(_ver: u32) -> u8 {
        0x01
    }
}

impl Encode for ClientboundEncryptionRequest {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        let id_bytes = self.server_id.as_bytes();
        VarInt(id_bytes.len() as i32).encode(dst)?;
        dst.put_slice(id_bytes);

        VarInt(self.public_key.len() as i32).encode(dst)?;
        dst.put_slice(&self.public_key);

        VarInt(self.verify_token.len() as i32).encode(dst)?;
        dst.put_slice(&self.verify_token);

        Ok(())
    }
}

impl Decode for ClientboundEncryptionRequest {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let id_len = VarInt::decode(src)?.0 as usize;
        if src.remaining() < id_len {
            return Err(ProtocolError::UnexpectedEof);
        }
        let mut id_bytes = vec![0u8; id_len];
        src.copy_to_slice(&mut id_bytes);
        let server_id = String::from_utf8(id_bytes).map_err(|_| ProtocolError::UnexpectedEof)?;

        let key_len = VarInt::decode(src)?.0 as usize;
        if src.remaining() < key_len {
            return Err(ProtocolError::UnexpectedEof);
        }
        let mut public_key = vec![0u8; key_len];
        src.copy_to_slice(&mut public_key);

        let token_len = VarInt::decode(src)?.0 as usize;
        if src.remaining() < token_len {
            return Err(ProtocolError::UnexpectedEof);
        }
        let mut verify_token = vec![0u8; token_len];
        src.copy_to_slice(&mut verify_token);

        let remaining_trailing_bytes = src.remaining();
        src.advance(remaining_trailing_bytes);

        Ok(Self {
            server_id,
            public_key,
            verify_token,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ServerboundEncryptionResponse {
    pub shared_secret: Vec<u8>,
    pub verify_token: Vec<u8>,
}

impl PacketId for ServerboundEncryptionResponse {
    fn packet_id(_ver: u32) -> u8 {
        0x01
    }
}

impl Encode for ServerboundEncryptionResponse {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        encode_byte_array(&self.shared_secret, dst)?;
        encode_byte_array(&self.verify_token, dst)
    }
}

impl Decode for ServerboundEncryptionResponse {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let shared_secret = decode_byte_array(src, "ServerboundEncryptionResponse shared_secret")?;
        let verify_token = decode_byte_array(src, "ServerboundEncryptionResponse verify_token")?;
        Ok(Self {
            shared_secret,
            verify_token,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ProfileProperty {
    pub name: String,
    pub value: String,
    pub signature: Option<String>,
}

impl Encode for ProfileProperty {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        encode_string(&self.name, dst)?;
        encode_string(&self.value, dst)?;
        match &self.signature {
            Some(sig) => {
                dst.put_u8(1);
                encode_string(sig, dst)?;
            },
            None => dst.put_u8(0),
        }
        Ok(())
    }
}

impl Decode for ProfileProperty {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let name = decode_string(src, "ProfileProperty name")?;
        let value = decode_string(src, "ProfileProperty value")?;
        if src.remaining() < 1 {
            return Err(ProtocolError::Io(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "Missing signature flag in ProfileProperty",
            )));
        }
        let signature = if src.get_u8() != 0 {
            Some(decode_string(src, "ProfileProperty signature")?)
        } else {
            None
        };
        Ok(Self {
            name,
            value,
            signature,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundLoginSuccess {
    pub uuid: uuid::Uuid,
    pub username: String,
    pub properties: Vec<ProfileProperty>,
}

impl PacketId for ClientboundLoginSuccess {
    fn packet_id(_ver: u32) -> u8 {
        0x02
    }
}

impl Encode for ClientboundLoginSuccess {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        let uuid_str = self.uuid.hyphenated().to_string();
        let uuid_bytes = uuid_str.as_bytes();
        VarInt(uuid_bytes.len() as i32).encode(dst)?;
        dst.put_slice(uuid_bytes);

        let user_bytes = self.username.as_bytes();
        VarInt(user_bytes.len() as i32).encode(dst)?;
        dst.put_slice(user_bytes);

        Ok(())
    }
}

impl Decode for ClientboundLoginSuccess {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let uuid_len = VarInt::decode(src)?.0 as usize;
        if src.remaining() < uuid_len {
            return Err(ProtocolError::Io(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "Missing bytes for LoginSuccess UUID",
            )));
        }
        let mut uuid_bytes = vec![0u8; uuid_len];
        src.copy_to_slice(&mut uuid_bytes);
        let uuid_str = String::from_utf8(uuid_bytes).map_err(|_| {
            ProtocolError::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Invalid UTF-8 inside LoginSuccess UUID",
            ))
        })?;

        let uuid = uuid::Uuid::parse_str(&uuid_str).map_err(|_| {
            ProtocolError::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Invalid string format for UUID parsing",
            ))
        })?;

        let user_len = VarInt::decode(src)?.0 as usize;
        if src.remaining() < user_len {
            return Err(ProtocolError::Io(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "Missing bytes for LoginSuccess Username",
            )));
        }
        let mut user_bytes = vec![0u8; user_len];
        src.copy_to_slice(&mut user_bytes);
        let username = String::from_utf8(user_bytes).map_err(|_| {
            ProtocolError::Io(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Invalid UTF-8 inside LoginSuccess Username",
            ))
        })?;

        Ok(Self {
            uuid,
            username,
            properties: Vec::new(),
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundSetCompression {
    pub threshold: VarInt,
}

impl PacketId for ClientboundSetCompression {
    fn packet_id(_ver: u32) -> u8 {
        0x03
    }
}

impl Encode for ClientboundSetCompression {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        self.threshold.encode(dst)
    }
}

impl Decode for ClientboundSetCompression {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let threshold = VarInt::decode(src)?;
        Ok(Self { threshold })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ClientboundLoginPluginRequest {
    pub message_id: VarInt,
    pub channel: String,
    pub data: Vec<u8>,
}

impl PacketId for ClientboundLoginPluginRequest {
    fn packet_id(ver: u32) -> u8 {
        if ver == 340 {
            0xFF
        } else {
            0x04
        }
    }
}

impl Encode for ClientboundLoginPluginRequest {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        self.message_id.encode(dst)?;
        encode_string(&self.channel, dst)?;
        dst.put_slice(&self.data);
        Ok(())
    }
}

impl Decode for ClientboundLoginPluginRequest {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let message_id = VarInt::decode(src)?;
        let channel = decode_string(src, "ClientboundLoginPluginRequest channel")?;
        let remaining = src.remaining();
        let mut data = vec![0u8; remaining];
        src.copy_to_slice(&mut data);
        Ok(Self {
            message_id,
            channel,
            data,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ServerboundLoginPluginResponse {
    pub message_id: VarInt,
    pub successful: bool,
    pub data: Option<Vec<u8>>,
}

impl PacketId for ServerboundLoginPluginResponse {
    fn packet_id(ver: u32) -> u8 {
        if ver == 340 {
            0xFF
        } else {
            0x02
        }
    }
}

impl Encode for ServerboundLoginPluginResponse {
    fn encode(&self, dst: &mut BytesMut) -> Result<(), ProtocolError> {
        self.message_id.encode(dst)?;
        dst.put_u8(if self.successful { 1 } else { 0 });
        if self.successful {
            if let Some(ref data) = self.data {
                dst.put_slice(data);
            }
        }
        Ok(())
    }
}

impl Decode for ServerboundLoginPluginResponse {
    fn decode(src: &mut Bytes) -> Result<Self, ProtocolError> {
        let message_id = VarInt::decode(src)?;
        if src.remaining() < 1 {
            return Err(ProtocolError::Io(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "Missing successful flag in ServerboundLoginPluginResponse",
            )));
        }
        let successful = src.get_u8() != 0;
        let data = if successful && src.remaining() > 0 {
            let mut d = vec![0u8; src.remaining()];
            src.copy_to_slice(&mut d);
            Some(d)
        } else {
            None
        };
        Ok(Self {
            message_id,
            successful,
            data,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ServerboundLoginAcknowledged;

impl PacketId for ServerboundLoginAcknowledged {
    fn packet_id(ver: u32) -> u8 {
        if ver == 340 {
            0xFF
        } else {
            0x03
        }
    }
}

impl Encode for ServerboundLoginAcknowledged {
    fn encode(&self, _dst: &mut BytesMut) -> Result<(), ProtocolError> {
        Ok(())
    }
}

impl Decode for ServerboundLoginAcknowledged {
    fn decode(_src: &mut Bytes) -> Result<Self, ProtocolError> {
        Ok(Self)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct AcknowledgeFinishConfiguration;

impl PacketId for AcknowledgeFinishConfiguration {
    fn packet_id(ver: u32) -> u8 {
        if ver == 340 {
            0xFF
        } else {
            0x02
        }
    }
}

impl Encode for AcknowledgeFinishConfiguration {
    fn encode(&self, _dst: &mut BytesMut) -> Result<(), ProtocolError> {
        Ok(())
    }
}

impl Decode for AcknowledgeFinishConfiguration {
    fn decode(_src: &mut Bytes) -> Result<Self, ProtocolError> {
        Ok(Self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn login_start_roundtrip() {
        let p = ServerboundLoginStart {
            username: "Steve".to_string(),
        };
        let mut buf = BytesMut::new();
        p.encode(&mut buf).unwrap();
        let mut b = buf.freeze();
        assert_eq!(ServerboundLoginStart::decode(&mut b).unwrap(), p);
    }

    #[test]
    fn login_disconnect_roundtrip() {
        let p = ClientboundLoginDisconnect {
            reason: "Banned".to_string(),
        };
        let mut buf = BytesMut::new();
        p.encode(&mut buf).unwrap();
        let mut b = buf.freeze();
        assert_eq!(ClientboundLoginDisconnect::decode(&mut b).unwrap(), p);
    }

    #[test]
    fn encryption_roundtrip() {
        let p = ClientboundEncryptionRequest {
            server_id: String::new(),
            public_key: vec![0xDE, 0xAD],
            verify_token: vec![0xBE, 0xEF],
        };
        let mut buf = BytesMut::new();
        p.encode(&mut buf).unwrap();
        let mut b = buf.freeze();
        assert_eq!(ClientboundEncryptionRequest::decode(&mut b).unwrap(), p);
    }

    #[test]
    fn login_success_roundtrip() {
        let p = ClientboundLoginSuccess {
            uuid: uuid::Uuid::new_v4(),
            username: "Steve".to_string(),
            properties: vec![ProfileProperty {
                name: "textures".to_string(),
                value: "abc123".to_string(),
                signature: Some("sig".to_string()),
            }],
        };
        let mut buf = BytesMut::new();
        p.encode(&mut buf).unwrap();
        let mut b = buf.freeze();
        let d = ClientboundLoginSuccess::decode(&mut b).unwrap();
        assert_eq!(d.uuid, p.uuid);
        assert_eq!(d.username, p.username);
    }

    #[test]
    fn set_compression_roundtrip() {
        let p = ClientboundSetCompression {
            threshold: VarInt(512),
        };
        let mut buf = BytesMut::new();
        p.encode(&mut buf).unwrap();
        let mut b = buf.freeze();
        assert_eq!(ClientboundSetCompression::decode(&mut b).unwrap(), p);
    }

    #[test]
    fn plugin_request_roundtrip() {
        let p = ClientboundLoginPluginRequest {
            message_id: VarInt(1),
            channel: "my:channel".to_string(),
            data: vec![1, 2, 3],
        };
        let mut buf = BytesMut::new();
        p.encode(&mut buf).unwrap();
        let mut b = buf.freeze();
        assert_eq!(ClientboundLoginPluginRequest::decode(&mut b).unwrap(), p);
    }

    #[test]
    fn plugin_response_roundtrip() {
        let p = ServerboundLoginPluginResponse {
            message_id: VarInt(1),
            successful: true,
            data: Some(vec![0xAB, 0xCD]),
        };
        let mut buf = BytesMut::new();
        p.encode(&mut buf).unwrap();
        let mut b = buf.freeze();
        assert_eq!(ServerboundLoginPluginResponse::decode(&mut b).unwrap(), p);
    }
}