This repository has been archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_handlers.go
347 lines (294 loc) · 8.47 KB
/
http_handlers.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/xml"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/coreos/go-omaha/omaha"
"github.com/julienschmidt/httprouter"
"html/template"
"io"
"io/ioutil"
"net/http"
"path"
"runtime"
)
const noupdateResponse = `
<?xml version="1.0" encoding="UTF-8"?>
<response protocol="3.0" server="update.core-os.net">
<daystart elapsed_seconds="0"></daystart>
<app appid="e96281a6-d1af-4bde-9a0a-97b76e56dc57" status="ok">
<updatecheck status="noupdate"></updatecheck>
</app>
</response>`
func homeHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
defer r.Body.Close()
http.Error(w, "", 404)
log.Infof("Someone tried to access '%s'", r.URL.String())
}
func fileHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
defer r.Body.Close()
log.Infof("Someone tried to access '%s'", r.URL.String())
fileid := r.URL.Query().Get("id")
log.Infof("Handling request for %v", fileid)
http.ServeFile(w, r, path.Join("storage", fileid))
}
func addPayloadHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
defer runtime.GC()
defer r.Body.Close()
receivedSha1 := r.URL.Query().Get("sha1")
if receivedSha1 == "" {
http.Error(w, "Missing parameter 'sha1'", 400)
return
}
receivedSha256 := r.URL.Query().Get("sha256")
if receivedSha256 == "" {
http.Error(w, "Missing parameter 'sha256'", 400)
return
}
size := r.ContentLength
versionString := r.URL.Query().Get("version")
if versionString == "" {
http.Error(w, "Missing parameter 'version'", 400)
return
}
channel := r.URL.Query().Get("channel")
if channel == "" {
http.Error(w, "Missing parameter 'channel'", 400)
return
}
data := make([]byte, size)
rcvsize, err := io.ReadFull(r.Body, data)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
versionData, err := parseVersionString(versionString)
if err != nil {
s := fmt.Sprintf("Could not parse 'version': %v", err.Error())
http.Error(w, s, 400)
return
}
log.Debugf("addPayloadHandler: received size is %v", rcvsize)
rawSha1 := sha1.Sum(data)
calculatedSha1 := base64.StdEncoding.EncodeToString(rawSha1[:])
rawSha256 := sha256.Sum256(data)
calculatedSha256 := base64.StdEncoding.EncodeToString(rawSha256[:])
if receivedSha1 != calculatedSha1 {
s := fmt.Sprintf("SHA1 validation failed, '%v' != '%v'", receivedSha1, calculatedSha1)
http.Error(w, s, 400)
return
}
if receivedSha256 != calculatedSha256 {
s := fmt.Sprintf("SHA256 validation failed, '%v' != '%v'", receivedSha256, calculatedSha256)
http.Error(w, s, 400)
return
}
id, err := fileBE.Store(data)
if err != nil {
log.Errorf("addPayloadHandler: storing data: %v", err.Error())
}
err = db.AddPayload(id, calculatedSha1, calculatedSha256, size, versionData)
if err != nil {
log.Errorf("addPayloadHandler: adding payload to db: %v", err.Error())
}
err = db.AttachPayloadToChannel(id, channel)
if err != nil {
log.Errorf("addPayloadHandler: adding payload to channel: %v", err.Error())
}
}
func attachPayloadToChannelHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
channel := r.URL.Query().Get("channel")
if channel == "" {
http.Error(w, "Missing parameter 'channel'", 400)
return
}
payload := r.URL.Query().Get("payload")
if channel == "" {
http.Error(w, "Missing parameter 'payload'", 400)
return
}
err := db.AttachPayloadToChannel(payload, channel)
if err != nil {
log.Errorf("addPayloadHandler: adding payload to channel: %v", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func deletePayloadHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
defer r.Body.Close()
id := r.URL.Query().Get("id")
if id == "" {
http.Error(w, "Missing parameter 'id'", 400)
return
}
channel := r.URL.Query().Get("channel")
if id == "" {
http.Error(w, "Missing parameter 'channel'", 400)
return
}
err := db.DeletePayload(id, channel)
if err != nil {
log.Errorf("deletePayloadHandler: removing DB entry for '%v' from channel '%v': %v", id, channel, err.Error())
http.Error(w, err.Error(), 500)
}
if !db.PayloadExists(id) {
err = fileBE.Delete(id)
if err != nil {
log.Errorf("deletePayloadHandler: removing file for '%v': %v", id, err.Error())
http.Error(w, err.Error(), 500)
}
}
}
func channelForceDowngradeGetHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
channel := ps.ByName("channel")
value, err := db.GetChannelForceDowngrade(channel)
if err != nil {
log.Errorf("channelForceDowngradeGetHandler: getting FD value for channel '%v': %v", channel, err.Error())
http.Error(w, err.Error(), 500)
return
}
if value {
fmt.Fprint(w, 1)
} else {
fmt.Fprint(w, 0)
}
}
func channelForceDowngradePostHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
channel := ps.ByName("channel")
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
value := string(body)
var boolValue bool
switch value {
case "1":
boolValue = true
case "0":
boolValue = false
default:
s := fmt.Sprintf("Invalid value '%v'", value)
http.Error(w, s, http.StatusBadRequest)
}
err = db.SetChannelForceDowngrade(channel, boolValue)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func updateHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
defer r.Body.Close()
log.Infof("Handling an update request from %v", r.RemoteAddr)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), 500)
log.Error(err.Error())
}
var reqStructure omaha.Request
log.Debugf("%v", string(body[:len(body)]))
err = xml.Unmarshal(body, &reqStructure)
if n := len(reqStructure.Apps); n != 1 {
log.Errorf("Client '%v' tried to update %v services", r.RemoteAddr, n)
http.Error(w, "I can handle only 1 app update.", 400)
return
}
log.Debugf("%#v", reqStructure)
logContext := log.WithFields(log.Fields{
"remoteAddr": r.RemoteAddr,
})
// protocol and hostname for local storage URL building
scheme := "http"
if h := r.Header.Get("X-Forwarded-Proto"); h != "" {
scheme = h
}
localUrl := fmt.Sprintf("%v://%v", scheme, r.Host)
resp := omaha.NewResponse(r.Host)
for _, appReq := range reqStructure.Apps {
appResponse := resp.AddApp(appReq.Id)
handleApiApp(logContext, localUrl, appReq, appResponse)
}
data, err := xml.MarshalIndent(resp, "", " ")
if err != nil {
log.Error(err.Error())
http.Error(w, "An internal error occured while marshalling a response", 500)
}
w.Header().Set("Content-Type", "application/xml")
w.Write(data)
}
func panelHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
defer r.Body.Close()
// TODO log panel access
data, err := ioutil.ReadFile("static/images.html")
if err != nil {
http.Error(w, err.Error(), 500)
log.Error(err.Error())
return
}
funcMap := template.FuncMap{
"toMB": func(i int64) string {
divided := float32(i) / 1048576
formatted := fmt.Sprintf("%.1f", divided)
return formatted
},
}
t, err := template.New("images").Funcs(funcMap).Parse(string(data))
if err != nil {
http.Error(w, err.Error(), 500)
log.Error(err.Error())
return
}
channels, err := db.ListChannels()
if err != nil {
log.Error(err.Error())
http.Error(w, "Failed to retrieve list of channels", 500)
return
}
var chosenChannel string
var forceDowngrade bool
var images []payload
var events []Event
if _, ok := r.URL.Query()["events"]; ok {
events, err = db.GetEvents()
if err != nil {
log.Error(err.Error())
http.Error(w, "Failed to retrieve events from the database", 500)
return
}
} else {
chosenChannel = r.URL.Query().Get("channel")
if chosenChannel == "" && len(channels) > 0 {
chosenChannel = channels[0]
}
forceDowngrade, err = db.GetChannelForceDowngrade(chosenChannel)
if err != nil {
log.Error(err.Error())
http.Error(w, "Failed to retrieve force_downgrade option for the channel", 500)
return
}
images, err = db.ListImages(chosenChannel)
if err != nil {
log.Error(err.Error())
http.Error(w, "Failed to retrieve images for the channel", 500)
return
}
}
panelData := struct {
Images []payload
Events []Event
Channels []string
CurrentChannel string
ForceDowngrade bool
}{
images,
events,
channels,
chosenChannel,
forceDowngrade,
}
err = t.Execute(w, panelData)
if err != nil {
log.Errorf("Error parsing panel template: %v", err)
http.Error(w, "Error parsing panel template", 500)
return
}
}