2022-01-10 04:47:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-03-31 04:56:59 +00:00
|
|
|
"github.com/jmcvetta/randutil"
|
2022-01-11 06:05:20 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-01-10 04:53:23 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-03-31 04:56:59 +00:00
|
|
|
"sort"
|
2022-01-10 04:53:23 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2022-01-10 04:47:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-01-10 04:53:23 +00:00
|
|
|
checkClient = &http.Client{
|
2022-04-02 08:47:14 +00:00
|
|
|
Timeout: 20 * time.Second,
|
2022-08-06 20:19:12 +00:00
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
checks = []serverCheck{
|
|
|
|
checkHttp,
|
|
|
|
checkTLS,
|
2022-01-10 04:53:23 +00:00
|
|
|
}
|
2022-01-10 04:47:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2022-04-02 08:47:14 +00:00
|
|
|
Available bool `json:"available"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Latitude float64 `json:"latitude"`
|
|
|
|
Longitude float64 `json:"longitude"`
|
|
|
|
Weight int `json:"weight"`
|
|
|
|
Continent string `json:"continent"`
|
|
|
|
Redirects prometheus.Counter `json:"-"`
|
|
|
|
LastChange time.Time `json:"lastChange"`
|
2022-01-10 04:47:40 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
type serverCheck func(server *Server, logFields log.Fields) (bool, error)
|
2022-01-11 06:10:36 +00:00
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
// checkStatus runs all status checks against a server
|
|
|
|
func (server *Server) checkStatus() {
|
|
|
|
logFields := log.Fields{
|
|
|
|
"host": server.Host,
|
2022-01-11 06:10:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
var res bool
|
|
|
|
var err error
|
2022-01-11 06:10:36 +00:00
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
for _, check := range checks {
|
|
|
|
res, err = check(server, logFields)
|
2022-01-11 06:10:36 +00:00
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
if err != nil {
|
|
|
|
logFields["error"] = err
|
2022-01-11 06:10:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
if !res {
|
|
|
|
break
|
|
|
|
}
|
2022-01-11 06:10:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
if !res {
|
|
|
|
if server.Available {
|
|
|
|
log.WithFields(logFields).Info("Server went offline")
|
2022-04-02 18:35:46 +00:00
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
server.Available = false
|
|
|
|
server.LastChange = time.Now()
|
|
|
|
} else {
|
|
|
|
log.WithFields(logFields).Debug("Server is still offline")
|
2022-04-02 18:35:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
return
|
|
|
|
} else {
|
2022-01-11 06:10:36 +00:00
|
|
|
if !server.Available {
|
|
|
|
server.Available = true
|
2022-04-02 08:47:14 +00:00
|
|
|
server.LastChange = time.Now()
|
2022-08-06 20:19:12 +00:00
|
|
|
log.WithFields(logFields).Info("Server is online")
|
2022-01-11 06:10:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:47:40 +00:00
|
|
|
type ServerList []*Server
|
|
|
|
|
|
|
|
func (s ServerList) checkLoop() {
|
2022-01-10 04:53:23 +00:00
|
|
|
t := time.NewTicker(60 * time.Second)
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-01-10 04:53:23 +00:00
|
|
|
for {
|
2022-01-11 06:05:20 +00:00
|
|
|
<-t.C
|
2022-03-31 04:56:59 +00:00
|
|
|
s.Check()
|
2022-01-10 04:53:23 +00:00
|
|
|
}
|
2022-01-10 04:47:40 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 04:53:23 +00:00
|
|
|
// Check will request the index from all servers
|
|
|
|
// If a server does not respond in 10 seconds, it is considered offline.
|
|
|
|
// This will wait until all checks are complete.
|
2022-01-10 04:47:40 +00:00
|
|
|
func (s ServerList) Check() {
|
2022-01-10 04:53:23 +00:00
|
|
|
var wg sync.WaitGroup
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-01-10 04:53:23 +00:00
|
|
|
for _, server := range s {
|
|
|
|
wg.Add(1)
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-01-10 04:53:23 +00:00
|
|
|
go func(server *Server) {
|
2022-01-11 06:05:20 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2022-01-11 06:10:36 +00:00
|
|
|
server.checkStatus()
|
2022-01-10 04:53:23 +00:00
|
|
|
}(server)
|
|
|
|
}
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-01-10 04:53:23 +00:00
|
|
|
wg.Wait()
|
2022-01-10 04:47:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 04:56:59 +00:00
|
|
|
// ComputedDistance is a wrapper that contains a Server and Distance.
|
|
|
|
type ComputedDistance struct {
|
|
|
|
Server *Server
|
|
|
|
Distance float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// DistanceList is a list of Computed Distances with an easy "Choices" func
|
|
|
|
type DistanceList []ComputedDistance
|
|
|
|
|
|
|
|
// Closest will use GeoIP on the IP provided and find the closest servers.
|
|
|
|
// When we have a list of x servers closest, we can choose a random or weighted one.
|
2022-01-10 04:53:23 +00:00
|
|
|
// Return values are the closest server, the distance, and if an error occurred.
|
2022-01-10 04:47:40 +00:00
|
|
|
func (s ServerList) Closest(ip net.IP) (*Server, float64, error) {
|
2022-04-01 02:04:19 +00:00
|
|
|
choiceInterface, exists := serverCache.Get(ip.String())
|
2022-03-31 04:56:59 +00:00
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
if !exists {
|
|
|
|
var city LocationLookup
|
|
|
|
err := db.Lookup(ip, &city)
|
2022-03-31 04:56:59 +00:00
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
c := make(DistanceList, len(s))
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
for i, server := range s {
|
|
|
|
if !server.Available {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-04-01 02:43:18 +00:00
|
|
|
distance := Distance(city.Location.Latitude, city.Location.Longitude, server.Latitude, server.Longitude)
|
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
c[i] = ComputedDistance{
|
|
|
|
Server: server,
|
2022-04-01 02:43:18 +00:00
|
|
|
Distance: distance,
|
2022-04-01 02:04:19 +00:00
|
|
|
}
|
2022-01-10 04:53:23 +00:00
|
|
|
}
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
// Sort by distance
|
2022-04-01 02:43:18 +00:00
|
|
|
sort.Slice(c, func(i int, j int) bool {
|
2022-04-01 02:04:19 +00:00
|
|
|
return c[i].Distance < c[j].Distance
|
|
|
|
})
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-04-02 18:35:46 +00:00
|
|
|
choiceCount := topChoices
|
|
|
|
|
|
|
|
if len(c) < topChoices {
|
|
|
|
choiceCount = len(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
choices := make([]randutil.Choice, choiceCount)
|
2022-04-01 02:43:18 +00:00
|
|
|
|
2022-04-02 18:35:46 +00:00
|
|
|
for i, item := range c[0:choiceCount] {
|
2022-04-01 04:04:27 +00:00
|
|
|
if item.Server == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-01 02:43:18 +00:00
|
|
|
choices[i] = randutil.Choice{
|
|
|
|
Weight: item.Server.Weight,
|
|
|
|
Item: item,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
choiceInterface = choices
|
2022-03-31 04:56:59 +00:00
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
serverCache.Add(ip.String(), choiceInterface)
|
|
|
|
}
|
|
|
|
|
|
|
|
choice, err := randutil.WeightedChoice(choiceInterface.([]randutil.Choice))
|
2022-03-31 04:56:59 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dist := choice.Item.(ComputedDistance)
|
|
|
|
|
2022-08-06 20:19:12 +00:00
|
|
|
if !dist.Server.Available {
|
|
|
|
// Choose a new server and refresh cache
|
|
|
|
serverCache.Remove(ip.String())
|
|
|
|
|
|
|
|
return s.Closest(ip)
|
|
|
|
}
|
|
|
|
|
2022-03-31 04:56:59 +00:00
|
|
|
return dist.Server, dist.Distance, nil
|
2022-01-10 04:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// haversin(θ) function
|
|
|
|
func hsin(theta float64) float64 {
|
2022-01-10 04:53:23 +00:00
|
|
|
return math.Pow(math.Sin(theta/2), 2)
|
2022-01-10 04:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Distance function returns the distance (in meters) between two points of
|
|
|
|
// a given longitude and latitude relatively accurately (using a spherical
|
2022-08-06 20:19:12 +00:00
|
|
|
// approximation of the Earth) through the Haversine Distance Formula for
|
2022-01-10 04:47:40 +00:00
|
|
|
// great arc distance on a sphere with accuracy for small distances
|
|
|
|
//
|
|
|
|
// point coordinates are supplied in degrees and converted into rad. in the func
|
|
|
|
//
|
|
|
|
// distance returned is METERS!!!!!!
|
|
|
|
// http://en.wikipedia.org/wiki/Haversine_formula
|
|
|
|
func Distance(lat1, lon1, lat2, lon2 float64) float64 {
|
2022-01-10 04:53:23 +00:00
|
|
|
// convert to radians
|
|
|
|
// must cast radius as float to multiply later
|
|
|
|
var la1, lo1, la2, lo2, r float64
|
|
|
|
la1 = lat1 * math.Pi / 180
|
|
|
|
lo1 = lon1 * math.Pi / 180
|
|
|
|
la2 = lat2 * math.Pi / 180
|
|
|
|
lo2 = lon2 * math.Pi / 180
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-01-10 04:53:23 +00:00
|
|
|
r = 6378100 // Earth radius in METERS
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-01-10 04:53:23 +00:00
|
|
|
// calculate
|
|
|
|
h := hsin(la2-la1) + math.Cos(la1)*math.Cos(la2)*hsin(lo2-lo1)
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-01-10 04:53:23 +00:00
|
|
|
return 2 * r * math.Asin(math.Sqrt(h))
|
|
|
|
}
|