go-teeworlds-protocol/teeworlds_client.go

173 lines
3.5 KiB
Go
Raw Normal View History

2024-06-01 02:34:53 +00:00
package main
import (
"bufio"
"bytes"
2024-06-01 02:34:53 +00:00
"fmt"
"net"
2024-06-01 03:01:48 +00:00
"os"
2024-06-01 02:34:53 +00:00
"slices"
"time"
)
const (
2024-06-01 03:45:32 +00:00
maxPacksize = 1400
msgCtrlConnect = 0x01
msgCtrlAccept = 0x02
msgCtrlToken = 0x05
msgCtrlClose = 0x04
msgSysMapChange = 0x05
2024-06-01 02:34:53 +00:00
)
func ctrlToken(myToken []byte) []byte {
header := []byte{0x04, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff}
ctrlToken := append([]byte{0x05}, myToken...)
zeros := []byte{512: 0}
data := slices.Concat(header, ctrlToken, zeros)
return data
}
2024-06-01 03:01:48 +00:00
func getConnection() (net.Conn, error) {
2024-06-01 02:34:53 +00:00
conn, err := net.Dial("udp", "127.0.0.1:8303")
if err != nil {
fmt.Printf("Some error %v", err)
}
2024-06-01 03:01:48 +00:00
return conn, err
}
2024-06-01 02:34:53 +00:00
2024-06-01 03:01:48 +00:00
func readNetwork(ch chan []byte, conn net.Conn) {
packet := make([]byte, maxPacksize)
2024-06-01 02:34:53 +00:00
for {
2024-06-01 03:01:48 +00:00
_, err := bufio.NewReader(conn).Read(packet)
2024-06-01 02:34:53 +00:00
if err == nil {
ch <- packet
} else {
fmt.Printf("Some error %v\n", err)
break
}
}
conn.Close()
}
2024-06-01 03:45:32 +00:00
type TeeworldsClient struct {
clientToken []byte
serverToken []byte
conn net.Conn
}
func (client TeeworldsClient) sendCtrlMsg(data []byte) {
flags := []byte{0x04, 0x00, 0x00}
packet := slices.Concat(flags, client.serverToken, data)
// fmt.Printf("sending %v\n", packet)
client.conn.Write(packet)
}
func (client TeeworldsClient) sendReady() {
packet := slices.Concat(
[]byte{0x00, 0x01, 0x01},
client.serverToken,
[]byte{0x40, 0x01, 0x02, 0x25},
)
client.conn.Write(packet)
}
func (client TeeworldsClient) sendInfo() {
info := []byte{0x40, 0x28, 0x01, 0x03, 0x30, 0x2E, 0x37, 0x20, 0x38, 0x30, 0x32, 0x66,
0x31, 0x62, 0x65, 0x36, 0x30, 0x61, 0x30, 0x35, 0x36, 0x36, 0x35, 0x66,
0x00, 0x6D, 0x79, 0x5F, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6F, 0x72, 0x64,
0x5F, 0x31, 0x32, 0x33, 0x00, 0x85, 0x1C, 0x00}
packet := slices.Concat(
[]byte{0x00, 0x00, 0x01},
client.serverToken,
info,
)
client.conn.Write(packet)
}
func isCtrlMsg(data []byte) bool {
return data[0] == 0x04
}
func isMapChange(data []byte) bool {
// unsafe and trol
return data[10] == msgSysMapChange
}
func byteSliceToString(s []byte) string {
n := bytes.IndexByte(s, 0)
if n >= 0 {
s = s[:n]
}
return string(s)
}
2024-06-01 03:45:32 +00:00
func (client *TeeworldsClient) onMessage(data []byte) {
if isCtrlMsg(data) {
ctrlMsg := data[7]
fmt.Printf("got ctrl msg %d\n", ctrlMsg)
if ctrlMsg == msgCtrlToken {
client.serverToken = data[8:12]
fmt.Printf("got token %v\n", client.serverToken)
client.sendCtrlMsg(slices.Concat([]byte{msgCtrlConnect}, client.clientToken))
} else if ctrlMsg == msgCtrlAccept {
fmt.Println("got accept")
client.sendInfo()
} else if ctrlMsg == msgCtrlClose {
// TODO: get length from packet header to determine if a reason is set or not
// len(data) -> is 1400 (maxPacketLen)
reason := byteSliceToString(data[8:])
fmt.Printf("disconnected (%s)\n", reason)
2024-06-01 03:45:32 +00:00
os.Exit(0)
} else {
fmt.Printf("unknown control message: %v\n", data)
}
} else if isMapChange(data) {
fmt.Println("got map change")
client.sendReady()
2024-06-01 02:34:53 +00:00
} else {
2024-06-01 03:01:48 +00:00
fmt.Printf("unknown message: %v\n", data)
2024-06-01 02:34:53 +00:00
}
}
func main() {
ch := make(chan []byte, maxPacksize)
2024-06-01 03:01:48 +00:00
conn, err := getConnection()
if err != nil {
fmt.Printf("error connecting %v\n", err)
os.Exit(1)
}
2024-06-01 03:45:32 +00:00
client := TeeworldsClient{
clientToken: []byte{0x01, 0x02, 0x03, 0x04},
serverToken: []byte{0xff, 0xff, 0xff, 0xff},
conn: conn,
}
go readNetwork(ch, client.conn)
2024-06-01 03:01:48 +00:00
2024-06-01 03:45:32 +00:00
conn.Write(ctrlToken(client.clientToken))
2024-06-01 02:34:53 +00:00
for {
time.Sleep(10_000_000)
select {
case msg := <-ch:
2024-06-01 03:45:32 +00:00
client.onMessage(msg)
2024-06-01 02:34:53 +00:00
default:
// do nothing
}
}
}