add isRconPacket(), isQueryPacket() and isResponsePacket() methods

response packets now logged at Debug level
This commit is contained in:
onyx-and-iris 2024-03-20 17:05:02 +00:00
parent 94a683fb3f
commit f2fd9354f0
2 changed files with 29 additions and 9 deletions

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
} }
@ -65,10 +78,9 @@ func (s *Session) proxyTo(buf []byte) error {
return err return err
} }
cmd := string(buf) if s.isRconPacket(buf) {
if cmd[:8] == "\xff\xff\xff\xffrcon" { parts := strings.Split(string(buf), " ")
parts := strings.Split(cmd, " ") log.Infof("From [%s] To [%s] Command: %s", s.caddr.IP.String(), s.serverConn.RemoteAddr().String(), strings.Join(parts[2:], " "))
log.Info("From [", s.caddr.IP, "] To [", s.serverConn.RemoteAddr().String(), "] Command: ", 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