target host is now configurable.

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

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