Documentation
¶
Index ¶
- Constants
- func NewEncryptedReader(r io.Reader, sharedSecret []byte) (io.Reader, error)
- func NewEncryptedWriter(w io.Writer, sharedSecret []byte) (io.Writer, error)
- func ReadBool(reader io.Reader) (bool, error)
- func ReadDouble(reader io.Reader) (float64, error)
- func ReadFloat(reader io.Reader) (float32, error)
- func ReadInt(reader io.Reader) (int32, error)
- func ReadLong(reader io.Reader) (int64, error)
- func ReadPosition(reader io.Reader) (x, y, z int, err error)
- func ReadPrefixedBytes(reader io.Reader) ([]byte, error)
- func ReadString(reader io.Reader) (string, error)
- func RegisterPacket(state ProtocolState, direction PacketDirection, id int32, ...)
- func RegisterStateHandler(state ProtocolState, handler StateHandler)
- func VarIntDecode(r io.Reader) (int32, error)
- func VarIntDecodeBytes(data []byte) (int32, int, error)
- func VarIntEncode(value int32) []byte
- func VarLongDecode(r io.Reader) (int64, error)
- func VarLongDecodeBytes(data []byte) (int64, int, error)
- func VarLongEncode(value int64) []byte
- type BasePacket
- type Builder
- func (b *Builder) AppendByte(b2 byte) *Builder
- func (b *Builder) Bytes() []byte
- func (b *Builder) WriteBool(value bool) *Builder
- func (b *Builder) WriteBytes(data []byte) *Builder
- func (b *Builder) WriteDouble(value float64) *Builder
- func (b *Builder) WriteFloat(value float32) *Builder
- func (b *Builder) WriteInt(value int32) *Builder
- func (b *Builder) WriteLong(value int64) *Builder
- func (b *Builder) WritePosition(x, y, z int) *Builder
- func (b *Builder) WritePrefixedBytes(data []byte) *Builder
- func (b *Builder) WriteString(s string) *Builder
- func (b *Builder) WriteUShort(value uint16) *Builder
- func (b *Builder) WriteUUID(uuid [16]byte) *Builder
- func (b *Builder) WriteVarInt(value int32) *Builder
- func (b *Builder) WriteVarLong(value int64) *Builder
- type ConnectionContext
- type ConnectionIntent
- type KnownPack
- type Packet
- type PacketConstructor
- type PacketDirection
- type PacketHandler
- type PacketReader
- type PacketWriter
- type ProtocolState
- type StateHandler
Constants ¶
const ( // Handshaking HandshakePacketID = 0x00 // Serverbound StatusRequestPacketID = 0x00 PingRequestPacketID = 0x01 // Clientbound StatusResponsePacketID = 0x00 PongResponsePacketID = 0x01 )
Status state packet IDs
const ( // Serverbound LoginStartPacketID = 0x00 LoginEncryptionPacketID = 0x01 LoginAcknowledgedPacketID = 0x03 // Clientbound LoginSuccessPacketID = 0x02 LoginCompressionPacketID = 0x03 // same value as LoginAcknowledgedPacketID but clientbound LoginDisconnectPacketID = 0x00 )
Login state packet IDs
const ( // Serverbound ConfigClientInformationPacketID = 0x00 ConfigPluginMessageServerPacketID = 0x02 ConfigFinishConfigurationServerPacketID = 0x03 ConfigKnownPacksServerPacketID = 0x07 // serverbound // Clientbound ConfigPluginMessageClientPacketID = 0x01 ConfigFinishConfigurationClientPacketID = 0x03 // same value as server-side but clientbound ConfigRegistryDataPacketID = 0x07 // clientbound ConfigFeatureFlagsPacketID = 0x0C ConfigUpdateTagsPacketID = 0x0D ConfigKnownPacksClientPacketID = 0x0E )
Configuration state packet IDs
const ( // Serverbound PlayConfirmTeleportationPacketID = 0x00 PlayChunkBatchReceivedPacketID = 0x0A PlayClientTickEndPacketID = 0x0C PlayClientInformationPacketID = 0x0D PlayKeepAliveServerPacketID = 0x1B PlayPlayerLoadedPacketID = 0x2B PlaySetPlayerPositionPacketID = 0x1D PlaySetPlayerPosRotPacketID = 0x1E PlaySetPlayerRotationPacketID = 0x1F PlaySetPlayerOnGroundPacketID = 0x20 // Clientbound PlayChunkBatchFinishedPacketID = 0x0B PlayChunkBatchStartPacketID = 0x0C PlayDisconnectPacketID = 0x20 PlayGameEventPacketID = 0x26 PlayKeepAliveClientPacketID = 0x2B PlayChunkDataPacketID = 0x2C PlayLoginPacketID = 0x30 PlayPlayerInfoUpdatePacketID = 0x44 PlaySynchronizePositionPacketID = 0x46 PlaySetCenterChunkPacketID = 0x5C PlaySetDefaultSpawnPositionPacketID = 0x5F PlaySetTickStatePacketID = 0x7D )
Play state packet IDs (protocol 773 / 1.21.10)
const ( // MaxPacketSize is the maximum size of an uncompressed packet MaxPacketSize = (1 << 21) - 1 // 2097151 bytes (2^21 - 1) // MaxCompressedPacketSize is the maximum size of a compressed packet MaxCompressedPacketSize = MaxPacketSize // MaxServerPacketSize is the maximum size of serverbound packet data MaxServerPacketSize = 1 << 23 // 8388608 bytes )
Variables ¶
This section is empty.
Functions ¶
func NewEncryptedReader ¶
NewEncryptedReader wraps a reader with Minecraft AES/CFB8 decryption.
func NewEncryptedWriter ¶
NewEncryptedWriter wraps a writer with Minecraft AES/CFB8 encryption.
func ReadDouble ¶
ReadDouble reads a big-endian float64.
func ReadPosition ¶
ReadPosition reads a Minecraft block position encoded as int64 (x:26, z:26, y:12).
func ReadPrefixedBytes ¶
ReadPrefixedBytes reads a VarInt length-prefixed byte slice.
func ReadString ¶
ReadString reads a VarInt length-prefixed UTF-8 string.
func RegisterPacket ¶
func RegisterPacket(state ProtocolState, direction PacketDirection, id int32, constructor PacketConstructor)
RegisterPacket registers a packet constructor for a state/direction/id tuple.
func RegisterStateHandler ¶
func RegisterStateHandler(state ProtocolState, handler StateHandler)
RegisterStateHandler registers a handler for a protocol state.
func VarIntDecode ¶
VarIntDecode reads a VarInt from a reader
func VarIntDecodeBytes ¶
VarIntDecodeBytes decodes a VarInt from a byte slice and returns the value and bytes read
func VarIntEncode ¶
VarInt encodes an integer using Minecraft's VarInt format VarInt can represent values from -2^31 to 2^31 - 1
func VarLongDecode ¶
VarLongDecode reads a VarLong from a reader
func VarLongDecodeBytes ¶
VarLongDecodeBytes decodes a VarLong from a byte slice and returns the value and bytes read
func VarLongEncode ¶
VarLongEncode encodes a long integer using Minecraft's VarLong format VarLong can represent values from -2^63 to 2^63 - 1
Types ¶
type BasePacket ¶
type BasePacket struct {
// contains filtered or unexported fields
}
BasePacket provides common functionality for all packets
func NewBasePacket ¶
func NewBasePacket(id int32, direction PacketDirection, state ProtocolState) BasePacket
NewBasePacket creates a new base packet
func (BasePacket) Direction ¶
func (p BasePacket) Direction() PacketDirection
Direction returns the packet direction
func (BasePacket) State ¶
func (p BasePacket) State() ProtocolState
State returns the protocol state
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is a helper for building packet data
func (*Builder) AppendByte ¶
WriteByte writes a single byte to the builder
func (*Builder) WriteBytes ¶
WriteBytes writes raw bytes to the builder.
func (*Builder) WriteDouble ¶
WriteDouble writes a 64-bit double (big endian) to the builder
func (*Builder) WriteFloat ¶
WriteFloat writes a 32-bit float (big endian) to the builder
func (*Builder) WritePosition ¶
WritePosition writes a block position encoded as a single int64 (x:26, z:26, y:12)
func (*Builder) WritePrefixedBytes ¶
WritePrefixedBytes writes VarInt-length-prefixed bytes
func (*Builder) WriteString ¶
WriteString writes a length-prefixed string (UTF-8)
func (*Builder) WriteUShort ¶
WriteUShort writes an unsigned 16-bit integer (big endian)
func (*Builder) WriteVarInt ¶
WriteVarInt writes a VarInt to the builder
func (*Builder) WriteVarLong ¶
WriteVarLong writes a VarLong to the builder
type ConnectionContext ¶
type ConnectionContext struct {
Conn net.Conn
Reader *PacketReader
Writer *PacketWriter
State ProtocolState
Done bool
Username string
UUID [16]byte
AwaitingEncryptionResponse bool
VerifyToken []byte
ClientLocale string
ClientViewDistance int8
ClientChatMode int32
ClientChatColors bool
ClientDisplayedSkinParts uint8
ClientMainHand int32
ClientTextFilteringEnabled bool
ClientAllowsServerListing bool
ClientParticleStatus int32
ClientBrand string
ConfigurationBrandSent bool
ConfigurationPreludeSent bool
AwaitingKnownPacksResponse bool
AwaitingFinishConfigurationAck bool
SentKnownPacks []KnownPack
ClientKnownPacks []KnownPack
// GameData holds state-specific data (e.g., *game.Player during Play state).
// Using interface{} to avoid circular imports between packet and game packages.
GameData interface{}
}
ConnectionContext holds data about a client connection and its protocol state.
func NewConnectionContext ¶
func NewConnectionContext(conn net.Conn) *ConnectionContext
func (*ConnectionContext) Close ¶
func (c *ConnectionContext) Close() error
func (*ConnectionContext) EnableEncryption ¶
func (c *ConnectionContext) EnableEncryption(sharedSecret []byte) error
EnableEncryption enables Minecraft AES/CFB8 encryption for both directions.
func (*ConnectionContext) MarkDone ¶
func (c *ConnectionContext) MarkDone()
MarkDone marks the connection as finished without closing the socket directly.
func (*ConnectionContext) SendPacket ¶
func (c *ConnectionContext) SendPacket(ph PacketHandler) error
SendPacket serializes and sends a packet handler through the active writer.
func (*ConnectionContext) TransitionToState ¶
func (c *ConnectionContext) TransitionToState(state ProtocolState) error
TransitionToState updates the protocol state and calls OnStateEnter on the new handler.
type ConnectionIntent ¶
type ConnectionIntent int
Connection intents for handshake
const ( IntentStatus ConnectionIntent = 1 IntentLogin ConnectionIntent = 2 IntentTransfer ConnectionIntent = 3 )
type PacketConstructor ¶
type PacketConstructor func() PacketHandler
type PacketDirection ¶
type PacketDirection int
PacketDirection indicates the direction of packet flow
const ( Clientbound PacketDirection = iota // Server -> Client Serverbound // Client -> Server )
type PacketHandler ¶
type PacketHandler interface {
// ID returns the packet ID
ID() int32
// Direction returns whether this is a clientbound or serverbound packet
Direction() PacketDirection
// State returns the protocol state this packet belongs to
State() ProtocolState
// Read reads the packet data from a byte slice
Read(data []byte) error
// Write writes the packet data to a byte slice
Write() ([]byte, error)
}
PacketHandler defines the interface for all Minecraft packets
func CreatePacketHandler ¶
func CreatePacketHandler(state ProtocolState, direction PacketDirection, id int32) (PacketHandler, bool)
CreatePacketHandler creates a new packet instance for the given state/direction/id.
type PacketReader ¶
type PacketReader struct {
// contains filtered or unexported fields
}
PacketReader handles reading packets from a connection
func NewPacketReader ¶
func NewPacketReader(r io.Reader) *PacketReader
NewPacketReader creates a new packet reader
func (*PacketReader) ReadPacket ¶
func (pr *PacketReader) ReadPacket() (*Packet, error)
ReadPacket reads a packet from the connection
func (*PacketReader) SetCompression ¶
func (pr *PacketReader) SetCompression(threshold int32)
SetCompression enables or disables compression
func (*PacketReader) SetReader ¶
func (pr *PacketReader) SetReader(r io.Reader)
SetReader replaces the underlying reader.
type PacketWriter ¶
type PacketWriter struct {
// contains filtered or unexported fields
}
PacketWriter handles writing packets to a connection. It is safe for concurrent use.
func NewPacketWriter ¶
func NewPacketWriter(w io.Writer) *PacketWriter
NewPacketWriter creates a new packet writer
func (*PacketWriter) SetCompression ¶
func (pw *PacketWriter) SetCompression(threshold int32)
SetCompression enables or disables compression for writer
func (*PacketWriter) SetWriter ¶
func (pw *PacketWriter) SetWriter(w io.Writer)
SetWriter replaces the underlying writer.
func (*PacketWriter) WritePacket ¶
func (pw *PacketWriter) WritePacket(p *Packet) error
WritePacket writes a packet to the connection
type ProtocolState ¶
type ProtocolState int
Protocol states for Minecraft server
const ( StateHandshaking ProtocolState = iota StateStatus StateLogin StateConfiguration StatePlay )
type StateHandler ¶
type StateHandler interface {
Handle(ctx *ConnectionContext, packet PacketHandler) error
// OnStateEnter is called when the connection transitions into this state.
OnStateEnter(ctx *ConnectionContext) error
}
StateHandler is responsible for processing packets in a specific protocol state.
func GetStateHandler ¶
func GetStateHandler(state ProtocolState) (StateHandler, bool)
GetStateHandler retrieves a registered state handler.