From 3e1317b95e771aaad747be719c125f2a23234483 Mon Sep 17 00:00:00 2001 From: HimbeerserverDE Date: Sat, 9 Dec 2023 00:03:17 +0100 Subject: [PATCH] fix default server randomness, introducing group support (#140) --- config.go | 14 ++++++++++---- doc/config.md | 7 +++++++ run.go | 5 +++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/config.go b/config.go index ed42084..03b1eae 100644 --- a/config.go +++ b/config.go @@ -49,6 +49,7 @@ type Config struct { NoTelnet bool TelnetAddr string BindAddr string + DefaultSrv string Servers map[string]Server ForceDefaultSrv bool KickOnNewPool bool @@ -200,12 +201,17 @@ func copyMapSlice[K comparable, V any](in map[K][]V) map[K][]V { // and information about it. The return values are uninitialized // if no servers exist. func (cnf Config) DefaultServerInfo() (string, Server) { - for name, srv := range Conf().Servers { - return name, srv + name, ok := cnf.RandomGroupServer(cnf.DefaultSrv) + if !ok { + return "", Server{} + } + + srv, ok := cnf.Servers[name] + if !ok { + return "", Server{} } - // No servers are configured. - return "", Server{} + return name, srv } // DefaultServerName returns the name of the default server. diff --git a/doc/config.md b/doc/config.md index c114d42..ce2c64f 100644 --- a/doc/config.md +++ b/doc/config.md @@ -93,6 +93,13 @@ Default: ":40000" Description: The proxy will listen for new clients on this address. ``` +> `DefaultSrv` +``` +Type: string +Default: "" +Description: The default server to connect new clients to. May be a group. +``` + > `Servers` ``` Type: map[string]Server diff --git a/run.go b/run.go index b017b67..9c3be9a 100644 --- a/run.go +++ b/run.go @@ -117,6 +117,11 @@ func runFunc() { cc.Kick("No servers are configured.") return } + if _, ok := conf.Servers[""]; !ok && conf.DefaultServerName() == "" { + cc.Log("<-", "no default server") + cc.Kick("No valid default server is configured.") + return + } srvName, srv := conf.DefaultServerInfo() lastSrv, err := authIface.LastSrv(cc.Name())