fix(sdkserver): enable corsWrapper and support raw/wrapped JSON payloads in /srtools endpoint

This commit is contained in:
CAtlas0784 committed 2026-10-17 03:36:10 +07:00
1 parent 31a9779029
commit 7aa3974233
1 file changed
+14 -7
+14 -7
View File
@@ -318,19 +318,20 @@ func srToolsSave(w http.ResponseWriter, r *http.Request) {
Data json.RawMessage `json:"data"`
}
body, err := io.ReadAll(r.Body)
if err == nil && len(body) > 0 {
_ = json.Unmarshal(body, &payload)
if err != nil || len(body) == 0 {
writeJSON(w, map[string]any{"message": "empty body", "status": 400})
return
}
if len(payload.Data) == 0 {
writeJSON(w, map[string]any{"message": "OK", "status": 200})
return
toWrite := body
if err := json.Unmarshal(body, &payload); err == nil && len(payload.Data) > 0 {
toWrite = payload.Data
}
// pretty-print and store; freesr-data.json uses the same layout the
// gameserver parses, so pass through unchanged
var pretty map[string]any
if err := json.Unmarshal(payload.Data, &pretty); err != nil {
if err := json.Unmarshal(toWrite, &pretty); err != nil {
writeJSON(w, map[string]any{"message": "malformed json: " + err.Error(), "status": 500})
return
}
@@ -339,6 +340,12 @@ func srToolsSave(w http.ResponseWriter, r *http.Request) {
writeJSON(w, map[string]any{"message": "failed to write freesr-data.json: " + err.Error(), "status": 500})
return
}
// Reload and sync persistent
if d, err := common.LoadFreesrData(); err == nil {
d.SavePersistent()
}
writeJSON(w, map[string]any{"message": "OK", "status": 200})
}
@@ -360,7 +367,7 @@ func Listen() error {
mux.HandleFunc("/account/risky/api/check", func(w http.ResponseWriter, r *http.Request) { riskyCheckReply(w) })
mux.HandleFunc("/account/ma-cn-passport/app/loginByPassword", func(w http.ResponseWriter, r *http.Request) { apnLoginReply(w) })
mux.HandleFunc("/account/ma-cn-session/app/verify", func(w http.ResponseWriter, r *http.Request) { apnVerifyReply(w) })
mux.HandleFunc("/srtools", srToolsSave)
mux.HandleFunc("/srtools", corsWrapper(srToolsSave))
registerAPIRoutes(mux)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" || r.URL.Path == "/dashboard" {