Skip to content

Commit

Permalink
feat: pretty server list output
Browse files Browse the repository at this point in the history
  • Loading branch information
ztelliot committed Jun 13, 2024
1 parent c9a57f3 commit ee202b6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/briandowns/spinner v1.23.0
github.com/go-ping/ping v1.1.0
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1
github.com/jedib0t/go-pretty/v6 v6.5.9
github.com/sirupsen/logrus v1.9.3
github.com/urfave/cli/v2 v2.27.2
golang.org/x/sys v0.20.0
Expand All @@ -17,7 +18,10 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
Expand Down
9 changes: 8 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,27 @@ github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1/go.mod h1:5YoVOkjYA
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jedib0t/go-pretty/v6 v6.5.9 h1:ACteMBRrrmm1gMsXe9PSTOClQ63IXDUt03H5U+UV8OU=
github.com/jedib0t/go-pretty/v6 v6.5.9/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
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/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
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-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
Expand Down
4 changes: 2 additions & 2 deletions speedtest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ func deQueue(s defs.Server, key string) bool {
return true
}

func MatchProvince(prov string, provinces *[]defs.ProvinceInfo) uint8 {
for _, p := range *provinces {
func MatchProvince(prov string, provinceMap *map[uint8]defs.ProvinceInfo) uint8 {
for _, p := range *provinceMap {
if p.ID == 0 {
continue
}
Expand Down
55 changes: 41 additions & 14 deletions speedtest/speedtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
_ "embed"
"errors"
"fmt"
"github.com/jedib0t/go-pretty/v6/table"
"math"
"math/rand"
"net"
Expand Down Expand Up @@ -186,6 +187,7 @@ func SpeedTest(c *cli.Context) error {

var ispInfo *defs.IPInfoResponse
var servers []defs.Server
var provinceMap map[uint8]defs.ProvinceInfo = nil

simple := true
if forceIPv6 || c.IsSet(defs.OptionServer) || c.IsSet(defs.OptionServerGroup) {
Expand Down Expand Up @@ -221,13 +223,6 @@ func SpeedTest(c *cli.Context) error {
}
}
} else {
var provinces []defs.ProvinceInfo
gocsv.UnmarshalBytes(ProvinceListByte, &provinces)
provinceMap := make(map[uint8]defs.ProvinceInfo)
for _, p := range provinces {
provinceMap[p.ID] = p
}

var _servers []string
if c.IsSet(defs.OptionServer) {
_tmpMap := make(map[string]byte)
Expand All @@ -241,6 +236,7 @@ func SpeedTest(c *cli.Context) error {

var _groups []string
if c.IsSet(defs.OptionServerGroup) {
provinceMap = initProvinceMap()
_tmpMap := make(map[string]byte)
for _, s := range c.StringSlice(defs.OptionServerGroup) {
sg := strings.Split(s, "@")
Expand All @@ -263,9 +259,9 @@ func SpeedTest(c *cli.Context) error {
var province uint8 = 0
if sgp != "" {
if sgp == "lo" {
province = MatchProvince(ispInfo.Province, &provinces)
province = MatchProvince(ispInfo.Province, &provinceMap)
} else {
for _, p := range provinces {
for _, p := range provinceMap {
if p.Code == sgp {
province = p.ID
break
Expand Down Expand Up @@ -303,9 +299,12 @@ func SpeedTest(c *cli.Context) error {

if !c.IsSet(defs.OptionServer) && !c.IsSet(defs.OptionServerGroup) && !c.Bool(defs.OptionList) {
if ispInfo != nil && (ispInfo.Province != "" || ispInfo.ISP != "") && ispInfo.Country == "中国" {
if provinceMap == nil {
provinceMap = initProvinceMap()
}
province, isp := uint8(0), uint8(0)
if ispInfo.Province != "" {
province = MatchProvince(ispInfo.Province, &provinces)
province = MatchProvince(ispInfo.Province, &provinceMap)
}
if ispInfo.ISP != "" {
isp = MatchISP(ispInfo.ISP)
Expand Down Expand Up @@ -347,6 +346,9 @@ func SpeedTest(c *cli.Context) error {
servers = append(servers, serversT...)
} else {
if g.Group != "" {
if provinceMap == nil {
provinceMap = initProvinceMap()
}
_g := strings.Split(g.Group, "@")
province, _ := strconv.Atoi(_g[0])
isp, _ := strconv.Atoi(_g[1])
Expand All @@ -371,22 +373,47 @@ func SpeedTest(c *cli.Context) error {

// if --list is given, list all the servers fetched and exit
if c.Bool(defs.OptionList) {
if provinceMap == nil {
provinceMap = initProvinceMap()
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"ID", "Name", "Prov", "City", "ISP", "v4", "v6"})

for _, svr := range servers {
var stacks []string
province := svr.Province
if province == "" && svr.Prov != 0 {
province = provinceMap[svr.Prov].Short
}
v4, v6 := "N", "N"
if svr.IP != "" {
stacks = append(stacks, "IPv4")
v4 = "Y"
}
if svr.IPv6 != "" {
stacks = append(stacks, "IPv6")
v6 = "Y"
}
fmt.Printf("%s: %s (%s%s) %v\n", svr.ID, svr.Name, svr.Province, defs.ISPMap[svr.ISP].Name, stacks)
t.AppendRow(table.Row{svr.ID, svr.Name, province, svr.City, defs.ISPMap[svr.ISP].Name, v4, v6})
}

t.Style().Options.DrawBorder = false
t.Style().Options.SeparateColumns = false
t.Render()
return nil
}

return doSpeedTest(c, servers, network, silent, noICMP, ispInfo)
}

func initProvinceMap() map[uint8]defs.ProvinceInfo {
var provinces []defs.ProvinceInfo
gocsv.UnmarshalBytes(ProvinceListByte, &provinces)
provinceMap := make(map[uint8]defs.ProvinceInfo)
for _, p := range provinces {
provinceMap[p.ID] = p
}
return provinceMap
}

func selectServer(logPre string, servers []defs.Server, network string, c *cli.Context, noICMP bool) (defs.Server, bool) {
if len(servers) > 10 {
r := rand.New(rand.NewSource(time.Now().Unix()))
Expand Down

0 comments on commit ee202b6

Please sign in to comment.