diff --git a/.gitignore b/.gitignore index 22b7cfc..ea0aadc 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ dlrouter-apt.yaml *.yaml !dlrouter.yaml *.exe +dlrouter cover.out \ No newline at end of file diff --git a/check.go b/check.go index 17414a5..2862c35 100644 --- a/check.go +++ b/check.go @@ -58,8 +58,8 @@ func (r *Redirector) checkHTTPScheme(server *Server, scheme string, logFields lo logFields["responseCode"] = res.StatusCode - if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusMovedPermanently || res.StatusCode == http.StatusFound || res.StatusCode == http.StatusNotFound { - if res.StatusCode == http.StatusMovedPermanently || res.StatusCode == http.StatusFound { + if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusMovedPermanently || res.StatusCode == http.StatusPermanentRedirect || res.StatusCode == http.StatusFound || res.StatusCode == http.StatusNotFound { + if res.StatusCode == http.StatusMovedPermanently || res.StatusCode == http.StatusFound || res.StatusCode == http.StatusPermanentRedirect { location := res.Header.Get("Location") logFields["url"] = location @@ -137,7 +137,7 @@ func (r *Redirector) checkTLS(server *Server, logFields log.Fields) (bool, error "server": server.Host, "host": host, "port": port, - }).Info("Checking TLS server") + }).Debug("Checking TLS server") if port == "" { port = "443" diff --git a/cmd/main.go b/cmd/main.go index e371aa2..1d5a482 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -74,7 +74,7 @@ func main() { } } - config.RootCAs = certs + config.SetRootCAs(certs) } config.ReloadFunc = func() { diff --git a/config.go b/config.go index 589d53f..3bf6034 100644 --- a/config.go +++ b/config.go @@ -204,7 +204,9 @@ func (r *Redirector) ReloadConfig() error { r.hostMap = hosts // Check top choices size - if r.config.TopChoices > len(r.servers) { + if r.config.TopChoices == 0 { + r.config.TopChoices = 3 + } else if r.config.TopChoices > len(r.servers) { r.config.TopChoices = len(r.servers) } diff --git a/http.go b/http.go index 9a2b144..def8ec7 100644 --- a/http.go +++ b/http.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "github.com/jmcvetta/randutil" + log "github.com/sirupsen/logrus" "net" "net/http" "net/url" @@ -28,6 +29,7 @@ func (r *Redirector) redirectHandler(w http.ResponseWriter, req *http.Request) { ipStr, _, err := net.SplitHostPort(req.RemoteAddr) if err != nil { + log.WithFields(log.Fields{"error": err, "remote": req.RemoteAddr}).Warning("Unable to parse host/port from request") http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -70,6 +72,7 @@ func (r *Redirector) redirectHandler(w http.ResponseWriter, req *http.Request) { choice, err := randutil.WeightedChoice(choices) if err != nil { + log.WithError(err).Warning("Unable to find a weighted choice for region") http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -92,6 +95,7 @@ func (r *Redirector) redirectHandler(w http.ResponseWriter, req *http.Request) { server, distance, err = r.servers.Closest(r, scheme, ip) if err != nil { + log.WithError(err).Warning("Unable to find closest server") http.Error(w, err.Error(), http.StatusInternalServerError) return } diff --git a/servers.go b/servers.go index 63cc62f..1f1df22 100644 --- a/servers.go +++ b/servers.go @@ -21,8 +21,8 @@ type Server struct { Weight int `json:"weight"` Continent string `json:"continent"` Protocols ProtocolList `json:"protocols"` - IncludeASN ASNList `json:"includeASN"` - ExcludeASN ASNList `json:"excludeASN"` + IncludeASN ASNList `json:"includeASN,omitempty"` + ExcludeASN ASNList `json:"excludeASN,omitempty"` Redirects prometheus.Counter `json:"-"` LastChange time.Time `json:"lastChange"` } @@ -125,6 +125,7 @@ func (s ServerList) Closest(r *Redirector, scheme string, ip net.IP) (*Server, f err := r.db.Lookup(ip, &city) if err != nil { + log.WithError(err).Warning("Unable to lookup location information") return nil, -1, err } @@ -135,28 +136,30 @@ func (s ServerList) Closest(r *Redirector, scheme string, ip net.IP) (*Server, f err = r.asnDB.Lookup(ip, &asn) if err != nil { + log.WithError(err).Warning("Unable to load ASN information") return nil, -1, err } hasASN = true } - c := make(DistanceList, len(s)) + c := make(DistanceList, 0) - for i, server := range s { + for _, server := range s { if !server.Available || !server.Protocols.Contains(scheme) || len(server.IncludeASN) > 0 && hasASN && !server.IncludeASN.Contains(asn.AutonomousSystemNumber) || len(server.ExcludeASN) > 0 && hasASN && server.ExcludeASN.Contains(asn.AutonomousSystemNumber) { + log.WithField("host", server.Host).WithField("proto", scheme).Debug("Skipping server due to protocol not containing supported protocol") continue } distance := Distance(city.Location.Latitude, city.Location.Longitude, server.Latitude, server.Longitude) - c[i] = ComputedDistance{ + c = append(c, ComputedDistance{ Server: server, Distance: distance, - } + }) } // Sort by distance @@ -170,13 +173,11 @@ func (s ServerList) Closest(r *Redirector, scheme string, ip net.IP) (*Server, f choiceCount = len(c) } + log.WithFields(log.Fields{"count": len(c)}).Debug("Picking from top choices") + choices := make([]randutil.Choice, choiceCount) for i, item := range c[0:choiceCount] { - if item.Server == nil { - continue - } - choices[i] = randutil.Choice{ Weight: item.Server.Weight, Item: item, @@ -191,6 +192,7 @@ func (s ServerList) Closest(r *Redirector, scheme string, ip net.IP) (*Server, f choice, err := randutil.WeightedChoice(choiceInterface.([]randutil.Choice)) if err != nil { + log.WithError(err).Warning("Unable to choose a weighted choice") return nil, -1, err }