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
*.so
*.dylib
bin/
# Test binary, built with `go test -c`
*.test

View File

@ -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())
}

View File

@ -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 {