forked from etix/mirrorbits
/
http.go
470 lines (402 loc) · 11.6 KB
/
http.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Copyright (c) 2014 Ludovic Fauvet
// Licensed under the MIT license
package main
import (
"encoding/json"
"fmt"
"github.com/etix/stoppableListener"
"github.com/garyburd/redigo/redis"
"html/template"
"math/rand"
"net"
"net/http"
"os"
"sort"
"strconv"
"strings"
"time"
)
// HTTP represents an instance of the HTTP webserver
type HTTP struct {
geoip *GeoIP
redis *redisobj
templates Templates
SListener *stoppableListener.StoppableListener
stats *Stats
cache *Cache
engine MirrorSelection
Restarting bool
}
// Templates is a struct embedding instances of the precompiled templates
type Templates struct {
mirrorlist *template.Template
mirrorstats *template.Template
}
// FileInfo is a struct embedding details about a file served by
// the redirector.
type FileInfo struct {
Path string `redis:"-"`
Size int64 `redis:"size" json:",omitempty"`
ModTime time.Time `redis:"modTime" json:",omitempty"`
Sha1 string `redis:"sha1" json:",omitempty"`
}
// MirrorlistPage is the resulting struct of a request and is
// used by the renderers to generate the final page.
type MirrorlistPage struct {
FileInfo FileInfo
MapURL string `json:"-"`
IP string
ClientInfo GeoIPRec
MirrorList Mirrors
ExcludedList Mirrors `json:",omitempty"`
Fallback bool `json:",omitempty"`
}
// HTTPServer is the constructor of the HTTP server
func HTTPServer(redis *redisobj, cache *Cache) *HTTP {
h := new(HTTP)
h.redis = redis
h.geoip = NewGeoIP()
h.templates.mirrorlist = template.Must(h.LoadTemplates("mirrorlist"))
h.templates.mirrorstats = template.Must(h.LoadTemplates("mirrorstats"))
h.cache = cache
h.stats = NewStats(redis)
h.engine = DefaultEngine{}
http.Handle("/", NewGzipHandler(h.requestDispatcher))
// Load the GeoIP database
if err := h.geoip.LoadGeoIP(); err != nil {
log.Critical("Can't load the GeoIP databases: %v", err)
if len(GetConfig().Fallbacks) > 0 {
log.Warning("All requests will be served by the backup mirrors!")
} else {
log.Error("Please configure fallback mirrors!")
}
}
// Initialize the random number generator
rand.Seed(time.Now().UnixNano())
return h
}
// SetListener can be used to set a different listener that should be used by the
// HTTP server. This is primarily used during seamless binary upgrade.
func (h *HTTP) SetListener(l net.Listener) {
/* Make the listener stoppable to be able to shutdown the server gracefully */
h.SListener = stoppableListener.Handle(l)
}
// Terminate terminates the current HTTP server gracefully
func (h *HTTP) Terminate() {
/* Close the listener, killing all active connections */
h.SListener.Close()
/* Commit the latest recorded stats to the database */
h.stats.Terminate()
}
// Reload the configuration
func (h *HTTP) Reload() {
// Reload the templates
if t, err := h.LoadTemplates("mirrorlist"); err == nil {
h.templates.mirrorlist = t //XXX lock needed?
} else {
log.Error("could not reload templates 'mirrorlist': %s", err.Error())
}
if t, err := h.LoadTemplates("mirrorstats"); err == nil {
h.templates.mirrorstats = t //XXX lock needed?
} else {
log.Error("could not reload templates 'mirrorstats': %s", err.Error())
}
}
// RunServer is the main function used to start the HTTP server
func (h *HTTP) RunServer() (err error) {
// If SListener isn't nil that means that we're running a seamless
// binary upgrade and we have recovered an already running listener
if h.SListener == nil {
proto := "tcp"
address := GetConfig().ListenAddress
if strings.HasPrefix(address, "unix:") {
proto = "unix"
address = strings.TrimPrefix(address, "unix:")
}
listener, err := net.Listen(proto, address)
if err != nil {
log.Fatal("Listen: ", err)
}
h.SetListener(listener)
}
server := &http.Server{
Handler: nil,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
fmt.Println("Service listening on", GetConfig().ListenAddress)
/* Serve until we receive a SIGTERM */
return server.Serve(h.SListener)
}
func (h *HTTP) requestDispatcher(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(w, r, h.templates)
w.Header().Set("Server", "Mirrorbits/"+VERSION)
switch ctx.Type() {
case MIRRORLIST:
fallthrough
case STANDARD:
h.mirrorHandler(w, r, ctx)
case MIRRORSTATS:
h.mirrorStatsHandler(w, r, ctx)
case FILESTATS:
h.fileStatsHandler(w, r, ctx)
}
}
func (h *HTTP) mirrorHandler(w http.ResponseWriter, r *http.Request, ctx *Context) {
//XXX it would be safer to recover in case of panic
// Check if the file exists in the local repository
if _, err := os.Stat(GetConfig().Repository + r.URL.Path); err != nil {
http.NotFound(w, r)
return
}
fileInfo := FileInfo{
Path: r.URL.Path,
}
remoteIP := r.Header.Get("X-Forwarded-For")
if remoteIP == "" {
remoteIP = remoteIpFromAddr(r.RemoteAddr)
}
if ctx.IsMirrorlist() {
fromip := ctx.QueryParam("fromip")
if net.ParseIP(fromip) != nil {
remoteIP = fromip
}
}
clientInfo := h.geoip.GetInfos(remoteIP) //TODO return a pointer?
mirrors, excluded, err := h.engine.Selection(ctx, h.cache, &fileInfo, clientInfo)
/* Handle errors */
fallback := false
if nerr, ok := err.(net.Error); ok || len(mirrors) == 0 {
/* Handle fallbacks */
fallbacks := GetConfig().Fallbacks
if len(fallbacks) > 0 {
fallback = true
for i, f := range fallbacks {
mirrors = append(mirrors, Mirror{
ID: fmt.Sprintf("fallback%d", i),
HttpURL: f.Url,
CountryCodes: strings.ToUpper(f.CountryCode),
CountryFields: []string{strings.ToUpper(f.CountryCode)},
ContinentCode: strings.ToUpper(f.ContinentCode)})
}
sort.Sort(ByRank{mirrors, clientInfo})
} else {
// No fallback in stock, there's nothing else we can do
http.Error(w, nerr.Error(), http.StatusInternalServerError)
}
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
page := &MirrorlistPage{
FileInfo: fileInfo,
MirrorList: mirrors,
ExcludedList: excluded,
ClientInfo: clientInfo,
IP: remoteIP,
Fallback: fallback,
}
var pageRenderer PageRenderer
if ctx.IsMirrorlist() {
pageRenderer = &MirrorListRenderer{}
} else {
switch GetConfig().OutputMode {
case "json":
pageRenderer = &JsonRenderer{}
case "redirect":
pageRenderer = &RedirectRenderer{}
case "auto":
accept := r.Header.Get("Accept")
if strings.Index(accept, "application/json") >= 0 {
pageRenderer = &JsonRenderer{}
} else {
pageRenderer = &RedirectRenderer{}
}
default:
http.Error(w, "No page renderer", http.StatusInternalServerError)
return
}
}
status, err := pageRenderer.Write(ctx, page)
if err != nil {
http.Error(w, err.Error(), status)
}
if !ctx.IsMirrorlist() {
logDownload(pageRenderer.Type(), status, page, err)
if len(mirrors) > 0 {
h.stats.CountDownload(mirrors[0], fileInfo)
}
}
return
}
// LoadTemplates pre-loads templates from the configured template directory
func (h *HTTP) LoadTemplates(name string) (t *template.Template, err error) {
t = template.New("t")
t.Funcs(template.FuncMap{
"add": add,
"sizeof": readableSize,
"version": version,
})
t, err = t.ParseFiles(
fmt.Sprintf("%s/base.html", GetConfig().Templates),
fmt.Sprintf("%s/%s.html", GetConfig().Templates, name))
if err != nil {
panic(err)
}
return t, err
}
type StatsFileNow struct {
Today int64
Month int64
Year int64
Total int64
}
type StatsFilePeriod struct {
Period string
Downloads int64
}
// See stats.go header for the storage structure
func (h *HTTP) fileStatsHandler(w http.ResponseWriter, r *http.Request, ctx *Context) {
var output []byte
rconn := h.redis.pool.Get()
defer rconn.Close()
req := strings.SplitN(ctx.QueryParam("stats"), "-", 3)
// Sanity check
for _, e := range req {
if e == "" {
continue
}
if _, err := strconv.ParseInt(e, 10, 0); err != nil {
http.Error(w, "Invalid period", http.StatusBadRequest)
return
}
}
if len(req) == 0 || req[0] == "" {
fkey := fmt.Sprintf("STATS_FILE_%s", time.Now().Format("2006_01_02"))
rconn.Send("MULTI")
for i := 0; i < 4; i++ {
rconn.Send("HGET", fkey, r.URL.Path)
fkey = fkey[:strings.LastIndex(fkey, "_")]
}
res, err := redis.Values(rconn.Do("EXEC"))
if err != nil && err != redis.ErrNil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s := &StatsFileNow{}
s.Today, _ = redis.Int64(res[0], err)
s.Month, _ = redis.Int64(res[1], err)
s.Year, _ = redis.Int64(res[2], err)
s.Total, _ = redis.Int64(res[3], err)
output, err = json.MarshalIndent(s, "", " ")
} else {
// Generate the redis key
dkey := "STATS_FILE_"
for _, e := range req {
dkey += fmt.Sprintf("%s_", e)
}
dkey = dkey[:len(dkey)-1]
v, err := redis.Int64(rconn.Do("HGET", dkey, r.URL.Path))
if err != nil && err != redis.ErrNil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
s := &StatsFilePeriod{Period: ctx.QueryParam("stats"), Downloads: v}
output, err = json.MarshalIndent(s, "", " ")
}
w.Write(output)
}
type MirrorStats struct {
ID string
Downloads int64
Bytes int64
}
type MirrorStatsPage struct {
List []MirrorStats
MirrorList []Mirror
}
type ByDownloadNumbers struct {
MirrorStatsSlice
}
func (b ByDownloadNumbers) Less(i, j int) bool {
if b.MirrorStatsSlice[i].Downloads > b.MirrorStatsSlice[j].Downloads {
return true
}
return false
}
type MirrorStatsSlice []MirrorStats
func (s MirrorStatsSlice) Len() int { return len(s) }
func (s MirrorStatsSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (h *HTTP) mirrorStatsHandler(w http.ResponseWriter, r *http.Request, ctx *Context) {
rconn := h.redis.pool.Get()
defer rconn.Close()
// Get all mirrors ID
mirrorsIDs, err := redis.Strings(rconn.Do("LRANGE", "MIRRORS", "0", "-1"))
if err != nil {
http.Error(w, "Cannot fetch the list of mirrors", http.StatusInternalServerError)
return
}
// <dlstats>
rconn.Send("MULTI")
// Get all mirrors stats
for _, id := range mirrorsIDs {
today := time.Now().Format("2006_01_02")
rconn.Send("HGET", "STATS_MIRROR_"+today, id)
rconn.Send("HGET", "STATS_MIRROR_BYTES_"+today, id)
}
stats, err := redis.Values(rconn.Do("EXEC"))
if err != nil {
http.Error(w, "Cannot fetch stats", http.StatusInternalServerError)
return
}
var results []MirrorStats
var index int64
for _, id := range mirrorsIDs {
var downloads int64
if v, _ := redis.String(stats[index], nil); v != "" {
downloads, _ = strconv.ParseInt(v, 10, 64)
}
var bytes int64
if v, _ := redis.String(stats[index+1], nil); v != "" {
bytes, _ = strconv.ParseInt(v, 10, 64)
}
s := MirrorStats{
ID: id,
Downloads: downloads,
Bytes: bytes,
}
results = append(results, s)
index += 2
}
sort.Sort(ByDownloadNumbers{results})
// </dlstats>
// <map>
var mirrors []Mirror
mirrors = make([]Mirror, 0, len(mirrorsIDs))
for _, mirrorID := range mirrorsIDs {
var mirror Mirror
reply, err := redis.Values(rconn.Do("HGETALL", fmt.Sprintf("MIRROR_%s", mirrorID)))
if err != nil {
continue
}
if len(reply) == 0 {
err = redis.ErrNil
continue
}
err = redis.ScanStruct(reply, &mirror)
if err != nil {
continue
}
mirror.CountryFields = strings.Fields(mirror.CountryCodes)
mirrors = append(mirrors, mirror)
}
// </map>
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err = ctx.Templates().mirrorstats.ExecuteTemplate(ctx.ResponseWriter(), "base", MirrorStatsPage{results, mirrors})
if err != nil {
log.Error("HTTP error: %s", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}