Skip to content

Commit

Permalink
Bugfixes and more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler committed Oct 18, 2022
1 parent 2992ba6 commit 5ab2215
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ dlrouter-apt.yaml
*.yaml
!dlrouter.yaml
*.exe
dlrouter
cover.out
6 changes: 3 additions & 3 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func main() {
}
}

config.RootCAs = certs
config.SetRootCAs(certs)
}

config.ReloadFunc = func() {
Expand Down
4 changes: 3 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/jmcvetta/randutil"
log "github.com/sirupsen/logrus"
"net"
"net/http"
"net/url"
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
22 changes: 12 additions & 10 deletions servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
}

Expand Down

0 comments on commit 5ab2215

Please sign in to comment.