Skip to content

Commit

Permalink
feat: get server ip from api
Browse files Browse the repository at this point in the history
  • Loading branch information
ztelliot committed May 30, 2024
1 parent f32ae57 commit 9d317a6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 55 deletions.
2 changes: 1 addition & 1 deletion defs/ipinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func ipip() (*IPInfoResponse, error) {
} `json:"data"`
}

if err := request("https://myip.ipip.net/json", &data); err != nil {
if err := request("http://myip6.ipip.net/json", &data); err != nil {
return nil, err
} else {
var ipInfo IPInfoResponse
Expand Down
10 changes: 6 additions & 4 deletions defs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ const (
type Server struct {
ID string `json:"id"`
Name string `json:"name"`
IP string `json:"-"`
IPv6 string `json:"-"`
IP string `json:"ip"`
IPv6 string `json:"ipv6"`
Host string `json:"host"`
Port uint16 `json:"port"`
Prov uint8 `json:"province"`
Expand Down Expand Up @@ -274,7 +274,7 @@ func (s *Server) Download(silent, useBytes, useMebi bool, requests int, duration

doDownload := func() {
resp, err := http.DefaultClient.Do(req)
if err != nil {
if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) && !os.IsTimeout(err) {
log.Debugf("Failed when making HTTP request: %s", err)
} else {
defer resp.Body.Close()
Expand Down Expand Up @@ -372,7 +372,9 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests, up
} else if err == nil {
defer resp.Body.Close()
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
log.Debugf("Failed when reading HTTP response: %s", err)
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) && !os.IsTimeout(err) {
log.Debugf("Failed when reading HTTP response: %s", err)
}
}

uploadDone <- struct{}{}
Expand Down
18 changes: 0 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
github.com/briandowns/spinner v1.23.0 h1:alDF2guRWqa/FOZZYWjlMIx2L6H0wyewPxo/CH4Pt2A=
github.com/briandowns/spinner v1.23.0/go.mod h1:rPG4gmXeN3wQV/TsAY4w8lPdIM6RX3yqeBQJSrbXjuE=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/go-ping/ping v1.1.0 h1:3MCGhVX4fyEUuhsfwPrsEdQw6xspHkv5zHsiSoDFZYw=
github.com/go-ping/ping v1.1.0/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a h1:RYfmiM0zluBJOiPDJseKLEN4BapJ42uSi9SZBQ2YyiA=
github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1 h1:FWNFq4fM1wPfcK40yHE5UO3RUdSNPaBC+j3PokzA6OQ=
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand All @@ -34,36 +28,24 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
14 changes: 11 additions & 3 deletions speedtest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func getServerList(c *cli.Context, servers *[]string, groups *[]string) ([]defs.
}
req.Header.Set("User-Agent", defs.ApiUA)

start := time.Now()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
Expand All @@ -156,6 +157,7 @@ func getServerList(c *cli.Context, servers *[]string, groups *[]string) ([]defs.
return nil, err
}
defer resp.Body.Close()
log.Debugf("Time taken to get server list: %s", time.Since(start))

var res struct {
Code int `json:"code"`
Expand Down Expand Up @@ -210,10 +212,10 @@ func getVersion(c *cli.Context) (*defs.Version, error) {
return &res.Data, nil
}

func getGlobalServerList(ip string) ([]defs.Server, error) {
func getGlobalServerList(ip string, ipv6 int) ([]defs.Server, error) {
var serversT []defs.ServerGlobal

uri := fmt.Sprintf("%s/dataServer/mobilematch_list.php?ip=%s&network=4&ipv6=0", GlobalSpeedAPI, ip)
uri := fmt.Sprintf("%s/dataServer/mobilematch_list.php?ip=%s&network=4&ipv6=%d", GlobalSpeedAPI, ip, ipv6)
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
return nil, err
Expand All @@ -238,7 +240,13 @@ func getGlobalServerList(ip string) ([]defs.Server, error) {
var servers []defs.Server
for _, s := range serversT {
port, _ := strconv.Atoi(s.Port)
servers = append(servers, defs.Server{ID: strconv.Itoa(s.ID), Name: s.Name, IP: s.IP, Host: s.IP, Port: uint16(port), Province: s.Prov, City: s.City, ISP: s.GetISP().ID})
_s := defs.Server{ID: strconv.Itoa(s.ID), Name: s.Name, Host: s.IP, Port: uint16(port), Province: s.Prov, City: s.City, ISP: s.GetISP().ID}
if ipv6 == 0 {
_s.IP = s.IP
} else {
_s.IPv6 = s.IP
}
servers = append(servers, _s)
}
return servers, nil
}
Expand Down
42 changes: 13 additions & 29 deletions speedtest/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"sync"
Expand All @@ -25,8 +24,6 @@ import (
"github.com/ztelliot/taierspeed-cli/report"
)

var DomainRe = regexp.MustCompile(`([a-zA-Z0-9][-a-zA-Z0-9]{0,62}\.)+([a-zA-Z][-a-zA-Z]{0,62})`)

//go:embed province.csv
var ProvinceListByte []byte

Expand Down Expand Up @@ -196,7 +193,7 @@ func SpeedTest(c *cli.Context) error {
}

simple := true
if forceIPv6 || c.Bool(defs.OptionList) || c.IsSet(defs.OptionServer) || c.IsSet(defs.OptionServerGroup) || ispInfo == nil || ispInfo.IP == "" {
if forceIPv6 || c.Bool(defs.OptionList) || c.IsSet(defs.OptionServer) || c.IsSet(defs.OptionServerGroup) || ispInfo == nil || ispInfo.IP == "" || ispInfo.Country != "中国" {
simple = false
}

Expand All @@ -207,7 +204,7 @@ func SpeedTest(c *cli.Context) error {
if simple {
var serversT []defs.Server

if serversT, err = getGlobalServerList(ispInfo.IP); err != nil {
if serversT, err = getGlobalServerList(ispInfo.IP, 0); err != nil {
log.Errorf("Error when fetching server list: %s", err)
return err
}
Expand Down Expand Up @@ -292,7 +289,9 @@ func SpeedTest(c *cli.Context) error {
for s := range _tmpMap {
_groups = append(_groups, s)
}
} else if !forceIPv6 && !c.Bool(defs.OptionList) {
}

if !c.IsSet(defs.OptionServer) && !c.IsSet(defs.OptionServerGroup) && !c.Bool(defs.OptionList) {
_groups = append(_groups, "31@1")
}

Expand All @@ -305,31 +304,16 @@ func SpeedTest(c *cli.Context) error {
var serversT []defs.Server

for _, n := range g.Node {
if DomainRe.MatchString(n.Host) {
if records, err := net.LookupHost(n.Host); err == nil {
for _, i := range records {
if strings.Contains(i, ":") {
n.IPv6 = i
} else {
n.IP = i
}
}
if n.IP != "" && !forceIPv6 {
if n.Host == "" {
n.Host = n.IP
}
} else {
if strings.Contains(n.Host, ":") {
n.IPv6 = n.Host
} else {
n.IP = n.Host
}
}

if forceIPv4 || forceIPv6 {
if forceIPv4 && n.IP == "" {
continue
}
if forceIPv6 && n.IPv6 == "" {
continue
} else if n.IPv6 != "" && !forceIPv4 {
if n.Host == "" {
n.Host = n.IPv6
}
} else {
continue
}
serversT = append(serversT, n)
}
Expand Down

0 comments on commit 9d317a6

Please sign in to comment.