/
api.go
219 lines (195 loc) · 5.6 KB
/
api.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
package main
import (
"./hostfile"
"bufio"
"code.google.com/p/gorilla/mux"
"encoding/json"
"fmt"
"github.com/surma/goappdata"
"github.com/surma/gouuid"
"log"
"net/http"
"os"
"os/exec"
"strings"
)
var (
blocklist = map[string]hostfile.Entry{}
)
func conditionalFailf(w http.ResponseWriter, cond bool, code int, sfmt string, v ...interface{}) bool {
if cond {
err := fmt.Sprintf(sfmt, v...)
http.Error(w, err, code)
log.Println(err)
}
return cond
}
func serveAPI(addr string) {
r := mux.NewRouter()
r.Path("/").Handler(http.RedirectHandler("/admin/", http.StatusMovedPermanently))
apirouter := r.PathPrefix("/api").Subrouter()
apirouter.Path("/").Methods("GET").HandlerFunc(apiListHandler)
apirouter.Path("/").Methods("POST").HandlerFunc(updateWrapper(apiAddHandler))
apirouter.Path("/state").Methods("POST").HandlerFunc(authenticationWrapper(updateWrapper(apiSetStateHandler)))
apirouter.Path("/state").Methods("GET").HandlerFunc(apiGetStateHandler)
apirouter.Path("/{uuid:[0-9a-f-]+}").Methods("GET").HandlerFunc(apiListSingleHandler)
apirouter.Path("/{uuid:[0-9a-f-]+}").Methods("DELETE").HandlerFunc(authenticationWrapper(updateWrapper(apiDeleteSingleHandler)))
r.PathPrefix("/admin/").Handler(http.StripPrefix("/admin", http.FileServer(http.Dir("./admin"))))
e := http.ListenAndServe(addr, r)
if e != nil {
log.Fatalf("serveAPI: Could not bind http server: %s", e)
}
}
func apiListHandler(w http.ResponseWriter, r *http.Request) {
d, e := json.Marshal(blocklist)
if conditionalFailf(w, e != nil, http.StatusInternalServerError, "List: Could not marshal hosts: %s", e) {
return
}
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(d)))
w.WriteHeader(http.StatusOK)
w.Write(d)
}
func apiAddHandler(w http.ResponseWriter, r *http.Request) {
d := json.NewDecoder(r.Body)
entry := hostfile.Entry{}
e := d.Decode(&entry)
if conditionalFailf(w, e != nil, http.StatusBadRequest, "Add: Received invalid request: %s", e) {
return
}
if conditionalFailf(w, !entry.Valid(), http.StatusBadRequest, "Add: Received invalid entry") {
return
}
uuid := gouuid.New()
blocklist[uuid.String()] = entry
w.WriteHeader(http.StatusCreated)
w.Write([]byte(uuid.String()))
}
func apiListSingleHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
entry, ok := blocklist[vars["uuid"]]
if conditionalFailf(w, !ok, http.StatusNotFound, "ListSingle: Unknown UUID %s", vars["uuid"]) {
return
}
d, e := json.Marshal(entry)
if conditionalFailf(w, e != nil, http.StatusInternalServerError, "ListSingle: Could not marshal hosts: %s", e) {
return
}
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(d)))
w.WriteHeader(http.StatusOK)
w.Write(d)
}
func apiDeleteSingleHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_, ok := blocklist[vars["uuid"]]
if conditionalFailf(w, !ok, http.StatusNotFound, "DeleteSingle: Unknown UUID %s", vars["uuid"]) {
return
}
delete(blocklist, vars["uuid"])
w.WriteHeader(http.StatusNoContent)
}
func apiSetStateHandler(w http.ResponseWriter, r *http.Request) {
line, _, e := bufio.NewReader(r.Body).ReadLine()
if conditionalFailf(w, e != nil, http.StatusBadRequest, "State: Received invalid request: %s", e) {
return
}
state := strings.TrimSpace(string(line))
switch state {
case "active":
active = true
case "inactive":
active = false
default:
conditionalFailf(w, true, http.StatusBadRequest, "State: Received invalid state: %s", state)
return
}
w.WriteHeader(http.StatusNoContent)
}
func apiGetStateHandler(w http.ResponseWriter, r *http.Request) {
if active {
w.Write([]byte("active"))
} else {
w.Write([]byte("inactive"))
}
}
func authenticationWrapper(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-DiplomaEnhancer-Token") != password {
log.Printf("Received invalid password")
w.WriteHeader(http.StatusUnauthorized)
return
}
h(w, r)
}
}
func updateWrapper(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h(w, r)
newhostfile := make([]hostfile.Block, len(originalhostfile))
copy(newhostfile, originalhostfile)
if active {
block := hostfile.Block{
Comment: []string{"DiplomaEnhancer:"},
Entries: make([]hostfile.Entry, 0, len(blocklist)),
}
for _, entry := range blocklist {
block.Entries = append(block.Entries, entry)
}
newhostfile = append(newhostfile, block)
}
e := saveBlocklist()
if e != nil {
log.Printf("Could not save to blocklist file: %s", e)
}
e = writeHostfile(hostfile.Hostfile(newhostfile))
if e != nil {
log.Fatalf("Could not write host file: %s")
}
if FLUSH_CMD == nil {
log.Printf("DNS cache flushing not implemented on this OS")
return
}
e = exec.Command(FLUSH_CMD[0], FLUSH_CMD[1:]...).Start()
if e != nil {
log.Printf("Could not flush dns cache: %s", e)
}
}
}
func saveBlocklist() error {
blocklistfile, e := goappdata.CreatePath("diplomaenhancer")
if e != nil {
return e
}
blocklistfile += "/blocklist"
f, e := os.Create(blocklistfile)
if e != nil {
return e
}
defer f.Close()
enc := json.NewEncoder(f)
e = enc.Encode(blocklist)
return e
}
func readBlocklist() error {
blocklistfile, e := goappdata.CreatePath("diplomaenhancer")
if e != nil {
return e
}
blocklistfile += "/blocklist"
f, e := os.Open(blocklistfile)
if e != nil {
return e
}
defer f.Close()
dec := json.NewDecoder(f)
e = dec.Decode(&blocklist)
return e
}
func writeHostfile(hostfile hostfile.Hostfile) error {
f, e := os.Create(HOSTFILE)
if e != nil {
return e
}
defer f.Close()
_, e = f.Write([]byte(hostfile.String()))
return e
}