mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2026-04-07 15:53:29 +00:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a8758a4d7 | |||
| 2c8ae43303 | |||
| 0935fc1190 | |||
| 3e039824de | |||
| 0c054377ba | |||
| 5ac3f805e2 | |||
| ab548d669a | |||
| 107f914d3b | |||
| 0fdf9d10c3 | |||
| 690fabddaf | |||
| f6f0044a84 | |||
| 725e6cfb3d | |||
| ca33a6a390 | |||
| dfcdb6a96a | |||
| 15e4cf6e42 | |||
| f2fd9354f0 | |||
| 94a683fb3f |
35
CHANGELOG.md
35
CHANGELOG.md
@@ -11,8 +11,41 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
|
|||||||
|
|
||||||
- [x]
|
- [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
|
## [0.1.0] - 2024-01-27
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
- only forward packets if the header matches q3 rcon/query.
|
- only forward packets if the header matches q3 rcon/query.
|
||||||
|
|
||||||
## [0.0.1] - 2024-01-27
|
## [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
|
### Added
|
||||||
|
|
||||||
- All source files for lilproxy including full commit history.
|
- All source files for lilproxy including full commit history.
|
||||||
|
|
||||||
|
[logrus]: https://github.com/sirupsen/logrus
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ Avoid sending plaintext rcon commands to the public game server port. In general
|
|||||||
|
|
||||||
### Special Thanks
|
### Special Thanks
|
||||||
|
|
||||||
[Dylan][user_link] For writing this proxy.
|
[Dylan][user_link] For writing [lilproxy][lilproxy_url].
|
||||||
|
|
||||||
[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
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
|
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,6 +31,18 @@ var (
|
|||||||
proxies, host string
|
proxies, host string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
proxies = os.Getenv("Q3RCON_PROXY")
|
proxies = os.Getenv("Q3RCON_PROXY")
|
||||||
if proxies == "" {
|
if proxies == "" {
|
||||||
@@ -39,6 +53,18 @@ func init() {
|
|||||||
if host == "" {
|
if host == "" {
|
||||||
host = "0.0.0.0"
|
host = "0.0.0.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug, err := getenvInt("Q3RCON_DEBUG")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if debug == 1 {
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
} else {
|
||||||
|
log.SetLevel(log.InfoLevel)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
18
debian/q3rcon-proxy.service
vendored
Normal file
18
debian/q3rcon-proxy.service
vendored
Normal 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
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package udpproxy
|
package udpproxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -8,24 +9,27 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Session struct {
|
type session struct {
|
||||||
serverConn *net.UDPConn
|
serverConn *net.UDPConn
|
||||||
proxyConn *net.UDPConn
|
proxyConn *net.UDPConn
|
||||||
caddr *net.UDPAddr
|
caddr *net.UDPAddr
|
||||||
updateTime time.Time
|
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)
|
serverConn, err := net.DialUDP("udp", nil, raddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
session := &Session{
|
session := &session{
|
||||||
serverConn: serverConn,
|
serverConn: serverConn,
|
||||||
proxyConn: proxyConn,
|
proxyConn: proxyConn,
|
||||||
caddr: caddr,
|
caddr: caddr,
|
||||||
updateTime: time.Now(),
|
updateTime: time.Now(),
|
||||||
|
validator: newValidator(),
|
||||||
}
|
}
|
||||||
|
|
||||||
go session.listen()
|
go session.listen()
|
||||||
@@ -33,12 +37,12 @@ func createSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPCon
|
|||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) listen() error {
|
func (s *session) listen() error {
|
||||||
for {
|
for {
|
||||||
buf := make([]byte, 2048)
|
buf := make([]byte, 2048)
|
||||||
n, err := s.serverConn.Read(buf)
|
n, err := s.serverConn.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,29 +50,48 @@ 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()
|
s.updateTime = time.Now()
|
||||||
_, err := s.proxyConn.WriteToUDP(buf, s.caddr)
|
_, err := s.proxyConn.WriteToUDP(buf, s.caddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
return 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[10:]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) proxyTo(buf []byte) error {
|
func (s *session) proxyTo(buf []byte) error {
|
||||||
|
if !s.isValidRequestPacket(buf) {
|
||||||
|
err := errors.New("not a rcon or query request packet")
|
||||||
|
log.Error(err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
s.updateTime = time.Now()
|
s.updateTime = time.Now()
|
||||||
_, err := s.serverConn.Write(buf)
|
_, err := s.serverConn.Write(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := string(buf)
|
if s.isRconRequestPacket(buf) {
|
||||||
if cmd[:8] == "\xff\xff\xff\xffrcon" {
|
parts := strings.SplitN(string(buf), " ", 3)
|
||||||
parts := strings.Split(cmd, " ")
|
log.Infof("From [%s] To [%s] Command: %s", s.caddr.IP, s.serverConn.RemoteAddr(), parts[len(parts)-1])
|
||||||
log.Info("From [", s.caddr.IP, "] To [", s.serverConn.RemoteAddr().String(), "] Command: ", strings.Join(parts[2:], " "))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ type Client struct {
|
|||||||
proxyConn *net.UDPConn
|
proxyConn *net.UDPConn
|
||||||
|
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
sessions map[string]*Session
|
sessions map[string]*session
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(port, target string) (*Client, error) {
|
func New(port, target string) (*Client, error) {
|
||||||
@@ -33,14 +33,10 @@ func New(port, target string) (*Client, error) {
|
|||||||
laddr: laddr,
|
laddr: laddr,
|
||||||
raddr: raddr,
|
raddr: raddr,
|
||||||
mutex: sync.RWMutex{},
|
mutex: sync.RWMutex{},
|
||||||
sessions: map[string]*Session{},
|
sessions: map[string]*session{},
|
||||||
}, nil
|
}, 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 {
|
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)
|
||||||
@@ -54,18 +50,14 @@ func (c *Client) ListenAndServe() error {
|
|||||||
buf := make([]byte, 2048)
|
buf := make([]byte, 2048)
|
||||||
n, caddr, err := c.proxyConn.ReadFromUDP(buf)
|
n, caddr, err := c.proxyConn.ReadFromUDP(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
}
|
|
||||||
|
|
||||||
if !c.isValidPacket(buf[:16]) {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
session, found := c.sessions[caddr.String()]
|
session, found := c.sessions[caddr.String()]
|
||||||
if !found {
|
if !found {
|
||||||
session, err = createSession(caddr, c.raddr, c.proxyConn)
|
session, err = newSession(caddr, c.raddr, c.proxyConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
53
pkg/udpproxy/validator.go
Normal file
53
pkg/udpproxy/validator.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package udpproxy
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
type validator struct {
|
||||||
|
rconRequestHeader []byte
|
||||||
|
getstatusRequestHeader []byte
|
||||||
|
getinfoRequestHeader []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"),
|
||||||
|
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) isRconRequestPacket(buf []byte) bool {
|
||||||
|
return bytes.Equal(buf[:len(v.rconRequestHeader)], v.rconRequestHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *validator) isQueryRequestPacket(buf []byte) bool {
|
||||||
|
return bytes.Equal(buf[:len(v.getstatusRequestHeader)], v.getstatusRequestHeader) || bytes.Equal(buf[:len(v.getinfoRequestHeader)], v.getinfoRequestHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *validator) isValidRequestPacket(buf []byte) bool {
|
||||||
|
return v.isRconRequestPacket(buf) || v.isQueryRequestPacket(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *validator) isRconResponsePacket(buf []byte) bool {
|
||||||
|
return bytes.Equal(buf[:len(v.rconResponseHeader)], v.rconResponseHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *validator) isQueryResponsePacket(buf []byte) bool {
|
||||||
|
return bytes.Equal(buf[:len(v.getstatusResponseHeader)], v.getstatusResponseHeader) || bytes.Equal(buf[:len(v.getinfoResponseHeader)], v.getinfoResponseHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *validator) isValidResponsePacket(buf []byte) bool {
|
||||||
|
return v.isRconResponsePacket(buf) || v.isQueryResponsePacket(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *validator) isBadRconResponse(buf []byte) bool {
|
||||||
|
return bytes.Equal(buf[len(v.rconResponseHeader):len(v.rconResponseHeader)+len(v.badRconIdentifier)], v.badRconIdentifier)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user