Files
elmprodvpn/selective-vpn-api/app/transport_singbox_profiles_card_delete.go

64 lines
1.6 KiB
Go

package app
import (
"net/http"
"strings"
)
func handleTransportSingBoxProfileCardDelete(w http.ResponseWriter, r *http.Request, id string) {
baseRevision, err := parseOptionalInt64(strings.TrimSpace(r.URL.Query().Get("base_revision")))
if err != nil {
writeJSON(w, http.StatusBadRequest, SingBoxProfilesResponse{
OK: false,
Code: singBoxProfileCodeBadBaseRevision,
Message: "invalid base_revision",
})
return
}
singBoxProfilesMu.Lock()
defer singBoxProfilesMu.Unlock()
st := loadSingBoxProfilesState()
idx := findSingBoxProfileIndex(st.Items, id)
if idx < 0 {
writeJSON(w, http.StatusNotFound, SingBoxProfilesResponse{
OK: false,
Code: singBoxProfileCodeNotFound,
Message: "not found",
})
return
}
cur := st.Items[idx]
if baseRevision > 0 && baseRevision != cur.ProfileRevision {
writeJSON(w, http.StatusConflict, SingBoxProfilesResponse{
OK: false,
Code: singBoxProfileCodeRevisionMismatch,
Message: "base_revision mismatch",
Item: &cur,
})
return
}
st.Items = append(st.Items[:idx], st.Items[idx+1:]...)
ensureSingBoxProfilesActiveID(&st)
st.Revision++
if err := saveSingBoxProfilesState(st); err != nil {
writeJSON(w, http.StatusInternalServerError, SingBoxProfilesResponse{
OK: false,
Code: singBoxProfileCodeSaveFailed,
Message: "save failed: " + err.Error(),
})
return
}
_ = writeSingBoxSecrets(id, nil)
events.push("singbox_profile_saved", map[string]any{
"id": id,
"deleted": true,
})
writeJSON(w, http.StatusOK, SingBoxProfilesResponse{
OK: true,
Message: "deleted",
ActiveProfileID: st.ActiveProfileID,
})
}