QR2: Apply lint suggestions

This commit is contained in:
Palapeli 2026-04-06 07:07:38 -04:00
parent 0ea3d33158
commit 5258702a04
No known key found for this signature in database
GPG Key ID: 1FFE8F556A474925
7 changed files with 55 additions and 25 deletions

View File

@ -7,6 +7,9 @@ import (
"strings"
"time"
"wwfc/common"
"wwfc/logging"
"github.com/logrusorgru/aurora/v3"
)
func sendChallenge(conn net.PacketConn, addr net.UDPAddr, session Session, lookupAddr uint64) {
@ -47,11 +50,15 @@ func sendChallenge(conn net.PacketConn, addr net.UDPAddr, session Session, looku
response = append(response, 0)
go func() {
for {
conn.WriteTo(response, &addr)
for i := 0; i < 5; i++ {
_, err := conn.WriteTo(response, &addr)
time.Sleep(1 * time.Second)
if err != nil {
continue
}
mutex.Lock()
session, ok := sessions[lookupAddr]
if !ok || session.Authenticated || session.LastKeepAlive < time.Now().UTC().Unix()-60 {
@ -61,5 +68,9 @@ func sendChallenge(conn net.PacketConn, addr net.UDPAddr, session Session, looku
addr = session.Addr
mutex.Unlock()
}
logging.Info("QR2", "Failed to send challenge to", aurora.Cyan(addr.String()))
mutex.Lock()
defer mutex.Unlock()
removeSession(lookupAddr)
}()
}

View File

@ -139,6 +139,7 @@ func processResvOK(moduleName string, matchVersion int, reservation common.Match
}
func processTellAddr(moduleName string, sender *Session, destination *Session) {
common.MaybeUnused(moduleName)
if sender.groupPointer != nil && sender.groupPointer == destination.groupPointer {
// Just assume the connection is successful if TELL_ADDR is used
sender.Data["+conn_"+destination.Data["+joinindex"]] = "2"
@ -264,6 +265,8 @@ func ProcessGPStatusUpdate(profileID uint32, senderIP uint64, status string) {
}
func checkReservationAllowed(moduleName string, sender, destination *Session, joinType byte) string {
common.MaybeUnused(moduleName)
if sender.login == nil || destination.login == nil {
return ""
}
@ -602,11 +605,12 @@ func saveGroups() error {
if err != nil {
return err
}
defer func() {
common.ShouldNotError(file.Close())
}()
encoder := gob.NewEncoder(file)
err = encoder.Encode(groups)
file.Close()
return err
return encoder.Encode(groups)
}
// loadGroups loads the groups state from disk.
@ -616,10 +620,12 @@ func loadGroups() error {
if err != nil {
return err
}
defer func() {
common.ShouldNotError(file.Close())
}()
decoder := gob.NewDecoder(file)
err = decoder.Decode(&groups)
file.Close()
if err != nil {
return err
}

View File

@ -77,11 +77,12 @@ func getGroupsRaw(gameNames []string, groupNames []string) []GroupInfo {
SortedJoinIndex: []string{},
}
if group.MatchType == "0" || group.MatchType == "1" {
switch group.MatchType {
case "0", "1":
groupInfo.MatchType = "anybody"
} else if group.MatchType == "2" || group.MatchType == "3" {
case "2", "3":
groupInfo.MatchType = "private"
} else {
default:
groupInfo.MatchType = "unknown"
}

View File

@ -4,6 +4,7 @@ import (
"encoding/gob"
"os"
"strconv"
"wwfc/common"
)
type LoginInfo struct {
@ -71,11 +72,12 @@ func saveLogins() error {
if err != nil {
return err
}
defer func() {
common.ShouldNotError(file.Close())
}()
encoder := gob.NewEncoder(file)
err = encoder.Encode(logins)
file.Close()
return err
return encoder.Encode(logins)
}
// Load logins from a file. Expects the mutex to be locked, and the sessions to already be loaded.
@ -84,10 +86,12 @@ func loadLogins() error {
if err != nil {
return err
}
defer func() {
common.ShouldNotError(file.Close())
}()
decoder := gob.NewDecoder(file)
err = decoder.Decode(&logins)
file.Close()
if err != nil {
return err
}

View File

@ -92,7 +92,9 @@ func StartServer(reload bool) {
defer waitGroup.Done()
// Close the listener when the application closes.
defer conn.Close()
defer func() {
common.ShouldNotError(conn.Close())
}()
logging.Notice("QR2", "Listening on", aurora.BrightCyan(address))
for {
@ -115,7 +117,7 @@ func StartServer(reload bool) {
func Shutdown() {
inShutdown.SetTrue()
masterConn.Close()
common.ShouldNotError(masterConn.Close())
waitGroup.Wait()
mutex.Lock()
@ -179,7 +181,7 @@ func handleConnection(conn net.PacketConn, addr net.UDPAddr, buffer []byte) {
session.Authenticated = true
mutex.Unlock()
conn.WriteTo(createResponseHeader(ClientRegisteredReply, session.SessionID), &addr)
_, _ = conn.WriteTo(createResponseHeader(ClientRegisteredReply, session.SessionID), &addr)
} else {
mutex.Unlock()
}
@ -216,14 +218,14 @@ func handleConnection(conn net.PacketConn, addr net.UDPAddr, buffer []byte) {
case KeepAliveRequest:
// logging.Info(moduleName, "Command:", aurora.Yellow("KEEPALIVE"))
conn.WriteTo(createResponseHeader(KeepAliveRequest, 0), &addr)
_, _ = conn.WriteTo(createResponseHeader(KeepAliveRequest, 0), &addr)
session.LastKeepAlive = time.Now().UTC().Unix()
return
case AvailableRequest:
logging.Info("QR2", "Command:", aurora.Yellow("AVAILABLE"))
conn.WriteTo(createResponseHeader(AvailableRequest, 0), &addr)
_, _ = conn.WriteTo(createResponseHeader(AvailableRequest, 0), &addr)
return
case ClientRegisteredReply:

View File

@ -314,7 +314,8 @@ func processClientMessage(moduleName string, sender, receiver *Session, message
cmd := message[8]
common.LogMatchCommand(moduleName, destPid, cmd, matchData)
if cmd == common.MatchReservation {
switch cmd {
case common.MatchReservation:
resvError := checkReservationAllowed(moduleName, sender, receiver, matchData.Reservation.MatchType)
if resvError != "ok" {
if resvError == "restricted" || resvError == "restricted_join" {
@ -340,7 +341,8 @@ func processClientMessage(moduleName string, sender, receiver *Session, message
sender.Reservation = matchData
sender.ReservationID = receiver.SearchID
} else if cmd == common.MatchResvOK || cmd == common.MatchResvDeny || cmd == common.MatchResvWait {
case common.MatchResvOK, common.MatchResvDeny, common.MatchResvWait:
if receiver.ReservationID != sender.SearchID || receiver.Reservation.Reservation == nil {
logging.Warn(moduleName, "Destination has no reservation with the sender")
if receiver.groupPointer == nil || receiver.groupPointer != sender.groupPointer {
@ -361,7 +363,8 @@ func processClientMessage(moduleName string, sender, receiver *Session, message
} else if receiver.ReservationID == sender.SearchID {
receiver.ReservationID = 0
}
} else if cmd == common.MatchTellAddr {
case common.MatchTellAddr:
processTellAddr(moduleName, sender, receiver)
}

View File

@ -315,11 +315,12 @@ func saveSessions() error {
if err != nil {
return err
}
defer func() {
common.ShouldNotError(file.Close())
}()
encoder := gob.NewEncoder(file)
err = encoder.Encode(sessions)
file.Close()
return err
return encoder.Encode(sessions)
}
// Load the sessions from a file. Expects the mutex to be locked.
@ -328,10 +329,12 @@ func loadSessions() error {
if err != nil {
return err
}
defer func() {
common.ShouldNotError(file.Close())
}()
decoder := gob.NewDecoder(file)
err = decoder.Decode(&sessions)
file.Close()
if err != nil {
return err
}