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 | |
|---|---|---|---|
| 6cd90ae1dd | |||
| 826756eb6e | |||
| 05fb7bdd0f | |||
| e8f213fded | |||
| 3a8758a4d7 | |||
| 2c8ae43303 | |||
| 0935fc1190 | |||
| 3e039824de | |||
| 0c054377ba | |||
| 5ac3f805e2 | |||
| ab548d669a | |||
| 107f914d3b | |||
| 0fdf9d10c3 | |||
| 690fabddaf | |||
| f6f0044a84 | |||
| 725e6cfb3d | |||
| ca33a6a390 |
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]
|
||||
|
||||
## [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
|
||||
|
||||
@@ -22,7 +22,7 @@ Avoid sending plaintext rcon commands to the public game server port. In general
|
||||
|
||||
### 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
|
||||
|
||||
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
|
||||
@@ -9,24 +9,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 newSession(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()
|
||||
@@ -34,25 +37,9 @@ func newSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPConn)
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (s *Session) isRconPacket(buf []byte) bool {
|
||||
return string(buf[:8]) == "\xff\xff\xff\xffrcon"
|
||||
}
|
||||
|
||||
func (s *Session) isQueryPacket(buf []byte) bool {
|
||||
return string(buf[:13]) == "\xff\xff\xff\xffgetstatus" || string(buf[:11]) == "\xff\xff\xff\xffgetinfo"
|
||||
}
|
||||
|
||||
func (s *Session) isValidPacket(buf []byte) bool {
|
||||
return s.isRconPacket(buf) || s.isQueryPacket(buf)
|
||||
}
|
||||
|
||||
func (s *Session) isResponsePacket(buf []byte) bool {
|
||||
return string(buf[:9]) == "\xff\xff\xff\xffprint"
|
||||
}
|
||||
|
||||
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.Error(err)
|
||||
@@ -63,9 +50,11 @@ func (s *Session) listen() error {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) proxyFrom(buf []byte) error {
|
||||
if !s.isResponsePacket(buf) {
|
||||
return nil
|
||||
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()
|
||||
@@ -75,17 +64,20 @@ func (s *Session) proxyFrom(buf []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if log.GetLevel() == log.DebugLevel {
|
||||
parts := strings.Split(string(buf[10:]), " ")
|
||||
log.Debugf("Response: %s", strings.Join(parts, " "))
|
||||
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
|
||||
}
|
||||
|
||||
func (s *Session) proxyTo(buf []byte) error {
|
||||
if !s.isValidPacket(buf) {
|
||||
err := errors.New("not a rcon or query packet")
|
||||
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
|
||||
}
|
||||
@@ -97,9 +89,9 @@ func (s *Session) proxyTo(buf []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if s.isRconPacket(buf) {
|
||||
parts := strings.Split(string(buf), " ")
|
||||
log.Infof("From [%s] To [%s] Command: %s", s.caddr.IP.String(), s.serverConn.RemoteAddr().String(), 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
|
||||
|
||||
@@ -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,7 +33,7 @@ func New(port, target string) (*Client, error) {
|
||||
laddr: laddr,
|
||||
raddr: raddr,
|
||||
mutex: sync.RWMutex{},
|
||||
sessions: map[string]*Session{},
|
||||
sessions: map[string]*session{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -46,8 +46,8 @@ 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.Error(err)
|
||||
|
||||
59
pkg/udpproxy/validator.go
Normal file
59
pkg/udpproxy/validator.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package udpproxy
|
||||
|
||||
import "bytes"
|
||||
|
||||
type validator struct {
|
||||
rconRequestHeader []byte
|
||||
getstatusRequestHeader []byte
|
||||
getinfoRequestHeader []byte
|
||||
rconResponseHeader []byte
|
||||
getstatusResponseHeader []byte
|
||||
getinfoResponseHeader []byte
|
||||
badRconIdentifier []byte
|
||||
}
|
||||
|
||||
func newValidator() validator {
|
||||
v := validator{}
|
||||
v.rconRequestHeader = []byte("\xff\xff\xff\xffrcon")
|
||||
v.getstatusRequestHeader = []byte("\xff\xff\xff\xffgetstatus")
|
||||
v.getinfoRequestHeader = []byte("\xff\xff\xff\xffgetinfo")
|
||||
v.rconResponseHeader = []byte("\xff\xff\xff\xffprint\n")
|
||||
v.getstatusResponseHeader = []byte("\xff\xff\xff\xffstatusResponse\n")
|
||||
v.getinfoResponseHeader = []byte("\xff\xff\xff\xffinfoResponse\n")
|
||||
v.badRconIdentifier = []byte("Bad rcon")
|
||||
return v
|
||||
}
|
||||
|
||||
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) 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)
|
||||
}
|
||||
Reference in New Issue
Block a user