Compare commits

..

No commits in common. "c42df03858790ef92057e9949c15a340273d5bb5" and "51f22f480b709a0018be838b233601d3c258cf8d" have entirely different histories.

2 changed files with 7 additions and 41 deletions

View File

@ -5,7 +5,6 @@ import (
"os" "os"
"slices" "slices"
"strings" "strings"
"time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -31,28 +30,20 @@ func main() {
host = "0.0.0.0" host = "0.0.0.0"
} }
staleTimeout, err := getEnvInt("Q3RCON_STALE_SESSION_TIMEOUT")
if err != nil {
log.Fatalf("unable to parse Q3RCON_STALE_SESSION_TIMEOUT: %s", err.Error())
}
for _, proxy := range strings.Split(proxies, ";") { for _, proxy := range strings.Split(proxies, ";") {
go start(host, proxy, staleTimeout) go start(host, proxy)
} }
<-make(chan int) <-make(chan int)
} }
func start(host, proxy string, staleTimeout int) { func start(host, proxy string) {
port, target := func() (string, string) { port, target := func() (string, string) {
x := strings.Split(proxy, ":") x := strings.Split(proxy, ":")
return x[0], x[1] return x[0], x[1]
}() }()
c, err := udpproxy.New( c, err := udpproxy.New(fmt.Sprintf("%s:%s", host, port), fmt.Sprintf("127.0.0.1:%s", target))
fmt.Sprintf("%s:%s", host, port),
fmt.Sprintf("127.0.0.1:%s", target),
udpproxy.WithStaleTimeout(time.Duration(staleTimeout)*time.Minute))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -8,21 +8,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)
// WithStaleTimeout is a functional option to set the stale session timeout
func WithStaleTimeout(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")
return
}
c.timeout = timeout
}
}
type Client struct { type Client struct {
laddr *net.UDPAddr laddr *net.UDPAddr
raddr *net.UDPAddr raddr *net.UDPAddr
@ -31,11 +16,9 @@ type Client struct {
mutex sync.RWMutex mutex sync.RWMutex
sessions map[string]*session sessions map[string]*session
timeout time.Duration
} }
func New(port, target string, options ...Option) (*Client, error) { func New(port, target string) (*Client, error) {
laddr, err := net.ResolveUDPAddr("udp", port) laddr, err := net.ResolveUDPAddr("udp", port)
if err != nil { if err != nil {
return nil, err return nil, err
@ -46,19 +29,12 @@ func New(port, target string, options ...Option) (*Client, error) {
return nil, err return nil, err
} }
c := &Client{ return &Client{
laddr: laddr, laddr: laddr,
raddr: raddr, raddr: raddr,
mutex: sync.RWMutex{}, mutex: sync.RWMutex{},
sessions: map[string]*session{}, sessions: map[string]*session{},
timeout: 5 * time.Minute, }, nil
}
for _, o := range options {
o(c)
}
return c, nil
} }
func (c *Client) ListenAndServe() error { func (c *Client) ListenAndServe() error {
@ -100,9 +76,8 @@ func (c *Client) pruneSessions() {
for range ticker.C { for range ticker.C {
for _, session := range c.sessions { for _, session := range c.sessions {
c.mutex.RLock() c.mutex.RLock()
if time.Since(session.updateTime) > c.timeout { if time.Since(session.updateTime) > time.Minute*5 {
delete(c.sessions, session.caddr.String()) delete(c.sessions, session.caddr.String())
log.Tracef("session for %s deleted", session.caddr)
} }
c.mutex.RUnlock() c.mutex.RUnlock()
} }