forked from palark/ovpn-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router-node.go
44 lines (38 loc) · 1004 Bytes
/
router-node.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
package main
import (
"errors"
"fmt"
"log"
"net/http"
"regexp"
"rpiadm/backend/auth"
"rpiadm/backend/rpi"
)
func (app *OvpnAdmin) handleNodeCommand(w http.ResponseWriter, r *http.Request) {
//log.Debug(r.RemoteAddr, " ", r.RequestURI)
if enableCors(&w, r) {
return
}
if !auth.HasReadRole(app.applicationPreferences.JwtData, r) {
w.WriteHeader(http.StatusForbidden)
return
}
re := regexp.MustCompile("^/api/node/(.*)$")
matches := re.FindStringSubmatch(r.URL.Path)
log.Printf("get config for node %s (%v)", r.URL.Path, matches)
if len(matches) > 0 {
payload, err := rpi.ReadNodeConfig(*ovpnConfigDir, matches[1])
if err != nil {
returnErrorMessage(w, http.StatusBadRequest, errors.New(fmt.Sprintf("Failed read config for user %s: %¨s", matches[1], err)))
return
}
err = returnJson(w, payload)
if err != nil {
log.Printf("error sending response")
}
return
} else {
returnErrorMessage(w, http.StatusBadRequest, errors.New("bad request"))
return
}
}