mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2025-04-11 08:03:53 +01:00
Compare commits
2 Commits
51f22f480b
...
c42df03858
Author | SHA1 | Date | |
---|---|---|---|
c42df03858 | |||
64c1df645d |
@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
@ -30,20 +31,28 @@ func main() {
|
||||
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, ";") {
|
||||
go start(host, proxy)
|
||||
go start(host, proxy, staleTimeout)
|
||||
}
|
||||
|
||||
<-make(chan int)
|
||||
}
|
||||
|
||||
func start(host, proxy string) {
|
||||
func start(host, proxy string, staleTimeout int) {
|
||||
port, target := func() (string, string) {
|
||||
x := strings.Split(proxy, ":")
|
||||
return x[0], x[1]
|
||||
}()
|
||||
|
||||
c, err := udpproxy.New(fmt.Sprintf("%s:%s", host, port), fmt.Sprintf("127.0.0.1:%s", target))
|
||||
c, err := udpproxy.New(
|
||||
fmt.Sprintf("%s:%s", host, port),
|
||||
fmt.Sprintf("127.0.0.1:%s", target),
|
||||
udpproxy.WithStaleTimeout(time.Duration(staleTimeout)*time.Minute))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -8,6 +8,21 @@ import (
|
||||
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 {
|
||||
laddr *net.UDPAddr
|
||||
raddr *net.UDPAddr
|
||||
@ -16,9 +31,11 @@ type Client struct {
|
||||
|
||||
mutex sync.RWMutex
|
||||
sessions map[string]*session
|
||||
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func New(port, target string) (*Client, error) {
|
||||
func New(port, target string, options ...Option) (*Client, error) {
|
||||
laddr, err := net.ResolveUDPAddr("udp", port)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -29,12 +46,19 @@ func New(port, target string) (*Client, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{
|
||||
c := &Client{
|
||||
laddr: laddr,
|
||||
raddr: raddr,
|
||||
mutex: sync.RWMutex{},
|
||||
sessions: map[string]*session{},
|
||||
}, nil
|
||||
timeout: 5 * time.Minute,
|
||||
}
|
||||
|
||||
for _, o := range options {
|
||||
o(c)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListenAndServe() error {
|
||||
@ -76,8 +100,9 @@ func (c *Client) pruneSessions() {
|
||||
for range ticker.C {
|
||||
for _, session := range c.sessions {
|
||||
c.mutex.RLock()
|
||||
if time.Since(session.updateTime) > time.Minute*5 {
|
||||
if time.Since(session.updateTime) > c.timeout {
|
||||
delete(c.sessions, session.caddr.String())
|
||||
log.Tracef("session for %s deleted", session.caddr)
|
||||
}
|
||||
c.mutex.RUnlock()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user