diff --git a/api/account.go b/api/account.go index 768c6d6..790bea8 100644 --- a/api/account.go +++ b/api/account.go @@ -35,13 +35,13 @@ type AccountInfoResponse struct { func (s *Server) handleAccountInfo(w http.ResponseWriter, r *http.Request) { username, err := getUsernameFromRequest(r) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + httpError(w, r, err.Error(), http.StatusBadRequest) return } uuid, err := getUuidFromRequest(r) // lazy if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + httpError(w, r, err.Error(), http.StatusBadRequest) return } @@ -66,7 +66,7 @@ func (s *Server) handleAccountInfo(w http.ResponseWriter, r *http.Request) { response, err := json.Marshal(AccountInfoResponse{Username: username, LastSessionSlot: latestSaveId}) if err != nil { - http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) return } @@ -80,17 +80,17 @@ func (s *Server) handleAccountRegister(w http.ResponseWriter, r *http.Request) { var request AccountRegisterRequest err := json.NewDecoder(r.Body).Decode(&request) if err != nil { - http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) return } if !isValidUsername(request.Username) { - http.Error(w, "invalid username", http.StatusBadRequest) + httpError(w, r, "invalid username", http.StatusBadRequest) return } if len(request.Password) < 6 { - http.Error(w, "invalid password", http.StatusBadRequest) + httpError(w, r, "invalid password", http.StatusBadRequest) return } @@ -98,7 +98,7 @@ func (s *Server) handleAccountRegister(w http.ResponseWriter, r *http.Request) { _, err = rand.Read(uuid) if err != nil { - http.Error(w, fmt.Sprintf("failed to generate uuid: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to generate uuid: %s", err), http.StatusInternalServerError) return } @@ -106,13 +106,13 @@ func (s *Server) handleAccountRegister(w http.ResponseWriter, r *http.Request) { _, err = rand.Read(salt) if err != nil { - http.Error(w, fmt.Sprintf("failed to generate salt: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to generate salt: %s", err), http.StatusInternalServerError) return } err = db.AddAccountRecord(uuid, request.Username, argon2.IDKey([]byte(request.Password), salt, argonTime, argonMemory, argonThreads, argonKeyLength), salt) if err != nil { - http.Error(w, fmt.Sprintf("failed to add account record: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to add account record: %s", err), http.StatusInternalServerError) return } @@ -127,33 +127,33 @@ func (s *Server) handleAccountLogin(w http.ResponseWriter, r *http.Request) { var request AccountLoginRequest err := json.NewDecoder(r.Body).Decode(&request) if err != nil { - http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) return } if !isValidUsername(request.Username) { - http.Error(w, "invalid username", http.StatusBadRequest) + httpError(w, r, "invalid username", http.StatusBadRequest) return } if len(request.Password) < 6 { - http.Error(w, "invalid password", http.StatusBadRequest) + httpError(w, r, "invalid password", http.StatusBadRequest) return } key, salt, err := db.FetchAccountKeySaltFromUsername(request.Username) if err != nil { if err == sql.ErrNoRows { - http.Error(w, "account doesn't exist", http.StatusBadRequest) + httpError(w, r, "account doesn't exist", http.StatusBadRequest) return } - http.Error(w, err.Error(), http.StatusInternalServerError) + httpError(w, r, err.Error(), http.StatusInternalServerError) return } if !bytes.Equal(key, argon2.IDKey([]byte(request.Password), salt, argonTime, argonMemory, argonThreads, argonKeyLength)) { - http.Error(w, "password doesn't match", http.StatusBadRequest) + httpError(w, r, "password doesn't match", http.StatusBadRequest) return } @@ -161,19 +161,19 @@ func (s *Server) handleAccountLogin(w http.ResponseWriter, r *http.Request) { _, err = rand.Read(token) if err != nil { - http.Error(w, fmt.Sprintf("failed to generate token: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to generate token: %s", err), http.StatusInternalServerError) return } err = db.AddAccountSession(request.Username, token) if err != nil { - http.Error(w, "failed to add account session", http.StatusInternalServerError) + httpError(w, r, "failed to add account session", http.StatusInternalServerError) return } response, err := json.Marshal(AccountLoginResponse{Token: base64.StdEncoding.EncodeToString(token)}) if err != nil { - http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) return } @@ -184,23 +184,23 @@ func (s *Server) handleAccountLogin(w http.ResponseWriter, r *http.Request) { func (s *Server) handleAccountLogout(w http.ResponseWriter, r *http.Request) { token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization")) if err != nil { - http.Error(w, fmt.Sprintf("failed to decode token: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to decode token: %s", err), http.StatusBadRequest) return } if len(token) != 32 { - http.Error(w, "invalid token", http.StatusBadRequest) + httpError(w, r, "invalid token", http.StatusBadRequest) return } err = db.RemoveSessionFromToken(token) if err != nil { if err == sql.ErrNoRows { - http.Error(w, "token not found", http.StatusBadRequest) + httpError(w, r, "token not found", http.StatusBadRequest) return } - http.Error(w, "failed to remove account session", http.StatusInternalServerError) + httpError(w, r, "failed to remove account session", http.StatusInternalServerError) return } diff --git a/api/daily.go b/api/daily.go index 70a79de..c65fc20 100644 --- a/api/daily.go +++ b/api/daily.go @@ -81,7 +81,7 @@ func (s *Server) handleSeed(w http.ResponseWriter) { func (s *Server) handleRankings(w http.ResponseWriter, r *http.Request) { uuid, err := getUuidFromRequest(r) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + httpError(w, r, err.Error(), http.StatusBadRequest) return } @@ -94,7 +94,7 @@ func (s *Server) handleRankings(w http.ResponseWriter, r *http.Request) { if r.URL.Query().Has("category") { category, err = strconv.Atoi(r.URL.Query().Get("category")) if err != nil { - http.Error(w, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest) return } } @@ -103,7 +103,7 @@ func (s *Server) handleRankings(w http.ResponseWriter, r *http.Request) { if r.URL.Query().Has("page") { page, err = strconv.Atoi(r.URL.Query().Get("page")) if err != nil { - http.Error(w, fmt.Sprintf("failed to convert page: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to convert page: %s", err), http.StatusBadRequest) return } } @@ -115,7 +115,7 @@ func (s *Server) handleRankings(w http.ResponseWriter, r *http.Request) { response, err := json.Marshal(rankings) if err != nil { - http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) return } @@ -130,7 +130,7 @@ func (s *Server) handleRankingPageCount(w http.ResponseWriter, r *http.Request) if r.URL.Query().Has("category") { category, err = strconv.Atoi(r.URL.Query().Get("category")) if err != nil { - http.Error(w, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest) return } } @@ -142,7 +142,7 @@ func (s *Server) handleRankingPageCount(w http.ResponseWriter, r *http.Request) response, err := json.Marshal(pageCount) if err != nil { - http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) return } diff --git a/api/error.go b/api/error.go new file mode 100644 index 0000000..2fe0c42 --- /dev/null +++ b/api/error.go @@ -0,0 +1,11 @@ +package api + +import ( + "log" + "net/http" +) + +func httpError(w http.ResponseWriter, r *http.Request, error string, code int) { + log.Printf("%s: %s %d\n", r.URL.Path, error, code) + http.Error(w, error, code) +} diff --git a/api/game.go b/api/game.go index f3e1b54..c288346 100644 --- a/api/game.go +++ b/api/game.go @@ -41,10 +41,10 @@ func updateStats() { } // /game/playercount - get player count -func (s *Server) handlePlayerCountGet(w http.ResponseWriter) { +func (s *Server) handlePlayerCountGet(w http.ResponseWriter, r *http.Request) { response, err := json.Marshal(playerCount) if err != nil { - http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) return } @@ -52,14 +52,14 @@ func (s *Server) handlePlayerCountGet(w http.ResponseWriter) { } // /game/titlestats - get title stats -func (s *Server) handleTitleStatsGet(w http.ResponseWriter) { +func (s *Server) handleTitleStatsGet(w http.ResponseWriter, r *http.Request) { titleStats := &defs.TitleStats{ PlayerCount: playerCount, BattleCount: battleCount, } response, err := json.Marshal(titleStats) if err != nil { - http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) return } @@ -67,10 +67,10 @@ func (s *Server) handleTitleStatsGet(w http.ResponseWriter) { } // /game/classicsessioncount - get classic session count -func (s *Server) handleClassicSessionCountGet(w http.ResponseWriter) { +func (s *Server) handleClassicSessionCountGet(w http.ResponseWriter, r *http.Request) { response, err := json.Marshal(classicSessionCount) if err != nil { - http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) return } diff --git a/api/generic.go b/api/generic.go index fec36a9..7235c10 100644 --- a/api/generic.go +++ b/api/generic.go @@ -35,11 +35,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.handleAccountLogout(w, r) case "/game/playercount": - s.handlePlayerCountGet(w) + s.handlePlayerCountGet(w, r) case "/game/titlestats": - s.handleTitleStatsGet(w) + s.handleTitleStatsGet(w, r) case "/game/classicsessioncount": - s.handleClassicSessionCountGet(w) + s.handleClassicSessionCountGet(w, r) case "/savedata/get": s.handleSavedataGet(w, r) diff --git a/api/savedata.go b/api/savedata.go index 4dadee0..260a744 100644 --- a/api/savedata.go +++ b/api/savedata.go @@ -22,7 +22,7 @@ const sessionSlotCount = 3 func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) { uuid, err := getUuidFromRequest(r) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + httpError(w, r, err.Error(), http.StatusBadRequest) return } @@ -30,13 +30,13 @@ func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) { case "0": // System system, err := readSystemSaveData(uuid) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + httpError(w, r, err.Error(), http.StatusInternalServerError) return } saveJson, err := json.Marshal(system) if err != nil { - http.Error(w, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError) return } @@ -44,30 +44,30 @@ func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) { case "1": // Session slotId, err := strconv.Atoi(r.URL.Query().Get("slot")) if err != nil { - http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) return } if slotId < 0 || slotId >= sessionSlotCount { - http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) return } session, err := readSessionSaveData(uuid, slotId) if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + httpError(w, r, err.Error(), http.StatusInternalServerError) return } saveJson, err := json.Marshal(session) if err != nil { - http.Error(w, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to marshal save to json: %s", err), http.StatusInternalServerError) return } w.Write(saveJson) default: - http.Error(w, "invalid data type", http.StatusBadRequest) + httpError(w, r, "invalid data type", http.StatusBadRequest) return } } @@ -76,7 +76,7 @@ func (s *Server) handleSavedataGet(w http.ResponseWriter, r *http.Request) { func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) { uuid, err := getUuidFromRequest(r) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + httpError(w, r, err.Error(), http.StatusBadRequest) return } @@ -92,31 +92,31 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) { var system defs.SystemSaveData err = json.NewDecoder(r.Body).Decode(&system) if err != nil { - http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) return } if system.TrainerId == 0 && system.SecretId == 0 { - http.Error(w, "invalid system data", http.StatusInternalServerError) + httpError(w, r, "invalid system data", http.StatusInternalServerError) return } err = db.UpdateAccountStats(uuid, system.GameStats) if err != nil { - http.Error(w, fmt.Sprintf("failed to update account stats: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to update account stats: %s", err), http.StatusBadRequest) return } var gobBuffer bytes.Buffer err = gob.NewEncoder(&gobBuffer).Encode(system) if err != nil { - http.Error(w, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError) return } zstdWriter, err := zstd.NewWriter(nil) if err != nil { - http.Error(w, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError) return } @@ -124,24 +124,24 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) { err = os.MkdirAll("userdata/"+hexUuid, 0755) if err != nil && !os.IsExist(err) { - http.Error(w, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError) return } err = os.WriteFile("userdata/"+hexUuid+"/system.pzs", compressed, 0644) if err != nil { - http.Error(w, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError) return } case "1": // Session slotId, err := strconv.Atoi(r.URL.Query().Get("slot")) if err != nil { - http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) return } if slotId < 0 || slotId >= sessionSlotCount { - http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) return } @@ -153,20 +153,20 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) { var session defs.SessionSaveData err = json.NewDecoder(r.Body).Decode(&session) if err != nil { - http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) return } var gobBuffer bytes.Buffer err = gob.NewEncoder(&gobBuffer).Encode(session) if err != nil { - http.Error(w, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to serialize save: %s", err), http.StatusInternalServerError) return } zstdWriter, err := zstd.NewWriter(nil) if err != nil { - http.Error(w, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to create zstd writer, %s", err), http.StatusInternalServerError) return } @@ -174,17 +174,17 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) { err = os.MkdirAll("userdata/"+hexUuid, 0755) if err != nil && !os.IsExist(err) { - http.Error(w, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to create userdata folder: %s", err), http.StatusInternalServerError) return } err = os.WriteFile(fmt.Sprintf("userdata/%s/%s.pzs", hexUuid, fileName), compressed, 0644) if err != nil { - http.Error(w, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to write save file: %s", err), http.StatusInternalServerError) return } default: - http.Error(w, "invalid data type", http.StatusBadRequest) + httpError(w, r, "invalid data type", http.StatusBadRequest) return } @@ -195,7 +195,7 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) { func (s *Server) handleSavedataDelete(w http.ResponseWriter, r *http.Request) { uuid, err := getUuidFromRequest(r) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + httpError(w, r, err.Error(), http.StatusBadRequest) return } @@ -210,18 +210,18 @@ func (s *Server) handleSavedataDelete(w http.ResponseWriter, r *http.Request) { case "0": // System err := os.Remove("userdata/" + hexUuid + "/system.pzs") if err != nil && !os.IsNotExist(err) { - http.Error(w, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError) return } case "1": // Session slotId, err := strconv.Atoi(r.URL.Query().Get("slot")) if err != nil { - http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) return } if slotId < 0 || slotId >= sessionSlotCount { - http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) return } @@ -232,11 +232,11 @@ func (s *Server) handleSavedataDelete(w http.ResponseWriter, r *http.Request) { err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hexUuid, fileName)) if err != nil && !os.IsNotExist(err) { - http.Error(w, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError) return } default: - http.Error(w, "invalid data type", http.StatusBadRequest) + httpError(w, r, "invalid data type", http.StatusBadRequest) return } @@ -251,7 +251,7 @@ type SavedataClearResponse struct { func (s *Server) handleSavedataClear(w http.ResponseWriter, r *http.Request) { uuid, err := getUuidFromRequest(r) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + httpError(w, r, err.Error(), http.StatusBadRequest) return } @@ -262,19 +262,19 @@ func (s *Server) handleSavedataClear(w http.ResponseWriter, r *http.Request) { slotId, err := strconv.Atoi(r.URL.Query().Get("slot")) if err != nil { - http.Error(w, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to convert slot id: %s", err), http.StatusBadRequest) return } if slotId < 0 || slotId >= sessionSlotCount { - http.Error(w, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("slot id %d out of range", slotId), http.StatusBadRequest) return } var session defs.SessionSaveData err = json.NewDecoder(r.Body).Decode(&session) if err != nil { - http.Error(w, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) + httpError(w, r, fmt.Sprintf("failed to decode request body: %s", err), http.StatusBadRequest) return } @@ -301,7 +301,7 @@ func (s *Server) handleSavedataClear(w http.ResponseWriter, r *http.Request) { response, err := json.Marshal(SavedataClearResponse{Success: newCompletion}) if err != nil { - http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError) return } @@ -312,7 +312,7 @@ func (s *Server) handleSavedataClear(w http.ResponseWriter, r *http.Request) { err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", hex.EncodeToString(uuid), fileName)) if err != nil && !os.IsNotExist(err) { - http.Error(w, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError) + httpError(w, r, fmt.Sprintf("failed to delete save file: %s", err), http.StatusInternalServerError) return }