Get server token

This commit is contained in:
ChillerDragon 2024-06-01 10:34:53 +08:00
parent 4ca77e6adc
commit 3429fd1909
4 changed files with 78 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
teeworlds_client

View file

@ -1,2 +1,3 @@
# teeworlds_client
Me playing around

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module github.com/ChillerDragon/teeworlds_client
go 1.22.3

73
teeworlds_client.go Normal file
View file

@ -0,0 +1,73 @@
package main
import (
"bufio"
"fmt"
"net"
"slices"
"time"
)
const (
maxPacksize = 1400
msgCtrlToken = 0x04
)
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
}
func pumpNetwork(ch chan []byte) {
packet := make([]byte, maxPacksize)
conn, err := net.Dial("udp", "127.0.0.1:8303")
if err != nil {
fmt.Printf("Some error %v", err)
return
}
// myToken := []byte{0xfe, 0xed, 0xba, 0xbe}
myToken := []byte{0x01, 0x02, 0x03, 0x04}
conn.Write(ctrlToken(myToken))
for {
_, err = bufio.NewReader(conn).Read(packet)
if err == nil {
ch <- packet
} else {
fmt.Printf("Some error %v\n", err)
break
}
}
conn.Close()
}
func onMessage(data []byte) {
if data[0] == msgCtrlToken {
serverToken := data[8:12]
fmt.Printf("got token %v\n", serverToken)
} else {
fmt.Println("unknown message")
}
}
func main() {
ch := make(chan []byte, maxPacksize)
go pumpNetwork(ch)
for {
time.Sleep(10_000_000)
select {
case msg := <-ch:
onMessage(msg)
default:
// do nothing
}
}
}