Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
f974949a6c |
23
http.go
23
http.go
@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/jmcvetta/randutil"
|
||||
"github.com/spf13/viper"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@ -90,7 +89,13 @@ func redirectHandler(w http.ResponseWriter, r *http.Request) {
|
||||
redirectPath := path.Join(server.Path, r.URL.Path)
|
||||
|
||||
if dlMap != nil {
|
||||
if newPath, exists := dlMap[strings.TrimLeft(r.URL.Path, "/")]; exists {
|
||||
p := r.URL.Path
|
||||
|
||||
if p[0] != '/' {
|
||||
p = "/" + p
|
||||
}
|
||||
|
||||
if newPath, exists := dlMap[p]; exists {
|
||||
downloadsMapped.Inc()
|
||||
redirectPath = path.Join(server.Path, newPath)
|
||||
}
|
||||
@ -118,20 +123,6 @@ func redirectHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func reloadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
token := r.Header.Get("Authorization")
|
||||
|
||||
if token == "" || !strings.HasPrefix(token, "Bearer") || !strings.Contains(token, " ") {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
token = token[strings.Index(token, " ")+1:]
|
||||
|
||||
if token != viper.GetString("reloadToken") {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
reloadConfig()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
6
main.go
6
main.go
@ -79,20 +79,14 @@ type ServerConfig struct {
|
||||
|
||||
var (
|
||||
configFlag = flag.String("config", "", "configuration file path")
|
||||
flagDebug = flag.Bool("debug", false, "Enable debug logging")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if *flagDebug {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
|
||||
viper.SetDefault("bind", ":8080")
|
||||
viper.SetDefault("cacheSize", 1024)
|
||||
viper.SetDefault("topChoices", 3)
|
||||
viper.SetDefault("reloadKey", randSeq(32))
|
||||
|
||||
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
|
||||
|
3
map.go
3
map.go
@ -4,7 +4,6 @@ import (
|
||||
"encoding/csv"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func loadMap(file string) (map[string]string, error) {
|
||||
@ -33,7 +32,7 @@ func loadMap(file string) (map[string]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m[strings.TrimLeft(row[0], "/")] = strings.TrimLeft(row[1], "/")
|
||||
m[row[0]] = row[1]
|
||||
}
|
||||
|
||||
return m, nil
|
||||
|
42
servers.go
42
servers.go
@ -7,7 +7,6 @@ import (
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
@ -34,7 +33,7 @@ type Server struct {
|
||||
}
|
||||
|
||||
func (server *Server) checkStatus() {
|
||||
req, err := http.NewRequest(http.MethodGet, "http://"+server.Host+"/"+strings.TrimLeft(server.Path, "/"), nil)
|
||||
req, err := http.NewRequest(http.MethodGet, "https://"+server.Host+"/"+strings.TrimLeft(server.Path, "/"), nil)
|
||||
|
||||
req.Header.Set("User-Agent", "ArmbianRouter/1.0 (Go "+runtime.Version()+")")
|
||||
|
||||
@ -73,35 +72,6 @@ func (server *Server) checkStatus() {
|
||||
}
|
||||
|
||||
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 {
|
||||
location := res.Header.Get("Location")
|
||||
|
||||
responseFields["url"] = location
|
||||
|
||||
log.WithFields(responseFields).Debug("Server responded with redirect")
|
||||
|
||||
newUrl, err := url.Parse(location)
|
||||
|
||||
if err != nil {
|
||||
if server.Available {
|
||||
log.WithFields(responseFields).Warning("Server returned invalid url")
|
||||
server.Available = false
|
||||
server.LastChange = time.Now()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if newUrl.Scheme == "https" {
|
||||
if server.Available {
|
||||
responseFields["url"] = location
|
||||
log.WithFields(responseFields).Warning("Server returned https url for http request")
|
||||
server.Available = false
|
||||
server.LastChange = time.Now()
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if !server.Available {
|
||||
server.Available = true
|
||||
server.LastChange = time.Now()
|
||||
@ -191,15 +161,9 @@ func (s ServerList) Closest(ip net.IP) (*Server, float64, error) {
|
||||
return c[i].Distance < c[j].Distance
|
||||
})
|
||||
|
||||
choiceCount := topChoices
|
||||
choices := make([]randutil.Choice, topChoices)
|
||||
|
||||
if len(c) < topChoices {
|
||||
choiceCount = len(c)
|
||||
}
|
||||
|
||||
choices := make([]randutil.Choice, choiceCount)
|
||||
|
||||
for i, item := range c[0:choiceCount] {
|
||||
for i, item := range c[0:topChoices] {
|
||||
if item.Server == nil {
|
||||
continue
|
||||
}
|
||||
|
Reference in New Issue
Block a user