-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_services.go
90 lines (56 loc) · 1.4 KB
/
api_services.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"encoding/json"
"log"
"net/http"
"strings"
"github.com/gorilla/mux"
)
func servicesHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars[PARAM_NAME]
u := authenticated(r)
if u == nil {
w.WriteHeader(http.StatusUnauthorized)
} else {
switch r.Method {
case http.MethodGet:
// TODO: check service name
out, err := getUnitProperty(SERVICE_WIREGUARD, PROPERTY_ACTIVESTATE)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
} else {
j, err := json.Marshal(Property{Active: out,})
if err != nil {
log.Println(err)
} else {
w.Write(j)
}
}
case http.MethodPut:
if !checkParam(name) {
w.WriteHeader(http.StatusBadRequest)
} else {
// TODO: add sqlite3 to store services
if strings.ToLower(name) == WIREGUARD {
method := r.FormValue(PARAM_METHOD)
if checkSystemdMethod(method) {
_, err := callSystemd(method, SERVICE_WIREGUARD)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
} else {
log.Println("Invalid systemd method")
w.WriteHeader(http.StatusBadRequest)
}
}
}
case http.MethodPost:
// TODO: add service, need to consider security
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
} // servicesHandler