46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func handleTransportSingBoxProfileApply(w http.ResponseWriter, r *http.Request, id string) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
body, checkBinary, restart, err := decodeTransportSingBoxProfileApplyRequest(r)
|
|
if err != nil {
|
|
http.Error(w, "bad json", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
singBoxProfilesMu.Lock()
|
|
defer singBoxProfilesMu.Unlock()
|
|
|
|
status, resp := executeTransportSingBoxProfileApplyLocked(id, body, checkBinary, restart)
|
|
writeJSON(w, status, resp)
|
|
}
|
|
|
|
func decodeTransportSingBoxProfileApplyRequest(r *http.Request) (SingBoxProfileApplyRequest, bool, bool, error) {
|
|
var body SingBoxProfileApplyRequest
|
|
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 SingBoxProfileApplyRequest{}, false, false, err
|
|
}
|
|
}
|
|
checkBinary := true
|
|
if body.CheckBinary != nil {
|
|
checkBinary = *body.CheckBinary
|
|
}
|
|
restart := true
|
|
if body.Restart != nil {
|
|
restart = *body.Restart
|
|
}
|
|
return body, checkBinary, restart, nil
|
|
}
|