You've already forked armbian-router
mirror of
https://github.com/armbian/armbian-router.git
synced 2026-01-06 10:37:03 -08:00
Bugfixes and more logging
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,4 +4,5 @@ dlrouter-apt.yaml
|
||||
*.yaml
|
||||
!dlrouter.yaml
|
||||
*.exe
|
||||
dlrouter
|
||||
cover.out
|
||||
6
check.go
6
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"
|
||||
|
||||
@@ -74,7 +74,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
config.RootCAs = certs
|
||||
config.SetRootCAs(certs)
|
||||
}
|
||||
|
||||
config.ReloadFunc = func() {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
4
http.go
4
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
|
||||
}
|
||||
|
||||
22
servers.go
22
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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user