From e9cc5cc3e2eec86ba2326722010f4dc3e6495c7c Mon Sep 17 00:00:00 2001 From: wish Date: Thu, 4 Aug 2022 23:52:13 +1000 Subject: [PATCH] parse host as FQDN or IP --- config.json | 4 ++-- config/config.go | 8 ++++---- main.go | 14 ++++++++++++++ server/entranceserver/make_resp.go | 2 +- server/launcherserver/routes.go | 2 +- server/signserver/dsgn_resp.go | 2 +- 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/config.json b/config.json index c5bf94ca3..976756597 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,6 @@ { - "host_ip": "127.0.0.1", - "bin_path": "bin", + "Host": "127.0.0.1", + "BinPath": "bin", "DisableSoftCrash": false, "devmode": true, "devmodeoptions": { diff --git a/config/config.go b/config/config.go index 5009e5aac..8b7daf7ee 100644 --- a/config/config.go +++ b/config/config.go @@ -9,8 +9,8 @@ import ( // Config holds the global server-wide config. type Config struct { - HostIP string `mapstructure:"host_ip"` - BinPath string `mapstructure:"bin_path"` + Host string `mapstructure:"Host"` + BinPath string `mapstructure:"BinPath"` DisableSoftCrash bool // Disables the 'Press Return to exit' dialog allowing scripts to reboot the server automatically DevMode bool @@ -140,8 +140,8 @@ func LoadConfig() (*Config, error) { return nil, err } - if c.HostIP == "" { - c.HostIP = getOutboundIP4().To4().String() + if c.Host == "" { + c.Host = getOutboundIP4().To4().String() } return c, nil diff --git a/main.go b/main.go index c950d3bef..3638a0093 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "math/rand" + "net" "os" "os/signal" "syscall" @@ -48,6 +49,19 @@ func main() { preventClose("Database password is blank") } + if net.ParseIP(erupeConfig.Host) == nil { + ips, _ := net.LookupIP(erupeConfig.Host) + for _, ip := range ips { + if ip != nil { + erupeConfig.Host = ip.String() + break + } + } + if net.ParseIP(erupeConfig.Host) == nil { + preventClose("Invalid host address") + } + } + // Discord bot var discordBot *discordbot.DiscordBot = nil diff --git a/server/entranceserver/make_resp.go b/server/entranceserver/make_resp.go index 2a46a3630..c0358faef 100644 --- a/server/entranceserver/make_resp.go +++ b/server/entranceserver/make_resp.go @@ -28,7 +28,7 @@ func encodeServerInfo(config *config.Config, s *Server) []byte { panic(err) } if si.IP == "" { - si.IP = config.HostIP + si.IP = config.Host } bf.WriteUint32(binary.LittleEndian.Uint32(net.ParseIP(si.IP).To4())) bf.WriteUint16(16 + uint16(serverIdx)) diff --git a/server/launcherserver/routes.go b/server/launcherserver/routes.go index c951bf08a..59c47bba7 100644 --- a/server/launcherserver/routes.go +++ b/server/launcherserver/routes.go @@ -12,7 +12,7 @@ import ( func serverList(s *Server, w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, ``, - s.erupeConfig.HostIP, + s.erupeConfig.Host, s.erupeConfig.Sign.Port, ) } diff --git a/server/signserver/dsgn_resp.go b/server/signserver/dsgn_resp.go index 17701002a..fee38c1d3 100644 --- a/server/signserver/dsgn_resp.go +++ b/server/signserver/dsgn_resp.go @@ -47,7 +47,7 @@ func (s *Session) makeSignInResp(uid int) []byte { bf.WriteUint32(0xFFFFFFFF) // login_token_number bf.WriteBytes([]byte(token)) // login_token bf.WriteUint32(uint32(time.Now().Unix())) // current time - ps.Uint8(bf, fmt.Sprintf("%s:%d", s.server.erupeConfig.HostIP, s.server.erupeConfig.Entrance.Port), false) + ps.Uint8(bf, fmt.Sprintf("%s:%d", s.server.erupeConfig.Host, s.server.erupeConfig.Entrance.Port), false) lastPlayed := uint32(0) for _, char := range chars {