forked from palark/ovpn-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router-openvpn.go
70 lines (61 loc) · 1.83 KB
/
router-openvpn.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
package main
import (
"errors"
"log"
"net/http"
"rpiadm/backend/auth"
"rpiadm/backend/openvpn"
"rpiadm/backend/shell"
)
func (app *OvpnAdmin) handleOpenvpnCommand(w http.ResponseWriter, r *http.Request) {
log.Printf("openvpn %s, %s", r.Method, r.URL.Path)
if enableCors(&w, r) {
return
}
if !auth.HasReadRole(app.applicationPreferences.JwtData, r) {
returnErrorMessage(w, http.StatusUnauthorized, errors.New("access denied"))
return
}
log.Printf("call %s %s", r.Method, r.URL.Path)
if r.URL.Path == "/api/openvpn/crl" && r.Method == "GET" {
app.listCrl(w)
return
}
if r.URL.Path == "/api/openvpn/gen-dh" && r.Method == "POST" {
err := openvpn.CreateDhFile(app.easyrsa)
if err != nil {
returnErrorMessage(w, http.StatusUnprocessableEntity, err)
return
}
return
}
if r.URL.Path == "/api/openvpn/init-pki" && r.Method == "POST" {
err := app.easyrsa.InitPki()
if err != nil {
returnErrorMessage(w, http.StatusUnprocessableEntity, err)
return
}
return
}
returnErrorMessage(w, http.StatusBadRequest, errors.New("bad request"))
}
func (app *OvpnAdmin) listCrl(w http.ResponseWriter) {
if len(app.serverConf.CrlVerify) == 0 {
returnErrorMessage(w, http.StatusBadRequest, errors.New("no crl active"))
return
}
//log.Printf("load crl %s", crlPath)
certs := make([]*openvpn.Certificate, 0)
for _, client := range app.clients {
//log.Printf("existing cert %s, serial: %s", client.Certificate.CommonName, client.Certificate.SerialNumber)
certs = append(certs, client.Certificate)
}
crlList, err := openvpn.GetCrlList(shell.AbsolutizePath(app.serverConf.SourceFile, app.serverConf.CrlVerify), certs)
if err != nil {
log.Printf("error %v", err)
returnErrorMessage(w, http.StatusInternalServerError, errors.New("cant parse crl"))
return
}
//log.Printf("crl %v", crlList)
returnJson(w, crlList)
}