mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2026-04-07 15:53:29 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d0e3f5863a | |||
| a00796254d | |||
| 80ef18f705 | |||
| 40d79063f5 | |||
| 02e73a21c0 | |||
| 5e399b8590 | |||
| 28baa2ed00 | |||
| 58c95879cc | |||
| af0a72eb67 |
@@ -1,6 +1,6 @@
|
||||
*
|
||||
!cmd/
|
||||
!pkg/
|
||||
!*.go
|
||||
|
||||
!go.mod
|
||||
!go.sum
|
||||
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@@ -9,9 +9,9 @@
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}/cmd/q3rcon-proxy/main.go",
|
||||
"program": "${workspaceFolder}/cmd/q3rcon-proxy/",
|
||||
"env": {
|
||||
"Q3RCON_PROXY": "28961:28960",
|
||||
"Q3RCON_PORTS_MAPPING": "28961:28960",
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@@ -4,14 +4,16 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
|
||||
udpproxy "github.com/onyx-and-iris/q3rcon-proxy"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// proxyConfig holds the configuration for a single UDP proxy server.
|
||||
type proxyConfig struct {
|
||||
proxyHost string
|
||||
targetHost string
|
||||
@@ -41,6 +43,27 @@ func main() {
|
||||
Usage: "Proxy and target ports (proxy:target)",
|
||||
Sources: cli.EnvVars("Q3RCON_PORTS_MAPPING"),
|
||||
Required: true,
|
||||
Action: func(ctx context.Context, cmd *cli.Command, v string) error {
|
||||
// Validate the ports mapping
|
||||
for mapping := range strings.SplitSeq(v, ";") {
|
||||
ports := strings.Split(mapping, ":")
|
||||
if len(ports) != 2 {
|
||||
return fmt.Errorf("invalid ports mapping: %s", mapping)
|
||||
}
|
||||
proxyPort, err := strconv.Atoi(ports[0])
|
||||
if err != nil || proxyPort < 1 || proxyPort > 65535 {
|
||||
return fmt.Errorf("invalid proxy port: %s", ports[0])
|
||||
}
|
||||
targetPort, err := strconv.Atoi(ports[1])
|
||||
if err != nil || targetPort < 1 || targetPort > 65535 {
|
||||
return fmt.Errorf("invalid target port: %s", ports[1])
|
||||
}
|
||||
if proxyPort == targetPort {
|
||||
return fmt.Errorf("proxy and target ports cannot be the same: %s", mapping)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
&cli.IntFlag{
|
||||
Name: "session-timeout",
|
||||
@@ -55,6 +78,14 @@ func main() {
|
||||
Sources: cli.EnvVars("Q3RCON_LOGLEVEL"),
|
||||
},
|
||||
},
|
||||
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
|
||||
logLevel, err := log.ParseLevel(cmd.String("loglevel"))
|
||||
if err != nil {
|
||||
return ctx, fmt.Errorf("invalid log level: %w", err)
|
||||
}
|
||||
log.SetLevel(logLevel)
|
||||
return ctx, nil
|
||||
},
|
||||
Action: func(_ context.Context, cmd *cli.Command) error {
|
||||
errChan := make(chan error)
|
||||
|
||||
@@ -66,13 +97,13 @@ func main() {
|
||||
sessionTimeout: cmd.Int("session-timeout"),
|
||||
}
|
||||
|
||||
go initProxy(cfg, errChan)
|
||||
go launchProxy(cfg, errChan)
|
||||
}
|
||||
|
||||
// We don't expect to receive any errors from the channels, but if we do, we log and return early.
|
||||
// Under normal circumstances, the main goroutine will block here
|
||||
// until the server is stopped or an error occurs.
|
||||
for err := range errChan {
|
||||
if err != nil {
|
||||
log.Errorf("Error: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -85,13 +116,16 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func initProxy(cfg proxyConfig, errChan chan error) {
|
||||
// launchProxy initializes the UDP proxy server with the given configuration.
|
||||
// It listens on the specified proxy host and port, and forwards traffic to the target host and port.
|
||||
// server.ListenAndServe blocks until the server is stopped or an error occurs.
|
||||
func launchProxy(cfg proxyConfig, errChan chan error) {
|
||||
proxyPort, targetPort := cfg.portsMapping[0], cfg.portsMapping[1]
|
||||
|
||||
hostAddr := fmt.Sprintf("%s:%s", cfg.proxyHost, proxyPort)
|
||||
proxyAddr := fmt.Sprintf("%s:%s", cfg.targetHost, targetPort)
|
||||
|
||||
c, err := udpproxy.New(
|
||||
server, err := udpproxy.New(
|
||||
hostAddr, proxyAddr,
|
||||
udpproxy.WithSessionTimeout(time.Duration(cfg.sessionTimeout)*time.Minute))
|
||||
if err != nil {
|
||||
@@ -101,5 +135,5 @@ func initProxy(cfg proxyConfig, errChan chan error) {
|
||||
|
||||
log.Printf("q3rcon-proxy initialized: [proxy] (%s) [target] (%s)", hostAddr, proxyAddr)
|
||||
|
||||
errChan <- c.ListenAndServe()
|
||||
errChan <- server.ListenAndServe()
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
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
|
||||
}
|
||||
4
debian/q3rcon-proxy.service
vendored
4
debian/q3rcon-proxy.service
vendored
@@ -6,9 +6,9 @@ After=network.target
|
||||
[Service]
|
||||
Type=simple
|
||||
User=gameservers
|
||||
Environment="Q3RCON_PROXY=20000:28960;20001:28961;20002:28962"
|
||||
Environment="Q3RCON_PORTS_MAPPING=20000:28960;20001:28961;20002:28962"
|
||||
Environment="Q3RCON_HOST=0.0.0.0"
|
||||
Environment="Q3RCON_DEBUG=0"
|
||||
Environment="Q3RCON_LOGLEVEL=info"
|
||||
|
||||
ExecStart=/usr/local/bin/q3rcon-proxy
|
||||
Restart=always
|
||||
|
||||
22
option.go
Normal file
22
option.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package udpproxy
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Option is a functional option type that allows us to configure the Client.
|
||||
type Option func(*Client)
|
||||
|
||||
// WithSessionTimeout is a functional option to set the session timeout
|
||||
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 20 minutes")
|
||||
return
|
||||
}
|
||||
|
||||
c.sessionTimeout = timeout
|
||||
}
|
||||
}
|
||||
@@ -7,21 +7,6 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Option is a functional option type that allows us to configure the Client.
|
||||
type Option func(*Client)
|
||||
|
||||
// WithSessionTimeout is a functional option to set the session timeout
|
||||
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 20 minutes")
|
||||
return
|
||||
}
|
||||
|
||||
c.sessionTimeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
laddr *net.UDPAddr
|
||||
raddr *net.UDPAddr
|
||||
Reference in New Issue
Block a user