forked from etix/mirrorbits
/
utils.go
240 lines (201 loc) · 5.11 KB
/
utils.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
// Copyright (c) 2014 Ludovic Fauvet
// Licensed under the MIT license
package main
import (
"bufio"
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"github.com/garyburd/redigo/redis"
"io"
"math"
"os"
"strings"
"time"
)
const (
DegToRad = 0.017453292519943295769236907684886127134428718885417 // N[Pi/180, 50]
RadToDeg = 57.295779513082320876798154814105170332405472466564 // N[180/Pi, 50]
)
var welcome = ` _______ __ __ __ __
| | |__|.----.----.-----.----.| |--.|__| |_.-----.
| | || _| _| _ | _|| _ || | _|__ --|
|__|_|__|__||__| |__| |_____|__| |_____||__|____|_____|
`
func enableMirror(id string) error {
return setMirrorEnabled(id, true)
}
func disableMirror(id string) error {
return setMirrorEnabled(id, false)
}
func setMirrorEnabled(id string, state bool) error {
r := NewRedis()
conn, err := r.connect()
if err != nil {
return err
}
defer conn.Close()
key := fmt.Sprintf("MIRROR_%s", id)
_, err = conn.Do("HMSET", key, "enabled", state)
// Publish update
conn.Do("PUBLISH", MIRROR_UPDATE, id)
return err
}
func markMirrorUp(id string) error {
return setMirrorState(id, true, "")
}
func markMirrorDown(id string, reason string) error {
return setMirrorState(id, false, reason)
}
func setMirrorState(id string, state bool, reason string) error {
r := NewRedis()
conn, err := r.connect()
if err != nil {
return err
}
defer conn.Close()
key := fmt.Sprintf("MIRROR_%s", id)
previousState, err := redis.Bool(conn.Do("HGET", key, "up"))
if err != nil {
return err
}
var args []interface{}
args = append(args, key, "up", state, "excludeReason", reason)
if state != previousState {
args = append(args, "stateSince", time.Now().Unix())
}
_, err = conn.Do("HMSET", args...)
if state != previousState {
// Publish update
conn.Do("PUBLISH", MIRROR_UPDATE, id)
}
return err
}
// Add a trailing slash to the URL
func normalizeURL(url string) string {
if url != "" && !strings.HasSuffix(url, "/") {
url += "/"
}
return url
}
// Generate a human readable sha1 hash of the given file path
func hashFile(path string) (hash string, err error) {
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
reader := bufio.NewReader(f)
sha1Hash := sha1.New()
_, err = io.Copy(sha1Hash, reader)
if err != nil {
return
}
hash = hex.EncodeToString(sha1Hash.Sum(nil))
return
}
// Return the distance in km between two coordinates
func getDistanceKm(lat1, lon1, lat2, lon2 float32) float32 {
var R float32 = 6371 // radius of the earth in Km
dLat := (lat2 - lat1) * float32(DegToRad)
dLon := (lon2 - lon1) * float32(DegToRad)
a := math.Sin(float64(dLat/2))*math.Sin(float64(dLat/2)) + math.Cos(float64(lat1*DegToRad))*math.Cos(float64(lat2*DegToRad))*math.Sin(float64(dLon/2))*math.Sin(float64(dLon/2))
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
return R * float32(c)
}
func getMirrorMapUrl(mirrors Mirrors, clientInfo GeoIPRec) string {
var buffer bytes.Buffer
buffer.WriteString("//maps.googleapis.com/maps/api/staticmap?size=520x320&sensor=false&visual_refresh=true")
if clientInfo.isValid() {
buffer.WriteString(fmt.Sprintf("&markers=size:mid|color:red|%f,%f", clientInfo.Latitude, clientInfo.Longitude))
}
count := 1
for i, mirror := range mirrors {
if count > 9 {
break
}
if i == 0 && clientInfo.isValid() {
// Draw a path between the client and the mirror
buffer.WriteString(fmt.Sprintf("&path=color:0x17ea0bdd|weight:5|%f,%f|%f,%f",
clientInfo.Latitude, clientInfo.Longitude,
mirror.Latitude, mirror.Longitude))
}
color := "blue"
if mirror.Weight > 0 {
color = "green"
}
buffer.WriteString(fmt.Sprintf("&markers=color:%s|label:%d|%f,%f", color, count, mirror.Latitude, mirror.Longitude))
count++
}
return buffer.String()
}
func min(v1, v2 int) int {
if v1 < v2 {
return v1
}
return v2
}
func max(v1, v2 int) int {
if v1 > v2 {
return v1
}
return v2
}
func add(x, y int) int {
return x + y
}
func version() string {
return VERSION
}
// Return true is `a` is contained in `list`
// Warning: this is slow, don't use it for long datasets
func isInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func isAdditionalCountry(clientInfo GeoIPRec, list []string) bool {
if clientInfo.GeoIPRecord == nil {
return false
}
for i, b := range list {
if i > 0 && b == clientInfo.CountryCode {
return true
}
}
return false
}
func isPrimaryCountry(clientInfo GeoIPRec, list []string) bool {
if clientInfo.GeoIPRecord == nil {
return false
}
if len(list) > 0 && list[0] == clientInfo.CountryCode {
return true
}
return false
}
// Return true if a stop has been requested
func isStopped(stop chan bool) bool {
select {
case <-stop:
return true
default:
return false
}
}
// Return a file size in a human readable form
func readableSize(value int64) string {
units := []string{"bytes", "KB", "MB", "GB", "TB"}
v := float64(value)
for _, u := range units {
if v < 1024 || u == "TB" {
return fmt.Sprintf("%3.1f %s", v, u)
}
v /= 1024
}
return ""
}