armbian-router/http.go

138 lines
2.7 KiB
Go
Raw Permalink Normal View History

2022-01-10 04:47:40 +00:00
package main
import (
"encoding/json"
2022-01-10 04:53:23 +00:00
"fmt"
"net"
"net/http"
"net/url"
"path"
2022-01-10 04:47:40 +00:00
)
func statusHandler(w http.ResponseWriter, r *http.Request) {
2022-01-10 04:53:23 +00:00
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
2022-01-10 04:47:40 +00:00
}
func legacyMirrorsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
mirrors := make(map[string][]string)
for _, server := range servers {
u := &url.URL{
Scheme: r.URL.Scheme,
Host: server.Host,
Path: server.Path,
}
mirrors[server.Continent] = append(mirrors[server.Continent], u.String())
}
mirrors["default"] = append(mirrors["NA"], mirrors["EU"]...)
json.NewEncoder(w).Encode(mirrors)
}
func mirrorsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(servers)
}
func redirectHandler(w http.ResponseWriter, r *http.Request) {
2022-01-10 04:53:23 +00:00
ipStr, _, err := net.SplitHostPort(r.RemoteAddr)
2022-01-10 04:47:40 +00:00
2022-01-10 04:53:23 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2022-01-10 04:47:40 +00:00
2022-01-10 04:53:23 +00:00
ip := net.ParseIP(ipStr)
2022-01-10 04:47:40 +00:00
2022-01-10 04:53:23 +00:00
// TODO: This is temporary to allow testing on private addresses.
if ip.IsPrivate() {
ip = net.ParseIP("1.1.1.1")
}
2022-01-10 04:47:40 +00:00
2022-01-10 04:53:23 +00:00
server, distance, err := servers.Closest(ip)
2022-01-10 04:47:40 +00:00
2022-01-10 04:53:23 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2022-01-10 04:47:40 +00:00
scheme := r.URL.Scheme
if scheme == "" {
scheme = "https"
}
redirectPath := path.Join(server.Path, r.URL.Path)
if dlMap != nil {
p := r.URL.Path
if p[0] != '/' {
p = "/" + p
}
if newPath, exists := dlMap[p]; exists {
downloadsMapped.Inc()
redirectPath = path.Join(server.Path, newPath)
}
}
2022-01-10 04:53:23 +00:00
u := &url.URL{
Scheme: scheme,
2022-01-10 04:53:23 +00:00
Host: server.Host,
Path: redirectPath,
2022-01-10 04:53:23 +00:00
}
2022-01-10 04:47:40 +00:00
server.Redirects.Inc()
redirectsServed.Inc()
2022-01-10 04:53:23 +00:00
w.Header().Set("X-Geo-Distance", fmt.Sprintf("%f", distance))
w.Header().Set("Location", u.String())
w.WriteHeader(http.StatusFound)
}
func reloadHandler(w http.ResponseWriter, r *http.Request) {
reloadConfig()
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
2022-01-10 05:15:07 +00:00
func dlMapHandler(w http.ResponseWriter, r *http.Request) {
if dlMap == nil {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(dlMap)
}
func geoIPHandler(w http.ResponseWriter, r *http.Request) {
ipStr, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
ip := net.ParseIP(ipStr)
var city City
err = db.Lookup(ip, &city)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(city)
}