You've already forked speedtest-go
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
fc6e99b45c | |||
c815103e20 | |||
19432dbb37 | |||
31d51deba5 | |||
cf93e8c545 | |||
b138d9b6bc |
@ -23,7 +23,7 @@ Works with mobile versions too.
|
||||
* Results sharing (optional)
|
||||
* Multiple Points of Test (optional)
|
||||
* Compatible with PHP frontend predefined endpoints (with `.php` suffixes)
|
||||
* Supports [Proxy Protocol](https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt)
|
||||
* Supports [Proxy Protocol](https://www.haproxy.org/download/2.3/doc/proxy-protocol.txt) (without TLV support yet)
|
||||
|
||||

|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@ -30,6 +28,7 @@ type Config struct {
|
||||
}
|
||||
|
||||
var (
|
||||
configFile string = ""
|
||||
loadedConfig *Config = nil
|
||||
)
|
||||
|
||||
@ -51,8 +50,12 @@ func init() {
|
||||
viper.AddConfigPath(".")
|
||||
}
|
||||
|
||||
func Load() Config {
|
||||
func Load(configPath string) Config {
|
||||
var conf Config
|
||||
|
||||
configFile = configPath
|
||||
viper.SetConfigFile(configPath)
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
log.Warnf("No config file found in search paths, using default values")
|
||||
@ -70,33 +73,9 @@ func Load() Config {
|
||||
return conf
|
||||
}
|
||||
|
||||
func LoadFile(configFile string) Config {
|
||||
var conf Config
|
||||
|
||||
f, err := os.OpenFile(configFile, os.O_RDONLY, 0444)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open config file: %s", err)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
if err := viper.ReadConfig(f); err != nil {
|
||||
log.Fatalf("Error reading config: %s", err)
|
||||
}
|
||||
|
||||
if err := viper.Unmarshal(&conf); err != nil {
|
||||
log.Fatalf("Error parsing config: %s", err)
|
||||
}
|
||||
|
||||
loadedConfig = &conf
|
||||
|
||||
return conf
|
||||
}
|
||||
|
||||
func LoadedConfig() *Config {
|
||||
if loadedConfig == nil {
|
||||
Load()
|
||||
Load(configFile)
|
||||
}
|
||||
return loadedConfig
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
connectionStringTemplate = `%s:%s@%s/%s`
|
||||
connectionStringTemplate = `%s:%s@%s/%s?parseTime=true`
|
||||
)
|
||||
|
||||
type MySQL struct {
|
||||
|
1
go.mod
1
go.mod
@ -20,6 +20,7 @@ require (
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/spf13/viper v1.7.1
|
||||
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26
|
||||
go.etcd.io/bbolt v1.3.5
|
||||
golang.org/x/image v0.0.0-20200801110659-972c09e46d76
|
||||
golang.org/x/sys v0.0.0-20200819035508-9a32b3aa38f5 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -212,6 +212,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
|
||||
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
|
||||
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26 h1:UFHFmFfixpmfRBcxuu+LA9l8MdURWVdVNUHxO5n1d2w=
|
||||
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26/go.mod h1:IGhd0qMDsUa9acVjsbsT7bu3ktadtGOHI79+idTew/M=
|
||||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
|
||||
go.etcd.io/bbolt v1.3.2 h1:Z/90sZLPOeCy2PwprqkFa25PdkusRzaj9P8zm/KNyvk=
|
||||
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
||||
|
12
main.go
12
main.go
@ -3,11 +3,12 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/librespeed/speedtest/config"
|
||||
"github.com/librespeed/speedtest/database"
|
||||
"github.com/librespeed/speedtest/results"
|
||||
"github.com/librespeed/speedtest/web"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -16,14 +17,7 @@ var (
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var conf config.Config
|
||||
if *optConfig != "" {
|
||||
conf = config.LoadFile(*optConfig)
|
||||
} else {
|
||||
conf = config.Load()
|
||||
}
|
||||
|
||||
conf := config.Load(*optConfig)
|
||||
web.SetServerLocation(&conf)
|
||||
results.Initialize(&conf)
|
||||
database.SetDBInfo(&conf)
|
||||
|
@ -5,19 +5,19 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/umahmood/haversine"
|
||||
|
||||
"github.com/librespeed/speedtest/config"
|
||||
"github.com/librespeed/speedtest/results"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
serverLat, serverLng float64
|
||||
serverCoord haversine.Coord
|
||||
)
|
||||
|
||||
func getRandomData(length int) []byte {
|
||||
@ -67,95 +67,89 @@ func getIPInfo(addr string) results.IPInfoResponse {
|
||||
return ret
|
||||
}
|
||||
|
||||
func SetServerLocation(conf *config.Config) (float64, float64) {
|
||||
func SetServerLocation(conf *config.Config) {
|
||||
if conf.ServerLat > 0 && conf.ServerLng > 0 {
|
||||
log.Infof("Configured server coordinates: %.6f, %.6f", conf.ServerLat, conf.ServerLng)
|
||||
return conf.ServerLat, conf.ServerLng
|
||||
serverCoord.Lat = conf.ServerLat
|
||||
serverCoord.Lon = conf.ServerLng
|
||||
return
|
||||
}
|
||||
|
||||
var ret results.IPInfoResponse
|
||||
resp, err := http.DefaultClient.Get(getIPInfoURL(""))
|
||||
if err != nil {
|
||||
log.Errorf("Error getting repsonse from ipinfo.io: %s", err)
|
||||
return 0, 0
|
||||
return
|
||||
}
|
||||
raw, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Errorf("Error reading response from ipinfo.io: %s", err)
|
||||
return 0, 0
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err := json.Unmarshal(raw, &ret); err != nil {
|
||||
log.Errorf("Error parsing response from ipinfo.io: %s", err)
|
||||
return 0, 0
|
||||
return
|
||||
}
|
||||
|
||||
var lat, lng float64
|
||||
if ret.Location != "" {
|
||||
lat, lng = parseLocationString(ret.Location)
|
||||
serverCoord, err = parseLocationString(ret.Location)
|
||||
if err != nil {
|
||||
log.Errorf("Cannot get server coordinates: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
log.Infof("Fetched server coordinates: %.6f, %.6f", lat, lng)
|
||||
|
||||
return lat, lng
|
||||
log.Infof("Fetched server coordinates: %.6f, %.6f", serverCoord.Lat, serverCoord.Lon)
|
||||
}
|
||||
|
||||
func parseLocationString(location string) (float64, float64) {
|
||||
func parseLocationString(location string) (haversine.Coord, error) {
|
||||
var coord haversine.Coord
|
||||
|
||||
parts := strings.Split(location, ",")
|
||||
if len(parts) != 2 {
|
||||
log.Errorf("Unknown location format: %s", location)
|
||||
return 0, 0
|
||||
err := fmt.Errorf("unknown location format: %s", location)
|
||||
log.Error(err)
|
||||
return coord, err
|
||||
}
|
||||
|
||||
lat, err := strconv.ParseFloat(parts[0], 64)
|
||||
if err != nil {
|
||||
log.Errorf("Error parsing latitude: %s", parts[0])
|
||||
return 0, 0
|
||||
return coord, err
|
||||
}
|
||||
|
||||
lng, err := strconv.ParseFloat(parts[1], 64)
|
||||
if err != nil {
|
||||
log.Errorf("Error parsing longitude: %s", parts[0])
|
||||
return 0, 0
|
||||
return coord, err
|
||||
}
|
||||
|
||||
return lat, lng
|
||||
coord.Lat = lat
|
||||
coord.Lon = lng
|
||||
|
||||
return coord, nil
|
||||
}
|
||||
|
||||
func calculateDistance(clientLocation string, unit string) string {
|
||||
clientLat, clientLng := parseLocationString(clientLocation)
|
||||
|
||||
radlat1 := float64(math.Pi * serverLat / 180)
|
||||
radlat2 := float64(math.Pi * clientLat / 180)
|
||||
|
||||
theta := float64(serverLng - clientLng)
|
||||
radtheta := float64(math.Pi * theta / 180)
|
||||
|
||||
dist := math.Sin(radlat1)*math.Sin(radlat2) + math.Cos(radlat1)*math.Cos(radlat2)*math.Cos(radtheta)
|
||||
|
||||
if dist > 1 {
|
||||
dist = 1
|
||||
clientCoord, err := parseLocationString(clientLocation)
|
||||
if err != nil {
|
||||
log.Errorf("Error parsing client coordinates: %s", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
dist = math.Acos(dist)
|
||||
dist = dist * 180 / math.Pi
|
||||
dist = dist * 60 * 1.1515
|
||||
|
||||
dist, km := haversine.Distance(clientCoord, serverCoord)
|
||||
unitString := " mi"
|
||||
|
||||
switch unit {
|
||||
case "km":
|
||||
dist = dist * 1.609344
|
||||
dist = km
|
||||
unitString = " km"
|
||||
case "NM":
|
||||
dist = dist * 0.8684
|
||||
dist = km * 0.539957
|
||||
unitString = " NM"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%d%s", round(dist), unitString)
|
||||
}
|
||||
|
||||
func round(v float64) int {
|
||||
r := int(math.Round(v))
|
||||
return 10 * ((r + 9) / 10)
|
||||
return fmt.Sprintf("%.2f%s", dist, unitString)
|
||||
}
|
||||
|
Reference in New Issue
Block a user