Port entrance server to Go

This commit is contained in:
Andrew Gutekanst
2020-01-01 08:46:36 +09:00
parent caf9fc91ed
commit 1c6b7ef257
4 changed files with 151 additions and 235 deletions

View File

@@ -0,0 +1,80 @@
package entranceserver
import (
"encoding/hex"
"fmt"
"io"
"net"
"github.com/Andoryuuta/Erupe/network"
)
func handleEntranceServerConnection(conn net.Conn) {
// Client initalizes the connection with a one-time buffer of 8 NULL bytes.
nullInit := make([]byte, 8)
n, err := io.ReadFull(conn, nullInit)
if err != nil {
fmt.Println(err)
return
} else if n != len(nullInit) {
fmt.Println("io.ReadFull couldn't read the full 8 byte init.")
return
}
cc := network.NewCryptConn(conn)
for {
pkt, err := cc.ReadPacket()
if err != nil {
return
}
fmt.Printf("Got entrance server command:\n%s\n", hex.Dump(pkt))
data := makeResp([]ServerInfo{
ServerInfo{
IP: net.ParseIP("127.0.0.1"),
Unk2: 0,
Type: 1,
Season: 0,
Unk6: 3,
Name: "AErupe Server in Go! @localhost",
AllowedClientFlags: 4096,
Channels: []ChannelInfo{
ChannelInfo{
Port: 54001,
MaxPlayers: 100,
CurrentPlayers: 3,
Unk4: 0,
Unk5: 0,
Unk6: 0,
Unk7: 0,
Unk8: 0,
Unk9: 0,
Unk10: 319,
Unk11: 248,
Unk12: 159,
Unk13: 12345,
},
},
},
})
cc.SendPacket(data)
}
}
func DoEntranceServer(listenAddr string) {
l, err := net.Listen("tcp", listenAddr)
if err != nil {
panic(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
panic(err)
}
go handleEntranceServerConnection(conn)
}
}

119
entranceserver/make_resp.go Normal file
View File

@@ -0,0 +1,119 @@
package entranceserver
import (
"encoding/binary"
"net"
"github.com/Andoryuuta/byteframe"
)
func paddedString(x string, size uint) []byte {
out := make([]byte, size)
copy(out, x)
// Null terminate it.
out[len(out)-1] = 0
return out
}
// ServerInfo represents an entry in the serverlist.
type ServerInfo struct {
IP net.IP
Unk2 uint16
Type uint8 // Server type. 0=?, 1=open, 2=cities, 3=newbie, 4=bar
Season uint8 // Server activity. 0 = green, 1 = orange, 2 = blue
Unk6 uint8 // Something to do with server recommendation on 0, 3, and 5.
Name string // Server name, 66 byte null terminated Shift-JIS.
// 4096(PC, PS3/PS4)?, 8258(PC, PS3/PS4)?, 8192 == nothing?
// THIS ONLY EXISTS IF Binary8Header.type == "SV2", NOT "SVR"!
AllowedClientFlags uint32
Channels []ChannelInfo
}
// ChannelInfo represents an entry in a server's channel list.
type ChannelInfo struct {
Port uint16
//ChannelIndex uint16
MaxPlayers uint16
CurrentPlayers uint16
Unk4 uint16
Unk5 uint16
Unk6 uint16
Unk7 uint16
Unk8 uint16
Unk9 uint16
Unk10 uint16
Unk11 uint16
Unk12 uint16
Unk13 uint16
}
func encodeServerInfo(serverInfos []ServerInfo) []byte {
bf := byteframe.NewByteFrame()
for serverIdx, si := range serverInfos {
bf.WriteUint32(binary.LittleEndian.Uint32(si.IP))
bf.WriteUint16(16 + uint16(serverIdx))
bf.WriteUint16(si.Unk2)
bf.WriteUint16(uint16(len(si.Channels)))
bf.WriteUint8(si.Type)
bf.WriteUint8(si.Season)
bf.WriteUint8(si.Unk6)
bf.WriteBytes(paddedString(si.Name, 66))
bf.WriteUint32(si.AllowedClientFlags)
for channelIdx, ci := range si.Channels {
bf.WriteUint16(ci.Port)
bf.WriteUint16(16 + uint16(channelIdx))
bf.WriteUint16(ci.MaxPlayers)
bf.WriteUint16(ci.CurrentPlayers)
bf.WriteUint16(ci.Unk4)
bf.WriteUint16(ci.Unk5)
bf.WriteUint16(ci.Unk6)
bf.WriteUint16(ci.Unk7)
bf.WriteUint16(ci.Unk8)
bf.WriteUint16(ci.Unk9)
bf.WriteUint16(ci.Unk10)
bf.WriteUint16(ci.Unk11)
bf.WriteUint16(ci.Unk12)
bf.WriteUint16(ci.Unk13)
}
}
return bf.Data()
}
func makeHeader(data []byte, respType string, entryCount uint16, key byte) []byte {
bf := byteframe.NewByteFrame()
bf.WriteBytes([]byte(respType))
bf.WriteUint16(entryCount)
bf.WriteUint16(uint16(len(data)))
if len(data) > 0 {
bf.WriteUint32(CalcSum32(data))
bf.WriteBytes(data)
}
dataToEncrypt := bf.Data()
bf = byteframe.NewByteFrame()
bf.WriteUint8(key)
bf.WriteBytes(EncryptBin8(dataToEncrypt, key))
return bf.Data()
}
func makeResp(servers []ServerInfo) []byte {
rawServerData := encodeServerInfo(servers)
bf := byteframe.NewByteFrame()
bf.WriteBytes(makeHeader(rawServerData, "SV2", uint16(len(servers)), 0x00))
// TODO(Andoryuuta): Figure out what this user data is.
// Is it for the friends list at the world selection screen?
// If so, how does it work without the entrance server connection being authenticated?
bf.WriteBytes(makeHeader([]byte{}, "USR", 0, 0x00))
return bf.Data()
}