-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
266 lines (232 loc) · 6.26 KB
/
server.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
htmlTemp "html/template"
"log"
"net"
"net/http"
"strconv"
"strings"
"text/template"
"github.com/aos/wgdash/wgcli"
"github.com/skip2/go-qrcode"
)
// Peer is any client device added that connects to the wg server
type Peer struct {
Active bool
ID int
Name string
PrivateKey string
PublicKey string
VirtualIP string
KeepAlive int
}
// WgServer holds all configuration of our server, including the router
type WgServer struct {
PublicIP string
Port string
VirtualIP string
CIDR string
DNS string
PublicKey string
PrivateKey string
WgConfigPath string
Active bool
Peers []Peer
mux *http.ServeMux
}
// NewWgServer instantiates the server
func NewWgServer() *WgServer {
wgServer := LoadServerConfig()
wgServer.mux = http.NewServeMux()
wgServer.Routes()
wgServer.ActivateServer()
return wgServer
}
func (s *WgServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}
func (s *WgServer) renderTemplatePage(tmplFname string, data interface{}) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t, err := htmlTemp.ParseFiles("templates/base.html.tmpl", "templates/"+tmplFname)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
err = t.Execute(w, data)
if err != nil {
panic(err)
}
})
}
func (s *WgServer) handleAPI() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/api/" {
http.Error(w, "Couldn't find anything here :(", http.StatusNotFound)
return
}
urlParts := strings.Split(r.URL.Path, "/")
switch urlParts[2] {
case "peers":
s.handlePeersAPI(w, r)
}
})
}
func (s *WgServer) handlePeersAPI(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
urlParts := strings.Split(r.URL.Path, "/")
if len(urlParts) < 4 {
http.Error(w, "Did not specify peer ID", http.StatusBadRequest)
return
}
id, err := strconv.Atoi(urlParts[3])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for _, p := range s.Peers {
if p.ID == id {
_, ipNet, err := net.ParseCIDR(s.VirtualIP + "/" + s.CIDR)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
params := r.URL.Query()
qrCode := false
if v, ok := params["qr"]; ok && v[0] == "true" {
qrCode = true
}
var buf bytes.Buffer
tmpl := template.Must(template.ParseFiles("templates/peer.conf.tmpl"))
tmpl.Execute(&buf, struct {
VirtualIP string
PrivateKey string
ServerPublicKey string
PublicIP string
Port string
AllowedIPs string
}{
VirtualIP: p.VirtualIP,
PrivateKey: p.PrivateKey,
ServerPublicKey: s.PublicKey,
PublicIP: s.PublicIP,
Port: s.Port,
AllowedIPs: ipNet.String(),
})
if qrCode {
qr, err := qrcode.Encode(buf.String(), qrcode.Medium, 256)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("error (get_peer - generate qr code): %s\n", err.Error())
}
encoder := base64.NewEncoder(base64.StdEncoding, w)
encoder.Write(qr)
} else {
w.Header().Set("Content-Disposition", "attachment; filename=wg0.conf")
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(buf.Bytes())
}
return
}
}
http.Error(w, fmt.Sprintf("Peer %d not found", id), http.StatusNotFound)
case "POST":
var p Peer
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Printf("error (add_peer - decode JSON): %s\n", err.Error())
return
}
defer r.Body.Close()
keys, err := wgcli.GenerateKeyPair()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("error (add_peer - generate key pair): %s\n", err.Error())
return
}
peerIP, err := s.nextAvailableIP(p.VirtualIP)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Printf("error: (add_peer - available IP): %s\n", err.Error())
return
}
p.VirtualIP = peerIP
p.PrivateKey = keys["privateKey"]
p.PublicKey = keys["publicKey"]
if len(s.Peers) <= 0 {
p.ID = len(s.Peers) + 1
} else {
p.ID = s.Peers[len(s.Peers)-1].ID + 1 // always increment IDs
}
// We want to make sure that wg is actually running here
if s.Active {
err = wgcli.AddPeer(p.PublicKey, p.VirtualIP, p.KeepAlive)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("error: (add_peer - wg add peer): %s\n", err.Error())
return
}
}
s.Peers = append(s.Peers, p)
err = s.saveBothConfigs()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("error: (add_peer - save configs): %s\n", err.Error())
return
}
js, err := json.Marshal(struct {
StatusCode int
Message string
PeerID int
PeerVirtualIP string
}{
200,
"Peer added successfully",
p.ID,
p.VirtualIP,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("success (add_peer): %s\n", js)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
case "DELETE":
urlParts := strings.Split(r.URL.Path, "/")
if len(urlParts) < 4 {
http.Error(w, "Did not specify peer ID", http.StatusBadRequest)
return
}
id, err := strconv.Atoi(urlParts[3])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for i, p := range s.Peers {
if p.ID == id {
if s.Active {
err = wgcli.RemovePeer(p.PublicKey)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
s.Peers = append(s.Peers[:i], s.Peers[i+1:]...)
err = s.saveBothConfigs()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
return
}
}
http.Error(w, fmt.Sprintf("Peer %d not found", id), http.StatusNotFound)
case "PUT":
http.Error(w, "Not implemented yet", http.StatusNotImplemented)
}
}