forked from armbian/armbian-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
351 lines (267 loc) · 7.69 KB
/
config.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
package redirector
import (
"crypto/tls"
"crypto/x509"
"github.com/armbian/redirector/db"
lru "github.com/hashicorp/golang-lru"
"github.com/oschwald/maxminddb-golang"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
)
// Config represents our application's configuration.
type Config struct {
// BindAddress is the address to bind our webserver to.
BindAddress string `mapstructure:"bind"`
// GeoDBPath is the path to the MaxMind GeoLite2 City DB.
GeoDBPath string `mapstructure:"geodb"`
// ASNDBPath is the path to the GeoLite2 ASN DB.
ASNDBPath string `mapstructure:"asndb"`
// MapFile is a file used to map download urls via redirect.
MapFile string `mapstructure:"dl_map"`
// CacheSize is the number of items to keep in the LRU cache.
CacheSize int `mapstructure:"cacheSize"`
// TopChoices is the number of servers to use in a rotation.
// With the default being 3, the top 3 servers will be rotated based on weight.
TopChoices int `mapstructure:"topChoices"`
// ReloadToken is a secret token used for web-based reload.
ReloadToken string `mapstructure:"reloadToken"`
// CheckURL is the url used to verify mirror versions
CheckURL string `mapstructure:"checkUrl"`
// ServerList is a list of ServerConfig structs, which gets parsed into servers.
ServerList []ServerConfig `mapstructure:"servers"`
// ReloadFunc is called when a reload is done via http api.
ReloadFunc func()
// RootCAs is a list of CA certificates, which we parse from Mozilla directly.
RootCAs *x509.CertPool
checkClient *http.Client
}
// SetRootCAs sets the root ca files, and creates the http client for checks
// This **MUST** be called before r.checkClient is used.
func (c *Config) SetRootCAs(cas *x509.CertPool) {
c.RootCAs = cas
t := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: cas,
},
}
c.checkClient = &http.Client{
Transport: t,
Timeout: 20 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
}
func Remove[V comparable](collection []V, value V) []V {
return lo.Filter(collection, func(item V, _ int) bool {
return item != value
})
}
// ReloadConfig is called to reload the server's configuration.
func (r *Redirector) ReloadConfig() error {
log.Info("Loading configuration...")
var err error
// Load maxmind database
if r.db != nil {
err = r.db.Close()
if err != nil {
return errors.Wrap(err, "Unable to close database")
}
}
if r.asnDB != nil {
err = r.asnDB.Close()
if err != nil {
return errors.Wrap(err, "Unable to close asn database")
}
}
// db can be hot-reloaded if the file changed
r.db, err = maxminddb.Open(r.config.GeoDBPath)
if err != nil {
return errors.Wrap(err, "Unable to open database")
}
if r.config.ASNDBPath != "" {
r.asnDB, err = maxminddb.Open(r.config.ASNDBPath)
if err != nil {
return errors.Wrap(err, "Unable to open asn database")
}
}
// Refresh server cache if size changed
if r.serverCache == nil {
r.serverCache, err = lru.New(r.config.CacheSize)
} else {
r.serverCache.Resize(r.config.CacheSize)
}
// Purge the cache to ensure we don't have any invalid servers in it
r.serverCache.Purge()
// Reload map file
if err := r.reloadMap(); err != nil {
return errors.Wrap(err, "Unable to load map file")
}
// Reload server list
if err := r.reloadServers(); err != nil {
return errors.Wrap(err, "Unable to load servers")
}
// Create mirror map
mirrors := make(map[string][]*Server)
for _, server := range r.servers {
mirrors[server.Continent] = append(mirrors[server.Continent], server)
}
mirrors["default"] = append(mirrors["NA"], mirrors["EU"]...)
r.regionMap = mirrors
hosts := make(map[string]*Server)
for _, server := range r.servers {
hosts[server.Host] = server
}
r.hostMap = hosts
// Check top choices size
if r.config.TopChoices == 0 {
r.config.TopChoices = 3
} else if r.config.TopChoices > len(r.servers) {
r.config.TopChoices = len(r.servers)
}
// Force check
go r.servers.Check(r, r.checks)
return nil
}
func (r *Redirector) reloadServers() error {
log.WithField("count", len(r.config.ServerList)).Info("Loading servers")
var wg sync.WaitGroup
existing := make(map[string]int)
for i, server := range r.servers {
existing[server.Host] = i
}
hosts := make(map[string]bool)
var hostsLock sync.Mutex
for _, server := range r.config.ServerList {
wg.Add(1)
var prefix string
if !strings.HasPrefix(server.Server, "http") {
prefix = "https://"
}
u, err := url.Parse(prefix + server.Server)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"server": server,
}).Warning("Server is invalid")
return err
}
i := -1
if v, exists := existing[u.Host]; exists {
i = v
}
go func(i int, server ServerConfig, u *url.URL) {
defer wg.Done()
s, err := r.addServer(server, u)
if err != nil {
log.WithError(err).Warning("Unable to add server")
return
}
hostsLock.Lock()
hosts[u.Host] = true
hostsLock.Unlock()
if _, ok := existing[u.Host]; ok {
s.Redirects = r.servers[i].Redirects
r.servers[i] = s
} else {
s.Redirects = promauto.NewCounter(prometheus.CounterOpts{
Name: "armbian_router_redirects_" + metricReplacer.Replace(u.Host),
Help: "The number of redirects for server " + u.Host,
})
r.servers = append(r.servers, s)
log.WithFields(log.Fields{
"server": u.Host,
"path": u.Path,
"latitude": s.Latitude,
"longitude": s.Longitude,
}).Info("Added server")
}
}(i, server, u)
}
wg.Wait()
// Remove servers that no longer exist in the config
for i := len(r.servers) - 1; i >= 0; i-- {
if _, exists := hosts[r.servers[i].Host]; exists {
continue
}
log.WithFields(log.Fields{
"server": r.servers[i].Host,
}).Info("Removed server")
r.servers = append(r.servers[:i], r.servers[i+1:]...)
}
return nil
}
var metricReplacer = strings.NewReplacer(".", "_", "-", "_")
// addServer takes ServerConfig and constructs a server.
// This will create duplicate servers, but it will overwrite existing ones when changed.
func (r *Redirector) addServer(server ServerConfig, u *url.URL) (*Server, error) {
s := &Server{
Available: true,
Host: u.Host,
Path: u.Path,
Latitude: server.Latitude,
Longitude: server.Longitude,
Continent: server.Continent,
Weight: server.Weight,
Protocols: []string{"http", "https"},
}
if len(server.Protocols) > 0 {
for _, proto := range server.Protocols {
if !lo.Contains(s.Protocols, proto) {
s.Protocols = append(s.Protocols, proto)
}
}
}
// Defaults to 10 to allow servers to be set lower for lower priority
if s.Weight == 0 {
s.Weight = 10
}
ips, err := net.LookupIP(u.Host)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"server": s.Host,
}).Warning("Could not resolve address")
return nil, err
}
var city db.City
err = r.db.Lookup(ips[0], &city)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"server": s.Host,
"ip": ips[0],
}).Warning("Could not geolocate address")
return nil, err
}
if s.Continent == "" {
s.Continent = city.Continent.Code
}
if s.Latitude == 0 && s.Longitude == 0 {
s.Latitude = city.Location.Latitude
s.Longitude = city.Location.Longitude
}
return s, nil
}
func (r *Redirector) reloadMap() error {
mapFile := r.config.MapFile
if mapFile == "" {
return nil
}
log.WithField("file", mapFile).Info("Loading download map")
newMap, err := loadMapFile(mapFile)
if err != nil {
return err
}
r.dlMap = newMap
return nil
}