forked from palark/ovpn-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router-user.go
94 lines (86 loc) · 2.3 KB
/
router-user.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
91
92
93
94
package main
import (
"errors"
"log"
"net/http"
"regexp"
"rpiadm/backend/auth"
"rpiadm/backend/model"
)
func (app *OvpnAdmin) handleUserCommand(w http.ResponseWriter, r *http.Request) {
if enableCors(&w, r) {
return
}
if r.URL.Path == "/api/user/" && r.Method == "POST" {
app.userCreateHandler(w, r)
return
} else if r.URL.Path == "/api/user/" && r.Method == "GET" {
app.userListHandler(w, r)
return
}
regUser := regexp.MustCompile("^/api/user/([^/]*)/(.*)$")
matches := regUser.FindStringSubmatch(r.URL.Path)
if len(matches) < 2 {
returnErrorMessage(w, http.StatusBadRequest, errors.New("bad request"))
return
}
username := matches[1]
cmd := matches[2]
log.Printf("exec cmd %s for user %s", cmd, username)
if r.Method == "PUT" {
if cmd == "ccd" {
retCode, e := app.userApplyCcdHandler(w, r, username)
if e != nil {
returnErrorMessage(w, retCode, e)
}
return
}
} else if r.Method == "GET" {
if cmd == "conf" {
app.buildClientOvpnConfigFile(w, r, username)
return
}
//if cmd == "ccd" {
// app.userShowCcdHandler(w, r, username)
// return
//} else
} else if r.Method == "DELETE" {
app.userDeleteHandler(w, r, username)
return
} else if r.Method == "POST" {
if cmd == "kill" {
app.apiConnectionKill(w, r, username)
return
} else if cmd == "revoke" {
app.userRevokeHandler(w, r, username)
return
} else if cmd == "unrevoke" {
app.userUnrevokeHandler(w, r, username)
return
} else if cmd == "rotate" {
app.userRotateHandler(w, r, username)
return
} else if cmd == "change-password" {
app.userChangePasswordHandler(w, r, username)
return
}
}
returnErrorMessage(w, http.StatusBadRequest, errors.New("bad request"))
}
func (app *OvpnAdmin) userListHandler(w http.ResponseWriter, r *http.Request) {
if !auth.HasReadRole(app.applicationPreferences.JwtData, r) {
w.WriteHeader(http.StatusForbidden)
return
}
//app.updateCertificateStats(openvpn.IndexTxtParserCertificate(shell.ReadFile(*indexTxtPath)))
clients := make([]*model.Device, 0)
for _, client := range app.clients {
if client.Certificate.Flag != "D" && (app.serverConf == nil || client.Username != app.serverConf.MasterCn) {
clients = append(clients, client)
}
}
err := returnJson(w, clients)
if err != nil {
log.Printf("error sending response")
}
}