57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type trafficAppMarksPostInput struct {
|
|
Op TrafficAppMarksOp
|
|
Target string
|
|
Cgroup string
|
|
CgroupRaw string
|
|
Unit string
|
|
Command string
|
|
AppKey string
|
|
TimeoutSec int
|
|
}
|
|
|
|
func decodeTrafficAppMarksPostInput(r *http.Request) (trafficAppMarksPostInput, string) {
|
|
var body TrafficAppMarksRequest
|
|
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 trafficAppMarksPostInput{}, "bad json"
|
|
}
|
|
}
|
|
|
|
in := trafficAppMarksPostInput{
|
|
Op: TrafficAppMarksOp(strings.ToLower(strings.TrimSpace(string(body.Op)))),
|
|
Target: strings.ToLower(strings.TrimSpace(body.Target)),
|
|
Cgroup: strings.TrimSpace(body.Cgroup),
|
|
CgroupRaw: body.Cgroup,
|
|
Unit: strings.TrimSpace(body.Unit),
|
|
Command: strings.TrimSpace(body.Command),
|
|
AppKey: strings.TrimSpace(body.AppKey),
|
|
TimeoutSec: body.TimeoutSec,
|
|
}
|
|
if in.Op == "" {
|
|
return trafficAppMarksPostInput{}, "missing op"
|
|
}
|
|
if in.Target == "" {
|
|
return trafficAppMarksPostInput{}, "missing target"
|
|
}
|
|
if in.Target != "vpn" && in.Target != "direct" {
|
|
return trafficAppMarksPostInput{}, "target must be vpn|direct"
|
|
}
|
|
if (in.Op == TrafficAppMarksAdd || in.Op == TrafficAppMarksDel) && in.Cgroup == "" {
|
|
return trafficAppMarksPostInput{}, "missing cgroup"
|
|
}
|
|
if in.TimeoutSec < 0 {
|
|
return trafficAppMarksPostInput{}, "timeout_sec must be >= 0"
|
|
}
|
|
return in, ""
|
|
}
|