diff --git a/cmd/q3rcon-proxy/main.go b/cmd/q3rcon-proxy/main.go index a194ed5..ada33be 100644 --- a/cmd/q3rcon-proxy/main.go +++ b/cmd/q3rcon-proxy/main.go @@ -3,7 +3,7 @@ package main import ( "fmt" "os" - "strconv" + "slices" "strings" log "github.com/sirupsen/logrus" @@ -11,7 +11,33 @@ import ( "github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy" ) -func start(proxy string) { +func main() { + logLevel, err := getEnvInt("Q3RCON_LOGLEVEL") + if err != nil { + log.Fatalf("unable to parse Q3RCON_LEVEL: %s", err.Error()) + } + if slices.Contains(log.AllLevels, log.Level(logLevel)) { + log.SetLevel(log.Level(logLevel)) + } + + proxies := os.Getenv("Q3RCON_PROXY") + if proxies == "" { + log.Fatal("env Q3RCON_PROXY required") + } + + host := os.Getenv("Q3RCON_HOST") + if host == "" { + host = "0.0.0.0" + } + + for _, proxy := range strings.Split(proxies, ";") { + go start(host, proxy) + } + + <-make(chan int) +} + +func start(host, proxy string) { port, target := func() (string, string) { x := strings.Split(proxy, ":") return x[0], x[1] @@ -26,51 +52,3 @@ func start(proxy string) { log.Fatal(c.ListenAndServe()) } - -var ( - proxies, host string -) - -func getenvInt(key string) (int, error) { - s := os.Getenv(key) - if s == "" { - return 0, nil - } - v, err := strconv.Atoi(s) - if err != nil { - return 0, err - } - return v, nil -} - -func init() { - proxies = os.Getenv("Q3RCON_PROXY") - if proxies == "" { - log.Fatal("env Q3RCON_PROXY required") - } - - host = os.Getenv("Q3RCON_HOST") - if host == "" { - host = "0.0.0.0" - } - - debug, err := getenvInt("Q3RCON_DEBUG") - if err != nil { - log.Fatal(err) - } - - if debug == 1 { - log.SetLevel(log.DebugLevel) - } else { - log.SetLevel(log.InfoLevel) - } - -} - -func main() { - for _, proxy := range strings.Split(proxies, ";") { - go start(proxy) - } - - <-make(chan int) -} diff --git a/cmd/q3rcon-proxy/util.go b/cmd/q3rcon-proxy/util.go new file mode 100644 index 0000000..1bf4329 --- /dev/null +++ b/cmd/q3rcon-proxy/util.go @@ -0,0 +1,18 @@ +package main + +import ( + "os" + "strconv" +) + +func getEnvInt(key string) (int, error) { + s := os.Getenv(key) + if s == "" { + return 0, nil + } + v, err := strconv.Atoi(s) + if err != nil { + return 0, err + } + return v, nil +}