mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2025-01-18 00:30:54 +00:00
add isRconPacket(), isQueryPacket() and isResponsePacket() methods
response packets now logged at Debug level
This commit is contained in:
parent
94a683fb3f
commit
f2fd9354f0
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user