2024-06-02 02:58:09 +00:00
|
|
|
package main
|
2024-06-01 02:34:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-06-01 03:54:12 +00:00
|
|
|
"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"
|
2024-06-06 04:51:18 +00:00
|
|
|
|
|
|
|
"github.com/teeworlds-go/huffman"
|
2024-06-17 03:37:44 +00:00
|
|
|
"github.com/teeworlds-go/teeworlds/chunk"
|
2024-06-17 05:11:59 +00:00
|
|
|
"github.com/teeworlds-go/teeworlds/packer"
|
2024-06-06 04:51:18 +00:00
|
|
|
"github.com/teeworlds-go/teeworlds/packet"
|
2024-06-01 02:34:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-06-01 03:45:32 +00:00
|
|
|
maxPacksize = 1400
|
|
|
|
|
2024-06-17 05:47:33 +00:00
|
|
|
msgCtrlKeepAlive = 0x00
|
|
|
|
msgCtrlConnect = 0x01
|
|
|
|
msgCtrlAccept = 0x02
|
|
|
|
msgCtrlToken = 0x05
|
|
|
|
msgCtrlClose = 0x04
|
2024-06-01 03:45:32 +00:00
|
|
|
|
2024-06-17 05:47:33 +00:00
|
|
|
msgSysMapChange = 2
|
|
|
|
msgSysConReady = 5
|
|
|
|
msgSysSnapSingle = 8
|
2024-06-17 03:37:44 +00:00
|
|
|
|
|
|
|
msgGameReadyToEnter = 8
|
|
|
|
msgGameMotd = 1
|
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 10:27:54 +00:00
|
|
|
func readNetwork(ch chan<- []byte, conn net.Conn) {
|
2024-06-17 03:37:44 +00:00
|
|
|
buf := make([]byte, maxPacksize)
|
2024-06-01 02:34:53 +00:00
|
|
|
|
|
|
|
for {
|
2024-06-17 03:37:44 +00:00
|
|
|
len, err := bufio.NewReader(conn).Read(buf)
|
|
|
|
packet := make([]byte, len)
|
|
|
|
copy(packet[:], buf[:])
|
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 {
|
2024-06-01 10:27:54 +00:00
|
|
|
clientToken [4]byte
|
|
|
|
serverToken [4]byte
|
2024-06-01 03:45:32 +00:00
|
|
|
conn net.Conn
|
2024-06-17 05:47:33 +00:00
|
|
|
|
|
|
|
Ack int
|
2024-06-01 03:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client TeeworldsClient) sendCtrlMsg(data []byte) {
|
2024-06-17 05:47:33 +00:00
|
|
|
header := packet.PacketHeader{
|
|
|
|
Flags: packet.PacketFlags{
|
|
|
|
Connless: false,
|
|
|
|
Compression: false,
|
|
|
|
Resend: false,
|
|
|
|
Control: true,
|
|
|
|
},
|
|
|
|
Ack: client.Ack,
|
|
|
|
NumChunks: 0,
|
|
|
|
Token: client.serverToken,
|
|
|
|
}
|
2024-06-01 03:45:32 +00:00
|
|
|
|
2024-06-17 05:47:33 +00:00
|
|
|
packet := slices.Concat(header.Pack(), data)
|
2024-06-01 03:45:32 +00:00
|
|
|
client.conn.Write(packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client TeeworldsClient) sendReady() {
|
|
|
|
packet := slices.Concat(
|
|
|
|
[]byte{0x00, 0x01, 0x01},
|
2024-06-01 10:27:54 +00:00
|
|
|
client.serverToken[:],
|
2024-06-01 03:45:32 +00:00
|
|
|
[]byte{0x40, 0x01, 0x02, 0x25},
|
|
|
|
)
|
|
|
|
client.conn.Write(packet)
|
|
|
|
}
|
|
|
|
|
2024-06-17 05:47:33 +00:00
|
|
|
func (client TeeworldsClient) sendKeepAlive() {
|
|
|
|
client.sendCtrlMsg([]byte{msgCtrlKeepAlive})
|
|
|
|
}
|
|
|
|
|
2024-06-01 03:45:32 +00:00
|
|
|
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},
|
2024-06-01 10:27:54 +00:00
|
|
|
client.serverToken[:],
|
2024-06-01 03:45:32 +00:00
|
|
|
info,
|
|
|
|
)
|
|
|
|
|
|
|
|
client.conn.Write(packet)
|
|
|
|
}
|
|
|
|
|
2024-06-01 11:00:47 +00:00
|
|
|
func (client TeeworldsClient) sendStartInfo() {
|
|
|
|
info := []byte{
|
|
|
|
0x41, 0x14, 0x03, 0x36, 0x67, 0x6f, 0x70, 0x68, 0x65, 0x72, 0x00, 0x00, 0x40, 0x67, 0x72, 0x65,
|
|
|
|
0x65, 0x6e, 0x73, 0x77, 0x61, 0x72, 0x64, 0x00, 0x64, 0x75, 0x6f, 0x64, 0x6f, 0x6e, 0x6e, 0x79,
|
|
|
|
0x00, 0x00, 0x73, 0x74, 0x61, 0x6e, 0x64, 0x61, 0x72, 0x64, 0x00, 0x73, 0x74, 0x61, 0x6e, 0x64,
|
|
|
|
0x61, 0x72, 0x64, 0x00, 0x73, 0x74, 0x61, 0x6e, 0x64, 0x61, 0x72, 0x64, 0x00, 0x01, 0x01, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x80, 0xfc, 0xaf, 0x05, 0xeb, 0x83, 0xd0, 0x0a, 0x80, 0xfe, 0x07, 0x80, 0xfe,
|
|
|
|
0x07, 0x80, 0xfe, 0x07, 0x80, 0xfe, 0x07,
|
|
|
|
}
|
|
|
|
|
|
|
|
packet := slices.Concat(
|
|
|
|
[]byte{0x00, 0x04, 0x01},
|
|
|
|
client.serverToken[:],
|
|
|
|
info,
|
|
|
|
)
|
|
|
|
|
|
|
|
client.conn.Write(packet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client TeeworldsClient) sendEnterGame() {
|
|
|
|
enter := []byte{
|
|
|
|
0x40, 0x01, 0x04, 0x27,
|
|
|
|
}
|
|
|
|
|
|
|
|
packet := slices.Concat(
|
|
|
|
[]byte{0x00, 0x07, 0x01},
|
|
|
|
client.serverToken[:],
|
|
|
|
enter,
|
|
|
|
)
|
|
|
|
|
|
|
|
client.conn.Write(packet)
|
|
|
|
}
|
|
|
|
|
2024-06-01 03:45:32 +00:00
|
|
|
func isCtrlMsg(data []byte) bool {
|
|
|
|
return data[0] == 0x04
|
|
|
|
}
|
|
|
|
|
|
|
|
func isMapChange(data []byte) bool {
|
|
|
|
// unsafe and trol
|
|
|
|
return data[10] == msgSysMapChange
|
|
|
|
}
|
|
|
|
|
2024-06-01 11:00:47 +00:00
|
|
|
func isCompressed(data []byte) bool {
|
|
|
|
// we don't talk about it
|
|
|
|
return data[0] == 0x10
|
|
|
|
}
|
|
|
|
|
|
|
|
func numberOfChunks(data []byte) int {
|
|
|
|
return int(data[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
func isReadyToEnter(data []byte) bool {
|
|
|
|
return !isCompressed(data) && numberOfChunks(data) == 3
|
|
|
|
}
|
|
|
|
|
|
|
|
func isConReady(data []byte) bool {
|
|
|
|
// unsafe and yemDX level troling
|
|
|
|
return isCompressed(data) // data[10] == msgSysMapChange
|
|
|
|
}
|
|
|
|
|
2024-06-01 03:54:12 +00:00
|
|
|
func byteSliceToString(s []byte) string {
|
|
|
|
n := bytes.IndexByte(s, 0)
|
|
|
|
if n >= 0 {
|
|
|
|
s = s[:n]
|
|
|
|
}
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
2024-06-17 05:11:59 +00:00
|
|
|
func (client *TeeworldsClient) onSystemMsg(msg int, chunk chunk.Chunk, u *packer.Unpacker) {
|
2024-06-17 03:37:44 +00:00
|
|
|
if msg == msgSysMapChange {
|
|
|
|
fmt.Println("got map change")
|
|
|
|
client.sendReady()
|
|
|
|
} else if msg == msgSysConReady {
|
|
|
|
fmt.Println("got ready")
|
|
|
|
client.sendStartInfo()
|
2024-06-17 05:47:33 +00:00
|
|
|
} else if msg == msgSysSnapSingle {
|
|
|
|
tick := u.GetInt()
|
|
|
|
fmt.Printf("got snap single tick=%d\n", tick)
|
|
|
|
client.sendKeepAlive()
|
2024-06-17 03:37:44 +00:00
|
|
|
} else {
|
|
|
|
fmt.Printf("unknown system message id=%d data=%x\n", msg, chunk.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 05:11:59 +00:00
|
|
|
func (client *TeeworldsClient) onGameMsg(msg int, chunk chunk.Chunk, u *packer.Unpacker) {
|
2024-06-17 03:37:44 +00:00
|
|
|
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)
|
|
|
|
|
2024-06-17 05:47:33 +00:00
|
|
|
if chunk.Header.Flags.Vital {
|
|
|
|
client.Ack++
|
|
|
|
}
|
|
|
|
|
2024-06-17 05:11:59 +00:00
|
|
|
u := packer.Unpacker{}
|
|
|
|
u.Reset(chunk.Data)
|
|
|
|
|
|
|
|
msg := u.GetInt()
|
2024-06-17 03:37:44 +00:00
|
|
|
|
|
|
|
sys := msg&1 != 0
|
|
|
|
msg >>= 1
|
|
|
|
|
|
|
|
if sys {
|
2024-06-17 05:11:59 +00:00
|
|
|
client.onSystemMsg(msg, chunk, &u)
|
2024-06-17 03:37:44 +00:00
|
|
|
} else {
|
2024-06-17 05:11:59 +00:00
|
|
|
client.onGameMsg(msg, chunk, &u)
|
2024-06-17 03:37:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-06-06 04:51:18 +00:00
|
|
|
header := packet.PacketHeader{}
|
2024-06-17 03:37:44 +00:00
|
|
|
headerRaw := data[:7]
|
|
|
|
payload := data[7:]
|
|
|
|
header.Unpack(headerRaw)
|
2024-06-06 04:51:18 +00:00
|
|
|
|
|
|
|
if header.Flags.Control {
|
2024-06-17 03:37:44 +00:00
|
|
|
ctrlMsg := payload[0]
|
2024-06-01 03:45:32 +00:00
|
|
|
fmt.Printf("got ctrl msg %d\n", ctrlMsg)
|
|
|
|
if ctrlMsg == msgCtrlToken {
|
2024-06-17 03:37:44 +00:00
|
|
|
copy(client.serverToken[:], payload[1:5])
|
2024-06-01 11:00:47 +00:00
|
|
|
fmt.Printf("got server token %x\n", client.serverToken)
|
2024-06-01 10:27:54 +00:00
|
|
|
client.sendCtrlMsg(slices.Concat([]byte{msgCtrlConnect}, client.clientToken[:]))
|
2024-06-01 03:45:32 +00:00
|
|
|
} else if ctrlMsg == msgCtrlAccept {
|
|
|
|
fmt.Println("got accept")
|
|
|
|
client.sendInfo()
|
|
|
|
} else if ctrlMsg == msgCtrlClose {
|
2024-06-01 03:54:12 +00:00
|
|
|
// TODO: get length from packet header to determine if a reason is set or not
|
|
|
|
// len(data) -> is 1400 (maxPacketLen)
|
|
|
|
|
2024-06-17 03:37:44 +00:00
|
|
|
reason := byteSliceToString(payload)
|
2024-06-01 03:54:12 +00:00
|
|
|
fmt.Printf("disconnected (%s)\n", reason)
|
|
|
|
|
2024-06-01 03:45:32 +00:00
|
|
|
os.Exit(0)
|
|
|
|
} else {
|
2024-06-01 11:00:47 +00:00
|
|
|
fmt.Printf("unknown control message: %x\n", data)
|
2024-06-01 03:45:32 +00:00
|
|
|
}
|
2024-06-17 03:37:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if header.Flags.Compression {
|
|
|
|
fmt.Printf("got compressed data: %x\n", payload)
|
2024-06-06 04:51:18 +00:00
|
|
|
huff := huffman.Huffman{}
|
2024-06-17 03:37:44 +00:00
|
|
|
var err error
|
|
|
|
payload, err = huff.Decompress(payload)
|
2024-06-06 04:51:18 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("huffman error: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-06-17 03:37:44 +00:00
|
|
|
fmt.Printf("got decompressed: %x\n", payload)
|
2024-06-01 02:34:53 +00:00
|
|
|
}
|
2024-06-17 03:37:44 +00:00
|
|
|
|
|
|
|
client.onPacketPayload(headerRaw, payload)
|
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 10:27:54 +00:00
|
|
|
client := &TeeworldsClient{
|
|
|
|
clientToken: [4]byte{0x01, 0x02, 0x03, 0x04},
|
|
|
|
serverToken: [4]byte{0xff, 0xff, 0xff, 0xff},
|
2024-06-01 03:45:32 +00:00
|
|
|
conn: conn,
|
|
|
|
}
|
|
|
|
|
|
|
|
go readNetwork(ch, client.conn)
|
2024-06-01 03:01:48 +00:00
|
|
|
|
2024-06-01 10:27:54 +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-17 03:37:44 +00:00
|
|
|
client.onPacket(msg)
|
2024-06-01 02:34:53 +00:00
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|