mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2025-08-08 03:51:55 +00:00
Compare commits
No commits in common. "d0e3f5863afa27d7572f52e96f98c5cc1d047b14" and "5e399b85901fba725cd6cf7f643b51291202b15d" have entirely different histories.
d0e3f5863a
...
5e399b8590
@ -1,6 +1,6 @@
|
|||||||
*
|
*
|
||||||
!cmd/
|
!cmd/
|
||||||
!*.go
|
!pkg/
|
||||||
|
|
||||||
!go.mod
|
!go.mod
|
||||||
!go.sum
|
!go.sum
|
@ -4,16 +4,14 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
udpproxy "github.com/onyx-and-iris/q3rcon-proxy"
|
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
|
||||||
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
|
||||||
@ -43,27 +41,6 @@ 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",
|
||||||
@ -97,13 +74,13 @@ func main() {
|
|||||||
sessionTimeout: cmd.Int("session-timeout"),
|
sessionTimeout: cmd.Int("session-timeout"),
|
||||||
}
|
}
|
||||||
|
|
||||||
go launchProxy(cfg, errChan)
|
go initProxy(cfg, errChan)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Under normal circumstances, the main goroutine will block here
|
// We don't expect to receive any errors from the channels, but if we do, we log and return early.
|
||||||
// until the server is stopped or an error occurs.
|
|
||||||
for err := range errChan {
|
for err := range errChan {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Errorf("Error: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,16 +93,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// launchProxy initializes the UDP proxy server with the given configuration.
|
func initProxy(cfg proxyConfig, errChan chan error) {
|
||||||
// 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)
|
||||||
|
|
||||||
server, err := udpproxy.New(
|
c, 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 {
|
||||||
@ -135,5 +109,5 @@ func launchProxy(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 <- server.ListenAndServe()
|
errChan <- c.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
22
option.go
22
option.go
@ -1,22 +0,0 @@
|
|||||||
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,6 +7,21 @@ 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
|
Loading…
x
Reference in New Issue
Block a user