From 05fb7bdd0f3299cce8d6b3555f065fe082d62d0a Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Sun, 14 Apr 2024 08:27:30 +0100 Subject: [PATCH] add compare helper method split up long conditionals --- pkg/udpproxy/validator.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/pkg/udpproxy/validator.go b/pkg/udpproxy/validator.go index 12eb5c9..6ef4d8c 100644 --- a/pkg/udpproxy/validator.go +++ b/pkg/udpproxy/validator.go @@ -13,24 +13,28 @@ type validator struct { } func newValidator() validator { - v := validator{ - rconRequestHeader: []byte("\xff\xff\xff\xffrcon"), - getstatusRequestHeader: []byte("\xff\xff\xff\xffgetstatus"), - getinfoRequestHeader: []byte("\xff\xff\xff\xffgetinfo"), - rconResponseHeader: []byte("\xff\xff\xff\xffprint\n"), - getstatusResponseHeader: []byte("\xff\xff\xff\xffstatusResponse\n"), - getinfoResponseHeader: []byte("\xff\xff\xff\xffinfoResponse\n"), - badRconIdentifier: []byte("Bad rcon"), - } + v := validator{} + v.rconRequestHeader = []byte("\xff\xff\xff\xffrcon") + v.getstatusRequestHeader = []byte("\xff\xff\xff\xffgetstatus") + v.getinfoRequestHeader = []byte("\xff\xff\xff\xffgetinfo") + v.rconResponseHeader = []byte("\xff\xff\xff\xffprint\n") + v.getstatusResponseHeader = []byte("\xff\xff\xff\xffstatusResponse\n") + v.getinfoResponseHeader = []byte("\xff\xff\xff\xffinfoResponse\n") + v.badRconIdentifier = []byte("Bad rcon") return v } +func (v *validator) compare(buf, header []byte) bool { + return bytes.Equal(buf[:len(header)], header) +} + func (v *validator) isRconRequestPacket(buf []byte) bool { - return bytes.Equal(buf[:len(v.rconRequestHeader)], v.rconRequestHeader) + return v.compare(buf, v.rconRequestHeader) } func (v *validator) isQueryRequestPacket(buf []byte) bool { - return bytes.Equal(buf[:len(v.getstatusRequestHeader)], v.getstatusRequestHeader) || bytes.Equal(buf[:len(v.getinfoRequestHeader)], v.getinfoRequestHeader) + return v.compare(buf, v.getstatusRequestHeader) || + v.compare(buf, v.getinfoRequestHeader) } func (v *validator) isValidRequestPacket(buf []byte) bool { @@ -38,11 +42,12 @@ func (v *validator) isValidRequestPacket(buf []byte) bool { } func (v *validator) isRconResponsePacket(buf []byte) bool { - return bytes.Equal(buf[:len(v.rconResponseHeader)], v.rconResponseHeader) + return v.compare(buf, v.rconResponseHeader) } func (v *validator) isQueryResponsePacket(buf []byte) bool { - return bytes.Equal(buf[:len(v.getstatusResponseHeader)], v.getstatusResponseHeader) || bytes.Equal(buf[:len(v.getinfoResponseHeader)], v.getinfoResponseHeader) + return v.compare(buf, v.getstatusResponseHeader) || + v.compare(buf, v.getinfoResponseHeader) } func (v *validator) isValidResponsePacket(buf []byte) bool { @@ -50,5 +55,5 @@ func (v *validator) isValidResponsePacket(buf []byte) bool { } func (v *validator) isBadRconResponse(buf []byte) bool { - return bytes.Equal(buf[len(v.rconResponseHeader):len(v.rconResponseHeader)+len(v.badRconIdentifier)], v.badRconIdentifier) + return v.compare(buf[len(v.rconResponseHeader):], v.badRconIdentifier) }