mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2026-04-07 15:53:29 +00:00
Compare commits
10 Commits
v1.2.0
...
add-target
| Author | SHA1 | Date | |
|---|---|---|---|
| a1ecf85cbb | |||
| 6c83d6ad2c | |||
| c2266ac9d9 | |||
| b0a6ba8180 | |||
| 9b4a05c0f4 | |||
| bfe31c28c8 | |||
| abc1ea9d3f | |||
| 82ca15f70e | |||
| 8cb5bc03c5 | |||
| 939d419438 |
6
.dockerignore
Normal file
6
.dockerignore
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
*
|
||||||
|
!cmd/
|
||||||
|
!pkg/
|
||||||
|
|
||||||
|
!go.mod
|
||||||
|
!go.sum
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,6 +7,7 @@
|
|||||||
*.dll
|
*.dll
|
||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
|
bin/
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
# Test binary, built with `go test -c`
|
||||||
*.test
|
*.test
|
||||||
|
|||||||
32
CHANGELOG.md
32
CHANGELOG.md
@@ -11,6 +11,38 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
|
|||||||
|
|
||||||
- [x]
|
- [x]
|
||||||
|
|
||||||
|
## [1.4.0] - 2024-11-29
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- new environment variable `Q3RCON_TARGET_HOST` for setting the host the gameserver is on.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- environment variable `Q3RCON_HOST` renamed to `Q3RCON_PROXY_HOST`
|
||||||
|
- environment variable `Q3RCON_PROXY` renamed to `Q3RCON_TARGET_PORTS`.
|
||||||
|
- default session timeout changed from 5 to 20 minutes.
|
||||||
|
|
||||||
|
## [1.3.0] - 2024-10-23
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Add sessionCache for tracking sessions.
|
||||||
|
- Functional option `WithStaleTimeout` renamed to `WithSessionTimeout`
|
||||||
|
|
||||||
|
## [1.2.0] - 2024-10-19
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- optional function `WithStaleTimeout`, use it to configure the session timeout value.
|
||||||
|
- it defaults to 5 minutes.
|
||||||
|
|
||||||
|
## [1.1.0] - 2024-09-28
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- connection (challenge) requests are now logged.
|
||||||
|
|
||||||
## [0.6.0] - 2024-03-21
|
## [0.6.0] - 2024-03-21
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
15
Dockerfile
15
Dockerfile
@@ -1,4 +1,4 @@
|
|||||||
FROM golang:1.21
|
FROM golang:1.21 AS build_image
|
||||||
|
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
|
|
||||||
@@ -6,9 +6,16 @@ WORKDIR /usr/src/app
|
|||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
RUN go mod download && go mod verify
|
RUN go mod download && go mod verify
|
||||||
|
|
||||||
# build binary and place into /usr/local/bin/
|
|
||||||
COPY . .
|
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
|
# Command to run when starting the container
|
||||||
ENTRYPOINT [ "q3rcon-proxy" ]
|
ENTRYPOINT [ "./q3rcon-proxy" ]
|
||||||
42
Makefile
42
Makefile
@@ -1,2 +1,40 @@
|
|||||||
go-build:
|
program = q3rcon-proxy
|
||||||
go build ./cmd/q3rcon-proxy/
|
|
||||||
|
GO = @go
|
||||||
|
BIN_DIR := bin
|
||||||
|
|
||||||
|
WINDOWS=$(BIN_DIR)/$(program)_windows_amd64.exe
|
||||||
|
LINUX=$(BIN_DIR)/$(program)_linux_amd64
|
||||||
|
VERSION=$(shell git describe --tags --always --long --dirty)
|
||||||
|
|
||||||
|
.DEFAULT_GOAL := build
|
||||||
|
|
||||||
|
.PHONY: fmt vet build windows linux test clean
|
||||||
|
fmt:
|
||||||
|
$(GO) fmt ./...
|
||||||
|
|
||||||
|
vet: fmt
|
||||||
|
$(GO) vet ./...
|
||||||
|
|
||||||
|
build: vet windows linux | $(BIN_DIR)
|
||||||
|
@echo version: $(VERSION)
|
||||||
|
|
||||||
|
windows: $(WINDOWS)
|
||||||
|
|
||||||
|
linux: $(LINUX)
|
||||||
|
|
||||||
|
|
||||||
|
$(WINDOWS):
|
||||||
|
env GOOS=windows GOARCH=amd64 go build -v -o $(WINDOWS) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/q3rcon-proxy/
|
||||||
|
|
||||||
|
$(LINUX):
|
||||||
|
env GOOS=linux GOARCH=amd64 go build -v -o $(LINUX) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/q3rcon-proxy/
|
||||||
|
|
||||||
|
test:
|
||||||
|
$(GO) test ./...
|
||||||
|
|
||||||
|
$(BIN_DIR):
|
||||||
|
@mkdir -p $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -rv $(BIN_DIR)
|
||||||
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
|
### 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:
|
for example:
|
||||||
|
|
||||||
```bash
|
```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.
|
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
|
[lilproxy_url]: https://github.com/dgparker/lilproxy
|
||||||
[user_link]: https://github.com/dgparker
|
[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
|
||||||
@@ -21,43 +21,53 @@ func main() {
|
|||||||
log.SetLevel(log.Level(logLevel))
|
log.SetLevel(log.Level(logLevel))
|
||||||
}
|
}
|
||||||
|
|
||||||
proxies := os.Getenv("Q3RCON_PROXY")
|
proxyHost := os.Getenv("Q3RCON_PROXY_HOST")
|
||||||
|
if proxyHost == "" {
|
||||||
|
proxyHost = "0.0.0.0"
|
||||||
|
}
|
||||||
|
|
||||||
|
targetHost := os.Getenv("Q3RCON_TARGET_HOST")
|
||||||
|
if targetHost == "" {
|
||||||
|
targetHost = "127.0.0.1"
|
||||||
|
}
|
||||||
|
|
||||||
|
proxies := os.Getenv("Q3RCON_TARGET_PORTS")
|
||||||
if proxies == "" {
|
if proxies == "" {
|
||||||
log.Fatal("env Q3RCON_PROXY required")
|
log.Fatal("env Q3RCON_TARGET_PORTS required")
|
||||||
}
|
}
|
||||||
|
|
||||||
host := os.Getenv("Q3RCON_HOST")
|
sessionTimeout, err := getEnvInt("Q3RCON_SESSION_TIMEOUT")
|
||||||
if host == "" {
|
|
||||||
host = "0.0.0.0"
|
|
||||||
}
|
|
||||||
|
|
||||||
staleTimeout, err := getEnvInt("Q3RCON_STALE_SESSION_TIMEOUT")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("unable to parse Q3RCON_STALE_SESSION_TIMEOUT: %s", err.Error())
|
log.Fatalf("unable to parse Q3RCON_SESSION_TIMEOUT: %s", err.Error())
|
||||||
|
}
|
||||||
|
if sessionTimeout == 0 {
|
||||||
|
sessionTimeout = 20
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, proxy := range strings.Split(proxies, ";") {
|
for _, proxy := range strings.Split(proxies, ";") {
|
||||||
go start(host, proxy, staleTimeout)
|
go start(proxyHost, targetHost, proxy, sessionTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
<-make(chan int)
|
<-make(chan struct{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func start(host, proxy string, staleTimeout int) {
|
func start(proxyHost, targetHost, ports string, sessionTimeout int) {
|
||||||
port, target := func() (string, string) {
|
proxyPort, targetPort := func() (string, string) {
|
||||||
x := strings.Split(proxy, ":")
|
x := strings.Split(ports, ":")
|
||||||
return x[0], x[1]
|
return x[0], x[1]
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
hostAddr := fmt.Sprintf("%s:%s", proxyHost, proxyPort)
|
||||||
|
proxyAddr := fmt.Sprintf("%s:%s", targetHost, targetPort)
|
||||||
|
|
||||||
c, err := udpproxy.New(
|
c, err := udpproxy.New(
|
||||||
fmt.Sprintf("%s:%s", host, port),
|
hostAddr, proxyAddr,
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("q3rcon-proxy initialized: [proxy] (%s:%s) [target] (127.0.0.1:%s)", host, port, target)
|
log.Printf("q3rcon-proxy initialized: [proxy] (%s) [target] (%s)", hostAddr, proxyAddr)
|
||||||
|
|
||||||
log.Fatal(c.ListenAndServe())
|
log.Fatal(c.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
|||||||
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,7 +2,6 @@ package udpproxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -11,15 +10,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)
|
||||||
|
|
||||||
// WithStaleTimeout is a functional option to set the stale session timeout
|
// WithSessionTimeout is a functional option to set the session timeout
|
||||||
func WithStaleTimeout(timeout time.Duration) Option {
|
func WithSessionTimeout(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 20 minutes")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.timeout = timeout
|
c.sessionTimeout = timeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,14 +28,12 @@ type Client struct {
|
|||||||
|
|
||||||
proxyConn *net.UDPConn
|
proxyConn *net.UDPConn
|
||||||
|
|
||||||
mutex sync.RWMutex
|
sessionCache sessionCache
|
||||||
sessions map[string]*session
|
sessionTimeout time.Duration
|
||||||
|
|
||||||
timeout time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(port, target string, options ...Option) (*Client, error) {
|
func New(proxy, target string, options ...Option) (*Client, error) {
|
||||||
laddr, err := net.ResolveUDPAddr("udp", port)
|
laddr, err := net.ResolveUDPAddr("udp", proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -47,11 +44,10 @@ func New(port, target string, options ...Option) (*Client, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c := &Client{
|
c := &Client{
|
||||||
laddr: laddr,
|
laddr: laddr,
|
||||||
raddr: raddr,
|
raddr: raddr,
|
||||||
mutex: sync.RWMutex{},
|
sessionCache: newSessionCache(),
|
||||||
sessions: map[string]*session{},
|
sessionTimeout: 20 * time.Minute,
|
||||||
timeout: 5 * time.Minute,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range options {
|
for _, o := range options {
|
||||||
@@ -77,7 +73,7 @@ func (c *Client) ListenAndServe() error {
|
|||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, ok := c.sessions[caddr.String()]
|
session, ok := c.sessionCache.read(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 {
|
||||||
@@ -85,7 +81,7 @@ func (c *Client) ListenAndServe() error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
c.sessions[caddr.String()] = session
|
c.sessionCache.insert(caddr.String(), session)
|
||||||
}
|
}
|
||||||
|
|
||||||
go session.proxyTo(buf[:n])
|
go session.proxyTo(buf[:n])
|
||||||
@@ -95,16 +91,12 @@ 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.sessions {
|
for _, session := range c.sessionCache.data {
|
||||||
c.mutex.RLock()
|
if time.Since(session.updateTime) > c.sessionTimeout {
|
||||||
if time.Since(session.updateTime) > c.timeout {
|
c.sessionCache.delete(session.caddr.String())
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user