gottdad/main.go

102 lines
2.6 KiB
Go
Raw Normal View History

2020-06-14 16:27:57 +02:00
package main
import (
2020-06-14 16:32:21 +02:00
"bufio"
2020-06-14 22:39:17 +02:00
"encoding/binary"
2020-06-14 16:27:57 +02:00
"net"
)
func main() {
2020-06-14 16:32:21 +02:00
conn, err := net.Dial("tcp", "poop.siteop.biz:3977")
2020-06-14 16:27:57 +02:00
failError(err, "net.Dial")
logInfoDebug("Connected to poop.siteop.biz:3977")
2020-06-14 16:44:40 +02:00
//send auth
p := PacketAdminJoin{
2020-06-14 16:48:00 +02:00
Packet: Packet{PType: AdminPacketAdminJoin},
2020-06-14 17:13:25 +02:00
Password: "plop",
AppName: "gottdad",
AppVersion: "alpha",
2020-06-14 16:44:40 +02:00
}
_, err = conn.Write(p.Bytes())
failError(err, "conn.Write")
2020-06-14 17:38:25 +02:00
logInfoDebug("Authentication sent (%v)", p.Bytes())
2020-06-14 22:16:39 +02:00
r := bufio.NewReader(conn)
2020-06-14 22:39:17 +02:00
b := make([]byte, 0xFFFF)
read := 0
n := 0
2020-06-14 16:44:40 +02:00
2020-06-14 22:39:17 +02:00
for {
p := Packet{}
2020-06-14 22:05:45 +02:00
for {
2020-06-14 22:39:17 +02:00
if read >= 3 {
2020-06-14 22:44:15 +02:00
logInfoDebug("Packet read")
2020-06-14 22:05:45 +02:00
break
}
2020-06-14 22:46:23 +02:00
n, err = r.Read(b[read:])
logErrorDebug(err, "r.Read")
read += n
logInfoDebug("Waiting for packet, read %d bytes.", read)
2020-06-14 22:39:17 +02:00
}
p.PLength = binary.LittleEndian.Uint16(b[0:])
p.PType = b[2]
if p.PLength <= 3 {
logInfoAlert("Wrong packet length")
break
}
2020-06-14 22:20:19 +02:00
2020-06-14 22:45:52 +02:00
logInfoDebug("Waiting for packet data : len : %d / type : %d", p.PLength, p.PType)
2020-06-14 22:44:39 +02:00
2020-06-14 22:39:17 +02:00
for {
2020-06-14 22:45:52 +02:00
if read >= int(p.PLength) {
logInfoDebug("Data read")
break
}
2020-06-14 22:39:17 +02:00
n, err = r.Read(b[read:])
logErrorDebug(err, "r.Read")
read += n
2020-06-14 22:43:37 +02:00
logInfoDebug("Waiting for data, read %d/%d bytes.", read, p.PLength)
2020-06-14 22:45:52 +02:00
2020-06-14 22:20:19 +02:00
}
2020-06-14 22:39:17 +02:00
2020-06-15 12:42:02 +02:00
switch p.PType {
case AdminPacketServerProtocol:
sp := PacketAdminServerProtocol{
Packet: p,
}
sp.Read(b[:p.PLength])
logInfoDebug("AdminPacketServerProtocol :\n- ProtocolVersion: %v\n- FurtherData: %v\n- UpdatePacketType: %v\n- FrequenciesAllowed: %b", sp.ProtocolVersion, sp.FurtherData, sp.UpdatePacketType, sp.FrequenciesAllowed)
case AdminPacketServerWelcome:
sp := PacketAdminServerWelcome{
Packet: p,
}
sp.Read(b[:p.PLength])
logInfoDebug("AdminPacketServerWelcome :\n- ServerName: %v\n- OpenTTDVersion: %v\n- Dedicated: %v\n- MapSeed: %x\n- MapLandscape: %v\n- MapStartDate: %v\n- Size: %v x %v", sp.ServerName, sp.OpenTTDVersion, sp.Dedicated, sp.MapSeed, sp.MapLandscape, sp.MapStartDate, sp.MapX, sp.MapY)
px := PacketAdminUpdateFrequency{
Packet: Packet{PType: AdminPacketAdminUpdateFrequency},
UpdateType: AdminUpdateDate,
UpdateFrequency: AdminFrequencyDaily,
}
2020-06-15 12:42:27 +02:00
_, err = conn.Write(px.Bytes())
2020-06-15 12:47:05 +02:00
case AdminPacketServerDate:
2020-06-15 12:46:43 +02:00
sp := PacketAdminServerDate{
Packet: p,
}
sp.Read(b[:p.PLength])
logInfoDebug("AdminPacketServerDate :\n- Date: %d", sp.Date)
2020-06-15 12:42:02 +02:00
default:
logInfoDebug("Packet fully read : len : %d / type : %d", p.PLength, p.PType)
}
2020-06-14 23:31:31 +02:00
2020-06-15 12:42:02 +02:00
readPacket(p, b[:p.PLength])
2020-06-14 23:31:31 +02:00
2020-06-14 22:39:17 +02:00
c := make([]byte, 0xFFFF)
copy(c, b[p.PLength:])
b = c
2020-06-14 22:42:34 +02:00
read -= int(p.PLength)
2020-06-14 22:39:17 +02:00
}
2020-06-14 16:27:57 +02:00
}