2022-01-10 04:47:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-01-10 05:11:15 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2022-01-10 05:13:42 +00:00
|
|
|
func statusHandler(w http.ResponseWriter, r *http.Request) {
|
2022-01-10 04:53:23 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2022-04-01 02:04:19 +00:00
|
|
|
w.Write([]byte("OK"))
|
2022-01-10 04:47:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
func legacyMirrorsHandler(w http.ResponseWriter, r *http.Request) {
|
2022-01-10 05:11:15 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2022-04-01 02:04:19 +00:00
|
|
|
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")
|
2022-01-10 05:11:15 +00:00
|
|
|
json.NewEncoder(w).Encode(servers)
|
|
|
|
}
|
|
|
|
|
2022-01-10 05:13:42 +00:00
|
|
|
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
|
|
|
|
2022-03-31 01:01:25 +00:00
|
|
|
scheme := r.URL.Scheme
|
|
|
|
|
|
|
|
if scheme == "" {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
|
2022-01-10 05:11:15 +00:00
|
|
|
redirectPath := path.Join(server.Path, r.URL.Path)
|
|
|
|
|
|
|
|
if dlMap != nil {
|
2022-04-01 02:04:19 +00:00
|
|
|
p := r.URL.Path
|
|
|
|
|
|
|
|
if p[0] != '/' {
|
|
|
|
p = "/" + p
|
|
|
|
}
|
|
|
|
|
|
|
|
if newPath, exists := dlMap[p]; exists {
|
2022-01-11 06:05:20 +00:00
|
|
|
downloadsMapped.Inc()
|
2022-01-10 05:11:15 +00:00
|
|
|
redirectPath = path.Join(server.Path, newPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 04:53:23 +00:00
|
|
|
u := &url.URL{
|
2022-03-31 01:01:25 +00:00
|
|
|
Scheme: scheme,
|
2022-01-10 04:53:23 +00:00
|
|
|
Host: server.Host,
|
2022-01-10 05:11:15 +00:00
|
|
|
Path: redirectPath,
|
2022-01-10 04:53:23 +00:00
|
|
|
}
|
2022-01-10 04:47:40 +00:00
|
|
|
|
2022-01-11 06:05:20 +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)
|
|
|
|
}
|
2022-01-10 05:13:42 +00:00
|
|
|
|
|
|
|
func reloadHandler(w http.ResponseWriter, r *http.Request) {
|
2022-04-01 02:04:19 +00:00
|
|
|
reloadConfig()
|
2022-01-10 05:13:42 +00:00
|
|
|
|
2022-03-31 01:37:47 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte("OK"))
|
2022-01-10 05:13:42 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|
2022-04-01 02:04:19 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|