2022-01-10 04:47:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-03-31 01:33:35 +00:00
|
|
|
"flag"
|
2022-03-31 01:18:25 +00:00
|
|
|
"github.com/chi-middleware/logrus-logger"
|
2022-03-31 01:00:28 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-03-31 04:56:59 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2022-01-10 04:53:23 +00:00
|
|
|
"github.com/oschwald/maxminddb-golang"
|
2022-01-11 06:05:20 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2022-01-10 04:53:23 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"net/http"
|
2022-03-31 01:37:47 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2022-01-10 04:47:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-01-10 04:53:23 +00:00
|
|
|
db *maxminddb.Reader
|
|
|
|
servers ServerList
|
2022-01-10 05:11:15 +00:00
|
|
|
|
|
|
|
dlMap map[string]string
|
2022-01-11 06:05:20 +00:00
|
|
|
|
|
|
|
redirectsServed = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "armbian_router_redirects",
|
|
|
|
Help: "The total number of processed redirects",
|
|
|
|
})
|
|
|
|
|
|
|
|
downloadsMapped = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "armbian_router_download_maps",
|
|
|
|
Help: "The total number of mapped download paths",
|
|
|
|
})
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
serverCache *lru.Cache
|
|
|
|
|
|
|
|
topChoices int
|
2022-01-10 04:47:40 +00:00
|
|
|
)
|
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
type LocationLookup struct {
|
2022-01-10 04:53:23 +00:00
|
|
|
Location struct {
|
|
|
|
Latitude float64 `maxminddb:"latitude"`
|
|
|
|
Longitude float64 `maxminddb:"longitude"`
|
|
|
|
} `maxminddb:"location"`
|
2022-01-10 04:47:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
// City represents a MaxmindDB city
|
|
|
|
type City struct {
|
|
|
|
Continent struct {
|
|
|
|
Code string `maxminddb:"code" json:"code"`
|
|
|
|
GeoNameID uint `maxminddb:"geoname_id" json:"geoname_id"`
|
|
|
|
Names map[string]string `maxminddb:"names" json:"names"`
|
|
|
|
} `maxminddb:"continent" json:"continent"`
|
|
|
|
Country struct {
|
|
|
|
GeoNameID uint `maxminddb:"geoname_id" json:"geoname_id"`
|
|
|
|
IsoCode string `maxminddb:"iso_code" json:"iso_code"`
|
|
|
|
Names map[string]string `maxminddb:"names" json:"names"`
|
|
|
|
} `maxminddb:"country" json:"country"`
|
|
|
|
Location struct {
|
|
|
|
AccuracyRadius uint16 `maxminddb:"accuracy_radius" json:'accuracy_radius'`
|
|
|
|
Latitude float64 `maxminddb:"latitude" json:"latitude"`
|
|
|
|
Longitude float64 `maxminddb:"longitude" json:"longitude"`
|
|
|
|
} `maxminddb:"location"`
|
|
|
|
RegisteredCountry struct {
|
|
|
|
GeoNameID uint `maxminddb:"geoname_id" json:"geoname_id"`
|
|
|
|
IsoCode string `maxminddb:"iso_code" json:"iso_code"`
|
|
|
|
Names map[string]string `maxminddb:"names" json:"names"`
|
|
|
|
} `maxminddb:"registered_country" json:"registered_country"`
|
|
|
|
}
|
|
|
|
|
2022-03-31 04:56:59 +00:00
|
|
|
type ServerConfig struct {
|
|
|
|
Server string `mapstructure:"server" yaml:"server"`
|
|
|
|
Latitude float64 `mapstructure:"latitude" yaml:"latitude"`
|
|
|
|
Longitude float64 `mapstructure:"longitude" yaml:"longitude"`
|
2022-04-01 02:04:19 +00:00
|
|
|
Continent string `mapstructure:"continent"`
|
2022-03-31 04:56:59 +00:00
|
|
|
Weight int `mapstructure:"weight" yaml:"weight"`
|
|
|
|
}
|
|
|
|
|
2022-03-31 01:33:35 +00:00
|
|
|
var (
|
|
|
|
configFlag = flag.String("config", "", "configuration file path")
|
|
|
|
)
|
|
|
|
|
2022-01-10 04:47:40 +00:00
|
|
|
func main() {
|
2022-03-31 01:44:58 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2022-01-10 05:13:42 +00:00
|
|
|
viper.SetDefault("bind", ":8080")
|
2022-03-31 04:56:59 +00:00
|
|
|
viper.SetDefault("cacheSize", 1024)
|
|
|
|
viper.SetDefault("topChoices", 3)
|
2022-01-10 05:13:42 +00:00
|
|
|
|
2022-01-10 04:53:23 +00:00
|
|
|
viper.SetConfigName("dlrouter") // name of config file (without extension)
|
|
|
|
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
|
|
|
|
viper.AddConfigPath("/etc/dlrouter/") // path to look for the config file in
|
|
|
|
viper.AddConfigPath("$HOME/.dlrouter") // call multiple times to add many search paths
|
|
|
|
viper.AddConfigPath(".") // optionally look for config in the working directory
|
2022-03-31 01:33:35 +00:00
|
|
|
|
|
|
|
if *configFlag != "" {
|
|
|
|
viper.SetConfigFile(*configFlag)
|
|
|
|
}
|
|
|
|
|
2022-03-31 04:56:59 +00:00
|
|
|
reloadConfig()
|
2022-01-11 06:05:20 +00:00
|
|
|
|
2022-01-10 04:53:23 +00:00
|
|
|
// Start check loop
|
|
|
|
go servers.checkLoop()
|
|
|
|
|
|
|
|
log.Info("Starting")
|
|
|
|
|
2022-03-31 01:00:28 +00:00
|
|
|
r := chi.NewRouter()
|
2022-01-10 04:53:23 +00:00
|
|
|
|
2022-03-31 01:00:28 +00:00
|
|
|
r.Use(RealIPMiddleware)
|
2022-03-31 01:18:25 +00:00
|
|
|
r.Use(logger.Logger("router", log.StandardLogger()))
|
2022-01-10 04:53:23 +00:00
|
|
|
|
2022-03-31 02:08:51 +00:00
|
|
|
r.Get("/status", statusHandler)
|
2022-04-01 02:04:19 +00:00
|
|
|
r.Get("/mirrors", legacyMirrorsHandler)
|
|
|
|
r.Get("/mirrors.json", mirrorsHandler)
|
2022-03-31 02:08:51 +00:00
|
|
|
r.Post("/reload", reloadHandler)
|
|
|
|
r.Get("/dl_map", dlMapHandler)
|
2022-04-01 02:04:19 +00:00
|
|
|
r.Get("/geoip", geoIPHandler)
|
2022-03-31 02:08:51 +00:00
|
|
|
r.Get("/metrics", promhttp.Handler().ServeHTTP)
|
2022-03-31 02:16:50 +00:00
|
|
|
|
|
|
|
r.NotFound(redirectHandler)
|
2022-03-31 01:00:28 +00:00
|
|
|
|
2022-03-31 01:37:47 +00:00
|
|
|
go http.ListenAndServe(viper.GetString("bind"), r)
|
|
|
|
|
2022-03-31 05:04:04 +00:00
|
|
|
log.Info("Ready")
|
|
|
|
|
2022-03-31 01:37:47 +00:00
|
|
|
c := make(chan os.Signal)
|
|
|
|
|
|
|
|
signal.Notify(c, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGHUP)
|
|
|
|
|
|
|
|
for {
|
|
|
|
sig := <-c
|
|
|
|
|
|
|
|
if sig != syscall.SIGHUP {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-03-31 04:56:59 +00:00
|
|
|
reloadConfig()
|
2022-03-31 01:37:47 +00:00
|
|
|
}
|
2022-01-10 04:47:40 +00:00
|
|
|
}
|