42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func handleTransportSingBoxProfileRollback(w http.ResponseWriter, r *http.Request, id string) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
body, restart, err := decodeTransportSingBoxProfileRollbackRequest(r)
|
|
if err != nil {
|
|
http.Error(w, "bad json", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
singBoxProfilesMu.Lock()
|
|
defer singBoxProfilesMu.Unlock()
|
|
|
|
status, resp := executeTransportSingBoxProfileRollbackLocked(id, body, restart)
|
|
writeJSON(w, status, resp)
|
|
}
|
|
|
|
func decodeTransportSingBoxProfileRollbackRequest(r *http.Request) (SingBoxProfileRollbackRequest, bool, error) {
|
|
var body SingBoxProfileRollbackRequest
|
|
if r.Body != nil {
|
|
defer r.Body.Close()
|
|
if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err != nil && err != io.EOF {
|
|
return SingBoxProfileRollbackRequest{}, false, err
|
|
}
|
|
}
|
|
restart := true
|
|
if body.Restart != nil {
|
|
restart = *body.Restart
|
|
}
|
|
return body, restart, nil
|
|
}
|