-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
defensive.go
117 lines (99 loc) · 2.77 KB
/
defensive.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
package wasabeehttps
import (
"encoding/json"
"fmt"
"net/http"
"github.com/wasabee-project/Wasabee-Server/log"
"github.com/wasabee-project/Wasabee-Server/model"
)
func getDefensiveKeys(res http.ResponseWriter, req *http.Request) {
gid, err := getAgentID(req)
if err != nil {
http.Error(res, jsonError(err), http.StatusUnauthorized)
return
}
dkl, err := gid.ListDefensiveKeys()
if err != nil {
log.Warn(err)
http.Error(res, jsonError(err), http.StatusInternalServerError)
return
}
json.NewEncoder(res).Encode(dkl)
}
func setDefensiveKey(res http.ResponseWriter, req *http.Request) {
var dk model.DefensiveKey
gid, err := getAgentID(req)
if err != nil {
http.Error(res, err.Error(), http.StatusUnauthorized)
return
}
if !contentTypeIs(req, jsonTypeShort) {
err := fmt.Errorf("wasabee-IITC plugin version is too old, please update")
log.Warnw(err.Error(), "GID", gid)
http.Error(res, jsonError(err), http.StatusNotAcceptable)
return
}
if err := json.NewDecoder(req.Body).Decode(&dk); err != nil {
log.Error(err)
http.Error(res, jsonError(err), http.StatusInternalServerError)
return
}
if dk.Name == "" {
err := fmt.Errorf("empty portal name")
log.Warnw(err.Error(), "GID", gid)
http.Error(res, jsonStatusEmpty, http.StatusNotAcceptable)
return
}
if dk.Lat == "" || dk.Lon == "" {
err := fmt.Errorf("empty portal location")
log.Warnw(err.Error(), "GID", gid)
http.Error(res, jsonStatusEmpty, http.StatusNotAcceptable)
return
}
err = gid.InsertDefensiveKey(dk)
if err != nil {
log.Warn(err)
http.Error(res, jsonError(err), http.StatusInternalServerError)
return
}
fmt.Fprint(res, jsonStatusOK)
}
func setDefensiveKeyBulk(res http.ResponseWriter, req *http.Request) {
gid, err := getAgentID(req)
if err != nil {
http.Error(res, err.Error(), http.StatusUnauthorized)
return
}
if !contentTypeIs(req, jsonTypeShort) {
err := fmt.Errorf("JSON required")
log.Warnw(err.Error(), "gid", gid)
http.Error(res, jsonError(err), http.StatusNotAcceptable)
return
}
var dkl []model.DefensiveKey
if err := json.NewDecoder(req.Body).Decode(&dkl); err != nil {
log.Errorw(err.Error(), "gid", gid)
http.Error(res, jsonError(err), http.StatusInternalServerError)
return
}
for _, dk := range dkl {
if dk.Name == "" {
err := fmt.Errorf("empty portal name")
log.Warnw(err.Error(), "gid", gid)
http.Error(res, jsonStatusEmpty, http.StatusNotAcceptable)
return
}
if dk.Lat == "" || dk.Lon == "" {
err := fmt.Errorf("empty portal location")
log.Warnw(err.Error(), "gid", gid)
http.Error(res, jsonStatusEmpty, http.StatusNotAcceptable)
return
}
err = gid.InsertDefensiveKey(dk)
if err != nil {
http.Error(res, jsonError(err), http.StatusInternalServerError)
return
}
}
fmt.Fprint(res, jsonStatusOK)
}