Use proper chunk parsing instead of magic values

This commit is contained in:
ChillerDragon 2024-06-17 11:37:44 +08:00
parent a58425ea56
commit 47163dce87
2 changed files with 97 additions and 30 deletions

View file

@ -6,6 +6,25 @@ import (
"testing" "testing"
) )
func TestSplitterMotdSettingsReady(t *testing.T) {
// real map change packet from a vanilla teeworlds 0.7 server
// dumped with the go client
payload := []byte{
0x40, 0x02, 0x02, 0x02, 0x00, 0x40, 0x07, 0x03, 0x22, 0x01, 0x00, 0x01, 0x00, 0x01, 0x08, 0x40, 0x01, 0x04, 0x0b,
}
chunks := UnpackChunks(payload)
{
got := len(chunks)
want := 3
if want != got {
t.Errorf("got %v, wanted %v", got, want)
}
}
}
func TestSplitterMapChange(t *testing.T) { func TestSplitterMapChange(t *testing.T) {
// real map change packet from a vanilla teeworlds 0.7 server // real map change packet from a vanilla teeworlds 0.7 server
// dumped with the go client // dumped with the go client
@ -15,10 +34,10 @@ func TestSplitterMapChange(t *testing.T) {
// } // }
payload := []byte { payload := []byte {
0x3a, 0x01, 0x05, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x5f, 0x70, 0x69, 0x63, 0x6b, 0x75, 0x70, 0x73, 0x40, 0x3a, 0x01, 0x05, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x5f, 0x70, 0x69, 0x63, 0x6b, 0x75, 0x70,
0x00, 0x91, 0xa7, 0xf2, 0xa0, 0x05, 0x8b, 0x11, 0x08, 0xa8, 0x15, 0xa9, 0x19, 0x38, 0xaf, 0xfd, 0xb6, 0x73, 0x00, 0x91, 0xa7, 0xf2, 0xa0, 0x05, 0x8b, 0x11, 0x08, 0xa8, 0x15, 0xa9, 0x19, 0x38, 0xaf, 0xfd,
0xcb, 0xf1, 0xdb, 0x5a, 0xfc, 0x73, 0x34, 0xae, 0xa3, 0x68, 0xa7, 0xfa, 0x35, 0x54, 0x37, 0xf9, 0x39, 0xb6, 0xcb, 0xf1, 0xdb, 0x5a, 0xfc, 0x73, 0x34, 0xae, 0xa3, 0x68, 0xa7, 0xfa, 0x35, 0x54, 0x37, 0xf9,
0x96, 0xd6, 0x05, 0x25, 0xef, 0x7b, 0x22, 0x40, 0x9d, 0x39, 0x96, 0xd6, 0x05, 0x25, 0xef, 0x7b, 0x22, 0x40, 0x9d,
} }
chunks := UnpackChunks(payload) chunks := UnpackChunks(payload)

View file

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/teeworlds-go/huffman" "github.com/teeworlds-go/huffman"
"github.com/teeworlds-go/teeworlds/chunk"
"github.com/teeworlds-go/teeworlds/packet" "github.com/teeworlds-go/teeworlds/packet"
) )
@ -21,8 +22,11 @@ const (
msgCtrlToken = 0x05 msgCtrlToken = 0x05
msgCtrlClose = 0x04 msgCtrlClose = 0x04
msgSysMapChange = 0x05 msgSysMapChange = 2
msgGameMotd = 0x02 msgSysConReady = 5
msgGameReadyToEnter = 8
msgGameMotd = 1
) )
func ctrlToken(myToken []byte) []byte { func ctrlToken(myToken []byte) []byte {
@ -42,10 +46,12 @@ func getConnection() (net.Conn, error) {
} }
func readNetwork(ch chan<- []byte, conn net.Conn) { func readNetwork(ch chan<- []byte, conn net.Conn) {
packet := make([]byte, maxPacksize) buf := make([]byte, maxPacksize)
for { for {
_, err := bufio.NewReader(conn).Read(packet) len, err := bufio.NewReader(conn).Read(buf)
packet := make([]byte, len)
copy(packet[:], buf[:])
if err == nil { if err == nil {
ch <- packet ch <- packet
} else { } else {
@ -162,15 +168,63 @@ func byteSliceToString(s []byte) string {
return string(s) return string(s)
} }
func (client *TeeworldsClient) onMessage(data []byte) { func (client *TeeworldsClient) onSystemMsg(msg int, chunk chunk.Chunk) {
if msg == msgSysMapChange {
fmt.Println("got map change")
client.sendReady()
} else if msg == msgSysConReady {
fmt.Println("got ready")
client.sendStartInfo()
} else {
fmt.Printf("unknown system message id=%d data=%x\n", msg, chunk.Data)
}
}
func (client *TeeworldsClient) onGameMsg(msg int, chunk chunk.Chunk) {
if msg == msgGameReadyToEnter {
fmt.Println("got ready to enter")
client.sendEnterGame()
} else {
fmt.Printf("unknown game message id=%d data=%x\n", msg, chunk.Data)
}
}
func (client *TeeworldsClient) onMessage(chunk chunk.Chunk) {
fmt.Printf("got chunk size=%d data=%v\n", chunk.Header.Size, chunk.Data)
// TODO: we need to int unpack this because it can be 2 bytes long
msg := int(chunk.Data[0])
sys := msg&1 != 0
msg >>= 1
if sys {
client.onSystemMsg(msg, chunk)
} else {
client.onGameMsg(msg, chunk)
}
}
func (client *TeeworldsClient) onPacketPayload(header []byte, data []byte) {
fmt.Printf("got payload: %x %x\n", header, data)
chunks := chunk.UnpackChunks(data)
for _, c := range chunks {
client.onMessage(c)
}
}
func (client *TeeworldsClient) onPacket(data []byte) {
header := packet.PacketHeader{} header := packet.PacketHeader{}
header.Unpack(data) headerRaw := data[:7]
payload := data[7:]
header.Unpack(headerRaw)
if header.Flags.Control { if header.Flags.Control {
ctrlMsg := data[7] ctrlMsg := payload[0]
fmt.Printf("got ctrl msg %d\n", ctrlMsg) fmt.Printf("got ctrl msg %d\n", ctrlMsg)
if ctrlMsg == msgCtrlToken { if ctrlMsg == msgCtrlToken {
copy(client.serverToken[:], data[8:12]) copy(client.serverToken[:], payload[1:5])
fmt.Printf("got server token %x\n", client.serverToken) fmt.Printf("got server token %x\n", client.serverToken)
client.sendCtrlMsg(slices.Concat([]byte{msgCtrlConnect}, client.clientToken[:])) client.sendCtrlMsg(slices.Concat([]byte{msgCtrlConnect}, client.clientToken[:]))
} else if ctrlMsg == msgCtrlAccept { } else if ctrlMsg == msgCtrlAccept {
@ -180,35 +234,29 @@ func (client *TeeworldsClient) onMessage(data []byte) {
// TODO: get length from packet header to determine if a reason is set or not // TODO: get length from packet header to determine if a reason is set or not
// len(data) -> is 1400 (maxPacketLen) // len(data) -> is 1400 (maxPacketLen)
reason := byteSliceToString(data[8:]) reason := byteSliceToString(payload)
fmt.Printf("disconnected (%s)\n", reason) fmt.Printf("disconnected (%s)\n", reason)
os.Exit(0) os.Exit(0)
} else { } else {
fmt.Printf("unknown control message: %x\n", data) fmt.Printf("unknown control message: %x\n", data)
} }
} else if header.Flags.Compression { return
payload := data[8:] }
fmt.Printf("got compressed data: %v\n", payload)
if header.Flags.Compression {
fmt.Printf("got compressed data: %x\n", payload)
huff := huffman.Huffman{} huff := huffman.Huffman{}
decompressed, err := huff.Decompress(payload) var err error
payload, err = huff.Decompress(payload)
if err != nil { if err != nil {
fmt.Printf("huffman error: %v\n", err) fmt.Printf("huffman error: %v\n", err)
return return
} }
fmt.Printf("got decompressed: %v\n", decompressed) fmt.Printf("got decompressed: %x\n", payload)
} else if isMapChange(data) {
fmt.Println("got map change")
client.sendReady()
} else if isConReady(data) {
fmt.Println("got ready")
client.sendStartInfo()
} else if isReadyToEnter(data) {
fmt.Println("got ready to enter")
client.sendEnterGame()
} else {
fmt.Printf("unknown message: %x\n", data)
} }
client.onPacketPayload(headerRaw, payload)
} }
func main() { func main() {
@ -234,7 +282,7 @@ func main() {
time.Sleep(10_000_000) time.Sleep(10_000_000)
select { select {
case msg := <-ch: case msg := <-ch:
client.onMessage(msg) client.onPacket(msg)
default: default:
// do nothing // do nothing
} }