mc_protocol
Rust implementation of the Minecraft Java Edition network protocol primitives: serialization of basic types, packet framing, AES-128-CFB8 encryption, and Zlib compression.
Features
- Serialize and deserialize all basic protocol types: booleans, integers, floats, strings, UUIDs, VarInt, VarLong, Option, Vec, and raw bytes.
- Packet framing with length-prefixed read/write, both sync and async.
- AES-128-CFB8 stream encryption (sync and async wrappers).
- Zlib packet compression with configurable threshold.
#[derive(Packet)]macro to generateSerialize,Deserialize, andPacketIdfor packet structs.
Feature Flags
| Feature | Description | Default |
|---|---|---|
async |
Async I/O via Tokio | enabled |
encryption |
AES-128-CFB8 via OpenSSL | disabled |
compression |
Zlib via flate2 | enabled |
Usage
Derive macro
Define a packet struct and derive serialization automatically:
use ;
assert_eq!;
Serialization
All basic types implement Serialize and Deserialize:
use ;
use VarInt;
use Cursor;
let mut buf = Vecnew;
VarInt.serialize.unwrap;
// buf == [0xac, 0x02]
let v = deserialize.unwrap;
assert_eq!;
Packet framing (sync)
use ;
use TcpStream;
let mut stream = connect?;
let raw = read_sync?;
let packet = raw.as_uncompressed?;
println!;
let up = new;
up.write_sync?;
Packet framing (async)
use ;
use TcpStream;
let mut stream = connect.await?;
let raw = read_async.await?;
let packet = raw.as_uncompressed?;
let up = new;
up.write_async.await?;
Compression
use UncompressedPacket;
let up = new;
let threshold = Some;
let raw = up.to_raw_packet_compressed?;
let decoded = raw.uncompress?;
Encryption (sync)
use ;
let key: = shared_secret;
let mut encryptor = new?;
let mut decryptor = new?;
let ciphertext = encryptor.encrypt?;
let plaintext = decryptor.decrypt?;
Encryption (async stream)
use Cfb8Stream;
use TcpStream;
let stream = connect.await?;
let key: = shared_secret;
let mut encrypted = new_from_tcp?;
// All I/O through `encrypted` is transparently encrypted
License
MIT