add compare helper method

split up long conditionals
This commit is contained in:
onyx-and-iris 2024-04-14 08:27:30 +01:00
parent e8f213fded
commit 05fb7bdd0f

View File

@ -13,24 +13,28 @@ type validator struct {
} }
func newValidator() validator { func newValidator() validator {
v := validator{ v := validator{}
rconRequestHeader: []byte("\xff\xff\xff\xffrcon"), v.rconRequestHeader = []byte("\xff\xff\xff\xffrcon")
getstatusRequestHeader: []byte("\xff\xff\xff\xffgetstatus"), v.getstatusRequestHeader = []byte("\xff\xff\xff\xffgetstatus")
getinfoRequestHeader: []byte("\xff\xff\xff\xffgetinfo"), v.getinfoRequestHeader = []byte("\xff\xff\xff\xffgetinfo")
rconResponseHeader: []byte("\xff\xff\xff\xffprint\n"), v.rconResponseHeader = []byte("\xff\xff\xff\xffprint\n")
getstatusResponseHeader: []byte("\xff\xff\xff\xffstatusResponse\n"), v.getstatusResponseHeader = []byte("\xff\xff\xff\xffstatusResponse\n")
getinfoResponseHeader: []byte("\xff\xff\xff\xffinfoResponse\n"), v.getinfoResponseHeader = []byte("\xff\xff\xff\xffinfoResponse\n")
badRconIdentifier: []byte("Bad rcon"), v.badRconIdentifier = []byte("Bad rcon")
}
return v return v
} }
func (v *validator) compare(buf, header []byte) bool {
return bytes.Equal(buf[:len(header)], header)
}
func (v *validator) isRconRequestPacket(buf []byte) bool { 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 { 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 { 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 { 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 { 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 { 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 { 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)
} }