2 Commits

Author SHA1 Message Date
20ae76ff06 Fix distance sorting, add env variable for local IP to test
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2022-03-31 22:43:18 -04:00
b4ed1fc1a3 Check redirect path and add trailing slash if necessary
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2022-03-31 22:23:51 -04:00
2 changed files with 28 additions and 19 deletions

17
http.go
View File

@ -6,7 +6,9 @@ import (
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"os"
"path" "path"
"strings"
) )
func statusHandler(w http.ResponseWriter, r *http.Request) { func statusHandler(w http.ResponseWriter, r *http.Request) {
@ -49,9 +51,14 @@ func redirectHandler(w http.ResponseWriter, r *http.Request) {
ip := net.ParseIP(ipStr) ip := net.ParseIP(ipStr)
// TODO: This is temporary to allow testing on private addresses. if ip.IsLoopback() || ip.IsPrivate() {
if ip.IsPrivate() { overrideIP := os.Getenv("OVERRIDE_IP")
ip = net.ParseIP("1.1.1.1")
if overrideIP == "" {
overrideIP = "1.1.1.1"
}
ip = net.ParseIP(overrideIP)
} }
server, distance, err := servers.Closest(ip) server, distance, err := servers.Closest(ip)
@ -82,6 +89,10 @@ func redirectHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
if strings.HasSuffix(r.URL.Path, "/") && !strings.HasSuffix(redirectPath, "/") {
redirectPath += "/"
}
u := &url.URL{ u := &url.URL{
Scheme: scheme, Scheme: scheme,
Host: server.Host, Host: server.Host,

View File

@ -123,19 +123,6 @@ type ComputedDistance struct {
// DistanceList is a list of Computed Distances with an easy "Choices" func // DistanceList is a list of Computed Distances with an easy "Choices" func
type DistanceList []ComputedDistance type DistanceList []ComputedDistance
func (d DistanceList) Choices() []randutil.Choice {
c := make([]randutil.Choice, len(d))
for i, item := range d {
c[i] = randutil.Choice{
Weight: item.Server.Weight,
Item: item,
}
}
return c
}
// Closest will use GeoIP on the IP provided and find the closest servers. // 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. // When we have a list of x servers closest, we can choose a random or weighted one.
// Return values are the closest server, the distance, and if an error occurred. // Return values are the closest server, the distance, and if an error occurred.
@ -157,18 +144,29 @@ func (s ServerList) Closest(ip net.IP) (*Server, float64, error) {
continue continue
} }
distance := Distance(city.Location.Latitude, city.Location.Longitude, server.Latitude, server.Longitude)
c[i] = ComputedDistance{ c[i] = ComputedDistance{
Server: server, Server: server,
Distance: Distance(city.Location.Latitude, city.Location.Longitude, server.Latitude, server.Longitude), Distance: distance,
} }
} }
// Sort by distance // Sort by distance
sort.Slice(s, func(i int, j int) bool { sort.Slice(c, func(i int, j int) bool {
return c[i].Distance < c[j].Distance return c[i].Distance < c[j].Distance
}) })
choiceInterface = c[0:topChoices].Choices() choices := make([]randutil.Choice, topChoices)
for i, item := range c[0:topChoices] {
choices[i] = randutil.Choice{
Weight: item.Server.Weight,
Item: item,
}
}
choiceInterface = choices
serverCache.Add(ip.String(), choiceInterface) serverCache.Add(ip.String(), choiceInterface)
} }