This repository was archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
111 lines (90 loc) · 2.5 KB
/
Copy pathserver.go
File metadata and controls
111 lines (90 loc) · 2.5 KB
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
package main
import (
"encoding/hex"
"encoding/json"
"log"
"net"
"net/http"
"strings"
)
type ServerCreationRequest struct {
Type string
Metadata map[string]string
}
type ServerDeletionRequest struct {
IP string
}
func list(res http.ResponseWriter, _ *http.Request) {
serverData, _ := json.Marshal(servers)
res.Header().Add("Content-Type", "application/json")
_, _ = res.Write(serverData)
}
func add(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
http.Error(res, "method not allowed", http.StatusMethodNotAllowed)
return
}
var reqData ServerCreationRequest
err := json.NewDecoder(req.Body).Decode(&reqData)
if err != nil {
http.Error(res, "invalid post body", http.StatusBadRequest)
return
}
server, err := AddServer(reqData.Type, reqData.Metadata)
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
log.Printf("added server %s of type %s", server.IP, server.Type)
jsonServer, _ := json.Marshal(server)
res.Header().Add("Content-Type", "application/json")
_, _ = res.Write(jsonServer)
}
func remove(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
http.Error(res, "method not allowed", http.StatusMethodNotAllowed)
return
}
var reqData ServerDeletionRequest
err := json.NewDecoder(req.Body).Decode(&reqData)
if err != nil {
http.Error(res, "invalid post body", http.StatusBadRequest)
return
}
err = DeleteServer(IPAddr{
&net.IPAddr{
IP: net.ParseIP(reqData.IP),
Zone: "",
},
})
if err != nil {
http.Error(res, "invalid ip address", http.StatusBadRequest)
return
}
jsonResponse, _ := json.Marshal(map[string]bool {
"deleted": true,
})
res.Header().Add("Content-Type", "application/json")
_, _ = res.Write(jsonResponse)
}
func auth(next http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
token := strings.TrimPrefix(req.Header.Get("Authorization"), "Bearer ")
decoded, err := hex.DecodeString(token)
if err != nil || !config.Secret.Compare(Secret{decoded}) {
http.Error(res, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(res, req)
})
}
func StartServer() {
http.Handle("/list", auth(http.HandlerFunc(list)))
http.Handle("/add", auth(http.HandlerFunc(add)))
http.Handle("/remove", auth(http.HandlerFunc(remove)))
log.Printf("starting listener on %s", config.Listen)
err := http.ListenAndServe(config.Listen, nil)
if err != nil {
log.Fatalln("failed to start listener on port", err)
}
}