From c2266ac9d9c1649efaeaf61c3fe8a4dca6b8533d Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 29 Nov 2024 04:33:47 +0000 Subject: [PATCH] target host is now configurable. stale session timeout default value increased --- .gitignore | 1 + cmd/q3rcon-proxy/main.go | 42 +++++++++++++++++++++++++--------------- pkg/udpproxy/udpproxy.go | 8 ++++---- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index cfb7e69..caf27b1 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ *.dll *.so *.dylib +bin/ # Test binary, built with `go test -c` *.test diff --git a/cmd/q3rcon-proxy/main.go b/cmd/q3rcon-proxy/main.go index 5a72b2d..062c8f7 100644 --- a/cmd/q3rcon-proxy/main.go +++ b/cmd/q3rcon-proxy/main.go @@ -21,43 +21,53 @@ func main() { log.SetLevel(log.Level(logLevel)) } - proxies := os.Getenv("Q3RCON_PROXY") - if proxies == "" { - log.Fatal("env Q3RCON_PROXY required") + proxyHost := os.Getenv("Q3RCON_PROXY_HOST") + if proxyHost == "" { + proxyHost = "0.0.0.0" } - host := os.Getenv("Q3RCON_HOST") - if host == "" { - host = "0.0.0.0" + targetHost := os.Getenv("Q3RCON_TARGET_HOST") + if targetHost == "" { + targetHost = "127.0.0.1" + } + + proxies := os.Getenv("Q3RCON_TARGET_PORTS") + if proxies == "" { + log.Fatal("env Q3RCON_TARGET_PORTS required") } sessionTimeout, err := getEnvInt("Q3RCON_SESSION_TIMEOUT") if err != nil { log.Fatalf("unable to parse Q3RCON_SESSION_TIMEOUT: %s", err.Error()) } - - for _, proxy := range strings.Split(proxies, ";") { - go start(host, proxy, sessionTimeout) + if sessionTimeout == 0 { + sessionTimeout = 20 } - <-make(chan int) + for _, proxy := range strings.Split(proxies, ";") { + go start(proxyHost, targetHost, proxy, sessionTimeout) + } + + <-make(chan struct{}) } -func start(host, proxy string, sessionTimeout int) { - port, target := func() (string, string) { - x := strings.Split(proxy, ":") +func start(proxyHost, targetHost, ports string, sessionTimeout int) { + proxyPort, targetPort := func() (string, string) { + x := strings.Split(ports, ":") return x[0], x[1] }() + hostAddr := fmt.Sprintf("%s:%s", proxyHost, proxyPort) + proxyAddr := fmt.Sprintf("%s:%s", targetHost, targetPort) + c, err := udpproxy.New( - fmt.Sprintf("%s:%s", host, port), - fmt.Sprintf("127.0.0.1:%s", target), + hostAddr, proxyAddr, udpproxy.WithSessionTimeout(time.Duration(sessionTimeout)*time.Minute)) if err != nil { log.Fatal(err) } - log.Printf("q3rcon-proxy initialized: [proxy] (%s:%s) [target] (127.0.0.1:%s)", host, port, target) + log.Printf("q3rcon-proxy initialized: [proxy] (%s) [target] (%s)", hostAddr, proxyAddr) log.Fatal(c.ListenAndServe()) } diff --git a/pkg/udpproxy/udpproxy.go b/pkg/udpproxy/udpproxy.go index 7885370..5198d10 100644 --- a/pkg/udpproxy/udpproxy.go +++ b/pkg/udpproxy/udpproxy.go @@ -14,7 +14,7 @@ type Option func(*Client) func WithSessionTimeout(timeout time.Duration) Option { return func(c *Client) { if timeout < time.Minute { - log.Warnf("cannot set stale session timeout to less than 1 minute.. defaulting to 5 minutes") + log.Warnf("cannot set stale session timeout to less than 1 minute.. defaulting to 20 minutes") return } @@ -32,8 +32,8 @@ type Client struct { sessionTimeout time.Duration } -func New(port, target string, options ...Option) (*Client, error) { - laddr, err := net.ResolveUDPAddr("udp", port) +func New(proxy, target string, options ...Option) (*Client, error) { + laddr, err := net.ResolveUDPAddr("udp", proxy) if err != nil { return nil, err } @@ -47,7 +47,7 @@ func New(port, target string, options ...Option) (*Client, error) { laddr: laddr, raddr: raddr, sessionCache: newSessionCache(), - sessionTimeout: 5 * time.Minute, + sessionTimeout: 20 * time.Minute, } for _, o := range options {