diff --git a/serverbrowser/filter.go b/serverbrowser/filter.go index 90ac9c2..6502c51 100644 --- a/serverbrowser/filter.go +++ b/serverbrowser/filter.go @@ -15,7 +15,7 @@ import ( // Example: dwc_mver = 90 and dwc_pid != 43 and maxplayers = 11 and numplayers < 11 and dwc_mtype = 0 and dwc_hoststate = 2 and dwc_suspend = 0 and (rk = 'vs' and ev >= 4250 and ev <= 5750 and p = 0) -func filterServers(moduleName string, servers []map[string]string, queryGame string, expression string, publicIP string) []map[string]string { +func filterServers(moduleName string, servers []map[string]string, queryGame string, expression string) []map[string]string { // Matchmaking search tree, err := filter.Parse(expression) if err != nil { diff --git a/serverbrowser/filter_test.go b/serverbrowser/filter_test.go index b0d2dc9..a3564c9 100644 --- a/serverbrowser/filter_test.go +++ b/serverbrowser/filter_test.go @@ -6,30 +6,27 @@ import ( "wwfc/filter" ) -func parseFilter(t *testing.T, expression string) error { +func parseFilter(t *testing.T, expression string) { _, err := filter.Parse(expression) if err != nil { t.Error(err) } - return err } -func evalFilter(t *testing.T, expression string, queryGame string, context map[string]string) (int64, error) { +func evalFilter(t *testing.T, expression string, queryGame string, context map[string]string) { tree, err := filter.Parse(expression) if err != nil { t.Error(err) - return 0, err + return } ret, err := filter.Eval(tree, context, queryGame) if err != nil { t.Error(err) - return 0, err + return } fmt.Printf("ret: %d\n", ret) - - return ret, err } func TestParseFilter(t *testing.T) { diff --git a/serverbrowser/main.go b/serverbrowser/main.go index 8dbb4aa..f667be7 100644 --- a/serverbrowser/main.go +++ b/serverbrowser/main.go @@ -43,18 +43,13 @@ func StartServer(reload bool) { // Load connection state file, err := os.Open("state/sb_connections.gob") - if err != nil { - panic(err) - } + common.ShouldNotError(err) + defer func() { + common.ShouldNotError(file.Close()) + }() decoder := gob.NewDecoder(file) - - err = decoder.Decode(&connBuffers) - file.Close() - - if err != nil { - panic(err) - } + common.ShouldNotError(decoder.Decode(&connBuffers)) logging.Notice("SB", "Loaded", aurora.Cyan(len(connBuffers)), "connections") } @@ -62,17 +57,13 @@ func StartServer(reload bool) { func Shutdown() { // Save connection state file, err := os.OpenFile("state/sb_connections.gob", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) - if err != nil { - panic(err) - } + common.ShouldNotError(err) + defer func() { + common.ShouldNotError(file.Close()) + }() encoder := gob.NewEncoder(file) - - err = encoder.Encode(connBuffers) - file.Close() - if err != nil { - panic(err) - } + common.ShouldNotError(encoder.Encode(connBuffers)) logging.Notice("SB", "Saved", aurora.Cyan(len(connBuffers)), "connections") } @@ -108,7 +99,7 @@ func HandlePacket(index uint64, data []byte, address string) { if len(*buffer)+len(data) > 0x1000 { logging.Error(moduleName, "Buffer overflow") - common.CloseConnection(ServerName, index) + _ = common.CloseConnection(ServerName, index) buffer = nil return } @@ -124,7 +115,7 @@ func HandlePacket(index uint64, data []byte, address string) { packetSize := binary.BigEndian.Uint16((*buffer)[:2]) if packetSize < 3 || packetSize > 0x1000 { logging.Error(moduleName, "Invalid packet size - terminating") - common.CloseConnection(ServerName, index) + _ = common.CloseConnection(ServerName, index) buffer = nil return } diff --git a/serverbrowser/server.go b/serverbrowser/server.go index 931c7fb..c8955ec 100644 --- a/serverbrowser/server.go +++ b/serverbrowser/server.go @@ -41,12 +41,12 @@ const ( ) var ( - IndexOutOfBoundsError = errors.New("index is out of bounds") + ErrIndexOutOfBoundsError = errors.New("index is out of bounds") ) func popString(buffer []byte, index int) (string, int, error) { if index < 0 || index >= len(buffer) { - return "", 0, IndexOutOfBoundsError + return "", 0, ErrIndexOutOfBoundsError } str, err := common.GetString(buffer[index:]) @@ -62,10 +62,10 @@ func popBytes(buffer []byte, index int, size int) ([]byte, int, error) { bufferLen := len(buffer) if index < 0 || index >= bufferLen { - return nil, 0, IndexOutOfBoundsError + return nil, 0, ErrIndexOutOfBoundsError } if size < 0 || index+size > bufferLen { - return nil, 0, IndexOutOfBoundsError + return nil, 0, ErrIndexOutOfBoundsError } return buffer[index : index+size], index + size, nil @@ -73,7 +73,7 @@ func popBytes(buffer []byte, index int, size int) ([]byte, int, error) { func popUint32(buffer []byte, index int) (uint32, int, error) { if index < 0 || index+4 > len(buffer) { - return 0, 0, IndexOutOfBoundsError + return 0, 0, ErrIndexOutOfBoundsError } return binary.BigEndian.Uint32(buffer[index:]), index + 4, nil @@ -113,7 +113,7 @@ func handleServerListRequest(moduleName string, connIndex uint64, address string return } - options, index, err := popUint32(buffer, index) + options, _, err := popUint32(buffer, index) if err != nil { logging.Error(moduleName, "Invalid options") return @@ -178,7 +178,7 @@ func handleServerListRequest(moduleName string, connIndex uint64, address string // Self lookup is handled differently servers = filterSelfLookup(moduleName, qr2.GetSessionServers(), queryGame, match[1], callerPublicIP) } else { - servers = filterServers(moduleName, qr2.GetSessionServers(), queryGame, filter, callerPublicIP) + servers = filterServers(moduleName, qr2.GetSessionServers(), queryGame, filter) } } @@ -247,7 +247,8 @@ func handleServerListRequest(moduleName string, connIndex uint64, address string err = nil for _, s := range ipSplit { - val, err := strconv.ParseUint(s, 10, 8) + var val uint64 + val, err = strconv.ParseUint(s, 10, 8) if err != nil { break } @@ -328,10 +329,13 @@ func handleServerListRequest(moduleName string, connIndex uint64, address string } // Write the encrypted reply - common.SendPacket(ServerName, connIndex, common.EncryptTypeX([]byte(gameInfo.SecretKey), challenge, output)) + if err := common.SendPacket(ServerName, connIndex, common.EncryptTypeX([]byte(gameInfo.SecretKey), challenge, output)); err != nil { + logging.Error(moduleName, "Failed to send packet:", err) + } } func handleSendMessageRequest(moduleName string, connIndex uint64, address string, buffer []byte) { + common.MaybeUnused(connIndex) // Read search ID from buffer searchID := uint64(binary.BigEndian.Uint32(buffer[3:7])) searchID |= uint64(binary.BigEndian.Uint16(buffer[7:9])) << 32