forked from palark/ovpn-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router-ccd.go
72 lines (63 loc) · 2.02 KB
/
router-ccd.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
package main
import (
"encoding/json"
"errors"
"net/http"
"rpiadm/backend/auth"
"rpiadm/backend/openvpn"
"strings"
)
func extractNetmask(cidr string) string {
// cidr = "10.8.0.0 255.255.0.0"
parts := strings.Split(cidr, " ")
return parts[1]
}
func (app *OvpnAdmin) userApplyCcdHandler(w http.ResponseWriter, r *http.Request, username string) (int, error) {
if !auth.HasReadRole(app.applicationPreferences.JwtData, r) {
return http.StatusForbidden, errors.New("request forbidden")
}
device := app.getDevice(username)
if device == nil {
return http.StatusNotFound, errors.New("device not found")
}
if r.Body == nil {
return http.StatusBadRequest, errors.New("please send a request body")
}
var ccd openvpn.Ccd
err := json.NewDecoder(r.Body).Decode(&ccd)
if err != nil {
return http.StatusInternalServerError, errors.New("can't parse JSON body")
}
newCcd, err := app.applyCcd(device.Username, ccd)
if err != nil {
return http.StatusUnprocessableEntity, err
}
device.Ccd = newCcd
app.triggerBroadcastUser(device)
w.WriteHeader(http.StatusNoContent)
return 0, nil
}
func (app *OvpnAdmin) removeCcd(commonName string) {
openvpn.RemoveCcd(app.serverConf, commonName)
}
func (app *OvpnAdmin) applyCcd(commonName string, ccd openvpn.Ccd) (*openvpn.Ccd, error) {
for i, _ := range ccd.CustomRoutes {
ccd.CustomRoutes[i].Description = strings.Trim(ccd.CustomRoutes[i].Description, " ")
}
for i, _ := range ccd.CustomIRoutes {
ccd.CustomIRoutes[i].Description = strings.Trim(ccd.CustomIRoutes[i].Description, " ")
}
openvpnNetwork := openvpn.ConvertNetworkMaskCidr(app.serverConf.Server)
existingCcd := make([]*openvpn.Ccd, 0)
for _, client := range app.clients {
if client.Ccd != nil && client.Username != commonName {
existingCcd = append(existingCcd, client.Ccd)
}
}
err := openvpn.UpdateCcd(app.serverConf, openvpnNetwork, extractNetmask(app.serverConf.Server), ccd, commonName, existingCcd)
if err != nil {
return nil, err
}
// TODO: handle other options not exposed by the api
return &ccd, nil
}