Compare commits

...

7 Commits
v1.7.1 ... dev

Author SHA1 Message Date
github-actions[bot]
7897348669 chore: auto-update Go modules 2025-05-12 00:10:39 +00:00
6489d1e556 return value straight from errChan 2025-05-12 00:01:00 +01:00
d0e3f5863a rename initProxy to launchProxy
remove double log on error

add some explanatory comments.
2025-05-07 16:16:35 +01:00
a00796254d move the functional option into option.go 2025-05-07 16:15:32 +01:00
80ef18f705 upd .dockerignore 2025-05-07 15:56:44 +01:00
40d79063f5 add validation logic to ports-mapping flag 2025-05-07 15:41:34 +01:00
02e73a21c0 move udpproxy files into repo root 2025-05-07 11:38:24 +01:00
9 changed files with 65 additions and 39 deletions

View File

@ -1,6 +1,6 @@
* *
!cmd/ !cmd/
!pkg/ !*.go
!go.mod !go.mod
!go.sum !go.sum

View File

@ -4,14 +4,16 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy" udpproxy "github.com/onyx-and-iris/q3rcon-proxy"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
) )
// proxyConfig holds the configuration for a single UDP proxy server.
type proxyConfig struct { type proxyConfig struct {
proxyHost string proxyHost string
targetHost string targetHost string
@ -32,7 +34,7 @@ func main() {
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "target-host", Name: "target-host",
Value: "127.0.0.1", Value: "localhost",
Usage: "Target host address", Usage: "Target host address",
Sources: cli.EnvVars("Q3RCON_TARGET_HOST"), Sources: cli.EnvVars("Q3RCON_TARGET_HOST"),
}, },
@ -41,6 +43,27 @@ func main() {
Usage: "Proxy and target ports (proxy:target)", Usage: "Proxy and target ports (proxy:target)",
Sources: cli.EnvVars("Q3RCON_PORTS_MAPPING"), Sources: cli.EnvVars("Q3RCON_PORTS_MAPPING"),
Required: true, 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{ &cli.IntFlag{
Name: "session-timeout", Name: "session-timeout",
@ -74,32 +97,28 @@ func main() {
sessionTimeout: cmd.Int("session-timeout"), 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.
for err := range errChan { // If we receive an error we will log it and exit
if err != nil { return <-errChan
log.Errorf("Error: %v", err)
return err
}
}
return nil
}, },
} }
if err := cmd.Run(context.Background(), os.Args); err != nil { log.Fatal(cmd.Run(context.Background(), os.Args))
log.Fatal(err)
}
} }
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] proxyPort, targetPort := cfg.portsMapping[0], cfg.portsMapping[1]
hostAddr := fmt.Sprintf("%s:%s", cfg.proxyHost, proxyPort) hostAddr := fmt.Sprintf("%s:%s", cfg.proxyHost, proxyPort)
proxyAddr := fmt.Sprintf("%s:%s", cfg.targetHost, targetPort) proxyAddr := fmt.Sprintf("%s:%s", cfg.targetHost, targetPort)
c, err := udpproxy.New( server, err := udpproxy.New(
hostAddr, proxyAddr, hostAddr, proxyAddr,
udpproxy.WithSessionTimeout(time.Duration(cfg.sessionTimeout)*time.Minute)) udpproxy.WithSessionTimeout(time.Duration(cfg.sessionTimeout)*time.Minute))
if err != nil { if err != nil {
@ -109,5 +128,5 @@ func initProxy(cfg proxyConfig, errChan chan error) {
log.Printf("q3rcon-proxy initialized: [proxy] (%s) [target] (%s)", hostAddr, proxyAddr) log.Printf("q3rcon-proxy initialized: [proxy] (%s) [target] (%s)", hostAddr, proxyAddr)
errChan <- c.ListenAndServe() errChan <- server.ListenAndServe()
} }

4
go.mod
View File

@ -6,7 +6,7 @@ toolchain go1.24.1
require ( require (
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
github.com/urfave/cli/v3 v3.3.2 github.com/urfave/cli/v3 v3.3.3
) )
require golang.org/x/sys v0.32.0 // indirect require golang.org/x/sys v0.33.0 // indirect

8
go.sum
View File

@ -9,11 +9,11 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli/v3 v3.3.2 h1:BYFVnhhZ8RqT38DxEYVFPPmGFTEf7tJwySTXsVRrS/o= github.com/urfave/cli/v3 v3.3.3 h1:byCBaVdIXuLPIDm5CYZRVG6NvT7tv1ECqdU4YzlEa3I=
github.com/urfave/cli/v3 v3.3.2/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo= github.com/urfave/cli/v3 v3.3.3/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20= golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

22
option.go Normal file
View 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
}
}

View File

@ -7,21 +7,6 @@ import (
log "github.com/sirupsen/logrus" 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 { type Client struct {
laddr *net.UDPAddr laddr *net.UDPAddr
raddr *net.UDPAddr raddr *net.UDPAddr