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
/
http_handlers.go
222 lines (185 loc) · 5.26 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
package main
import (
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/xml"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/coreos/go-omaha/omaha"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"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) {
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) {
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) {
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 shutdownHandler(w http.ResponseWriter, r *http.Request) {
os.Exit(0)
}
func updateHandler(w http.ResponseWriter, r *http.Request) {
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) {
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
}
t, err := template.New("images").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
}
chosenChannel := r.URL.Query().Get("channel")
if chosenChannel == "" && len(channels) > 0 {
chosenChannel = channels[0]
}
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 []imageListElement
Channels []string
CurrentChannel string
}{
images,
channels,
chosenChannel,
}
t.Execute(w, panelData)
}