3 Commits

Author SHA1 Message Date
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
7138515904 ensure we're only logging rcon
(no query packets)
2024-03-08 21:42:39 +00:00
3 changed files with 59 additions and 9 deletions

View File

@@ -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"
) )
@@ -26,9 +28,21 @@ func start(proxy string) {
} }
var ( var (
proxies, host string proxies, host, debug 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() {

View File

@@ -15,7 +15,7 @@ type Session struct {
updateTime time.Time updateTime time.Time
} }
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
@@ -33,6 +33,14 @@ func createSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPCon
return session, nil return session, nil
} }
func (s *Session) isRconPacket(buf []byte) bool {
return string(buf[:8]) == "\xff\xff\xff\xffrcon"
}
func (s *Session) isResponsePacket(buf []byte) bool {
return string(buf[:9]) == "\xff\xff\xff\xffprint"
}
func (s *Session) listen() error { func (s *Session) listen() error {
for { for {
buf := make([]byte, 2048) buf := make([]byte, 2048)
@@ -54,6 +62,11 @@ func (s *Session) proxyFrom(buf []byte) error {
return err return err
} }
if s.isResponsePacket(buf) {
parts := strings.Split(string(buf[10:]), " ")
log.Debugf("Response: %s", strings.Join(parts, " "))
}
return nil return nil
} }
@@ -64,8 +77,11 @@ func (s *Session) proxyTo(buf []byte) error {
log.Println(err) log.Println(err)
return err return err
} }
parts := strings.Split(string(buf), " ")
log.Info("From [", s.caddr.IP, "] To [", s.serverConn.RemoteAddr().String(), "] Command: ", strings.Join(parts[2:], " ")) 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:], " "))
}
return nil return nil
} }

View File

@@ -37,8 +37,16 @@ func New(port, target string) (*Client, error) {
}, nil }, nil
} }
func (c *Client) isValidPacket(header []byte) bool { func (c *Client) isRconPacket(buf []byte) bool {
return string(header[:8]) == "\xff\xff\xff\xffrcon" || string(header[:13]) == "\xff\xff\xff\xffgetstatus" || string(header[:11]) == "\xff\xff\xff\xffgetinfo" return string(buf[:8]) == "\xff\xff\xff\xffrcon"
}
func (c *Client) isQueryPacket(buf []byte) bool {
return string(buf[:13]) == "\xff\xff\xff\xffgetstatus" || string(buf[:11]) == "\xff\xff\xff\xffgetinfo"
}
func (c *Client) isValidPacket(buf []byte) bool {
return c.isRconPacket(buf) || c.isQueryPacket(buf)
} }
func (c *Client) ListenAndServe() error { func (c *Client) ListenAndServe() error {
@@ -57,13 +65,13 @@ func (c *Client) ListenAndServe() error {
log.Println(err) log.Println(err)
} }
if !c.isValidPacket(buf[:16]) { if !c.isValidPacket(buf[:n]) {
continue 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.Println(err)
continue continue