2022-08-15 06:16:22 +00:00
|
|
|
package redirector
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-15 06:16:22 +00:00
|
|
|
"crypto/x509"
|
2022-03-31 04:56:59 +00:00
|
|
|
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"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
type Config struct {
|
|
|
|
BindAddress string `mapstructure:"bind"`
|
|
|
|
GeoDBPath string `mapstructure:"geodb"`
|
|
|
|
MapFile string `mapstructure:"dl_map"`
|
|
|
|
CacheSize int `mapstructure:"cacheSize"`
|
|
|
|
TopChoices int `mapstructure:"topChoices"`
|
|
|
|
ReloadToken string `mapstructure:"reloadToken"`
|
|
|
|
ServerList []ServerConfig `mapstructure:"servers"`
|
|
|
|
ReloadFunc func()
|
|
|
|
RootCAs *x509.CertPool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProtocolList []string
|
|
|
|
|
|
|
|
func (p ProtocolList) Contains(value string) bool {
|
|
|
|
for _, val := range p {
|
|
|
|
if value == val {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2022-04-02 08:47:14 +00:00
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
func (p ProtocolList) Append(value string) ProtocolList {
|
|
|
|
return append(p, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p ProtocolList) Remove(value string) ProtocolList {
|
|
|
|
index := -1
|
|
|
|
|
|
|
|
for i, val := range p {
|
|
|
|
if value == val {
|
|
|
|
index = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 04:56:59 +00:00
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
if index == -1 {
|
|
|
|
return p
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
p[index] = p[len(p)-1]
|
|
|
|
return p[:len(p)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Redirector) ReloadConfig() error {
|
|
|
|
log.Info("Loading configuration...")
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Load maxmind database
|
|
|
|
if r.db != nil {
|
|
|
|
err = r.db.Close()
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-15 06:16:22 +00:00
|
|
|
return errors.Wrap(err, "Unable to close database")
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2022-03-31 04:56:59 +00:00
|
|
|
// Refresh server cache if size changed
|
2022-08-15 06:16:22 +00:00
|
|
|
if r.serverCache == nil {
|
|
|
|
r.serverCache, err = lru.New(r.config.CacheSize)
|
2022-03-31 04:56:59 +00:00
|
|
|
} else {
|
2022-08-15 06:16:22 +00:00
|
|
|
r.serverCache.Resize(r.config.CacheSize)
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
// Purge the cache to ensure we don't have any invalid servers in it
|
2022-08-15 06:16:22 +00:00
|
|
|
r.serverCache.Purge()
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
// Reload map file
|
2022-08-15 06:16:22 +00:00
|
|
|
if err := r.reloadMap(); err != nil {
|
2022-08-14 07:42:49 +00:00
|
|
|
return errors.Wrap(err, "Unable to load map file")
|
|
|
|
}
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
// Reload server list
|
2022-08-15 06:16:22 +00:00
|
|
|
if err := r.reloadServers(); err != nil {
|
2022-08-14 07:42:49 +00:00
|
|
|
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)
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
for _, server := range r.servers {
|
2022-04-01 04:04:27 +00:00
|
|
|
mirrors[server.Continent] = append(mirrors[server.Continent], server)
|
|
|
|
}
|
|
|
|
|
|
|
|
mirrors["default"] = append(mirrors["NA"], mirrors["EU"]...)
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
r.regionMap = mirrors
|
2022-04-02 08:47:14 +00:00
|
|
|
|
|
|
|
hosts := make(map[string]*Server)
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
for _, server := range r.servers {
|
2022-04-02 08:47:14 +00:00
|
|
|
hosts[server.Host] = server
|
|
|
|
}
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
r.hostMap = hosts
|
2022-04-01 04:04:27 +00:00
|
|
|
|
2022-03-31 04:56:59 +00:00
|
|
|
// Check top choices size
|
2022-08-15 06:16:22 +00:00
|
|
|
if r.config.TopChoices > len(r.servers) {
|
|
|
|
r.config.TopChoices = len(r.servers)
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Force check
|
2022-08-15 06:16:22 +00:00
|
|
|
go r.servers.Check(r.checks)
|
2022-08-14 07:42:49 +00:00
|
|
|
|
|
|
|
return nil
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
func (r *Redirector) reloadServers() error {
|
|
|
|
log.WithField("count", len(r.config.ServerList)).Info("Loading servers")
|
2022-03-31 04:56:59 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
existing := make(map[string]int)
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
for i, server := range r.servers {
|
2022-03-31 04:56:59 +00:00
|
|
|
existing[server.Host] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts := make(map[string]bool)
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
for _, server := range r.config.ServerList {
|
2022-03-31 04:56:59 +00:00
|
|
|
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()
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
s := r.addServer(server, u)
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
if _, ok := existing[u.Host]; ok {
|
2022-08-15 06:16:22 +00:00
|
|
|
s.Redirects = r.servers[i].Redirects
|
2022-03-31 04:56:59 +00:00
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
r.servers[i] = s
|
2022-03-31 04:56:59 +00:00
|
|
|
} else {
|
|
|
|
s.Redirects = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "armbian_router_redirects_" + metricReplacer.Replace(u.Host),
|
|
|
|
Help: "The number of redirects for server " + u.Host,
|
|
|
|
})
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
r.servers = append(r.servers, s)
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
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
|
2022-08-15 06:16:22 +00:00
|
|
|
for i := len(r.servers) - 1; i >= 0; i-- {
|
|
|
|
if _, exists := hosts[r.servers[i].Host]; exists {
|
2022-03-31 04:56:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
2022-08-15 06:16:22 +00:00
|
|
|
"server": r.servers[i].Host,
|
2022-03-31 04:56:59 +00:00
|
|
|
}).Info("Removed server")
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
r.servers = append(r.servers[:i], r.servers[i+1:]...)
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
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.
|
2022-08-15 06:16:22 +00:00
|
|
|
func (r *Redirector) addServer(server ServerConfig, u *url.URL) *Server {
|
2022-03-31 04:56:59 +00:00
|
|
|
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-08-15 06:16:22 +00:00
|
|
|
Protocols: ProtocolList{"http", "https"},
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(server.Protocols) > 0 {
|
|
|
|
for _, proto := range server.Protocols {
|
|
|
|
if !s.Protocols.Contains(proto) {
|
|
|
|
s.Protocols = s.Protocols.Append(proto)
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|
|
|
|
|
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
|
2022-08-15 06:16:22 +00:00
|
|
|
err = r.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-15 06:16:22 +00:00
|
|
|
func (r *Redirector) reloadMap() error {
|
|
|
|
mapFile := r.config.MapFile
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-15 06:16:22 +00:00
|
|
|
r.dlMap = newMap
|
2022-08-14 07:42:49 +00:00
|
|
|
|
|
|
|
return nil
|
2022-03-31 04:56:59 +00:00
|
|
|
}
|