diff --git a/pkg/udpproxy/session.go b/pkg/udpproxy/session.go index 9e5967c..4c804bb 100644 --- a/pkg/udpproxy/session.go +++ b/pkg/udpproxy/session.go @@ -15,7 +15,7 @@ type Session struct { 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) if err != nil { return nil, err @@ -33,6 +33,14 @@ func createSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPCon 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 { for { buf := make([]byte, 2048) @@ -54,6 +62,11 @@ func (s *Session) proxyFrom(buf []byte) error { return err } + if s.isResponsePacket(buf) { + parts := strings.Split(string(buf[10:]), " ") + log.Debugf("Response: %s", strings.Join(parts, " ")) + } + return nil } @@ -65,10 +78,9 @@ func (s *Session) proxyTo(buf []byte) error { 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.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 diff --git a/pkg/udpproxy/udpproxy.go b/pkg/udpproxy/udpproxy.go index 15a5df5..24e5220 100644 --- a/pkg/udpproxy/udpproxy.go +++ b/pkg/udpproxy/udpproxy.go @@ -37,8 +37,16 @@ func New(port, target string) (*Client, error) { }, 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) isRconPacket(buf []byte) bool { + 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 { @@ -57,13 +65,13 @@ func (c *Client) ListenAndServe() error { log.Println(err) } - if !c.isValidPacket(buf[:16]) { + if !c.isValidPacket(buf[:n]) { continue } session, found := c.sessions[caddr.String()] if !found { - session, err = createSession(caddr, c.raddr, c.proxyConn) + session, err = newSession(caddr, c.raddr, c.proxyConn) if err != nil { log.Println(err) continue