Add connection webhook notifications (#392)

Also 

* Added decode of LoginStart message
* Add metrics backend constants
* Updated usage section
* Documented MaxFrameLength
This commit is contained in:
Geoff Bourne
2025-04-21 20:28:34 -05:00
committed by GitHub
parent a058d6e21d
commit cc590524c4
13 changed files with 585 additions and 66 deletions
+73
View File
@@ -1,8 +1,15 @@
package mcproto
import (
"bufio"
"bytes"
"encoding/hex"
"fmt"
"github.com/google/uuid"
"os"
"strings"
"testing"
"unicode"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -35,3 +42,69 @@ func TestReadVarInt(t *testing.T) {
})
}
}
func TestHandshakeThenStatus(t *testing.T) {
content, err := ReadHexDumpFile("handshake-status.hex")
require.NoError(t, err)
reader := bufio.NewReader(bytes.NewReader(content))
handshakePacket, err := ReadPacket(reader, nil, StateHandshaking)
require.NoError(t, err)
handshake, err := DecodeHandshake(handshakePacket.Data)
require.NoError(t, err)
assert.Equal(t, "localhost", handshake.ServerAddress)
assert.Equal(t, uint16(25565), handshake.ServerPort)
assert.Equal(t, 770 /*for 1.21.5*/, handshake.ProtocolVersion)
assert.Equal(t, StateStatus, handshake.NextState)
}
func TestHandshakeThenLoginStart(t *testing.T) {
content, err := ReadHexDumpFile("handshake-login-start.hex")
require.NoError(t, err)
reader := bufio.NewReader(bytes.NewReader(content))
handshakePacket, err := ReadPacket(reader, nil, StateHandshaking)
require.NoError(t, err)
handshake, err := DecodeHandshake(handshakePacket.Data)
require.NoError(t, err)
assert.Equal(t, "localhost", handshake.ServerAddress)
assert.Equal(t, uint16(25565), handshake.ServerPort)
assert.Equal(t, 770 /*for 1.21.5*/, handshake.ProtocolVersion)
assert.Equal(t, StateLogin, handshake.NextState)
loginStartPacket, err := ReadPacket(reader, nil, StateLogin)
require.NoError(t, err)
loginStart, err := DecodeLoginStart(loginStartPacket.Data)
require.NoError(t, err)
assert.Equal(t, "itzg", loginStart.Name)
assert.Equal(t, uuid.MustParse("5cddfd26-fc86-4981-b52e-c42bb10bfdef"), loginStart.PlayerUuid)
}
func ReadHexDumpFile(filename string) ([]byte, error) {
// Read the file content
content, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
// Convert content to string and clean it up
hexString := string(content)
// Remove whitespace and newlines
hexString = strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1 // Remove spaces, tabs, newlines
}
return r
}, hexString)
return hex.DecodeString(hexString)
}