mirror of
https://github.com/Mezeporta/Erupe.git
synced 2025-12-23 04:16:33 +01:00
Merge remote-tracking branch 'origin/main' into feature/restore-history
This commit is contained in:
@@ -5,10 +5,11 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/Andoryuuta/Erupe/config"
|
||||
"github.com/Andoryuuta/Erupe/network"
|
||||
"erupe-ce/network"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -17,7 +18,7 @@ import (
|
||||
type Server struct {
|
||||
sync.Mutex
|
||||
logger *zap.Logger
|
||||
erupeConfig *config.Config
|
||||
erupeConfig *_config.Config
|
||||
db *sqlx.DB
|
||||
listener net.Listener
|
||||
isShuttingDown bool
|
||||
@@ -27,7 +28,7 @@ type Server struct {
|
||||
type Config struct {
|
||||
Logger *zap.Logger
|
||||
DB *sqlx.DB
|
||||
ErupeConfig *config.Config
|
||||
ErupeConfig *_config.Config
|
||||
}
|
||||
|
||||
// NewServer creates a new Server type.
|
||||
@@ -57,7 +58,7 @@ func (s *Server) Start() error {
|
||||
|
||||
// Shutdown exits the server gracefully.
|
||||
func (s *Server) Shutdown() {
|
||||
s.logger.Debug("Shutting down")
|
||||
s.logger.Debug("Shutting down...")
|
||||
|
||||
s.Lock()
|
||||
s.isShuttingDown = true
|
||||
@@ -67,7 +68,7 @@ func (s *Server) Shutdown() {
|
||||
s.listener.Close()
|
||||
}
|
||||
|
||||
//acceptClients handles accepting new clients in a loop.
|
||||
// acceptClients handles accepting new clients in a loop.
|
||||
func (s *Server) acceptClients() {
|
||||
for {
|
||||
conn, err := s.listener.Accept()
|
||||
@@ -90,6 +91,7 @@ func (s *Server) acceptClients() {
|
||||
}
|
||||
|
||||
func (s *Server) handleEntranceServerConnection(conn net.Conn) {
|
||||
defer conn.Close()
|
||||
// Client initalizes the connection with a one-time buffer of 8 NULL bytes.
|
||||
nullInit := make([]byte, 8)
|
||||
n, err := io.ReadFull(conn, nullInit)
|
||||
@@ -109,12 +111,19 @@ func (s *Server) handleEntranceServerConnection(conn net.Conn) {
|
||||
return
|
||||
}
|
||||
|
||||
s.logger.Debug("Got entrance server command:\n", zap.String("raw", hex.Dump(pkt)))
|
||||
if s.erupeConfig.DevMode && s.erupeConfig.DevModeOptions.LogInboundMessages {
|
||||
fmt.Printf("[Client] -> [Server]\nData [%d bytes]:\n%s\n", len(pkt), hex.Dump(pkt))
|
||||
}
|
||||
|
||||
data := makeResp(s.erupeConfig.Entrance.Entries)
|
||||
local := false
|
||||
if strings.Split(conn.RemoteAddr().String(), ":")[0] == "127.0.0.1" {
|
||||
local = true
|
||||
}
|
||||
data := makeSv2Resp(s.erupeConfig, s, local)
|
||||
if len(pkt) > 5 {
|
||||
data = append(data, makeUsrResp(pkt, s)...)
|
||||
}
|
||||
cc.SendPacket(data)
|
||||
|
||||
// Close because we only need to send the response once.
|
||||
// Any further requests from the client will come from a new connection.
|
||||
conn.Close()
|
||||
}
|
||||
|
||||
@@ -2,53 +2,93 @@ package entranceserver
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"erupe-ce/common/stringsupport"
|
||||
_config "erupe-ce/config"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/Andoryuuta/Erupe/config"
|
||||
"github.com/Andoryuuta/byteframe"
|
||||
"erupe-ce/common/byteframe"
|
||||
"erupe-ce/server/channelserver"
|
||||
)
|
||||
|
||||
func paddedString(x string, size uint) []byte {
|
||||
out := make([]byte, size)
|
||||
copy(out, x)
|
||||
|
||||
// Null terminate it.
|
||||
out[len(out)-1] = 0
|
||||
return out
|
||||
}
|
||||
|
||||
func encodeServerInfo(serverInfos []config.EntranceServerInfo) []byte {
|
||||
func encodeServerInfo(config *_config.Config, s *Server, local bool) []byte {
|
||||
serverInfos := config.Entrance.Entries
|
||||
bf := byteframe.NewByteFrame()
|
||||
|
||||
for serverIdx, si := range serverInfos {
|
||||
bf.WriteUint32(binary.LittleEndian.Uint32(net.ParseIP(si.IP).To4()))
|
||||
// Prevent MezFes Worlds displaying on Z1
|
||||
if config.RealClientMode <= _config.Z1 {
|
||||
if si.Type == 6 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if config.RealClientMode <= _config.G6 {
|
||||
if si.Type == 5 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
sid := (4096 + serverIdx*256) * 6000
|
||||
if si.IP == "" {
|
||||
si.IP = config.Host
|
||||
}
|
||||
if local {
|
||||
bf.WriteUint32(0x0100007F) // 127.0.0.1
|
||||
} else {
|
||||
bf.WriteUint32(binary.LittleEndian.Uint32(net.ParseIP(si.IP).To4()))
|
||||
}
|
||||
bf.WriteUint16(16 + uint16(serverIdx))
|
||||
bf.WriteUint16(si.Unk2)
|
||||
bf.WriteUint16(0x0000)
|
||||
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)
|
||||
bf.WriteUint8(uint8(((channelserver.TimeAdjusted().Unix() / 86400) + int64(serverIdx)) % 3))
|
||||
if s.erupeConfig.RealClientMode >= _config.G1 {
|
||||
bf.WriteUint8(si.Recommended)
|
||||
}
|
||||
|
||||
if s.erupeConfig.RealClientMode <= _config.F5 {
|
||||
combined := append(stringsupport.UTF8ToSJIS(si.Name), []byte{0x00}...)
|
||||
combined = append(combined, stringsupport.UTF8ToSJIS(si.Description)...)
|
||||
bf.WriteBytes(stringsupport.PaddedString(string(combined), 65, false))
|
||||
} else if s.erupeConfig.RealClientMode <= _config.GG {
|
||||
combined := append(stringsupport.UTF8ToSJIS(si.Name), []byte{0x00}...)
|
||||
combined = append(combined, stringsupport.UTF8ToSJIS(si.Description)...)
|
||||
bf.WriteUint8(uint8(len(combined)))
|
||||
bf.WriteBytes(combined)
|
||||
} else {
|
||||
bf.WriteUint8(0) // Prevents malformed server name
|
||||
combined := append(stringsupport.UTF8ToSJIS(si.Name), []byte{0x00}...)
|
||||
combined = append(combined, stringsupport.UTF8ToSJIS(si.Description)...)
|
||||
bf.WriteBytes(stringsupport.PaddedString(string(combined), 65, false))
|
||||
}
|
||||
|
||||
if s.erupeConfig.RealClientMode >= _config.GG {
|
||||
bf.WriteUint32(si.AllowedClientFlags)
|
||||
}
|
||||
|
||||
for channelIdx, ci := range si.Channels {
|
||||
sid = (4096 + serverIdx*256) + (16 + channelIdx)
|
||||
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)
|
||||
var currentPlayers uint16
|
||||
s.db.QueryRow("SELECT current_players FROM servers WHERE server_id=$1", sid).Scan(¤tPlayers)
|
||||
bf.WriteUint16(currentPlayers)
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(319) // Unk
|
||||
bf.WriteUint16(252) // Unk
|
||||
bf.WriteUint16(248) // Unk
|
||||
bf.WriteUint16(12345) // Unk
|
||||
}
|
||||
}
|
||||
|
||||
bf.WriteUint32(uint32(channelserver.TimeAdjusted().Unix()))
|
||||
bf.WriteUint32(0x0000003C)
|
||||
return bf.Data()
|
||||
}
|
||||
|
||||
@@ -70,17 +110,63 @@ func makeHeader(data []byte, respType string, entryCount uint16, key byte) []byt
|
||||
return bf.Data()
|
||||
}
|
||||
|
||||
func makeResp(servers []config.EntranceServerInfo) []byte {
|
||||
rawServerData := encodeServerInfo(servers)
|
||||
func makeSv2Resp(config *_config.Config, s *Server, local bool) []byte {
|
||||
serverInfos := config.Entrance.Entries
|
||||
// Decrease by the number of MezFes Worlds
|
||||
var mf int
|
||||
if config.RealClientMode <= _config.Z1 {
|
||||
for _, si := range serverInfos {
|
||||
if si.Type == 6 {
|
||||
mf++
|
||||
}
|
||||
}
|
||||
}
|
||||
// and Return Worlds
|
||||
var ret int
|
||||
if config.RealClientMode <= _config.G6 {
|
||||
for _, si := range serverInfos {
|
||||
if si.Type == 5 {
|
||||
ret++
|
||||
}
|
||||
}
|
||||
}
|
||||
rawServerData := encodeServerInfo(config, s, local)
|
||||
|
||||
if s.erupeConfig.DevMode && s.erupeConfig.DevModeOptions.LogOutboundMessages {
|
||||
fmt.Printf("[Server] -> [Client]\nData [%d bytes]:\n%s\n", len(rawServerData), hex.Dump(rawServerData))
|
||||
}
|
||||
|
||||
respType := "SV2"
|
||||
if config.RealClientMode <= _config.G32 {
|
||||
respType = "SVR"
|
||||
}
|
||||
|
||||
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))
|
||||
|
||||
bf.WriteBytes(makeHeader(rawServerData, respType, uint16(len(serverInfos)-(mf+ret)), 0x00))
|
||||
return bf.Data()
|
||||
|
||||
}
|
||||
|
||||
func makeUsrResp(pkt []byte, s *Server) []byte {
|
||||
bf := byteframe.NewByteFrameFromBytes(pkt)
|
||||
_ = bf.ReadUint32() // ALL+
|
||||
_ = bf.ReadUint8() // 0x00
|
||||
userEntries := bf.ReadUint16()
|
||||
resp := byteframe.NewByteFrame()
|
||||
for i := 0; i < int(userEntries); i++ {
|
||||
cid := bf.ReadUint32()
|
||||
var sid uint16
|
||||
err := s.db.QueryRow("SELECT(SELECT server_id FROM sign_sessions WHERE char_id=$1) AS _", cid).Scan(&sid)
|
||||
if err != nil {
|
||||
resp.WriteBytes(make([]byte, 4))
|
||||
} else {
|
||||
resp.WriteUint16(sid)
|
||||
resp.WriteUint16(0)
|
||||
}
|
||||
}
|
||||
|
||||
if s.erupeConfig.DevMode && s.erupeConfig.DevModeOptions.LogOutboundMessages {
|
||||
fmt.Printf("[Server] -> [Client]\nData [%d bytes]:\n%s\n", len(resp.Data()), hex.Dump(resp.Data()))
|
||||
}
|
||||
|
||||
return makeHeader(resp.Data(), "USR", userEntries, 0x00)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user