mirror of
https://github.com/Mezeporta/Erupe.git
synced 2025-12-15 16:34:51 +01:00
Config / DB now its own package
This commit is contained in:
119
server/entrance/entrance_server.go
Normal file
119
server/entrance/entrance_server.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package entrance
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"erupe-ce/config"
|
||||
"erupe-ce/network"
|
||||
"erupe-ce/utils/logger"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// EntranceServer is a MHF entrance server.
|
||||
type EntranceServer struct {
|
||||
sync.Mutex
|
||||
logger logger.Logger
|
||||
listener net.Listener
|
||||
isShuttingDown bool
|
||||
}
|
||||
|
||||
// NewServer creates a new Server type.
|
||||
func NewServer() *EntranceServer {
|
||||
server := &EntranceServer{
|
||||
logger: logger.Get().Named("entrance"),
|
||||
}
|
||||
return server
|
||||
}
|
||||
|
||||
// Start starts the server in a new goroutine.
|
||||
func (server *EntranceServer) Start() error {
|
||||
|
||||
l, err := net.Listen("tcp", fmt.Sprintf(":%d", config.GetConfig().Entrance.Port))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
server.listener = l
|
||||
|
||||
go server.acceptClients()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shutdown exits the server gracefully.
|
||||
func (server *EntranceServer) Shutdown() {
|
||||
server.logger.Debug("Shutting down...")
|
||||
|
||||
server.Lock()
|
||||
server.isShuttingDown = true
|
||||
server.Unlock()
|
||||
|
||||
// This will cause the acceptor goroutine to error and exit gracefully.
|
||||
server.listener.Close()
|
||||
}
|
||||
|
||||
// acceptClients handles accepting new clients in a loop.
|
||||
func (server *EntranceServer) acceptClients() {
|
||||
for {
|
||||
conn, err := server.listener.Accept()
|
||||
if err != nil {
|
||||
// Check if we are shutting down and exit gracefully if so.
|
||||
server.Lock()
|
||||
shutdown := server.isShuttingDown
|
||||
server.Unlock()
|
||||
|
||||
if shutdown {
|
||||
break
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Start a new goroutine for the connection so that we don't block other incoming connections.
|
||||
go server.handleEntranceServerConnection(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func (server *EntranceServer) 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)
|
||||
if err != nil {
|
||||
server.logger.Warn("Failed to read 8 NULL init", zap.Error(err))
|
||||
return
|
||||
} else if n != len(nullInit) {
|
||||
server.logger.Warn("io.ReadFull couldn't read the full 8 byte init.")
|
||||
return
|
||||
}
|
||||
|
||||
// Create a new encrypted connection handler and read a packet from it.
|
||||
cc := network.NewCryptConn(conn)
|
||||
pkt, err := cc.ReadPacket()
|
||||
if err != nil {
|
||||
server.logger.Warn("Error reading packet", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
if config.GetConfig().DebugOptions.LogInboundMessages {
|
||||
fmt.Printf("[Client] -> [Server]\nData [%d bytes]:\n%s\n", len(pkt), hex.Dump(pkt))
|
||||
}
|
||||
|
||||
local := false
|
||||
if strings.Split(conn.RemoteAddr().String(), ":")[0] == "127.0.0.1" {
|
||||
local = true
|
||||
}
|
||||
data := makeSv2Resp(server, local)
|
||||
if len(pkt) > 5 {
|
||||
data = append(data, makeUsrResp(pkt, server)...)
|
||||
}
|
||||
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.
|
||||
}
|
||||
180
server/entrance/make_resp.go
Normal file
180
server/entrance/make_resp.go
Normal file
@@ -0,0 +1,180 @@
|
||||
package entrance
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"erupe-ce/config"
|
||||
"erupe-ce/network/crypto/bin8"
|
||||
"erupe-ce/utils/byteframe"
|
||||
"erupe-ce/utils/db"
|
||||
"erupe-ce/utils/gametime"
|
||||
|
||||
"erupe-ce/utils/stringsupport"
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
func encodeServerInfo(server *EntranceServer, local bool) []byte {
|
||||
serverInfos := config.GetConfig().Entrance.Entries
|
||||
bf := byteframe.NewByteFrame()
|
||||
|
||||
for serverIdx, si := range serverInfos {
|
||||
// Prevent MezFes Worlds displaying on Z1
|
||||
if config.GetConfig().ClientID <= config.Z1 {
|
||||
if si.Type == 6 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if config.GetConfig().ClientID <= config.G6 {
|
||||
if si.Type == 5 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if si.IP == "" {
|
||||
si.IP = config.GetConfig().Host
|
||||
}
|
||||
if local {
|
||||
bf.WriteUint32(binary.LittleEndian.Uint32(net.ParseIP("127.0.0.1").To4()))
|
||||
} else {
|
||||
bf.WriteUint32(binary.LittleEndian.Uint32(net.ParseIP(si.IP).To4()))
|
||||
}
|
||||
bf.WriteUint16(uint16(serverIdx | 16))
|
||||
bf.WriteUint16(0)
|
||||
bf.WriteUint16(uint16(len(si.Channels)))
|
||||
bf.WriteUint8(si.Type)
|
||||
bf.WriteUint8(uint8(((gametime.TimeAdjusted().Unix() / 86400) + int64(serverIdx)) % 3))
|
||||
if config.GetConfig().ClientID >= config.G1 {
|
||||
bf.WriteUint8(si.Recommended)
|
||||
}
|
||||
|
||||
fullName := append(append(stringsupport.UTF8ToSJIS(si.Name), []byte{0x00}...), stringsupport.UTF8ToSJIS(si.Description)...)
|
||||
if config.GetConfig().ClientID >= config.G1 && config.GetConfig().ClientID <= config.G5 {
|
||||
bf.WriteUint8(uint8(len(fullName)))
|
||||
bf.WriteBytes(fullName)
|
||||
} else {
|
||||
if config.GetConfig().ClientID >= config.G51 {
|
||||
bf.WriteUint8(0) // Ignored
|
||||
}
|
||||
bf.WriteBytes(stringsupport.PaddedString(string(fullName), 65, false))
|
||||
}
|
||||
|
||||
if config.GetConfig().ClientID >= config.GG {
|
||||
bf.WriteUint32(si.AllowedClientFlags)
|
||||
}
|
||||
|
||||
for channelIdx, ci := range si.Channels {
|
||||
sid := (serverIdx<<8 | 4096) + (channelIdx | 16)
|
||||
if config.GetConfig().DebugOptions.ProxyPort != 0 {
|
||||
bf.WriteUint16(config.GetConfig().DebugOptions.ProxyPort)
|
||||
} else {
|
||||
bf.WriteUint16(ci.Port)
|
||||
}
|
||||
bf.WriteUint16(uint16(channelIdx | 16))
|
||||
bf.WriteUint16(ci.MaxPlayers)
|
||||
var currentPlayers uint16
|
||||
database, err := db.GetDB() // Capture both return values
|
||||
if err != nil {
|
||||
server.logger.Fatal(fmt.Sprintf("Failed to get database instance: %s", err))
|
||||
}
|
||||
database.QueryRow("SELECT current_players FROM servers WHERE server_id=$1", sid).Scan(¤tPlayers)
|
||||
bf.WriteUint16(currentPlayers)
|
||||
bf.WriteUint16(0)
|
||||
bf.WriteUint16(0)
|
||||
bf.WriteUint16(0)
|
||||
bf.WriteUint16(0)
|
||||
bf.WriteUint16(0)
|
||||
bf.WriteUint16(0)
|
||||
bf.WriteUint16(319) // Unk
|
||||
bf.WriteUint16(254 - currentPlayers) // Unk
|
||||
bf.WriteUint16(255 - currentPlayers) // Unk
|
||||
bf.WriteUint16(12345)
|
||||
}
|
||||
}
|
||||
bf.WriteUint32(uint32(gametime.TimeAdjusted().Unix()))
|
||||
bf.WriteUint32(uint32(config.GetConfig().GameplayOptions.ClanMemberLimits[len(config.GetConfig().GameplayOptions.ClanMemberLimits)-1][1]))
|
||||
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(bin8.CalcSum32(data))
|
||||
bf.WriteBytes(data)
|
||||
}
|
||||
|
||||
dataToEncrypt := bf.Data()
|
||||
|
||||
bf = byteframe.NewByteFrame()
|
||||
bf.WriteUint8(key)
|
||||
bf.WriteBytes(bin8.EncryptBin8(dataToEncrypt, key))
|
||||
return bf.Data()
|
||||
}
|
||||
|
||||
func makeSv2Resp(server *EntranceServer, local bool) []byte {
|
||||
serverInfos := config.GetConfig().Entrance.Entries
|
||||
// Decrease by the number of MezFes Worlds
|
||||
var mf int
|
||||
if config.GetConfig().ClientID <= config.Z1 {
|
||||
for _, si := range serverInfos {
|
||||
if si.Type == 6 {
|
||||
mf++
|
||||
}
|
||||
}
|
||||
}
|
||||
// and Return Worlds
|
||||
var ret int
|
||||
if config.GetConfig().ClientID <= config.G6 {
|
||||
for _, si := range serverInfos {
|
||||
if si.Type == 5 {
|
||||
ret++
|
||||
}
|
||||
}
|
||||
}
|
||||
rawServerData := encodeServerInfo(server, local)
|
||||
|
||||
if config.GetConfig().DebugOptions.LogOutboundMessages {
|
||||
fmt.Printf("[Server] -> [Client]\nData [%d bytes]:\n%s\n", len(rawServerData), hex.Dump(rawServerData))
|
||||
}
|
||||
|
||||
respType := "SV2"
|
||||
if config.GetConfig().ClientID <= config.G32 {
|
||||
respType = "SVR"
|
||||
}
|
||||
|
||||
bf := byteframe.NewByteFrame()
|
||||
bf.WriteBytes(makeHeader(rawServerData, respType, uint16(len(serverInfos)-(mf+ret)), 0x00))
|
||||
return bf.Data()
|
||||
}
|
||||
|
||||
func makeUsrResp(pkt []byte, server *EntranceServer) []byte {
|
||||
bf := byteframe.NewByteFrameFromBytes(pkt)
|
||||
_ = bf.ReadUint32() // ALL+
|
||||
_ = bf.ReadUint8() // 0x00
|
||||
userEntries := bf.ReadUint16()
|
||||
resp := byteframe.NewByteFrame()
|
||||
database, err := db.GetDB()
|
||||
if err != nil {
|
||||
server.logger.Fatal(fmt.Sprintf("Failed to get database instance: %s", err))
|
||||
}
|
||||
for i := 0; i < int(userEntries); i++ {
|
||||
cid := bf.ReadUint32()
|
||||
var sid uint16
|
||||
err := database.QueryRow("SELECT(SELECT server_id FROM sign_sessions WHERE char_id=$1) AS _", cid).Scan(&sid)
|
||||
if err != nil {
|
||||
resp.WriteUint16(0)
|
||||
} else {
|
||||
resp.WriteUint16(sid)
|
||||
}
|
||||
resp.WriteUint16(0)
|
||||
}
|
||||
|
||||
if config.GetConfig().DebugOptions.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