2024-06-02 02:58:09 +00:00
|
|
|
package main
|
2024-06-01 02:34:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2024-06-01 03:01:48 +00:00
|
|
|
"os"
|
2024-06-21 02:40:49 +00:00
|
|
|
"strconv"
|
2024-06-01 02:34:53 +00:00
|
|
|
"time"
|
2024-06-06 04:51:18 +00:00
|
|
|
|
2024-06-20 04:10:22 +00:00
|
|
|
"github.com/teeworlds-go/teeworlds/messages7"
|
2024-06-19 04:57:32 +00:00
|
|
|
"github.com/teeworlds-go/teeworlds/network7"
|
2024-06-19 04:36:14 +00:00
|
|
|
"github.com/teeworlds-go/teeworlds/protocol7"
|
2024-06-01 02:34:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-06-01 03:45:32 +00:00
|
|
|
maxPacksize = 1400
|
2024-06-01 02:34:53 +00:00
|
|
|
)
|
|
|
|
|
2024-06-21 02:40:49 +00:00
|
|
|
func getConnection(serverIp string, serverPort int) (net.Conn, error) {
|
|
|
|
conn, err := net.Dial("udp", fmt.Sprintf("%s:%d", serverIp, serverPort))
|
2024-06-01 02:34:53 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ch := make(chan []byte, maxPacksize)
|
|
|
|
|
2024-06-21 02:40:49 +00:00
|
|
|
serverIp := "127.0.0.1"
|
|
|
|
serverPort := 8303
|
|
|
|
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
if os.Args[1][0] == '-' {
|
|
|
|
fmt.Println("usage: ./teeworlds [serverIp] [serverPort]")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
serverIp = os.Args[1]
|
|
|
|
}
|
|
|
|
if len(os.Args) > 2 {
|
|
|
|
var err error
|
|
|
|
serverPort, err = strconv.Atoi(os.Args[2])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := getConnection(serverIp, serverPort)
|
2024-06-01 03:01:48 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error connecting %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-06-19 04:36:14 +00:00
|
|
|
client := &protocol7.Connection{
|
|
|
|
ClientToken: [4]byte{0x01, 0x02, 0x03, 0x04},
|
|
|
|
ServerToken: [4]byte{0xff, 0xff, 0xff, 0xff},
|
2024-06-18 05:08:56 +00:00
|
|
|
Ack: 0,
|
2024-06-19 04:57:32 +00:00
|
|
|
Players: make([]protocol7.Player, network7.MaxClients),
|
2024-06-01 03:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-06-20 03:19:54 +00:00
|
|
|
go readNetwork(ch, conn)
|
2024-06-01 03:01:48 +00:00
|
|
|
|
2024-06-20 04:29:26 +00:00
|
|
|
tokenPacket := client.CtrlToken()
|
|
|
|
conn.Write(tokenPacket.Pack(client))
|
2024-06-01 02:34:53 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(10_000_000)
|
|
|
|
select {
|
|
|
|
case msg := <-ch:
|
2024-06-22 04:25:29 +00:00
|
|
|
packet := &protocol7.Packet{}
|
|
|
|
err := packet.Unpack(msg)
|
2024-06-20 03:08:52 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-06-22 04:25:29 +00:00
|
|
|
response := client.OnPacket(packet)
|
|
|
|
if response != nil {
|
2024-06-20 05:27:46 +00:00
|
|
|
// example of inspecting incoming trafic
|
2024-06-22 04:38:17 +00:00
|
|
|
for _, msg := range packet.Messages {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *messages7.SvChat:
|
|
|
|
// inspect incoming traffic
|
|
|
|
fmt.Printf("got incoming chat msg: %s\n", msg.Message)
|
|
|
|
default:
|
2024-06-20 05:27:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-20 04:10:22 +00:00
|
|
|
// example of modifying outgoing traffic
|
2024-06-22 04:25:29 +00:00
|
|
|
for i, msg := range response.Messages {
|
2024-06-22 04:38:17 +00:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *messages7.SvChat:
|
|
|
|
// inspect outgoing traffic
|
|
|
|
fmt.Printf("got outgoing chat msg: %s\n", msg.Message)
|
|
|
|
|
|
|
|
// change outgoing traffic
|
|
|
|
msg.Message += " (edited by go)"
|
|
|
|
packet.Messages[i] = msg
|
|
|
|
default:
|
2024-06-20 04:10:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 04:25:29 +00:00
|
|
|
if len(response.Messages) > 0 || response.Header.Flags.Resend {
|
|
|
|
conn.Write(response.Pack(client))
|
|
|
|
}
|
2024-06-20 03:08:52 +00:00
|
|
|
}
|
2024-06-01 02:34:53 +00:00
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|