Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main
import (
"context"
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
"os"
"strconv"
"strings"
"time"

"gopkg.in/alecthomas/kingpin.v2"

"github.com/showwin/speedtest-go/speedtest"
)

Expand All @@ -31,6 +32,7 @@ var (
noUpload = kingpin.Flag("no-upload", "Disable upload test.").Bool()
pingMode = kingpin.Flag("ping-mode", "Select a method for Ping. (support icmp/tcp/http)").Default("http").String()
debug = kingpin.Flag("debug", "Enable debug mode.").Short('d').Bool()
countryCode = kingpin.Flag("cc", "Filter By Country Code").String()
)

func main() {
Expand All @@ -54,6 +56,7 @@ func main() {
Keyword: *search,
NoDownload: *noDownload,
NoUpload: *noUpload,
CountryCode: *countryCode,
}))
speedtestClient.SetNThread(*thread)

Expand Down
37 changes: 37 additions & 0 deletions speedtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Server struct {
DLSpeed float64 `json:"dl_speed"`
ULSpeed float64 `json:"ul_speed"`
TestDuration TestDuration `json:"test_duration"`
CC string `json:"cc"`

Context *Speedtest `json:"-"`
}
Expand Down Expand Up @@ -212,6 +213,23 @@ func (s *Speedtest) FetchServerListContext(ctx context.Context) (Servers, error)
query.Set("lat", strconv.FormatFloat(s.config.Location.Lat, 'f', -1, 64))
query.Set("lon", strconv.FormatFloat(s.config.Location.Lon, 'f', -1, 64))
}

if len(s.config.CountryCode) > 0 {
var lowerCode = strings.ToLower(s.config.CountryCode)
var lat float64 = 0
var lon float64 = 0
for _, v := range Locations {
if v.CC == lowerCode {
lat = v.Lat
lon = v.Lon
}
}
if lat != 0 && lon != 0 {
query.Set("lat", strconv.FormatFloat(lat, 'f', -1, 64))
query.Set("lon", strconv.FormatFloat(lon, 'f', -1, 64))
}
}

u.RawQuery = query.Encode()
dbg.Printf("Retrieving servers: %s\n", u.String())
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
Expand Down Expand Up @@ -254,6 +272,25 @@ func (s *Speedtest) FetchServerListContext(ctx context.Context) (Servers, error)
if err = decoder.Decode(&servers); err != nil {
return servers, err
}
var CountryCode = ""
if s.config.Location != nil {
CountryCode = strings.ToUpper(s.config.Location.CC)
} else if len(s.config.CountryCode) > 0 {
CountryCode = strings.ToUpper(s.config.CountryCode)
} else {
defaultConfig, err := FetchUserInfo()
if err != nil {
return servers, err
}
CountryCode = defaultConfig.Country
}
var tmpServers Servers
for _, server := range servers {
if server.CC == CountryCode {
tmpServers = append(tmpServers, server)
}
}
servers = tmpServers
case typeXMLPayload:
var list ServerList
// Decode xml
Expand Down
21 changes: 21 additions & 0 deletions speedtest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"math"
"math/rand"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -204,3 +205,23 @@ func TestTotalDurationCount(t *testing.T) {
t.Error("addition in testDurationTotalCount didn't work")
}
}

func TestCityFlag(t *testing.T) {
testCC := "YISHUN"
testData := Servers{
{CC: "YISHUN"},
{CC: "TOKYO"},
{CC: "YISHUN"},
{CC: "TEST"},
}

var tmpServers Servers
for _, server := range testData {
if server.CC == strings.ToUpper(testCC) {
tmpServers = append(tmpServers, server)
}
}
if tmpServers.Len() != 2 && tmpServers[0].CC != testCC {
t.Fatalf("not match: %s", testCC)
}
}
5 changes: 3 additions & 2 deletions speedtest/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ type UserConfig struct {

Keyword string // Fuzzy search

NoDownload bool
NoUpload bool
NoDownload bool
NoUpload bool
CountryCode string
}

func parseAddr(addr string) (string, string) {
Expand Down
9 changes: 5 additions & 4 deletions speedtest/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const speedTestConfigUrl = "https://www.speedtest.net/speedtest-config.php"

// User represents information determined about the caller by speedtest.net
type User struct {
IP string `xml:"ip,attr"`
Lat string `xml:"lat,attr"`
Lon string `xml:"lon,attr"`
Isp string `xml:"isp,attr"`
IP string `xml:"ip,attr"`
Lat string `xml:"lat,attr"`
Lon string `xml:"lon,attr"`
Isp string `xml:"isp,attr"`
Country string `xml:"country,attr"`
}

// Users for decode xml
Expand Down