A bit of cleanup and documentation
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Tyler 2022-01-09 23:53:23 -05:00
parent c98b04f9c1
commit e6d3782450
4 changed files with 257 additions and 251 deletions

View File

@ -22,11 +22,12 @@ func redirectRequest(w http.ResponseWriter, r *http.Request) {
ip := net.ParseIP(ipStr)
// TODO: This is temporary to allow testing on private addresses.
if ip.IsPrivate() {
ip = net.ParseIP("1.1.1.1")
}
server, distance, err := settings.Servers.Closest(ip)
server, distance, err := servers.Closest(ip)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)

17
main.go
View File

@ -12,9 +12,10 @@ import (
var (
db *maxminddb.Reader
settings = &Settings{}
servers ServerList
)
// City represents a MaxmindDB city
type City struct {
Location struct {
Latitude float64 `maxminddb:"latitude"`
@ -22,10 +23,6 @@ type City struct {
} `maxminddb:"location"`
}
type Settings struct {
Servers ServerList
}
func main() {
viper.SetConfigName("dlrouter") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
@ -44,9 +41,9 @@ func main() {
log.WithError(err).Fatalln("Unable to open database")
}
servers := viper.GetStringSlice("servers")
serverList := viper.GetStringSlice("servers")
for _, server := range servers {
for _, server := range serverList {
var prefix string
if !strings.HasPrefix(server, "http") {
@ -84,7 +81,7 @@ func main() {
continue
}
settings.Servers = append(settings.Servers, &Server{
servers = append(servers, &Server{
Host: u.Host,
Path: u.Path,
Latitude: city.Location.Latitude,
@ -101,10 +98,10 @@ func main() {
log.Info("Servers added, checking statuses")
// Force initial check before running
settings.Servers.Check()
servers.Check()
// Start check loop
go settings.Servers.checkLoop()
go servers.checkLoop()
log.Info("Starting")

View File

@ -13,6 +13,8 @@ var (
forwardLimit = 5
)
// RealIPMiddleware is an implementation of reverse proxy checks.
// It uses the remote address to find the originating IP, as well as protocol
func RealIPMiddleware(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Treat unix socket as 127.0.0.1
@ -83,4 +85,3 @@ func realProto(r *http.Request) string {
return proto
}

View File

@ -37,6 +37,9 @@ func (s ServerList) checkLoop() {
}
}
// 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.
func (s ServerList) Check() {
var wg sync.WaitGroup
@ -49,6 +52,8 @@ func (s ServerList) Check() {
req.Header.Set("User-Agent", "ArmbianRouter/1.0 (Go "+runtime.Version()+")")
if err != nil {
// This should never happen.
log.WithError(err).Warning("Invalid request! This should not happen, please check config.")
return
}
@ -72,6 +77,8 @@ func (s ServerList) Check() {
wg.Wait()
}
// Closest will use GeoIP on the IP provided and find the closest server.
// Return values are the closest server, the distance, and if an error occurred.
func (s ServerList) Closest(ip net.IP) (*Server, float64, error) {
var city City
err := db.Lookup(ip, &city)