2020-06-02 05:47:39 -04:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-08-19 22:32:50 -04:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-08-19 22:22:47 -04:00
|
|
|
"github.com/umahmood/haversine"
|
|
|
|
|
2020-06-02 05:47:39 -04:00
|
|
|
"github.com/librespeed/speedtest/config"
|
|
|
|
"github.com/librespeed/speedtest/results"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-08-19 22:22:47 -04:00
|
|
|
serverCoord haversine.Coord
|
2020-06-02 05:47:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func getRandomData(length int) []byte {
|
|
|
|
data := make([]byte, length)
|
|
|
|
if _, err := rand.Read(data); err != nil {
|
|
|
|
log.Fatalf("Failed to generate random data: %s", err)
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIPInfoURL(address string) string {
|
|
|
|
apiKey := config.LoadedConfig().IPInfoAPIKey
|
|
|
|
|
|
|
|
ipInfoURL := `https://ipinfo.io/%s/json`
|
|
|
|
if address != "" {
|
|
|
|
ipInfoURL = fmt.Sprintf(ipInfoURL, address)
|
|
|
|
} else {
|
|
|
|
ipInfoURL = "https://ipinfo.io/json"
|
|
|
|
}
|
|
|
|
|
|
|
|
if apiKey != "" {
|
|
|
|
ipInfoURL += "?token=" + apiKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipInfoURL
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIPInfo(addr string) results.IPInfoResponse {
|
|
|
|
var ret results.IPInfoResponse
|
|
|
|
resp, err := http.DefaultClient.Get(getIPInfoURL(addr))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting response from ipinfo.io: %s", err)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error reading response from ipinfo.io: %s", err)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if err := json.Unmarshal(raw, &ret); err != nil {
|
|
|
|
log.Errorf("Error parsing response from ipinfo.io: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-08-19 22:22:47 -04:00
|
|
|
func SetServerLocation(conf *config.Config) {
|
2020-06-02 05:47:39 -04:00
|
|
|
if conf.ServerLat > 0 && conf.ServerLng > 0 {
|
|
|
|
log.Infof("Configured server coordinates: %.6f, %.6f", conf.ServerLat, conf.ServerLng)
|
2020-08-19 22:22:47 -04:00
|
|
|
serverCoord.Lat = conf.ServerLat
|
|
|
|
serverCoord.Lon = conf.ServerLng
|
|
|
|
return
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var ret results.IPInfoResponse
|
|
|
|
resp, err := http.DefaultClient.Get(getIPInfoURL(""))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting repsonse from ipinfo.io: %s", err)
|
2020-08-19 22:22:47 -04:00
|
|
|
return
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|
|
|
|
raw, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error reading response from ipinfo.io: %s", err)
|
2020-08-19 22:22:47 -04:00
|
|
|
return
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if err := json.Unmarshal(raw, &ret); err != nil {
|
|
|
|
log.Errorf("Error parsing response from ipinfo.io: %s", err)
|
2020-08-19 22:22:47 -04:00
|
|
|
return
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ret.Location != "" {
|
2020-08-19 22:22:47 -04:00
|
|
|
serverCoord, err = parseLocationString(ret.Location)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot get server coordinates: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|
|
|
|
|
2020-08-19 22:22:47 -04:00
|
|
|
log.Infof("Fetched server coordinates: %.6f, %.6f", serverCoord.Lat, serverCoord.Lon)
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|
|
|
|
|
2020-08-19 22:22:47 -04:00
|
|
|
func parseLocationString(location string) (haversine.Coord, error) {
|
|
|
|
var coord haversine.Coord
|
|
|
|
|
2020-06-02 05:47:39 -04:00
|
|
|
parts := strings.Split(location, ",")
|
|
|
|
if len(parts) != 2 {
|
2020-08-19 22:22:47 -04:00
|
|
|
err := fmt.Errorf("unknown location format: %s", location)
|
|
|
|
log.Error(err)
|
|
|
|
return coord, err
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
lat, err := strconv.ParseFloat(parts[0], 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error parsing latitude: %s", parts[0])
|
2020-08-19 22:22:47 -04:00
|
|
|
return coord, err
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
lng, err := strconv.ParseFloat(parts[1], 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error parsing longitude: %s", parts[0])
|
2020-08-19 22:22:47 -04:00
|
|
|
return coord, err
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|
|
|
|
|
2020-08-19 22:22:47 -04:00
|
|
|
coord.Lat = lat
|
|
|
|
coord.Lon = lng
|
|
|
|
|
|
|
|
return coord, nil
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func calculateDistance(clientLocation string, unit string) string {
|
2020-08-19 22:22:47 -04:00
|
|
|
clientCoord, err := parseLocationString(clientLocation)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error parsing client coordinates: %s", err)
|
|
|
|
return ""
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|
|
|
|
|
2020-08-19 22:22:47 -04:00
|
|
|
dist, km := haversine.Distance(clientCoord, serverCoord)
|
2020-06-02 05:47:39 -04:00
|
|
|
unitString := " mi"
|
2020-08-19 22:22:47 -04:00
|
|
|
|
2020-06-02 05:47:39 -04:00
|
|
|
switch unit {
|
|
|
|
case "km":
|
2020-08-19 22:22:47 -04:00
|
|
|
dist = km
|
2020-06-02 05:47:39 -04:00
|
|
|
unitString = " km"
|
|
|
|
case "NM":
|
2020-08-19 22:22:47 -04:00
|
|
|
dist = km * 0.539957
|
2020-06-02 05:47:39 -04:00
|
|
|
unitString = " NM"
|
|
|
|
}
|
|
|
|
|
2020-08-19 22:22:47 -04:00
|
|
|
return fmt.Sprintf("%.2f%s", dist, unitString)
|
2020-06-02 05:47:39 -04:00
|
|
|
}
|