move validator methods into Validator struct

embed Validator into Session

unexport Session
This commit is contained in:
onyx-and-iris 2024-03-26 10:47:53 +00:00
parent 5ac3f805e2
commit 0c054377ba
3 changed files with 38 additions and 32 deletions

View File

@ -9,20 +9,22 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
type Session struct { type session struct {
serverConn *net.UDPConn serverConn *net.UDPConn
proxyConn *net.UDPConn proxyConn *net.UDPConn
caddr *net.UDPAddr caddr *net.UDPAddr
updateTime time.Time 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) serverConn, err := net.DialUDP("udp", nil, raddr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
session := &Session{ session := &session{
serverConn: serverConn, serverConn: serverConn,
proxyConn: proxyConn, proxyConn: proxyConn,
caddr: caddr, caddr: caddr,
@ -34,31 +36,7 @@ func newSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPConn)
return session, nil return session, nil
} }
func (s *Session) isRconRequestPacket(buf []byte) bool { func (s *session) listen() error {
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 {
for { for {
buf := make([]byte, 2048) buf := make([]byte, 2048)
n, err := s.serverConn.Read(buf) 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) { if !s.isValidResponsePacket(buf) {
err := errors.New("not a rcon or query response packet") err := errors.New("not a rcon or query response packet")
log.Error(err.Error()) log.Error(err.Error())
@ -92,7 +70,7 @@ func (s *Session) proxyFrom(buf []byte) error {
return nil return nil
} }
func (s *Session) proxyTo(buf []byte) error { func (s *session) proxyTo(buf []byte) error {
if !s.isValidRequestPacket(buf) { if !s.isValidRequestPacket(buf) {
err := errors.New("not a rcon or query request packet") err := errors.New("not a rcon or query request packet")
log.Error(err.Error()) log.Error(err.Error())

View File

@ -15,7 +15,7 @@ type Client struct {
proxyConn *net.UDPConn proxyConn *net.UDPConn
mutex sync.RWMutex mutex sync.RWMutex
sessions map[string]*Session sessions map[string]*session
} }
func New(port, target string) (*Client, error) { func New(port, target string) (*Client, error) {
@ -33,7 +33,7 @@ func New(port, target string) (*Client, error) {
laddr: laddr, laddr: laddr,
raddr: raddr, raddr: raddr,
mutex: sync.RWMutex{}, mutex: sync.RWMutex{},
sessions: map[string]*Session{}, sessions: map[string]*session{},
}, nil }, nil
} }

28
pkg/udpproxy/validator.go Normal file
View File

@ -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)
}