/
webserver.go
95 lines (86 loc) · 2.68 KB
/
webserver.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
package main
import (
"crypto/subtle"
"encoding/json"
"net/http"
"time"
"github.com/gobuffalo/packr/v2"
log "github.com/sirupsen/logrus"
)
type webSocket struct {
Address string `json:"address"`
Last *int64 `json:"last"`
}
func webBasicAuth(handler http.HandlerFunc, username, password, realm string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if username != "" && password != "" {
user, pass, ok := r.BasicAuth()
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorized.\n"))
return
}
}
handler(w, r)
}
}
func webHandleFileServer(box *packr.Box, prefix string) http.HandlerFunc {
fs := http.FileServer(box)
realHandler := fs.ServeHTTP
return func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {
index, err := box.Find("index.html")
if err != nil {
http.NotFound(w, req)
}
w.WriteHeader(200)
w.Write(index)
return
}
realHandler(w, req)
}
}
func webGetList(w http.ResponseWriter, r *http.Request) {
rspSockets := []webSocket{}
for address, client := range clients {
last := time.Now().Unix() - client.Last
rspSocket := webSocket{
Address: address,
}
if client.Last > 0 {
rspSocket.Last = &last
}
rspSockets = append(rspSockets, rspSocket)
}
rspObject := struct {
Type string `json:"type"`
Version string `json:"version"`
Description string `json:"description"`
ListenAddress string `json:"listenAddress"`
DstAddress string `json:"dstAddress"`
Sockets []webSocket `json:"sockets"`
}{"server", Version, srConfig.Description, srConfig.ListenAddr, srConfig.DstAddr, rspSockets}
rspJSON, err := json.Marshal(rspObject)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("Internal server error"))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(rspJSON)
}
func webserver(listenAddr, username, password string) {
for {
realm := "engarde"
box := packr.New("webmanager", "../../webmanager/dist/webmanager")
http.HandleFunc("/", webBasicAuth(webHandleFileServer(box, "/"), username, password, realm))
http.HandleFunc("/api/v1/get-list", NoCache(webBasicAuth(webGetList, username, password, realm)))
log.Info("Management webserver listening on " + listenAddr)
if err := http.ListenAndServe(listenAddr, nil); err != nil {
log.Error(err)
time.Sleep(1 * time.Second)
}
}
}