gottdad/packet.go

158 lines
4.3 KiB
Go
Raw Normal View History

2020-06-14 16:27:57 +02:00
package main
2020-06-14 16:45:07 +02:00
import (
2020-06-14 23:40:04 +02:00
"bufio"
2020-06-14 16:45:07 +02:00
"bytes"
2020-06-14 17:23:07 +02:00
"encoding/binary"
2020-06-14 16:45:07 +02:00
)
2020-06-14 16:27:57 +02:00
const (
2020-06-14 23:39:30 +02:00
AdminPacketAdminJoin = 0
AdminPacketAdminQuit = 1
AdminPacketAdminUpdateFrequency = 2
AdminPacketAdminPoll = 3
AdminPacketAdminChat = 4
AdminPacketAdminRCon = 5
AdminPacketAdminGamescript = 6
AdminPacketAdminPing = 7
AdminPacketServerFull = 100
AdminPacketServerBanned = 101
AdminPacketServerError = 102
AdminPacketServerProtocol = 103
AdminPacketServerWelcome = 104
AdminPacketServerNewgame = 105
AdminPacketServerShutdown = 106
AdminPacketServerDate = 107
AdminPacketServerClientJoin = 108
AdminPacketServerClientInfo = 109
AdminPacketServerClientUpdate = 110
AdminPacketServerClientQuit = 111
AdminPacketServerClientError = 112
AdminPacketServerCompanyNew = 113
AdminPacketServerCompanyInfo = 114
AdminPacketServerCompanyUpdate = 115
AdminPacketServerCompanyRemove = 116
AdminPacketServerCompanyEconomy = 117
AdminPacketServerCompanyStats = 118
AdminPacketServerChat = 119
AdminPacketServerRCon = 120
AdminPacketServerConsole = 121
AdminPacketServerCmdNames = 122
AdminPacketServerCmdLogging = 123
AdminPacketServerGamescript = 124
AdminPacketServerRConEnd = 125
AdminPacketServerPong = 126
InvalidAdminPacket = 255
2020-06-14 16:27:57 +02:00
)
2020-06-14 16:45:07 +02:00
type Packet struct {
PLength uint16
PType uint8
}
type PacketAdminJoin struct {
Packet
2020-06-14 17:12:58 +02:00
Password string
AppName string
AppVersion string
2020-06-14 16:45:07 +02:00
}
2020-06-14 23:31:26 +02:00
type PacketAdminServerProtocol struct {
Packet
ProtocolVersion uint8
FurtherData bool
UpdatePacketType uint16
FrequenciesAllowed uint16
}
type PacketAdminServerWelcome struct {
Packet
ServerName string
OpenTTDVersion string
Dedicated bool
MapName string
MapSeed uint32
MapLandscape uint8
MapStartDate uint32
MapX uint16
MapY uint16
}
2020-06-14 16:45:07 +02:00
func (p *PacketAdminJoin) Bytes() []byte {
buf := new(bytes.Buffer)
2020-06-14 17:38:58 +02:00
p.PLength = uint16(len(p.Password) + len(p.AppName) + len(p.AppVersion) + 6)
2020-06-14 17:23:07 +02:00
2020-06-14 17:31:47 +02:00
binary.Write(buf, binary.LittleEndian, p.PLength)
binary.Write(buf, binary.LittleEndian, p.PType)
2020-06-14 17:23:07 +02:00
buf.WriteString(p.Password)
2020-06-14 17:34:19 +02:00
buf.WriteByte(0)
2020-06-14 17:23:07 +02:00
buf.WriteString(p.AppName)
2020-06-14 17:34:19 +02:00
buf.WriteByte(0)
2020-06-14 17:23:07 +02:00
buf.WriteString(p.AppVersion)
2020-06-14 17:34:19 +02:00
buf.WriteByte(0)
2020-06-14 17:12:58 +02:00
2020-06-14 16:45:07 +02:00
return buf.Bytes()
}
2020-06-14 23:31:26 +02:00
2020-06-14 23:32:55 +02:00
func (p *PacketAdminServerWelcome) Read(b []byte) {
2020-06-14 23:31:26 +02:00
r := bufio.NewReader(bytes.NewReader(b))
2020-06-14 23:50:16 +02:00
r.Discard(3)
2020-06-14 23:31:26 +02:00
p.ServerName, _ = r.ReadString(0)
p.OpenTTDVersion, _ = r.ReadString(0)
c, _ := r.ReadByte()
2020-06-14 23:41:47 +02:00
if c > 0 {
p.Dedicated = true
} else {
p.Dedicated = false
}
2020-06-14 23:31:26 +02:00
p.MapName, _ = r.ReadString(0)
bs := make([]byte, 13)
_, _ = r.Read(bs)
p.MapSeed = binary.LittleEndian.Uint32(bs[0:])
2020-06-14 23:43:33 +02:00
r.Discard(4)
c, _ = r.ReadByte()
p.MapLandscape = uint8(c)
2020-06-14 23:31:26 +02:00
p.MapStartDate = binary.LittleEndian.Uint32(bs[5:])
p.MapX = binary.LittleEndian.Uint16(bs[9:])
p.MapY = binary.LittleEndian.Uint16(bs[11:])
}
2020-06-15 10:45:56 +02:00
func (p *PacketAdminServerProtocol) Read(b []byte) {
2020-06-15 10:45:18 +02:00
r := bufio.NewReader(bytes.NewReader(b))
r.Discard(3)
2020-06-15 10:46:16 +02:00
c, _ := r.ReadByte()
2020-06-15 10:45:18 +02:00
p.ProtocolVersion = uint8(c)
c, _ = r.ReadByte()
if c > 0 {
p.FurtherData = true
} else {
p.FurtherData = false
}
bs := make([]byte, 13)
_, _ = r.Read(bs)
p.UpdatePacketType = binary.LittleEndian.Uint16(bs[0:])
p.FrequenciesAllowed = binary.LittleEndian.Uint16(bs[2:])
}
2020-06-14 23:31:26 +02:00
func handlePacket(p Packet, b []byte) {
switch p.PType {
case AdminPacketServerProtocol:
2020-06-15 10:45:18 +02:00
logInfoDebug("AdminPacketServerProtocol Raw : %v", b)
2020-06-15 10:45:56 +02:00
sp := PacketAdminServerProtocol{
2020-06-15 10:45:18 +02:00
Packet: p,
}
sp.Read(b)
logInfoDebug("AdminPacketServerProtocol :\n- ProtocolVersion: %v\n- FurtherData: %v\n- UpdatePacketType: %v\n- FrequenciesAllowed: %b", sp.ProtocolVersion, sp.FurtherData, sp.UpdatePacketType, sp.FrequenciesAllowed)
2020-06-14 23:31:26 +02:00
case AdminPacketServerWelcome:
2020-06-14 23:44:57 +02:00
logInfoDebug("AdminPacketServerWelcome Raw : %v", b)
2020-06-14 23:31:26 +02:00
sp := PacketAdminServerWelcome{
2020-06-14 23:32:22 +02:00
Packet: p,
2020-06-14 23:31:26 +02:00
}
sp.Read(b)
2020-06-14 23:53:00 +02:00
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)
2020-06-14 23:31:26 +02:00
default:
}
}