target host is now configurable.

stale session timeout default value increased
This commit is contained in:
onyx-and-iris 2024-11-29 04:33:47 +00:00
parent b0a6ba8180
commit c2266ac9d9
3 changed files with 31 additions and 20 deletions

1
.gitignore vendored
View File

@ -7,6 +7,7 @@
*.dll *.dll
*.so *.so
*.dylib *.dylib
bin/
# Test binary, built with `go test -c` # Test binary, built with `go test -c`
*.test *.test

View File

@ -21,43 +21,53 @@ func main() {
log.SetLevel(log.Level(logLevel)) log.SetLevel(log.Level(logLevel))
} }
proxies := os.Getenv("Q3RCON_PROXY") proxyHost := os.Getenv("Q3RCON_PROXY_HOST")
if proxies == "" { if proxyHost == "" {
log.Fatal("env Q3RCON_PROXY required") proxyHost = "0.0.0.0"
} }
host := os.Getenv("Q3RCON_HOST") targetHost := os.Getenv("Q3RCON_TARGET_HOST")
if host == "" { if targetHost == "" {
host = "0.0.0.0" 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") sessionTimeout, err := getEnvInt("Q3RCON_SESSION_TIMEOUT")
if err != nil { if err != nil {
log.Fatalf("unable to parse Q3RCON_SESSION_TIMEOUT: %s", err.Error()) log.Fatalf("unable to parse Q3RCON_SESSION_TIMEOUT: %s", err.Error())
} }
if sessionTimeout == 0 {
for _, proxy := range strings.Split(proxies, ";") { sessionTimeout = 20
go start(host, proxy, sessionTimeout)
} }
<-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) { func start(proxyHost, targetHost, ports string, sessionTimeout int) {
port, target := func() (string, string) { proxyPort, targetPort := func() (string, string) {
x := strings.Split(proxy, ":") x := strings.Split(ports, ":")
return x[0], x[1] return x[0], x[1]
}() }()
hostAddr := fmt.Sprintf("%s:%s", proxyHost, proxyPort)
proxyAddr := fmt.Sprintf("%s:%s", targetHost, targetPort)
c, err := udpproxy.New( c, err := udpproxy.New(
fmt.Sprintf("%s:%s", host, port), hostAddr, proxyAddr,
fmt.Sprintf("127.0.0.1:%s", target),
udpproxy.WithSessionTimeout(time.Duration(sessionTimeout)*time.Minute)) udpproxy.WithSessionTimeout(time.Duration(sessionTimeout)*time.Minute))
if err != nil { if err != nil {
log.Fatal(err) 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()) log.Fatal(c.ListenAndServe())
} }

View File

@ -14,7 +14,7 @@ type Option func(*Client)
func WithSessionTimeout(timeout time.Duration) Option { func WithSessionTimeout(timeout time.Duration) Option {
return func(c *Client) { return func(c *Client) {
if timeout < time.Minute { 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 return
} }
@ -32,8 +32,8 @@ type Client struct {
sessionTimeout time.Duration sessionTimeout time.Duration
} }
func New(port, target string, options ...Option) (*Client, error) { func New(proxy, target string, options ...Option) (*Client, error) {
laddr, err := net.ResolveUDPAddr("udp", port) laddr, err := net.ResolveUDPAddr("udp", proxy)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -47,7 +47,7 @@ func New(port, target string, options ...Option) (*Client, error) {
laddr: laddr, laddr: laddr,
raddr: raddr, raddr: raddr,
sessionCache: newSessionCache(), sessionCache: newSessionCache(),
sessionTimeout: 5 * time.Minute, sessionTimeout: 20 * time.Minute,
} }
for _, o := range options { for _, o := range options {