mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2025-04-19 20:13:49 +01:00
Compare commits
No commits in common. "bfe31c28c8c2129ff05717fbf6a17e9c15d126bd" and "8cb5bc03c50b4051235e2fc4341d56070b0ce5cb" have entirely different histories.
bfe31c28c8
...
8cb5bc03c5
@ -31,19 +31,19 @@ func main() {
|
|||||||
host = "0.0.0.0"
|
host = "0.0.0.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionTimeout, err := getEnvInt("Q3RCON_SESSION_TIMEOUT")
|
staleTimeout, err := getEnvInt("Q3RCON_STALE_SESSION_TIMEOUT")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to parse Q3RCON_SESSION_TIMEOUT: %s", err.Error())
|
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, sessionTimeout)
|
go start(host, proxy, staleTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
<-make(chan int)
|
<-make(chan int)
|
||||||
}
|
}
|
||||||
|
|
||||||
func start(host, proxy string, sessionTimeout int) {
|
func start(host, proxy string, staleTimeout int) {
|
||||||
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]
|
||||||
@ -52,7 +52,7 @@ func start(host, proxy string, sessionTimeout int) {
|
|||||||
c, err := udpproxy.New(
|
c, err := udpproxy.New(
|
||||||
fmt.Sprintf("%s:%s", host, port),
|
fmt.Sprintf("%s:%s", host, port),
|
||||||
fmt.Sprintf("127.0.0.1:%s", target),
|
fmt.Sprintf("127.0.0.1:%s", target),
|
||||||
udpproxy.WithSessionTimeout(time.Duration(sessionTimeout)*time.Minute))
|
udpproxy.WithStaleTimeout(time.Duration(staleTimeout)*time.Minute))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
package udpproxy
|
|
||||||
|
|
||||||
import "sync"
|
|
||||||
|
|
||||||
// sessionCache tracks connection sessions
|
|
||||||
type sessionCache struct {
|
|
||||||
mu sync.RWMutex
|
|
||||||
data map[string]*session
|
|
||||||
}
|
|
||||||
|
|
||||||
// newSessionCache creates a usable sessionCache.
|
|
||||||
func newSessionCache() sessionCache {
|
|
||||||
return sessionCache{
|
|
||||||
data: make(map[string]*session),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// read returns the associated session for an addr
|
|
||||||
func (sc *sessionCache) read(addr string) (*session, bool) {
|
|
||||||
sc.mu.RLock()
|
|
||||||
defer sc.mu.RUnlock()
|
|
||||||
|
|
||||||
v, ok := sc.data[addr]
|
|
||||||
return v, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// insert adds a session for a given addr.
|
|
||||||
func (sc *sessionCache) insert(addr string, session *session) {
|
|
||||||
sc.mu.Lock()
|
|
||||||
defer sc.mu.Unlock()
|
|
||||||
|
|
||||||
sc.data[addr] = session
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete removes the session for the given addr.
|
|
||||||
func (sc *sessionCache) delete(addr string) {
|
|
||||||
sc.mu.Lock()
|
|
||||||
defer sc.mu.Unlock()
|
|
||||||
|
|
||||||
delete(sc.data, addr)
|
|
||||||
}
|
|
@ -2,6 +2,7 @@ package udpproxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@ -10,15 +11,15 @@ import (
|
|||||||
// Option is a functional option type that allows us to configure the Client.
|
// Option is a functional option type that allows us to configure the Client.
|
||||||
type Option func(*Client)
|
type Option func(*Client)
|
||||||
|
|
||||||
// WithSessionTimeout is a functional option to set the session timeout
|
// WithStaleTimeout is a functional option to set the stale session timeout
|
||||||
func WithSessionTimeout(timeout time.Duration) Option {
|
func WithStaleTimeout(timeout time.Duration) Option {
|
||||||
return func(c *Client) {
|
return func(c *Client) {
|
||||||
if timeout < time.Minute {
|
if timeout < time.Minute {
|
||||||
log.Warnf("cannot set stale session timeout to less than 1 minute.. defaulting to 5 minutes")
|
log.Warnf("cannot set stale session timeout to less than 1 minute.. defaulting to 5 minutes")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.sessionTimeout = timeout
|
c.timeout = timeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,8 +29,10 @@ type Client struct {
|
|||||||
|
|
||||||
proxyConn *net.UDPConn
|
proxyConn *net.UDPConn
|
||||||
|
|
||||||
sessionCache sessionCache
|
mutex sync.RWMutex
|
||||||
sessionTimeout time.Duration
|
sessions map[string]*session
|
||||||
|
|
||||||
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(port, target string, options ...Option) (*Client, error) {
|
func New(port, target string, options ...Option) (*Client, error) {
|
||||||
@ -44,10 +47,11 @@ func New(port, target string, options ...Option) (*Client, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c := &Client{
|
c := &Client{
|
||||||
laddr: laddr,
|
laddr: laddr,
|
||||||
raddr: raddr,
|
raddr: raddr,
|
||||||
sessionCache: newSessionCache(),
|
mutex: sync.RWMutex{},
|
||||||
sessionTimeout: 5 * time.Minute,
|
sessions: map[string]*session{},
|
||||||
|
timeout: 5 * time.Minute,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range options {
|
for _, o := range options {
|
||||||
@ -73,7 +77,7 @@ func (c *Client) ListenAndServe() error {
|
|||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, ok := c.sessionCache.read(caddr.String())
|
session, ok := c.sessions[caddr.String()]
|
||||||
if !ok {
|
if !ok {
|
||||||
session, err = newSession(caddr, c.raddr, c.proxyConn)
|
session, err = newSession(caddr, c.raddr, c.proxyConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -81,7 +85,7 @@ func (c *Client) ListenAndServe() error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
c.sessionCache.insert(caddr.String(), session)
|
c.sessions[caddr.String()] = session
|
||||||
}
|
}
|
||||||
|
|
||||||
go session.proxyTo(buf[:n])
|
go session.proxyTo(buf[:n])
|
||||||
@ -91,12 +95,16 @@ func (c *Client) ListenAndServe() error {
|
|||||||
func (c *Client) pruneSessions() {
|
func (c *Client) pruneSessions() {
|
||||||
ticker := time.NewTicker(1 * time.Minute)
|
ticker := time.NewTicker(1 * time.Minute)
|
||||||
|
|
||||||
|
// the locks here could be abusive and i dont even know if this is a real
|
||||||
|
// problem but we definitely need to clean up stale sessions
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
for _, session := range c.sessionCache.data {
|
for _, session := range c.sessions {
|
||||||
if time.Since(session.updateTime) > c.sessionTimeout {
|
c.mutex.RLock()
|
||||||
c.sessionCache.delete(session.caddr.String())
|
if time.Since(session.updateTime) > c.timeout {
|
||||||
|
delete(c.sessions, session.caddr.String())
|
||||||
log.Tracef("session for %s deleted", session.caddr)
|
log.Tracef("session for %s deleted", session.caddr)
|
||||||
}
|
}
|
||||||
|
c.mutex.RUnlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user