mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2026-04-07 15:53:29 +00:00
Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6bffa14240 | |||
| 74493e6f61 | |||
| 51a0065e79 | |||
| f07a45c81b | |||
| a70fdecdbb | |||
| fc6ac8fa4e | |||
| f6ea67b88c | |||
| d18834b290 | |||
| 46874f2cf5 | |||
| 1c5e18294e | |||
|
|
331bb61882 | ||
| 3db79314ce | |||
|
|
ecfbd8771b | ||
|
|
991e063390 | ||
|
|
2e5ba6d6fc | ||
|
|
7455aeed0f | ||
|
|
5490f13175 | ||
|
|
539a9f00a6 | ||
|
|
f66cb52cce | ||
|
|
a7ab564de4 | ||
|
|
34c003694d | ||
|
|
3ecce5bb66 | ||
|
|
4d42600ecd | ||
|
|
0116d3dbe1 | ||
|
|
7897348669 | ||
| 6489d1e556 | |||
| d0e3f5863a | |||
| a00796254d | |||
| 80ef18f705 | |||
| 40d79063f5 | |||
| 02e73a21c0 |
@@ -1,6 +1,6 @@
|
|||||||
*
|
*
|
||||||
!cmd/
|
!cmd/
|
||||||
!pkg/
|
!*.go
|
||||||
|
|
||||||
!go.mod
|
!go.mod
|
||||||
!go.sum
|
!go.sum
|
||||||
30
.github/workflows/golang-ci.yml
vendored
Normal file
30
.github/workflows/golang-ci.yml
vendored
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: ['main']
|
||||||
|
paths:
|
||||||
|
- '**.go'
|
||||||
|
pull_request:
|
||||||
|
branches: ['main']
|
||||||
|
paths:
|
||||||
|
- '**.go'
|
||||||
|
jobs:
|
||||||
|
lint:
|
||||||
|
name: Lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: github.event_name == 'pull_request'
|
||||||
|
timeout-minutes: 3
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 1
|
||||||
|
- name: Setup Go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version: '1.24'
|
||||||
|
- name: golangci-lint
|
||||||
|
uses: golangci/golangci-lint-action@v9
|
||||||
|
with:
|
||||||
|
version: v2.6.0
|
||||||
|
args: --config .golangci.yml
|
||||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -25,4 +25,7 @@ go.work
|
|||||||
# End of gignore: github.com/onyx-and-iris/gignore
|
# End of gignore: github.com/onyx-and-iris/gignore
|
||||||
|
|
||||||
# Added by goreleaser init:
|
# Added by goreleaser init:
|
||||||
dist/
|
dist/
|
||||||
|
|
||||||
|
# env files
|
||||||
|
.envrc
|
||||||
142
.golangci.yml
Normal file
142
.golangci.yml
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
version: '2'
|
||||||
|
|
||||||
|
run:
|
||||||
|
timeout: 3m
|
||||||
|
tests: true
|
||||||
|
go: '1.24'
|
||||||
|
|
||||||
|
linters:
|
||||||
|
disable: [errcheck]
|
||||||
|
enable:
|
||||||
|
# Default enabled linters
|
||||||
|
- errcheck # Check for unchecked errors
|
||||||
|
- govet # Go's built-in vetting tool
|
||||||
|
- ineffassign # Detect ineffectual assignments
|
||||||
|
- staticcheck # Advanced static analysis
|
||||||
|
- unused # Check for unused code
|
||||||
|
# Additional useful linters
|
||||||
|
- misspell # Detect common misspellings
|
||||||
|
- unparam # Check for unused function parameters
|
||||||
|
- gosec # Security checks
|
||||||
|
- asciicheck # Check for non-ASCII characters
|
||||||
|
- errname # Check error variable names
|
||||||
|
- godot # Check for missing periods in comments
|
||||||
|
- revive # Highly configurable linter for style and correctness
|
||||||
|
- gocritic # Detect code issues and suggest improvements
|
||||||
|
- gocyclo # Check for cyclomatic complexity
|
||||||
|
- dupl # Check for code duplication
|
||||||
|
- predeclared # Check for shadowing of predeclared identifiers
|
||||||
|
- copyloopvar # Check for loop variable capture in goroutines
|
||||||
|
- errorlint # Check for common mistakes in error handling
|
||||||
|
- goconst # Check for repeated strings that could be constants
|
||||||
|
- gosmopolitan # Check for non-portable code
|
||||||
|
|
||||||
|
settings:
|
||||||
|
misspell:
|
||||||
|
locale: UK
|
||||||
|
|
||||||
|
errcheck:
|
||||||
|
check-type-assertions: true
|
||||||
|
check-blank: true
|
||||||
|
exclude-functions:
|
||||||
|
- fmt.Fprintf
|
||||||
|
- fmt.Fprintln
|
||||||
|
- fmt.Printf
|
||||||
|
- fmt.Println
|
||||||
|
- fmt.Errorf
|
||||||
|
|
||||||
|
revive:
|
||||||
|
severity: warning
|
||||||
|
rules:
|
||||||
|
# Code quality and style
|
||||||
|
- name: exported
|
||||||
|
arguments:
|
||||||
|
- 'checkPrivateReceivers'
|
||||||
|
- 'sayRepetitiveInsteadOfStutters'
|
||||||
|
- name: var-naming
|
||||||
|
- name: package-comments
|
||||||
|
- name: range-val-in-closure
|
||||||
|
- name: time-naming
|
||||||
|
- name: context-as-argument
|
||||||
|
- name: context-keys-type
|
||||||
|
- name: dot-imports
|
||||||
|
- name: empty-block
|
||||||
|
- name: error-return
|
||||||
|
- name: error-strings
|
||||||
|
- name: error-naming
|
||||||
|
- name: if-return
|
||||||
|
- name: increment-decrement
|
||||||
|
- name: indent-error-flow
|
||||||
|
- name: receiver-naming
|
||||||
|
- name: redefines-builtin-id
|
||||||
|
- name: superfluous-else
|
||||||
|
- name: unexported-return
|
||||||
|
- name: unreachable-code
|
||||||
|
- name: unused-parameter
|
||||||
|
- name: var-declaration
|
||||||
|
- name: blank-imports
|
||||||
|
- name: range
|
||||||
|
|
||||||
|
# Disabled rules (can be enabled if needed)
|
||||||
|
# - name: line-length-limit
|
||||||
|
# arguments: [120]
|
||||||
|
# - name: function-length
|
||||||
|
# arguments: [50, 0]
|
||||||
|
# - name: cyclomatic
|
||||||
|
# arguments: [10]
|
||||||
|
|
||||||
|
gosec:
|
||||||
|
excludes:
|
||||||
|
- G104 # Duplicated errcheck checks
|
||||||
|
- G115 # integer overflow conversion int -> uint32
|
||||||
|
|
||||||
|
exclusions:
|
||||||
|
warn-unused: false
|
||||||
|
rules:
|
||||||
|
# Exclude some linters from running on tests files.
|
||||||
|
- path: _test\.go
|
||||||
|
linters:
|
||||||
|
- gocyclo
|
||||||
|
- errcheck
|
||||||
|
- dupl
|
||||||
|
- gosec
|
||||||
|
|
||||||
|
paths:
|
||||||
|
- vendor
|
||||||
|
|
||||||
|
# Formatters configuration
|
||||||
|
formatters:
|
||||||
|
# Enable specific formatters
|
||||||
|
enable:
|
||||||
|
- gofumpt # Stricter gofmt alternative
|
||||||
|
- goimports # Organizes imports
|
||||||
|
- gci # Controls import order/grouping
|
||||||
|
- golines # Enforces line length
|
||||||
|
|
||||||
|
# Formatter-specific settings
|
||||||
|
settings:
|
||||||
|
goimports:
|
||||||
|
local-prefixes: [github.com/onyx-and-iris/q3rcon-proxy]
|
||||||
|
|
||||||
|
gci:
|
||||||
|
# Define import sections order
|
||||||
|
sections:
|
||||||
|
- standard # Standard library
|
||||||
|
- default # Everything else
|
||||||
|
- prefix(github.com/onyx-and-iris/q3rcon-proxy) # Current module
|
||||||
|
|
||||||
|
gofumpt:
|
||||||
|
extra-rules: true # Enable additional formatting rules
|
||||||
|
|
||||||
|
exclusions:
|
||||||
|
warn-unused: true
|
||||||
|
|
||||||
|
paths:
|
||||||
|
- vendor
|
||||||
|
|
||||||
|
issues:
|
||||||
|
# Limit the number of same issues reported to avoid spam
|
||||||
|
max-same-issues: 50
|
||||||
|
|
||||||
|
# Limit the number of issues per linter to keep output manageable
|
||||||
|
max-issues-per-linter: 100
|
||||||
@@ -22,6 +22,7 @@ builds:
|
|||||||
goos:
|
goos:
|
||||||
- linux
|
- linux
|
||||||
- windows
|
- windows
|
||||||
|
- darwin
|
||||||
goarch:
|
goarch:
|
||||||
- amd64
|
- amd64
|
||||||
|
|
||||||
@@ -46,6 +47,7 @@ changelog:
|
|||||||
exclude:
|
exclude:
|
||||||
- '^docs:'
|
- '^docs:'
|
||||||
- '^test:'
|
- '^test:'
|
||||||
|
- '^chore:'
|
||||||
|
|
||||||
release:
|
release:
|
||||||
footer: >-
|
footer: >-
|
||||||
|
|||||||
11
CHANGELOG.md
11
CHANGELOG.md
@@ -11,6 +11,16 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
|
|||||||
|
|
||||||
- [x]
|
- [x]
|
||||||
|
|
||||||
|
## [1.7.3] - 2026-02-26
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- *--version/-v* flag for printing the version of the CLI.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- now using [charmbracelet/log][charmlog] package for logging.
|
||||||
|
|
||||||
## [1.7.0] - 2025-06-05
|
## [1.7.0] - 2025-06-05
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
@@ -99,3 +109,4 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
|
|||||||
- All source files for lilproxy including full commit history.
|
- All source files for lilproxy including full commit history.
|
||||||
|
|
||||||
[logrus]: https://github.com/sirupsen/logrus
|
[logrus]: https://github.com/sirupsen/logrus
|
||||||
|
[charmlog]: https://github.com/charmbracelet/log
|
||||||
|
|||||||
12
Makefile
12
Makefile
@@ -5,24 +5,27 @@ BIN_DIR := bin
|
|||||||
|
|
||||||
WINDOWS=$(BIN_DIR)/$(PROGRAM)_windows_amd64.exe
|
WINDOWS=$(BIN_DIR)/$(PROGRAM)_windows_amd64.exe
|
||||||
LINUX=$(BIN_DIR)/$(PROGRAM)_linux_amd64
|
LINUX=$(BIN_DIR)/$(PROGRAM)_linux_amd64
|
||||||
VERSION=$(shell git log -n 1 --format=%h)
|
MACOS=$(BIN_DIR)/$(PROGRAM)_darwin_amd64
|
||||||
|
VERSION=$(shell git describe --tags $(shell git rev-list --tags --max-count=1))
|
||||||
|
|
||||||
.DEFAULT_GOAL := build
|
.DEFAULT_GOAL := build
|
||||||
|
|
||||||
.PHONY: fmt vet build windows linux test clean
|
.PHONY: fmt vet build windows linux macos test clean
|
||||||
fmt:
|
fmt:
|
||||||
$(GO) fmt ./...
|
$(GO) fmt ./...
|
||||||
|
|
||||||
vet: fmt
|
vet: fmt
|
||||||
$(GO) vet ./...
|
$(GO) vet ./...
|
||||||
|
|
||||||
build: vet windows linux | $(BIN_DIR)
|
build: vet windows linux macos | $(BIN_DIR)
|
||||||
@echo version: $(VERSION)
|
@echo version: $(VERSION)
|
||||||
|
|
||||||
windows: $(WINDOWS)
|
windows: $(WINDOWS)
|
||||||
|
|
||||||
linux: $(LINUX)
|
linux: $(LINUX)
|
||||||
|
|
||||||
|
macos: $(MACOS)
|
||||||
|
|
||||||
|
|
||||||
$(WINDOWS):
|
$(WINDOWS):
|
||||||
env GOOS=windows GOARCH=amd64 go build -v -o $(WINDOWS) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
|
env GOOS=windows GOARCH=amd64 go build -v -o $(WINDOWS) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
|
||||||
@@ -30,6 +33,9 @@ $(WINDOWS):
|
|||||||
$(LINUX):
|
$(LINUX):
|
||||||
env GOOS=linux GOARCH=amd64 go build -v -o $(LINUX) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
|
env GOOS=linux GOARCH=amd64 go build -v -o $(LINUX) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
|
||||||
|
|
||||||
|
$(MACOS):
|
||||||
|
env GOOS=darwin GOARCH=amd64 go build -v -o $(MACOS) -ldflags="-s -w -X main.version=$(VERSION)" ./cmd/$(PROGRAM)/
|
||||||
|
|
||||||
test:
|
test:
|
||||||
$(GO) test ./...
|
$(GO) test ./...
|
||||||
|
|
||||||
|
|||||||
16
Taskfile.yml
16
Taskfile.yml
@@ -10,8 +10,9 @@ vars:
|
|||||||
|
|
||||||
WINDOWS: '{{.BIN_DIR}}/{{.PROGRAM}}_windows_amd64.exe'
|
WINDOWS: '{{.BIN_DIR}}/{{.PROGRAM}}_windows_amd64.exe'
|
||||||
LINUX: '{{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64'
|
LINUX: '{{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64'
|
||||||
GIT_COMMIT:
|
MACOS: '{{.BIN_DIR}}/{{.PROGRAM}}_darwin_amd64'
|
||||||
sh: git log -n 1 --format=%h
|
VERSION:
|
||||||
|
sh: 'git describe --tags $(git rev-list --tags --max-count=1)'
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
default:
|
default:
|
||||||
@@ -25,6 +26,7 @@ tasks:
|
|||||||
cmds:
|
cmds:
|
||||||
- task: build-windows
|
- task: build-windows
|
||||||
- task: build-linux
|
- task: build-linux
|
||||||
|
- task: build-macos
|
||||||
|
|
||||||
vet:
|
vet:
|
||||||
desc: Vet the code
|
desc: Vet the code
|
||||||
@@ -40,13 +42,19 @@ tasks:
|
|||||||
build-windows:
|
build-windows:
|
||||||
desc: Build the q3rcon-proxy project for Windows
|
desc: Build the q3rcon-proxy project for Windows
|
||||||
cmds:
|
cmds:
|
||||||
- GOOS=windows GOARCH=amd64 go build -o {{.WINDOWS}} -ldflags="-X main.Version={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/
|
- GOOS=windows GOARCH=amd64 go build -o {{.WINDOWS}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
|
||||||
internal: true
|
internal: true
|
||||||
|
|
||||||
build-linux:
|
build-linux:
|
||||||
desc: Build the q3rcon-proxy project for Linux
|
desc: Build the q3rcon-proxy project for Linux
|
||||||
cmds:
|
cmds:
|
||||||
- GOOS=linux GOARCH=amd64 go build -o {{.LINUX}} -ldflags="-X main.Version={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/
|
- GOOS=linux GOARCH=amd64 go build -o {{.LINUX}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
|
||||||
|
internal: true
|
||||||
|
|
||||||
|
build-macos:
|
||||||
|
desc: Build the q3rcon-proxy project for macOS
|
||||||
|
cmds:
|
||||||
|
- GOOS=darwin GOARCH=amd64 go build -o {{.MACOS}} -ldflags="-X main.version={{.VERSION}}" ./cmd/{{.PROGRAM}}/
|
||||||
internal: true
|
internal: true
|
||||||
|
|
||||||
test:
|
test:
|
||||||
|
|||||||
@@ -1,17 +1,37 @@
|
|||||||
|
// Package main implements a command-line application for a Quake 3 RCON proxy server.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime/debug"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
|
"github.com/charmbracelet/log"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
|
||||||
|
udpproxy "github.com/onyx-and-iris/q3rcon-proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var version string // Version holds the application version, set at build time using ldflags.
|
||||||
|
|
||||||
|
// versionFromBuild retrieves the version information from the build metadata.
|
||||||
|
func versionFromBuild() string {
|
||||||
|
if version != "" {
|
||||||
|
return version
|
||||||
|
}
|
||||||
|
|
||||||
|
info, ok := debug.ReadBuildInfo()
|
||||||
|
if !ok {
|
||||||
|
return "(unable to read version)"
|
||||||
|
}
|
||||||
|
return strings.Split(info.Main.Version, "-")[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
// proxyConfig holds the configuration for a single UDP proxy server.
|
||||||
type proxyConfig struct {
|
type proxyConfig struct {
|
||||||
proxyHost string
|
proxyHost string
|
||||||
targetHost string
|
targetHost string
|
||||||
@@ -20,9 +40,14 @@ type proxyConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
cli.VersionPrinter = func(cmd *cli.Command) {
|
||||||
|
fmt.Printf("q3rcon-proxy version: %s\n", cmd.Root().Version)
|
||||||
|
}
|
||||||
|
|
||||||
cmd := &cli.Command{
|
cmd := &cli.Command{
|
||||||
Name: "q3rcon-proxy",
|
Name: "q3rcon-proxy",
|
||||||
Usage: "A Quake 3 RCON proxy server",
|
Usage: "A Quake 3 RCON proxy server",
|
||||||
|
Version: versionFromBuild(),
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "proxy-host",
|
Name: "proxy-host",
|
||||||
@@ -32,7 +57,7 @@ func main() {
|
|||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "target-host",
|
Name: "target-host",
|
||||||
Value: "127.0.0.1",
|
Value: "localhost",
|
||||||
Usage: "Target host address",
|
Usage: "Target host address",
|
||||||
Sources: cli.EnvVars("Q3RCON_TARGET_HOST"),
|
Sources: cli.EnvVars("Q3RCON_TARGET_HOST"),
|
||||||
},
|
},
|
||||||
@@ -41,6 +66,30 @@ func main() {
|
|||||||
Usage: "Proxy and target ports (proxy:target)",
|
Usage: "Proxy and target ports (proxy:target)",
|
||||||
Sources: cli.EnvVars("Q3RCON_PORTS_MAPPING"),
|
Sources: cli.EnvVars("Q3RCON_PORTS_MAPPING"),
|
||||||
Required: true,
|
Required: true,
|
||||||
|
Action: func(_ context.Context, _ *cli.Command, v string) error {
|
||||||
|
// Validate the ports mapping
|
||||||
|
for mapping := range strings.SplitSeq(v, ";") {
|
||||||
|
ports := strings.Split(mapping, ":")
|
||||||
|
if len(ports) != 2 {
|
||||||
|
return fmt.Errorf("invalid ports mapping: %s", mapping)
|
||||||
|
}
|
||||||
|
proxyPort, err := strconv.Atoi(ports[0])
|
||||||
|
if err != nil || proxyPort < 1 || proxyPort > 65535 {
|
||||||
|
return fmt.Errorf("invalid proxy port: %s", ports[0])
|
||||||
|
}
|
||||||
|
targetPort, err := strconv.Atoi(ports[1])
|
||||||
|
if err != nil || targetPort < 1 || targetPort > 65535 {
|
||||||
|
return fmt.Errorf("invalid target port: %s", ports[1])
|
||||||
|
}
|
||||||
|
if proxyPort == targetPort {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"proxy and target ports cannot be the same: %s",
|
||||||
|
mapping,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
},
|
},
|
||||||
&cli.IntFlag{
|
&cli.IntFlag{
|
||||||
Name: "session-timeout",
|
Name: "session-timeout",
|
||||||
@@ -74,17 +123,12 @@ func main() {
|
|||||||
sessionTimeout: cmd.Int("session-timeout"),
|
sessionTimeout: cmd.Int("session-timeout"),
|
||||||
}
|
}
|
||||||
|
|
||||||
go initProxy(cfg, errChan)
|
go launchProxy(cfg, errChan)
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't expect to receive any errors from the channels, but if we do, we log and return early.
|
// Under normal circumstances, the main goroutine will block here.
|
||||||
for err := range errChan {
|
// If we receive an error we will log it and exit
|
||||||
if err != nil {
|
return <-errChan
|
||||||
log.Errorf("Error: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,13 +137,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func initProxy(cfg proxyConfig, errChan chan error) {
|
// launchProxy initialises the UDP proxy server with the given configuration.
|
||||||
|
// It listens on the specified proxy host and port, and forwards traffic to the target host and port.
|
||||||
|
// server.ListenAndServe blocks until the server is stopped or an error occurs.
|
||||||
|
func launchProxy(cfg proxyConfig, errChan chan<- error) {
|
||||||
proxyPort, targetPort := cfg.portsMapping[0], cfg.portsMapping[1]
|
proxyPort, targetPort := cfg.portsMapping[0], cfg.portsMapping[1]
|
||||||
|
|
||||||
hostAddr := fmt.Sprintf("%s:%s", cfg.proxyHost, proxyPort)
|
hostAddr := fmt.Sprintf("%s:%s", cfg.proxyHost, proxyPort)
|
||||||
proxyAddr := fmt.Sprintf("%s:%s", cfg.targetHost, targetPort)
|
proxyAddr := fmt.Sprintf("%s:%s", cfg.targetHost, targetPort)
|
||||||
|
|
||||||
c, err := udpproxy.New(
|
server, err := udpproxy.New(
|
||||||
hostAddr, proxyAddr,
|
hostAddr, proxyAddr,
|
||||||
udpproxy.WithSessionTimeout(time.Duration(cfg.sessionTimeout)*time.Minute))
|
udpproxy.WithSessionTimeout(time.Duration(cfg.sessionTimeout)*time.Minute))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -107,7 +154,7 @@ func initProxy(cfg proxyConfig, errChan chan error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("q3rcon-proxy initialized: [proxy] (%s) [target] (%s)", hostAddr, proxyAddr)
|
log.Infof("q3rcon-proxy initialised: [proxy] (%s) [target] (%s)", hostAddr, proxyAddr)
|
||||||
|
|
||||||
errChan <- c.ListenAndServe()
|
errChan <- server.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|||||||
22
go.mod
22
go.mod
@@ -5,8 +5,24 @@ go 1.24.0
|
|||||||
toolchain go1.24.1
|
toolchain go1.24.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/charmbracelet/log v0.4.2
|
||||||
github.com/urfave/cli/v3 v3.3.2
|
github.com/urfave/cli/v3 v3.6.2
|
||||||
)
|
)
|
||||||
|
|
||||||
require golang.org/x/sys v0.32.0 // indirect
|
require (
|
||||||
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||||
|
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
|
||||||
|
github.com/charmbracelet/lipgloss v1.1.0 // indirect
|
||||||
|
github.com/charmbracelet/x/ansi v0.8.0 // indirect
|
||||||
|
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
|
||||||
|
github.com/charmbracelet/x/term v0.2.1 // indirect
|
||||||
|
github.com/go-logfmt/logfmt v0.6.0 // indirect
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/mattn/go-runewidth v0.0.16 // indirect
|
||||||
|
github.com/muesli/termenv v0.16.0 // indirect
|
||||||
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||||
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
|
||||||
|
golang.org/x/sys v0.41.0 // indirect
|
||||||
|
)
|
||||||
|
|||||||
52
go.sum
52
go.sum
@@ -1,20 +1,44 @@
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||||
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
||||||
|
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs=
|
||||||
|
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk=
|
||||||
|
github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
|
||||||
|
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
|
||||||
|
github.com/charmbracelet/log v0.4.2 h1:hYt8Qj6a8yLnvR+h7MwsJv/XvmBJXiueUcI3cIxsyig=
|
||||||
|
github.com/charmbracelet/log v0.4.2/go.mod h1:qifHGX/tc7eluv2R6pWIpyHDDrrb/AG71Pf2ysQu5nw=
|
||||||
|
github.com/charmbracelet/x/ansi v0.8.0 h1:9GTq3xq9caJW8ZrBTe0LIe2fvfLR/bYXKTx2llXn7xE=
|
||||||
|
github.com/charmbracelet/x/ansi v0.8.0/go.mod h1:wdYl/ONOLHLIVmQaxbIYEC/cRKOQyjTkowiI4blgS9Q=
|
||||||
|
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8=
|
||||||
|
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=
|
||||||
|
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
|
||||||
|
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
|
||||||
|
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
|
||||||
|
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
|
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
||||||
|
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/urfave/cli/v3 v3.6.2 h1:lQuqiPrZ1cIz8hz+HcrG0TNZFxU70dPZ3Yl+pSrH9A8=
|
||||||
github.com/urfave/cli/v3 v3.3.2 h1:BYFVnhhZ8RqT38DxEYVFPPmGFTEf7tJwySTXsVRrS/o=
|
github.com/urfave/cli/v3 v3.6.2/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
|
||||||
github.com/urfave/cli/v3 v3.3.2/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||||
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||||
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||||
|
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
24
option.go
Normal file
24
option.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package udpproxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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 20 minutes",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.sessionTimeout = timeout
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
"github.com/charmbracelet/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type session struct {
|
type session struct {
|
||||||
@@ -19,7 +19,7 @@ type session struct {
|
|||||||
validator
|
validator
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPConn) (*session, error) {
|
func newSession(caddr, raddr *net.UDPAddr, proxyConn *net.UDPConn) (*session, error) {
|
||||||
serverConn, err := net.DialUDP("udp", nil, raddr)
|
serverConn, err := net.DialUDP("udp", nil, raddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -81,7 +81,11 @@ func (s *session) proxyTo(buf []byte) error {
|
|||||||
var err error
|
var err error
|
||||||
if s.isChallengeRequestPacket(buf) {
|
if s.isChallengeRequestPacket(buf) {
|
||||||
parts := strings.SplitN(string(buf), " ", 3)
|
parts := strings.SplitN(string(buf), " ", 3)
|
||||||
err = fmt.Errorf("invalid challenge from %s with GUID: %s", s.caddr.IP, parts[len(parts)-1])
|
err = fmt.Errorf(
|
||||||
|
"invalid challenge from %s with GUID: %s",
|
||||||
|
s.caddr.IP,
|
||||||
|
parts[len(parts)-1],
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
err = errors.New("not a rcon or query request packet")
|
err = errors.New("not a rcon or query request packet")
|
||||||
}
|
}
|
||||||
@@ -98,7 +102,12 @@ func (s *session) proxyTo(buf []byte) error {
|
|||||||
|
|
||||||
if s.isRconRequestPacket(buf) {
|
if s.isRconRequestPacket(buf) {
|
||||||
parts := strings.SplitN(string(buf), " ", 3)
|
parts := strings.SplitN(string(buf), " ", 3)
|
||||||
log.Infof("From [%s] To [%s] Command: %s", s.caddr.IP, s.serverConn.RemoteAddr(), parts[len(parts)-1])
|
log.Infof(
|
||||||
|
"From [%s] To [%s] Command: %s",
|
||||||
|
s.caddr.IP,
|
||||||
|
s.serverConn.RemoteAddr(),
|
||||||
|
parts[len(parts)-1],
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -2,7 +2,7 @@ package udpproxy
|
|||||||
|
|
||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
// sessionCache tracks connection sessions
|
// sessionCache tracks connection sessions.
|
||||||
type sessionCache struct {
|
type sessionCache struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
data map[string]*session
|
data map[string]*session
|
||||||
@@ -15,7 +15,7 @@ func newSessionCache() sessionCache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// read returns the associated session for an addr
|
// read returns the associated session for an addr.
|
||||||
func (sc *sessionCache) read(addr string) (*session, bool) {
|
func (sc *sessionCache) read(addr string) (*session, bool) {
|
||||||
sc.mu.RLock()
|
sc.mu.RLock()
|
||||||
defer sc.mu.RUnlock()
|
defer sc.mu.RUnlock()
|
||||||
@@ -1,27 +1,16 @@
|
|||||||
|
// Package udpproxy implements a simple UDP proxy server that forwards rcon and query packets between clients and a target server.
|
||||||
package udpproxy
|
package udpproxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
"github.com/charmbracelet/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Option is a functional option type that allows us to configure the Client.
|
// Client represents a UDP proxy server that forwards rcon and query packets between clients and a target server.
|
||||||
type Option func(*Client)
|
// It maintains a session cache to manage client sessions and handles packet forwarding between clients and the target server.
|
||||||
|
|
||||||
// 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 20 minutes")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.sessionTimeout = timeout
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
laddr *net.UDPAddr
|
laddr *net.UDPAddr
|
||||||
raddr *net.UDPAddr
|
raddr *net.UDPAddr
|
||||||
@@ -32,15 +21,16 @@ type Client struct {
|
|||||||
sessionTimeout time.Duration
|
sessionTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New creates a new Client with the specified proxy and target addresses, and applies any provided options.
|
||||||
func New(proxy, target string, options ...Option) (*Client, error) {
|
func New(proxy, target string, options ...Option) (*Client, error) {
|
||||||
laddr, err := net.ResolveUDPAddr("udp", proxy)
|
laddr, err := net.ResolveUDPAddr("udp", proxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("invalid proxy address: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
raddr, err := net.ResolveUDPAddr("udp", target)
|
raddr, err := net.ResolveUDPAddr("udp", target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("invalid target address: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c := &Client{
|
c := &Client{
|
||||||
@@ -57,11 +47,13 @@ func New(proxy, target string, options ...Option) (*Client, error) {
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListenAndServe starts the UDP proxy server and listens for incoming packets from clients.
|
||||||
|
// It reads packets from the proxy connection, checks for existing sessions, and forwards packets to the target server.
|
||||||
func (c *Client) ListenAndServe() error {
|
func (c *Client) ListenAndServe() error {
|
||||||
var err error
|
var err error
|
||||||
c.proxyConn, err = net.ListenUDP("udp", c.laddr)
|
c.proxyConn, err = net.ListenUDP("udp", c.laddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to listen on proxy address: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go c.pruneSessions()
|
go c.pruneSessions()
|
||||||
@@ -95,7 +87,7 @@ func (c *Client) pruneSessions() {
|
|||||||
for _, session := range c.sessionCache.data {
|
for _, session := range c.sessionCache.data {
|
||||||
if time.Since(session.updateTime) > c.sessionTimeout {
|
if time.Since(session.updateTime) > c.sessionTimeout {
|
||||||
c.sessionCache.delete(session.caddr.String())
|
c.sessionCache.delete(session.caddr.String())
|
||||||
log.Tracef("session for %s deleted", session.caddr)
|
log.Debugf("session for %s deleted", session.caddr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user