-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
503 lines (420 loc) · 11.9 KB
/
main.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"
"sync"
"time"
"github.com/hashicorp/hcl"
)
const VERSION = "0.0.6"
type Config struct {
Listen string // HTTP listen address. ":8084"
Data string // Storage location for cached files. "/var/remirror"
Mirrors []Mirror
}
type Mirror struct {
// Prefix specifies a path that should be sent
// to a certain upstream. E.g. "/archlinux/"
Prefix string
// Upstream specifies the upstream protocol and host.
// You may also specify a path, in which case Prefix is
// stripped from the incoming request, and what is left is
// appended to the upstream path component.
//
// E.g. "https://mirrors.kernel.org" (/archlinux/somepackage will be preserved)
// E.g. "http://mirror.cs.umn.edu/arch/" (/archlinux/thing will transform to /arch/thing)
Upstream string
// Upstreams specifies multiple Upstream entries. You can specify both (all will be used).
Upstreams []string
// Local should be used instead of Upstream for a locally served folder.
// Incoming requests will have Prefix stripped off before being sent to Local.
// E.g. "/home/you/localrepos/archlinux"
Local string
// If nil, default match set will be used
Matches []Match
}
type Match struct {
Prefix string
Suffix string
Skip bool // skip = true means this is a "don't match" rule
}
func (mirror Mirror) String() string {
s := mirror.Local
if s == "" {
count := 0
if mirror.Upstream != "" {
s = mirror.Upstream
count++
}
if s == "" && len(mirror.Upstreams) > 0 {
s = mirror.Upstreams[0]
}
count += len(mirror.Upstreams)
if count > 1 {
s += fmt.Sprintf(" (+ %d more...)", count-1)
}
}
s += " "
for i, m := range mirror.Matches {
ss := m.Prefix + "*" + m.Suffix
if m.Skip {
ss += " skip"
}
if i+1 < len(mirror.Matches) {
ss += ", "
}
s += ss
}
return fmt.Sprintf("%-20s » %s", mirror.Prefix, s)
}
var (
http_client = http.Client{}
downloads_mu sync.Mutex
downloads = map[string]*Download{}
)
type Download struct {
resp *http.Response
tmp_path string
tmp_done chan struct{} // will be closed when download is done and final bytes written
}
func (mirror Mirror) should_cache(path string) bool {
// Special rules for Debian/Ubuntu
if strings.HasSuffix(path, "/Packages.gz") || strings.HasSuffix(path, "/Sources.gz") {
return false
}
// Special rules for Arch
if strings.HasSuffix(path, ".abs.tar.gz") ||
strings.HasSuffix(path, ".db.tar.gz") ||
strings.HasSuffix(path, ".files.tar.gz") ||
strings.HasSuffix(path, ".links.tar.gz") {
return false
}
// Use custom match rules?
if len(mirror.Matches) > 0 {
for _, m := range mirror.Matches {
if strings.HasPrefix(path, m.Prefix) &&
strings.HasSuffix(path, m.Suffix) {
return !m.Skip
}
}
return false
}
// Otherwise cache everything that looks like an archive.
if strings.HasSuffix(path, ".xz") ||
strings.HasSuffix(path, ".gz") ||
strings.HasSuffix(path, ".bz2") ||
strings.HasSuffix(path, ".zip") ||
strings.HasSuffix(path, ".tgz") ||
strings.HasSuffix(path, ".rpm") ||
strings.HasSuffix(path, "-rpm.bin") ||
strings.HasSuffix(path, ".deb") ||
strings.HasSuffix(path, ".jar") ||
strings.HasSuffix(path, ".xz.sig") {
return true
}
return false
}
func (mirror Mirror) CreateHandler(config *Config, fileserver http.Handler) (http.Handler, error) {
if mirror.Local != "" {
return http.StripPrefix(mirror.Prefix, http.FileServer(http.Dir(mirror.Local))), nil
}
upstreams := []*url.URL{}
if mirror.Upstream != "" {
upstream, err := url.Parse(mirror.Upstream)
if err != nil {
return nil, err
}
upstreams = append(upstreams, upstream)
}
for _, u := range mirror.Upstreams {
upstream, err := url.Parse(u)
if err != nil {
return nil, err
}
upstreams = append(upstreams, upstream)
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method + " http://" + r.Host + r.RequestURI)
err := func() error {
for _, upstream := range upstreams {
local_path := ""
remote_url := upstream.Scheme + "://" + upstream.Host
// Ugh... This is not the right way to do this.
// I'm not sure how to make it encode + to %,
// while not encoding /
remote_url = strings.Replace(remote_url, "+", "%2B", -1)
if upstream.Path == "" {
remote_url += path.Clean(r.URL.Path)
} else {
remote_url += path.Clean(upstream.Path + "/" + strings.TrimPrefix(r.URL.Path, mirror.Prefix))
}
if mirror.should_cache(remote_url) {
local_path = config.Data + path.Clean(r.URL.Path)
_, err := os.Stat(local_path)
if err == nil {
fileserver.ServeHTTP(w, r)
return nil
}
}
var download *Download
var ok bool
downloads_mu.Lock()
if r.Header.Get("Range") == "" && local_path != "" {
download, ok = downloads[local_path]
if ok {
fh, err := os.Open(download.tmp_path)
downloads_mu.Unlock()
if err != nil {
return err
}
return tmp_download(local_path, w, download, fh)
}
}
// downloads_mu is still locked. take care.
// we need to keep it locked until we have
// registered a download, opened a temp file,
// and saved it's path into the tmp_path in
// the struct.
// then we need to make sure to release.
log.Println("-->", remote_url)
req, err := http.NewRequest("GET", remote_url, nil)
if err != nil {
downloads_mu.Unlock()
return err
}
for k, vs := range r.Header {
if !hopHeaders[k] {
for _, v := range vs {
req.Header.Add(k, v)
}
}
}
resp, err := http_client.Do(req)
if err != nil {
downloads_mu.Unlock()
return err
}
defer resp.Body.Close()
// Try another mirror if we get certain status codes
if resp.StatusCode == 404 ||
resp.StatusCode == 500 ||
resp.StatusCode == 503 {
downloads_mu.Unlock()
continue
}
out := io.Writer(w)
tmp_path := ""
var tmp_needs_final_close io.Closer
// We don't want to cache the result if the server
// returns with a 206 Partial Content
if resp.StatusCode == 200 && local_path != "" {
tmp, err := ioutil.TempFile(config.Data, "remirror_tmp_")
if err != nil {
downloads_mu.Unlock()
return err
}
tmp_needs_final_close = tmp
tmp_path = tmp.Name()
//fmt.Println("tmp", tmp_path)
defer tmp.Close()
defer os.Remove(tmp_path)
out = io.MultiWriter(out, tmp)
// at this point we have a "successful" download in
// progress. save into the struct.
download = &Download{
resp: resp,
tmp_path: tmp_path,
tmp_done: make(chan struct{}),
}
downloads[local_path] = download
}
// release the mutex. if we have a successful download in
// progress, we have stored it correctly so far. if not,
// we unlock, leaving the download struct unmodified. the
// next request to try that URL will retry.
downloads_mu.Unlock()
// however we quit, we want to clear the download in progress
// entry. this deferred func should run before the deferred
// cleanup funcs above, so the filehandle should still be
// valid when we clear it out.
defer func() {
if download == nil {
// we didn't end up using the map for some reason.
// (maybe empty content length, non 200 response, etc)
return
}
// make sure final close has been called. things might still
// be writing, and we need that to be done before
// we close tmp_done
_ = tmp_needs_final_close.Close()
close(download.tmp_done)
downloads_mu.Lock()
delete(downloads, local_path)
downloads_mu.Unlock()
}()
write_resp_headers(w, resp)
n, err := io.Copy(out, resp.Body)
if err != nil {
log.Println(err)
return nil
}
if n != resp.ContentLength && resp.ContentLength != -1 {
log.Printf("Short data returned from server (Content-Length %d received %d)\n", resp.ContentLength, n)
// Not really an HTTP error, leave it up to the client.
// but we aren't going to save our response to the cache.
return nil
}
if tmp_path != "" {
os.MkdirAll(path.Dir(local_path), 0755)
err = tmp_needs_final_close.Close()
if err != nil {
log.Println(err)
return nil
}
// clear from struct before renaming
if download != nil {
close(download.tmp_done)
downloads_mu.Lock()
delete(downloads, local_path)
downloads_mu.Unlock()
download = nil // so we don't re-close
}
err = os.Rename(tmp_path, local_path)
if err != nil {
log.Println(err)
return nil
}
log.Println(">:)")
}
return nil
}
return HTTPError(404)
}()
he, ok := err.(HTTPError)
if ok {
http.Error(w, he.Error(), he.Code())
fmt.Println("\t\t", he.Error())
} else if err != nil {
http.Error(w, err.Error(), 500)
fmt.Println("\t\t500 " + err.Error())
}
}), nil
}
func load_configs(config *Config) error {
try := []string{"remirror.hcl"}
home := os.Getenv("HOME")
if home != "" {
try = append(try, home+"/.remirror.hcl")
}
try = append(try, "/etc/remirror.hcl")
for _, t := range try {
_, err := os.Stat(t)
if err == nil {
log.Printf("Loading configuration from %#v ...\n", t)
config_bytes, err := ioutil.ReadFile(t)
if err != nil {
return err
}
if err := hcl.Unmarshal(config_bytes, config); err != nil {
return err
}
return nil
}
}
return fmt.Errorf("No files found: Create one of %s", strings.Join(try, ", "))
}
func main() {
for _, arg := range os.Args[1:] {
if arg == "--version" {
fmt.Println("remirror", VERSION)
os.Exit(0)
}
fmt.Println("Unhandled argument", arg)
os.Exit(1)
}
config := &Config{}
if err := load_configs(config); err != nil {
log.Fatalf("Config error: %v", err)
}
fileserver := http.FileServer(http.Dir(config.Data))
for _, mirror := range config.Mirrors {
handler, err := mirror.CreateHandler(config, fileserver)
if err == nil {
log.Println(mirror, " ✓ ")
http.Handle(mirror.Prefix, handler)
} else {
log.Println(mirror, " ✗ Error:", err)
}
}
log.Println("remirror listening on HTTP", config.Listen, "with data cache", config.Data)
log.Fatal(http.ListenAndServe(config.Listen, nil))
}
func write_resp_headers(w http.ResponseWriter, resp *http.Response) {
for k, vs := range resp.Header {
if k == "Accept-Ranges" {
continue
}
for _, v := range vs {
//fmt.Printf("proxy back header %#v\t%#v\n", k, v)
w.Header().Add(k, v)
}
}
w.Header().Set("Server", "remirror")
w.WriteHeader(resp.StatusCode)
}
// return a download in progress started by another request
func tmp_download(local_path string, w http.ResponseWriter, download *Download, tmp io.ReadCloser) error {
defer tmp.Close()
write_resp_headers(w, download.resp)
written := int64(0)
done := false
last := time.Now()
for {
n, err := io.Copy(w, tmp)
if n < 0 {
panic(fmt.Sprintf("io.Copy returned n %d: Not what I expected!", n))
}
written += n
if err != nil && err != io.EOF {
log.Printf("Error while reading concurrent download %#s from %#s: %v\n",
local_path, download.tmp_path, err)
// Not an HTTP error: just return, and the client will hopefully
// handle a short read correctly.
return nil
}
if n > 0 {
// cool, try another copy. hopefully the file
// has more bytes now
last = time.Now()
continue
}
if done {
return nil
}
// sleep for a bit so the other download has a chance to write
// more bytes.
select {
case <-time.After(time.Second):
// 60 second timeout for the other goroutine to at least write _something_
if time.Since(last) > time.Minute {
log.Println("Timeout while reading concurrent download %#s from %#s\n",
local_path,
download.tmp_path)
// Not an HTTP error: just return, and the client will hopefully
// handle a short read correctly.
return nil
}
continue
case <-download.tmp_done:
done = true
continue
}
}
}