6 Commits

Author SHA1 Message Date
fc6e99b45c Let Viper handle the config file 2020-11-10 16:33:19 +08:00
c815103e20 Merge pull request #11 from JackyCZJ/master
fix(mysql) timestamp marshall error , fix
2020-08-24 18:32:48 +08:00
19432dbb37 fix(mysql) timestamp marshall error , fix
Signed-off-by: JackyCZJ <chenzj@esixnetwork.net>
2020-08-24 18:28:36 +08:00
31d51deba5 Update README 2020-08-20 15:44:41 +08:00
cf93e8c545 gofmt 2020-08-20 10:32:50 +08:00
b138d9b6bc Use a proven library to calculate distance
Also rounding distance at 2 decimals instead of round to 5
2020-08-20 10:22:47 +08:00
7 changed files with 53 additions and 83 deletions

View File

@ -23,7 +23,7 @@ Works with mobile versions too.
* Results sharing (optional) * Results sharing (optional)
* Multiple Points of Test (optional) * Multiple Points of Test (optional)
* Compatible with PHP frontend predefined endpoints (with `.php` suffixes) * 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)
![Screencast](https://speedtest.zzz.cat/speedtest.webp) ![Screencast](https://speedtest.zzz.cat/speedtest.webp)

View File

@ -1,8 +1,6 @@
package config package config
import ( import (
"os"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -30,6 +28,7 @@ type Config struct {
} }
var ( var (
configFile string = ""
loadedConfig *Config = nil loadedConfig *Config = nil
) )
@ -51,8 +50,12 @@ func init() {
viper.AddConfigPath(".") viper.AddConfigPath(".")
} }
func Load() Config { func Load(configPath string) Config {
var conf Config var conf Config
configFile = configPath
viper.SetConfigFile(configPath)
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok { if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Warnf("No config file found in search paths, using default values") log.Warnf("No config file found in search paths, using default values")
@ -70,33 +73,9 @@ func Load() Config {
return conf 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 { func LoadedConfig() *Config {
if loadedConfig == nil { if loadedConfig == nil {
Load() Load(configFile)
} }
return loadedConfig return loadedConfig
} }

View File

@ -11,7 +11,7 @@ import (
) )
const ( const (
connectionStringTemplate = `%s:%s@%s/%s` connectionStringTemplate = `%s:%s@%s/%s?parseTime=true`
) )
type MySQL struct { type MySQL struct {

1
go.mod
View File

@ -20,6 +20,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1 github.com/spf13/viper v1.7.1
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26
go.etcd.io/bbolt v1.3.5 go.etcd.io/bbolt v1.3.5
golang.org/x/image v0.0.0-20200801110659-972c09e46d76 golang.org/x/image v0.0.0-20200801110659-972c09e46d76
golang.org/x/sys v0.0.0-20200819035508-9a32b3aa38f5 // indirect golang.org/x/sys v0.0.0-20200819035508-9a32b3aa38f5 // indirect

2
go.sum
View File

@ -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 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= 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/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= 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 h1:Z/90sZLPOeCy2PwprqkFa25PdkusRzaj9P8zm/KNyvk=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=

12
main.go
View File

@ -3,11 +3,12 @@ package main
import ( import (
"flag" "flag"
log "github.com/sirupsen/logrus"
"github.com/librespeed/speedtest/config" "github.com/librespeed/speedtest/config"
"github.com/librespeed/speedtest/database" "github.com/librespeed/speedtest/database"
"github.com/librespeed/speedtest/results" "github.com/librespeed/speedtest/results"
"github.com/librespeed/speedtest/web" "github.com/librespeed/speedtest/web"
log "github.com/sirupsen/logrus"
) )
var ( var (
@ -16,14 +17,7 @@ var (
func main() { func main() {
flag.Parse() flag.Parse()
conf := config.Load(*optConfig)
var conf config.Config
if *optConfig != "" {
conf = config.LoadFile(*optConfig)
} else {
conf = config.Load()
}
web.SetServerLocation(&conf) web.SetServerLocation(&conf)
results.Initialize(&conf) results.Initialize(&conf)
database.SetDBInfo(&conf) database.SetDBInfo(&conf)

View File

@ -5,19 +5,19 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
log "github.com/sirupsen/logrus"
"github.com/umahmood/haversine"
"github.com/librespeed/speedtest/config" "github.com/librespeed/speedtest/config"
"github.com/librespeed/speedtest/results" "github.com/librespeed/speedtest/results"
log "github.com/sirupsen/logrus"
) )
var ( var (
serverLat, serverLng float64 serverCoord haversine.Coord
) )
func getRandomData(length int) []byte { func getRandomData(length int) []byte {
@ -67,95 +67,89 @@ func getIPInfo(addr string) results.IPInfoResponse {
return ret return ret
} }
func SetServerLocation(conf *config.Config) (float64, float64) { func SetServerLocation(conf *config.Config) {
if conf.ServerLat > 0 && conf.ServerLng > 0 { if conf.ServerLat > 0 && conf.ServerLng > 0 {
log.Infof("Configured server coordinates: %.6f, %.6f", conf.ServerLat, conf.ServerLng) 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 var ret results.IPInfoResponse
resp, err := http.DefaultClient.Get(getIPInfoURL("")) resp, err := http.DefaultClient.Get(getIPInfoURL(""))
if err != nil { if err != nil {
log.Errorf("Error getting repsonse from ipinfo.io: %s", err) log.Errorf("Error getting repsonse from ipinfo.io: %s", err)
return 0, 0 return
} }
raw, err := ioutil.ReadAll(resp.Body) raw, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Errorf("Error reading response from ipinfo.io: %s", err) log.Errorf("Error reading response from ipinfo.io: %s", err)
return 0, 0 return
} }
defer resp.Body.Close() defer resp.Body.Close()
if err := json.Unmarshal(raw, &ret); err != nil { if err := json.Unmarshal(raw, &ret); err != nil {
log.Errorf("Error parsing response from ipinfo.io: %s", err) log.Errorf("Error parsing response from ipinfo.io: %s", err)
return 0, 0 return
} }
var lat, lng float64
if ret.Location != "" { 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) log.Infof("Fetched server coordinates: %.6f, %.6f", serverCoord.Lat, serverCoord.Lon)
return lat, lng
} }
func parseLocationString(location string) (float64, float64) { func parseLocationString(location string) (haversine.Coord, error) {
var coord haversine.Coord
parts := strings.Split(location, ",") parts := strings.Split(location, ",")
if len(parts) != 2 { if len(parts) != 2 {
log.Errorf("Unknown location format: %s", location) err := fmt.Errorf("unknown location format: %s", location)
return 0, 0 log.Error(err)
return coord, err
} }
lat, err := strconv.ParseFloat(parts[0], 64) lat, err := strconv.ParseFloat(parts[0], 64)
if err != nil { if err != nil {
log.Errorf("Error parsing latitude: %s", parts[0]) log.Errorf("Error parsing latitude: %s", parts[0])
return 0, 0 return coord, err
} }
lng, err := strconv.ParseFloat(parts[1], 64) lng, err := strconv.ParseFloat(parts[1], 64)
if err != nil { if err != nil {
log.Errorf("Error parsing longitude: %s", parts[0]) 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 { func calculateDistance(clientLocation string, unit string) string {
clientLat, clientLng := parseLocationString(clientLocation) clientCoord, err := parseLocationString(clientLocation)
if err != nil {
radlat1 := float64(math.Pi * serverLat / 180) log.Errorf("Error parsing client coordinates: %s", err)
radlat2 := float64(math.Pi * clientLat / 180) return ""
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
} }
dist = math.Acos(dist) dist, km := haversine.Distance(clientCoord, serverCoord)
dist = dist * 180 / math.Pi
dist = dist * 60 * 1.1515
unitString := " mi" unitString := " mi"
switch unit { switch unit {
case "km": case "km":
dist = dist * 1.609344 dist = km
unitString = " km" unitString = " km"
case "NM": case "NM":
dist = dist * 0.8684 dist = km * 0.539957
unitString = " NM" unitString = " NM"
} }
return fmt.Sprintf("%d%s", round(dist), unitString) return fmt.Sprintf("%.2f%s", dist, unitString)
}
func round(v float64) int {
r := int(math.Round(v))
return 10 * ((r + 9) / 10)
} }