diff --git a/pkg/udpproxy/session.go b/pkg/udpproxy/session.go index 143ffe6..9121c58 100644 --- a/pkg/udpproxy/session.go +++ b/pkg/udpproxy/session.go @@ -9,20 +9,22 @@ 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, @@ -34,31 +36,7 @@ func newSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPConn) return session, nil } -func (s *Session) isRconRequestPacket(buf []byte) bool { - return string(buf[:8]) == "\xff\xff\xff\xffrcon" -} - -func (s *Session) isQueryRequestPacket(buf []byte) bool { - return string(buf[:13]) == "\xff\xff\xff\xffgetstatus" || string(buf[:11]) == "\xff\xff\xff\xffgetinfo" -} - -func (s *Session) isValidRequestPacket(buf []byte) bool { - return s.isRconRequestPacket(buf) || s.isQueryRequestPacket(buf) -} - -func (s *Session) isRconResponsePacket(buf []byte) bool { - return string(buf[:9]) == "\xff\xff\xff\xffprint" -} - -func (s *Session) isQueryResponsePacket(buf []byte) bool { - return string(buf[:18]) == "\xff\xff\xff\xffstatusResponse" || string(buf[:16]) == "\xff\xff\xff\xffinfoResponse" -} - -func (s *Session) isValidResponsePacket(buf []byte) bool { - return s.isRconResponsePacket(buf) || s.isQueryResponsePacket(buf) -} - -func (s *Session) listen() error { +func (s *session) listen() error { for { buf := make([]byte, 2048) n, err := s.serverConn.Read(buf) @@ -71,7 +49,7 @@ 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()) @@ -92,7 +70,7 @@ func (s *Session) proxyFrom(buf []byte) error { 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()) diff --git a/pkg/udpproxy/udpproxy.go b/pkg/udpproxy/udpproxy.go index dadadb2..f2e66c9 100644 --- a/pkg/udpproxy/udpproxy.go +++ b/pkg/udpproxy/udpproxy.go @@ -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 } diff --git a/pkg/udpproxy/validator.go b/pkg/udpproxy/validator.go new file mode 100644 index 0000000..631a504 --- /dev/null +++ b/pkg/udpproxy/validator.go @@ -0,0 +1,28 @@ +package udpproxy + +type validator struct { +} + +func (v *validator) isRconRequestPacket(buf []byte) bool { + return string(buf[:8]) == "\xff\xff\xff\xffrcon" +} + +func (v *validator) isQueryRequestPacket(buf []byte) bool { + return string(buf[:13]) == "\xff\xff\xff\xffgetstatus" || string(buf[:11]) == "\xff\xff\xff\xffgetinfo" +} + +func (v *validator) isValidRequestPacket(buf []byte) bool { + return v.isRconRequestPacket(buf) || v.isQueryRequestPacket(buf) +} + +func (v *validator) isRconResponsePacket(buf []byte) bool { + return string(buf[:9]) == "\xff\xff\xff\xffprint" +} + +func (v *validator) isQueryResponsePacket(buf []byte) bool { + return string(buf[:18]) == "\xff\xff\xff\xffstatusResponse" || string(buf[:16]) == "\xff\xff\xff\xffinfoResponse" +} + +func (v *validator) isValidResponsePacket(buf []byte) bool { + return v.isRconResponsePacket(buf) || v.isQueryResponsePacket(buf) +}