| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package scan |
| 5 |
|
| 6 |
import ( |
| 7 |
"bufio" |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"net" |
| 12 |
"net/http" |
| 13 |
"strconv" |
| 14 |
"time" |
| 15 |
|
| 16 |
. "github.com/etix/mirrorbits/config" |
| 17 |
"github.com/etix/mirrorbits/core" |
| 18 |
"github.com/etix/mirrorbits/database" |
| 19 |
"github.com/etix/mirrorbits/mirrors" |
| 20 |
"github.com/etix/mirrorbits/utils" |
| 21 |
) |
| 22 |
|
| 23 |
var ( |
| 24 |
userAgent = "Mirrorbits/" + core.VERSION + " TRACE" |
| 25 |
clientTimeout = time.Duration(20 * time.Second) |
| 26 |
clientDeadline = time.Duration(40 * time.Second) |
| 27 |
|
| 28 |
// ErrNoTrace is returned when no trace file is found |
| 29 |
ErrNoTrace = errors.New("No trace file") |
| 30 |
) |
| 31 |
|
| 32 |
// Trace is the internal trace handler |
| 33 |
type Trace struct { |
| 34 |
redis *database.Redis |
| 35 |
transport http.Transport |
| 36 |
httpClient http.Client |
| 37 |
stop <-chan struct{} |
| 38 |
} |
| 39 |
|
| 40 |
// NewTraceHandler returns a new instance of the trace file handler. |
| 41 |
// Trace files are used to compute the time offset between a mirror |
| 42 |
// and the local repository. |
| 43 |
func NewTraceHandler(redis *database.Redis, stop <-chan struct{}) *Trace { |
| 44 |
t := &Trace{ |
| 45 |
redis: redis, |
| 46 |
stop: stop, |
| 47 |
} |
| 48 |
|
| 49 |
t.transport = http.Transport{ |
| 50 |
DisableKeepAlives: true, |
| 51 |
MaxIdleConnsPerHost: 0, |
| 52 |
Dial: func(network, addr string) (net.Conn, error) { |
| 53 |
deadline := time.Now().Add(clientDeadline) |
| 54 |
c, err := net.DialTimeout(network, addr, clientTimeout) |
| 55 |
if err != nil { |
| 56 |
return nil, err |
| 57 |
} |
| 58 |
c.SetDeadline(deadline) |
| 59 |
return c, nil |
| 60 |
}, |
| 61 |
} |
| 62 |
|
| 63 |
t.httpClient = http.Client{ |
| 64 |
Transport: &t.transport, |
| 65 |
} |
| 66 |
|
| 67 |
return t |
| 68 |
} |
| 69 |
|
| 70 |
// GetLastUpdate connects in HTTP to the mirror to get the latest |
| 71 |
// trace file and computes the offset of the mirror. |
| 72 |
func (t *Trace) GetLastUpdate(mirror mirrors.Mirror) error { |
| 73 |
traceFile := GetConfig().TraceFileLocation |
| 74 |
|
| 75 |
if len(traceFile) == 0 { |
| 76 |
return ErrNoTrace |
| 77 |
} |
| 78 |
|
| 79 |
log.Debugf("Getting latest trace file for %s...", mirror.Name) |
| 80 |
|
| 81 |
// Prepare the mirror URL |
| 82 |
var mirrorURL string |
| 83 |
if utils.HasAnyPrefix(mirror.HttpURL, "http://", "https://") { |
| 84 |
mirrorURL = mirror.HttpURL |
| 85 |
} else if mirror.HttpsUp == true { |
| 86 |
mirrorURL = "https://" + mirror.HttpURL |
| 87 |
} else { |
| 88 |
mirrorURL = "http://" + mirror.HttpURL |
| 89 |
} |
| 90 |
|
| 91 |
// Prepare the HTTP request |
| 92 |
req, err := http.NewRequest("GET", utils.ConcatURL(mirrorURL, traceFile), nil) |
| 93 |
req.Header.Set("User-Agent", userAgent) |
| 94 |
req.Close = true |
| 95 |
|
| 96 |
// Prepare contexts |
| 97 |
ctx, cancel := context.WithTimeout(req.Context(), clientDeadline) |
| 98 |
ctx = context.WithValue(ctx, core.ContextMirrorID, mirror.ID) |
| 99 |
req = req.WithContext(ctx) |
| 100 |
defer cancel() |
| 101 |
|
| 102 |
go func() { |
| 103 |
select { |
| 104 |
case <-t.stop: |
| 105 |
cancel() |
| 106 |
case <-ctx.Done(): |
| 107 |
} |
| 108 |
}() |
| 109 |
|
| 110 |
resp, err := t.httpClient.Do(req) |
| 111 |
if err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
defer resp.Body.Close() |
| 115 |
|
| 116 |
scanner := bufio.NewScanner(bufio.NewReader(resp.Body)) |
| 117 |
scanner.Split(bufio.ScanWords) |
| 118 |
scanner.Scan() |
| 119 |
if err := scanner.Err(); err != nil { |
| 120 |
return err |
| 121 |
} |
| 122 |
|
| 123 |
timestamp, err := strconv.ParseInt(scanner.Text(), 10, 64) |
| 124 |
if err != nil { |
| 125 |
return err |
| 126 |
} |
| 127 |
|
| 128 |
conn := t.redis.Get() |
| 129 |
defer conn.Close() |
| 130 |
|
| 131 |
_, err = conn.Do("HSET", fmt.Sprintf("MIRROR_%d", mirror.ID), "lastModTime", timestamp) |
| 132 |
if err != nil { |
| 133 |
return err |
| 134 |
} |
| 135 |
|
| 136 |
// Publish an update on redis |
| 137 |
database.Publish(conn, database.MIRROR_UPDATE, strconv.Itoa(mirror.ID)) |
| 138 |
|
| 139 |
log.Debugf("[%s] trace last sync: %s", mirror.Name, time.Unix(timestamp, 0)) |
| 140 |
return nil |
| 141 |
} |