mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2026-04-09 16:43:30 +00:00
Compare commits
9 Commits
v1.1.0
...
b0a6ba8180
| Author | SHA1 | Date | |
|---|---|---|---|
| b0a6ba8180 | |||
| 9b4a05c0f4 | |||
| bfe31c28c8 | |||
| abc1ea9d3f | |||
| 82ca15f70e | |||
| 8cb5bc03c5 | |||
| 939d419438 | |||
| c42df03858 | |||
| 64c1df645d |
6
.dockerignore
Normal file
6
.dockerignore
Normal file
@@ -0,0 +1,6 @@
|
||||
*
|
||||
!cmd/
|
||||
!pkg/
|
||||
|
||||
!go.mod
|
||||
!go.sum
|
||||
15
Dockerfile
15
Dockerfile
@@ -1,4 +1,4 @@
|
||||
FROM golang:1.21
|
||||
FROM golang:1.21 AS build_image
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
@@ -6,9 +6,16 @@ WORKDIR /usr/src/app
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download && go mod verify
|
||||
|
||||
# build binary and place into /usr/local/bin/
|
||||
COPY . .
|
||||
RUN go build -v -o /usr/local/bin/q3rcon-proxy ./cmd/q3rcon-proxy/
|
||||
|
||||
# build binary, place into ./bin/
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -o ./bin/q3rcon-proxy ./cmd/q3rcon-proxy/
|
||||
|
||||
FROM scratch AS final_image
|
||||
|
||||
WORKDIR /bin/
|
||||
|
||||
COPY --from=build_image /usr/src/app/bin/q3rcon-proxy .
|
||||
|
||||
# Command to run when starting the container
|
||||
ENTRYPOINT [ "q3rcon-proxy" ]
|
||||
ENTRYPOINT [ "./q3rcon-proxy" ]
|
||||
10
README.md
10
README.md
@@ -8,12 +8,12 @@ Unfortunately the Q3Rcon engine ties the rcon port to the game servers public po
|
||||
|
||||
### Use
|
||||
|
||||
Run one or multiple rcon proxies by setting an environment variable `Q3RCON_PROXY`
|
||||
Run one or multiple rcon proxies by setting an environment variable `Q3RCON_TARGET_PORTS`
|
||||
|
||||
for example:
|
||||
|
||||
```bash
|
||||
export Q3RCON_PROXY="20000:28960;20001:28961;20002:28962"
|
||||
export Q3RCON_TARGET_PORTS="20000:28960;20001:28961;20002:28962"
|
||||
```
|
||||
|
||||
This would configure q3rcon-proxy to run 3 proxy servers listening on ports `20000`, `20001` and `20002` that redirect rcon requests to game servers on ports `28960`, `28961` and `28962` respectively.
|
||||
@@ -32,3 +32,9 @@ Set the log level with environment variable `Q3RCON_LOGLEVEL`:
|
||||
|
||||
[lilproxy_url]: https://github.com/dgparker/lilproxy
|
||||
[user_link]: https://github.com/dgparker
|
||||
|
||||
### Further Notes
|
||||
|
||||
For a compatible rcon client also written in Go consider checking out the [Q3 Rcon][q3rcon] package.
|
||||
|
||||
[q3rcon]: https://github.com/onyx-and-iris/q3rcon
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
sessionTimeout, err := getEnvInt("Q3RCON_SESSION_TIMEOUT")
|
||||
if err != nil {
|
||||
log.Fatalf("unable to parse Q3RCON_SESSION_TIMEOUT: %s", err.Error())
|
||||
}
|
||||
|
||||
for _, proxy := range strings.Split(proxies, ";") {
|
||||
go start(host, proxy)
|
||||
go start(host, proxy, sessionTimeout)
|
||||
}
|
||||
|
||||
<-make(chan int)
|
||||
}
|
||||
|
||||
func start(host, proxy string) {
|
||||
func start(host, proxy string, sessionTimeout 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.WithSessionTimeout(time.Duration(sessionTimeout)*time.Minute))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
41
pkg/udpproxy/sessioncache.go
Normal file
41
pkg/udpproxy/sessioncache.go
Normal file
@@ -0,0 +1,41 @@
|
||||
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,23 +2,37 @@ package udpproxy
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
"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 5 minutes")
|
||||
return
|
||||
}
|
||||
|
||||
c.sessionTimeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
laddr *net.UDPAddr
|
||||
raddr *net.UDPAddr
|
||||
|
||||
proxyConn *net.UDPConn
|
||||
|
||||
mutex sync.RWMutex
|
||||
sessions map[string]*session
|
||||
sessionCache sessionCache
|
||||
sessionTimeout 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 +43,18 @@ func New(port, target string) (*Client, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{
|
||||
laddr: laddr,
|
||||
raddr: raddr,
|
||||
mutex: sync.RWMutex{},
|
||||
sessions: map[string]*session{},
|
||||
}, nil
|
||||
c := &Client{
|
||||
laddr: laddr,
|
||||
raddr: raddr,
|
||||
sessionCache: newSessionCache(),
|
||||
sessionTimeout: 5 * time.Minute,
|
||||
}
|
||||
|
||||
for _, o := range options {
|
||||
o(c)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListenAndServe() error {
|
||||
@@ -53,7 +73,7 @@ func (c *Client) ListenAndServe() error {
|
||||
log.Error(err)
|
||||
}
|
||||
|
||||
session, ok := c.sessions[caddr.String()]
|
||||
session, ok := c.sessionCache.read(caddr.String())
|
||||
if !ok {
|
||||
session, err = newSession(caddr, c.raddr, c.proxyConn)
|
||||
if err != nil {
|
||||
@@ -61,7 +81,7 @@ func (c *Client) ListenAndServe() error {
|
||||
continue
|
||||
}
|
||||
|
||||
c.sessions[caddr.String()] = session
|
||||
c.sessionCache.insert(caddr.String(), session)
|
||||
}
|
||||
|
||||
go session.proxyTo(buf[:n])
|
||||
@@ -71,15 +91,12 @@ func (c *Client) ListenAndServe() error {
|
||||
func (c *Client) pruneSessions() {
|
||||
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 _, session := range c.sessions {
|
||||
c.mutex.RLock()
|
||||
if time.Since(session.updateTime) > time.Minute*5 {
|
||||
delete(c.sessions, session.caddr.String())
|
||||
for _, session := range c.sessionCache.data {
|
||||
if time.Since(session.updateTime) > c.sessionTimeout {
|
||||
c.sessionCache.delete(session.caddr.String())
|
||||
log.Tracef("session for %s deleted", session.caddr)
|
||||
}
|
||||
c.mutex.RUnlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user