mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2026-03-03 07:39:11 +00:00
Compare commits
4 Commits
331bb61882
...
f6ea67b88c
| Author | SHA1 | Date | |
|---|---|---|---|
| f6ea67b88c | |||
| d18834b290 | |||
| 46874f2cf5 | |||
| 1c5e18294e |
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
|
||||
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, godot, misspell]
|
||||
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
|
||||
10
Makefile
10
Makefile
@ -5,24 +5,27 @@ BIN_DIR := bin
|
||||
|
||||
WINDOWS=$(BIN_DIR)/$(PROGRAM)_windows_amd64.exe
|
||||
LINUX=$(BIN_DIR)/$(PROGRAM)_linux_amd64
|
||||
MACOS=$(BIN_DIR)/$(PROGRAM)_darwin_amd64
|
||||
VERSION=$(shell git log -n 1 --format=%h)
|
||||
|
||||
.DEFAULT_GOAL := build
|
||||
|
||||
.PHONY: fmt vet build windows linux test clean
|
||||
.PHONY: fmt vet build windows linux macos test clean
|
||||
fmt:
|
||||
$(GO) fmt ./...
|
||||
|
||||
vet: fmt
|
||||
$(GO) vet ./...
|
||||
|
||||
build: vet windows linux | $(BIN_DIR)
|
||||
build: vet windows linux macos | $(BIN_DIR)
|
||||
@echo version: $(VERSION)
|
||||
|
||||
windows: $(WINDOWS)
|
||||
|
||||
linux: $(LINUX)
|
||||
|
||||
macos: $(MACOS)
|
||||
|
||||
|
||||
$(WINDOWS):
|
||||
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):
|
||||
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:
|
||||
$(GO) test ./...
|
||||
|
||||
|
||||
@ -10,6 +10,7 @@ vars:
|
||||
|
||||
WINDOWS: '{{.BIN_DIR}}/{{.PROGRAM}}_windows_amd64.exe'
|
||||
LINUX: '{{.BIN_DIR}}/{{.PROGRAM}}_linux_amd64'
|
||||
MACOS: '{{.BIN_DIR}}/{{.PROGRAM}}_darwin_amd64'
|
||||
GIT_COMMIT:
|
||||
sh: git log -n 1 --format=%h
|
||||
|
||||
@ -25,6 +26,7 @@ tasks:
|
||||
cmds:
|
||||
- task: build-windows
|
||||
- task: build-linux
|
||||
- task: build-macos
|
||||
|
||||
vet:
|
||||
desc: Vet the code
|
||||
@ -49,6 +51,12 @@ tasks:
|
||||
- GOOS=linux GOARCH=amd64 go build -o {{.LINUX}} -ldflags="-X main.Version={{.GIT_COMMIT}}" ./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={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/
|
||||
internal: true
|
||||
|
||||
test:
|
||||
desc: Run tests
|
||||
cmds:
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
// Package main implements a command-line application for a Quake 3 RCON proxy server.
|
||||
package main
|
||||
|
||||
import (
|
||||
@ -8,9 +9,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
udpproxy "github.com/onyx-and-iris/q3rcon-proxy"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli/v3"
|
||||
|
||||
udpproxy "github.com/onyx-and-iris/q3rcon-proxy"
|
||||
)
|
||||
|
||||
// proxyConfig holds the configuration for a single UDP proxy server.
|
||||
@ -43,7 +45,7 @@ func main() {
|
||||
Usage: "Proxy and target ports (proxy:target)",
|
||||
Sources: cli.EnvVars("Q3RCON_PORTS_MAPPING"),
|
||||
Required: true,
|
||||
Action: func(ctx context.Context, cmd *cli.Command, v string) error {
|
||||
Action: func(_ context.Context, _ *cli.Command, v string) error {
|
||||
// Validate the ports mapping
|
||||
for mapping := range strings.SplitSeq(v, ";") {
|
||||
ports := strings.Split(mapping, ":")
|
||||
@ -59,7 +61,10 @@ func main() {
|
||||
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 fmt.Errorf(
|
||||
"proxy and target ports cannot be the same: %s",
|
||||
mapping,
|
||||
)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@ -13,7 +13,9 @@ type Option func(*Client)
|
||||
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")
|
||||
log.Warnf(
|
||||
"cannot set stale session timeout to less than 1 minute.. defaulting to 20 minutes",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
15
session.go
15
session.go
@ -19,7 +19,7 @@ type session struct {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -81,7 +81,11 @@ func (s *session) proxyTo(buf []byte) error {
|
||||
var err error
|
||||
if s.isChallengeRequestPacket(buf) {
|
||||
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 {
|
||||
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) {
|
||||
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
|
||||
|
||||
13
udpproxy.go
13
udpproxy.go
@ -1,12 +1,16 @@
|
||||
// Package udpproxy implements a simple UDP proxy server that forwards rcon and query packets between clients and a target server.
|
||||
package udpproxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Client represents a UDP proxy server that forwards rcon and query packets between clients and a target server.
|
||||
// It maintains a session cache to manage client sessions and handles packet forwarding between clients and the target server.
|
||||
type Client struct {
|
||||
laddr *net.UDPAddr
|
||||
raddr *net.UDPAddr
|
||||
@ -17,15 +21,16 @@ type Client struct {
|
||||
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) {
|
||||
laddr, err := net.ResolveUDPAddr("udp", proxy)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("invalid proxy address: %w", err)
|
||||
}
|
||||
|
||||
raddr, err := net.ResolveUDPAddr("udp", target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("invalid target address: %w", err)
|
||||
}
|
||||
|
||||
c := &Client{
|
||||
@ -42,11 +47,13 @@ func New(proxy, target string, options ...Option) (*Client, error) {
|
||||
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 {
|
||||
var err error
|
||||
c.proxyConn, err = net.ListenUDP("udp", c.laddr)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to listen on proxy address: %w", err)
|
||||
}
|
||||
|
||||
go c.pruneSessions()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user