2022-03-31 04:56:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
"github.com/oschwald/maxminddb-golang"
|
2022-08-14 07:42:49 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-03-31 04:56:59 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-08-14 07:42:49 +00:00
|
|
|
func reloadConfig() error {
|
2022-04-02 08:47:14 +00:00
|
|
|
log.Info("Loading configuration...")
|
|
|
|
|
2022-03-31 04:56:59 +00:00
|
|
|
err := viper.ReadInConfig() // Find and read the config file
|
|
|
|
|
|
|
|
if err != nil { // Handle errors reading the config file
|
2022-08-14 07:42:49 +00:00
|
|
|
return errors.Wrap(err, "Unable to read configuration")
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// db will never be reloaded.
|
|
|
|
if db == nil {
|
|
|
|
// Load maxmind database
|
|
|
|
db, err = maxminddb.Open(viper.GetString("geodb"))
|
|
|
|
|
|
|
|
if err != nil {
|
2022-08-14 07:42:49 +00:00
|
|
|
return errors.Wrap(err, "Unable to open database")
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh server cache if size changed
|
|
|
|
if serverCache == nil {
|
|
|
|
serverCache, err = lru.New(viper.GetInt("cacheSize"))
|
|
|
|
} else {
|
|
|
|
serverCache.Resize(viper.GetInt("cacheSize"))
|
|
|
|
}
|
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
// Purge the cache to ensure we don't have any invalid servers in it
|
|
|
|
serverCache.Purge()
|
|
|
|
|
2022-03-31 04:56:59 +00:00
|
|
|
// Set top choice count
|
|
|
|
topChoices = viper.GetInt("topChoices")
|
|
|
|
|
|
|
|
// Reload map file
|
2022-08-14 07:42:49 +00:00
|
|
|
if err := reloadMap(); err != nil {
|
|
|
|
return errors.Wrap(err, "Unable to load map file")
|
|
|
|
}
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
// Reload server list
|
2022-08-14 07:42:49 +00:00
|
|
|
if err := reloadServers(); err != nil {
|
|
|
|
return errors.Wrap(err, "Unable to load servers")
|
|
|
|
}
|
2022-03-31 04:56:59 +00:00
|
|
|
|
2022-04-01 04:04:27 +00:00
|
|
|
// Create mirror map
|
|
|
|
mirrors := make(map[string][]*Server)
|
|
|
|
|
|
|
|
for _, server := range servers {
|
|
|
|
mirrors[server.Continent] = append(mirrors[server.Continent], server)
|
|
|
|
}
|
|
|
|
|
|
|
|
mirrors["default"] = append(mirrors["NA"], mirrors["EU"]...)
|
|
|
|
|
2022-04-02 08:47:14 +00:00
|
|
|
regionMap = mirrors
|
|
|
|
|
|
|
|
hosts := make(map[string]*Server)
|
|
|
|
|
|
|
|
for _, server := range servers {
|
|
|
|
hosts[server.Host] = server
|
|
|
|
}
|
|
|
|
|
|
|
|
hostMap = hosts
|
2022-04-01 04:04:27 +00:00
|
|
|
|
2022-03-31 04:56:59 +00:00
|
|
|
// Check top choices size
|
|
|
|
if topChoices > len(servers) {
|
|
|
|
topChoices = len(servers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force check
|
|
|
|
go servers.Check()
|
2022-08-14 07:42:49 +00:00
|
|
|
|
|
|
|
return nil
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 07:42:49 +00:00
|
|
|
func reloadServers() error {
|
2022-03-31 04:56:59 +00:00
|
|
|
var serverList []ServerConfig
|
2022-08-14 07:42:49 +00:00
|
|
|
|
|
|
|
if err := viper.UnmarshalKey("servers", &serverList); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
existing := make(map[string]int)
|
|
|
|
|
|
|
|
for i, server := range servers {
|
|
|
|
existing[server.Host] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, server := range 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")
|
2022-08-14 07:42:49 +00:00
|
|
|
return err
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hosts[u.Host] = true
|
|
|
|
|
|
|
|
i := -1
|
|
|
|
|
|
|
|
if v, exists := existing[u.Host]; exists {
|
|
|
|
i = v
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(i int, server ServerConfig, u *url.URL) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
s := addServer(server, u)
|
|
|
|
|
|
|
|
if _, ok := existing[u.Host]; ok {
|
|
|
|
s.Redirects = servers[i].Redirects
|
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
|
|
|
servers = append(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(servers) - 1; i >= 0; i-- {
|
|
|
|
if _, exists := hosts[servers[i].Host]; exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"server": servers[i].Host,
|
|
|
|
}).Info("Removed server")
|
|
|
|
|
|
|
|
servers = append(servers[:i], servers[i+1:]...)
|
|
|
|
}
|
2022-08-14 07:42:49 +00:00
|
|
|
|
|
|
|
return nil
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 addServer(server ServerConfig, u *url.URL) *Server {
|
|
|
|
s := &Server{
|
|
|
|
Available: true,
|
|
|
|
Host: u.Host,
|
|
|
|
Path: u.Path,
|
|
|
|
Latitude: server.Latitude,
|
|
|
|
Longitude: server.Longitude,
|
2022-04-01 02:04:19 +00:00
|
|
|
Continent: server.Continent,
|
2022-03-31 04:56:59 +00:00
|
|
|
Weight: server.Weight,
|
|
|
|
}
|
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
// Defaults to 10 to allow servers to be set lower for lower priority
|
2022-03-31 04:56:59 +00:00
|
|
|
if s.Weight == 0 {
|
2022-04-01 02:04:19 +00:00
|
|
|
s.Weight = 10
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
ips, err := net.LookupIP(u.Host)
|
2022-03-31 04:56:59 +00:00
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"error": err,
|
|
|
|
"server": s.Host,
|
|
|
|
}).Warning("Could not resolve address")
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-31 04:56:59 +00:00
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
var city City
|
|
|
|
err = db.Lookup(ips[0], &city)
|
2022-03-31 04:56:59 +00:00
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"error": err,
|
|
|
|
"server": s.Host,
|
|
|
|
"ip": ips[0],
|
|
|
|
}).Warning("Could not geolocate address")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Continent == "" {
|
|
|
|
s.Continent = city.Continent.Code
|
|
|
|
}
|
2022-03-31 04:56:59 +00:00
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
if s.Latitude == 0 && s.Longitude == 0 {
|
2022-03-31 04:56:59 +00:00
|
|
|
s.Latitude = city.Location.Latitude
|
|
|
|
s.Longitude = city.Location.Longitude
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-08-14 07:42:49 +00:00
|
|
|
func reloadMap() error {
|
2022-03-31 04:56:59 +00:00
|
|
|
mapFile := viper.GetString("dl_map")
|
|
|
|
|
|
|
|
if mapFile == "" {
|
2022-08-14 07:42:49 +00:00
|
|
|
return nil
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.WithField("file", mapFile).Info("Loading download map")
|
|
|
|
|
2022-08-14 07:42:49 +00:00
|
|
|
newMap, err := loadMapFile(mapFile)
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-14 07:42:49 +00:00
|
|
|
return err
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dlMap = newMap
|
2022-08-14 07:42:49 +00:00
|
|
|
|
|
|
|
return nil
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|