24 Commits

Author SHA1 Message Date
51f22f480b add Logging section to README. 2024-09-28 15:37:38 +01:00
3d1c8f90f3 remove init function\
remove global vars proxies, host
2024-09-28 15:36:24 +01:00
2fc0f7a74f log challenge requests 2024-09-28 15:36:04 +01:00
6cd90ae1dd reuse single buffers 2024-09-28 00:38:55 +01:00
826756eb6e rename comparand arg 2024-04-14 19:13:56 +01:00
05fb7bdd0f add compare helper method
split up long conditionals
2024-04-14 08:27:30 +01:00
e8f213fded easier to read? 2024-04-11 23:40:25 +01:00
3a8758a4d7 remove string casts
compare byte slices
2024-04-11 19:24:43 +01:00
2c8ae43303 split 3 times, log last part 2024-04-03 23:58:58 +01:00
0935fc1190 rename isBadRconRequest to isBadRconResponse 2024-04-03 20:59:37 +01:00
3e039824de log bad rcon requests at info level
include client ip in log
2024-04-03 20:54:10 +01:00
0c054377ba move validator methods into Validator struct
embed Validator into Session

unexport Session
2024-03-26 10:47:53 +00:00
5ac3f805e2 add example debian unit file 2024-03-24 12:31:04 +00:00
ab548d669a no need to call String() explicitly here 2024-03-24 12:29:48 +00:00
107f914d3b upd changelog 2024-03-21 08:42:58 +00:00
0fdf9d10c3 upd special thanks with link to lilproxy 2024-03-21 08:23:35 +00:00
690fabddaf no need to split and rejoin here. 2024-03-21 07:35:07 +00:00
f6f0044a84 Only stringify rcon responses if debug enabled. 2024-03-20 21:49:05 +00:00
725e6cfb3d isQueryResponsePacket() and isValidResponsePacket() added
fixes bug relaying query responses
2024-03-20 21:03:37 +00:00
ca33a6a390 log invalid response packets 2024-03-20 18:10:15 +00:00
dfcdb6a96a move packet header check methods into Session
log invalid packets at error level
2024-03-20 18:02:55 +00:00
15e4cf6e42 remove global var debug 2024-03-20 17:10:00 +00:00
f2fd9354f0 add isRconPacket(), isQueryPacket() and isResponsePacket() methods
response packets now logged at Debug level
2024-03-20 17:05:02 +00:00
94a683fb3f Q3RCON_DEBUG env var added 2024-03-20 17:03:35 +00:00
9 changed files with 231 additions and 59 deletions

4
.gitignore vendored
View File

@@ -19,3 +19,7 @@
# Go workspace file
go.work
# testing
run.sh
server.yaml

View File

@@ -11,8 +11,41 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
- [x]
## [0.6.0] - 2024-03-21
### Added
- new environment variable `Q3RCON_DEBUG` for enabling debug logging. Defaults to 0.
- rcon responses are now logged at debug level
- invalid responses (rcon and query) now logged
### Changed
- All packet header checking methods moved into Session struct.
### Fixed
- a bug causing the proxy not to send back query responses
## [0.3.0] - 2024-03-08
### Added
- outgoing rcon requests now logged at info level
- new environment variable `Q3RCON_HOST` for specifying which ip to bind the proxy to. Defaults to `0.0.0.0`.
### Changed
- now using [logrus][logrus] package for logging.
### Fixed
- a `slice bounds out of range` error due to query packets being logged.
## [0.1.0] - 2024-01-27
### Added
- only forward packets if the header matches q3 rcon/query.
## [0.0.1] - 2024-01-27
@@ -20,3 +53,5 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
### Added
- All source files for lilproxy including full commit history.
[logrus]: https://github.com/sirupsen/logrus

View File

@@ -2,6 +2,10 @@
A modification of [lilproxy][lilproxy_url] that forwards only Q3 rcon/query packets. Useful for separating the rcon port from the game server port.
### Why
Unfortunately the Q3Rcon engine ties the rcon port to the game servers public port used for client connections. This proxy will allow you to run rcon through a separate whitelisted port.
### Use
Run one or multiple rcon proxies by setting an environment variable `Q3RCON_PROXY`
@@ -16,13 +20,15 @@ This would configure q3rcon-proxy to run 3 proxy servers listening on ports `200
Then just run the binary which you can compile yourself, download from `Releases` or use the included Dockerfile.
### Why
### Logging
Avoid sending plaintext rcon commands to the public game server port. In general I would advise anyone using rcon remotely to use a secured connection but perhaps you've passed rcon to a clan friend who doesn't know about secured connections. Now you can instruct them to use rcon only through a whitelisted port.
Set the log level with environment variable `Q3RCON_LOGLEVEL`:
`0 = Panic, 1 = Fatal, 2 = Error, 3 = Warning, 4 = Info, 5 = Debug, 6 = Trace`
### Special Thanks
[Dylan][user_link] For writing this proxy.
[Dylan][user_link] For writing [lilproxy][lilproxy_url].
[lilproxy_url]: https://github.com/dgparker/lilproxy
[user_link]: https://github.com/dgparker

View File

@@ -2,14 +2,42 @@ package main
import (
"fmt"
"log"
"os"
"slices"
"strings"
log "github.com/sirupsen/logrus"
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
)
func start(proxy string) {
func main() {
logLevel, err := getEnvInt("Q3RCON_LOGLEVEL")
if err != nil {
log.Fatalf("unable to parse Q3RCON_LEVEL: %s", err.Error())
}
if slices.Contains(log.AllLevels, log.Level(logLevel)) {
log.SetLevel(log.Level(logLevel))
}
proxies := os.Getenv("Q3RCON_PROXY")
if proxies == "" {
log.Fatal("env Q3RCON_PROXY required")
}
host := os.Getenv("Q3RCON_HOST")
if host == "" {
host = "0.0.0.0"
}
for _, proxy := range strings.Split(proxies, ";") {
go start(host, proxy)
}
<-make(chan int)
}
func start(host, proxy string) {
port, target := func() (string, string) {
x := strings.Split(proxy, ":")
return x[0], x[1]
@@ -24,27 +52,3 @@ func start(proxy string) {
log.Fatal(c.ListenAndServe())
}
var (
proxies, host string
)
func init() {
proxies = os.Getenv("Q3RCON_PROXY")
if proxies == "" {
log.Fatal("env Q3RCON_PROXY required")
}
host = os.Getenv("Q3RCON_HOST")
if host == "" {
host = "0.0.0.0"
}
}
func main() {
for _, proxy := range strings.Split(proxies, ";") {
go start(proxy)
}
<-make(chan int)
}

18
cmd/q3rcon-proxy/util.go Normal file
View File

@@ -0,0 +1,18 @@
package main
import (
"os"
"strconv"
)
func getEnvInt(key string) (int, error) {
s := os.Getenv(key)
if s == "" {
return 0, nil
}
v, err := strconv.Atoi(s)
if err != nil {
return 0, err
}
return v, nil
}

18
debian/q3rcon-proxy.service vendored Normal file
View File

@@ -0,0 +1,18 @@
[Unit]
Description=Q3Rcon Proxy Service
Wants=network.target
After=network.target
[Service]
Type=simple
User=gameservers
Environment="Q3RCON_PROXY=20000:28960;20001:28961;20002:28962"
Environment="Q3RCON_HOST=0.0.0.0"
Environment="Q3RCON_DEBUG=0"
ExecStart=/usr/local/bin/q3rcon-proxy
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target

View File

@@ -1,6 +1,8 @@
package udpproxy
import (
"errors"
"fmt"
"net"
"strings"
"time"
@@ -8,24 +10,27 @@ import (
log "github.com/sirupsen/logrus"
)
type Session struct {
type session struct {
serverConn *net.UDPConn
proxyConn *net.UDPConn
caddr *net.UDPAddr
updateTime time.Time
validator
}
func createSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPConn) (*Session, error) {
func newSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPConn) (*session, error) {
serverConn, err := net.DialUDP("udp", nil, raddr)
if err != nil {
return nil, err
}
session := &Session{
session := &session{
serverConn: serverConn,
proxyConn: proxyConn,
caddr: caddr,
updateTime: time.Now(),
validator: newValidator(),
}
go session.listen()
@@ -33,12 +38,12 @@ func createSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPCon
return session, nil
}
func (s *Session) listen() error {
for {
func (s *session) listen() error {
buf := make([]byte, 2048)
for {
n, err := s.serverConn.Read(buf)
if err != nil {
log.Println(err)
log.Error(err)
continue
}
@@ -46,29 +51,54 @@ func (s *Session) listen() error {
}
}
func (s *Session) proxyFrom(buf []byte) error {
func (s *session) proxyFrom(buf []byte) error {
if !s.isValidResponsePacket(buf) {
err := errors.New("not a rcon or query response packet")
log.Error(err.Error())
return err
}
s.updateTime = time.Now()
_, err := s.proxyConn.WriteToUDP(buf, s.caddr)
if err != nil {
log.Println(err)
log.Error(err)
return err
}
if s.isRconResponsePacket(buf) {
if s.isBadRconResponse(buf) {
log.Infof("Response: Bad rcon from %s", s.caddr.IP)
} else {
log.Debugf("Response: %s", string(buf[len(s.rconResponseHeader):]))
}
}
return nil
}
func (s *Session) proxyTo(buf []byte) error {
func (s *session) proxyTo(buf []byte) error {
if !s.isValidRequestPacket(buf) {
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])
} else {
err = errors.New("not a rcon or query request packet")
}
log.Error(err.Error())
return err
}
s.updateTime = time.Now()
_, err := s.serverConn.Write(buf)
if err != nil {
log.Println(err)
log.Error(err)
return err
}
cmd := string(buf)
if cmd[:8] == "\xff\xff\xff\xffrcon" {
parts := strings.Split(cmd, " ")
log.Info("From [", s.caddr.IP, "] To [", s.serverConn.RemoteAddr().String(), "] Command: ", strings.Join(parts[2:], " "))
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])
}
return nil

View File

@@ -15,7 +15,7 @@ type Client struct {
proxyConn *net.UDPConn
mutex sync.RWMutex
sessions map[string]*Session
sessions map[string]*session
}
func New(port, target string) (*Client, error) {
@@ -33,14 +33,10 @@ func New(port, target string) (*Client, error) {
laddr: laddr,
raddr: raddr,
mutex: sync.RWMutex{},
sessions: map[string]*Session{},
sessions: map[string]*session{},
}, nil
}
func (c *Client) isValidPacket(header []byte) bool {
return string(header[:8]) == "\xff\xff\xff\xffrcon" || string(header[:13]) == "\xff\xff\xff\xffgetstatus" || string(header[:11]) == "\xff\xff\xff\xffgetinfo"
}
func (c *Client) ListenAndServe() error {
var err error
c.proxyConn, err = net.ListenUDP("udp", c.laddr)
@@ -50,22 +46,18 @@ func (c *Client) ListenAndServe() error {
go c.pruneSessions()
for {
buf := make([]byte, 2048)
for {
n, caddr, err := c.proxyConn.ReadFromUDP(buf)
if err != nil {
log.Println(err)
log.Error(err)
}
if !c.isValidPacket(buf[:16]) {
continue
}
session, found := c.sessions[caddr.String()]
if !found {
session, err = createSession(caddr, c.raddr, c.proxyConn)
session, ok := c.sessions[caddr.String()]
if !ok {
session, err = newSession(caddr, c.raddr, c.proxyConn)
if err != nil {
log.Println(err)
log.Error(err)
continue
}

65
pkg/udpproxy/validator.go Normal file
View File

@@ -0,0 +1,65 @@
package udpproxy
import "bytes"
type validator struct {
rconRequestHeader []byte
getstatusRequestHeader []byte
getinfoRequestHeader []byte
getchallengeRequestHeader []byte
rconResponseHeader []byte
getstatusResponseHeader []byte
getinfoResponseHeader []byte
badRconIdentifier []byte
}
func newValidator() validator {
return validator{
rconRequestHeader: []byte("\xff\xff\xff\xffrcon"),
getstatusRequestHeader: []byte("\xff\xff\xff\xffgetstatus"),
getinfoRequestHeader: []byte("\xff\xff\xff\xffgetinfo"),
getchallengeRequestHeader: []byte("\xff\xff\xff\xffgetchallenge"),
rconResponseHeader: []byte("\xff\xff\xff\xffprint\n"),
getstatusResponseHeader: []byte("\xff\xff\xff\xffstatusResponse\n"),
getinfoResponseHeader: []byte("\xff\xff\xff\xffinfoResponse\n"),
badRconIdentifier: []byte("Bad rcon"),
}
}
func (v validator) compare(buf, c []byte) bool {
return bytes.Equal(buf[:len(c)], c)
}
func (v validator) isRconRequestPacket(buf []byte) bool {
return v.compare(buf, v.rconRequestHeader)
}
func (v validator) isQueryRequestPacket(buf []byte) bool {
return v.compare(buf, v.getstatusRequestHeader) ||
v.compare(buf, v.getinfoRequestHeader)
}
func (v validator) isValidRequestPacket(buf []byte) bool {
return v.isRconRequestPacket(buf) || v.isQueryRequestPacket(buf)
}
func (v validator) isChallengeRequestPacket(buf []byte) bool {
return v.compare(buf, v.getchallengeRequestHeader)
}
func (v validator) isRconResponsePacket(buf []byte) bool {
return v.compare(buf, v.rconResponseHeader)
}
func (v validator) isQueryResponsePacket(buf []byte) bool {
return v.compare(buf, v.getstatusResponseHeader) ||
v.compare(buf, v.getinfoResponseHeader)
}
func (v validator) isValidResponsePacket(buf []byte) bool {
return v.isRconResponsePacket(buf) || v.isQueryResponsePacket(buf)
}
func (v validator) isBadRconResponse(buf []byte) bool {
return v.compare(buf[len(v.rconResponseHeader):], v.badRconIdentifier)
}