Compare commits

...

4 Commits

Author SHA1 Message Date
f6ea67b88c enable revive linter 2026-02-15 16:02:07 +00:00
d18834b290 add macos target to makefile/taskfile 2026-02-15 15:56:13 +00:00
46874f2cf5 format fixes 2026-02-15 15:54:02 +00:00
1c5e18294e migrate golangci config to version 2
add golang-ci workflow
2026-02-15 15:53:35 +00:00
8 changed files with 221 additions and 12 deletions

30
.github/workflows/golang-ci.yml vendored Normal file
View 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
View 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

View File

@ -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
MACOS=$(BIN_DIR)/$(PROGRAM)_darwin_amd64
VERSION=$(shell git log -n 1 --format=%h) VERSION=$(shell git log -n 1 --format=%h)
.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 ./...

View File

@ -10,6 +10,7 @@ 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'
MACOS: '{{.BIN_DIR}}/{{.PROGRAM}}_darwin_amd64'
GIT_COMMIT: GIT_COMMIT:
sh: git log -n 1 --format=%h sh: git log -n 1 --format=%h
@ -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
@ -49,6 +51,12 @@ tasks:
- 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={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}/
internal: true 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: test:
desc: Run tests desc: Run tests
cmds: cmds:

View File

@ -1,3 +1,4 @@
// Package main implements a command-line application for a Quake 3 RCON proxy server.
package main package main
import ( import (
@ -8,9 +9,10 @@ import (
"strings" "strings"
"time" "time"
udpproxy "github.com/onyx-and-iris/q3rcon-proxy"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
udpproxy "github.com/onyx-and-iris/q3rcon-proxy"
) )
// proxyConfig holds the configuration for a single UDP proxy server. // proxyConfig holds the configuration for a single UDP proxy server.
@ -43,7 +45,7 @@ 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(ctx context.Context, cmd *cli.Command, v string) error { Action: func(_ context.Context, _ *cli.Command, v string) error {
// Validate the ports mapping // Validate the ports mapping
for mapping := range strings.SplitSeq(v, ";") { for mapping := range strings.SplitSeq(v, ";") {
ports := strings.Split(mapping, ":") ports := strings.Split(mapping, ":")
@ -59,7 +61,10 @@ func main() {
return fmt.Errorf("invalid target port: %s", ports[1]) return fmt.Errorf("invalid target port: %s", ports[1])
} }
if proxyPort == targetPort { 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 return nil

View File

@ -13,7 +13,9 @@ type Option func(*Client)
func WithSessionTimeout(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 20 minutes") log.Warnf(
"cannot set stale session timeout to less than 1 minute.. defaulting to 20 minutes",
)
return return
} }

View File

@ -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

View File

@ -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 package udpproxy
import ( import (
"fmt"
"net" "net"
"time" "time"
log "github.com/sirupsen/logrus" 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 { type Client struct {
laddr *net.UDPAddr laddr *net.UDPAddr
raddr *net.UDPAddr raddr *net.UDPAddr
@ -17,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{
@ -42,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()