diff --git a/README.md b/README.md index 7bdd902..68fc533 100644 --- a/README.md +++ b/README.md @@ -146,8 +146,6 @@ https://github.com/XIU2/CloudflareSpeedTest 写入结果文件;如路径含有空格请加上引号;值为空时不写入文件 [-o ""];(默认 result.csv) -dd 禁用下载测速;禁用后测速结果会按延迟排序 (默认按下载速度排序);(默认 启用) - -ipv6 - IPv6测速模式;确保 IP 段数据文件内只包含 IPv6 IP段,软件不支持同时测速 IPv4+IPv6;(默认 IPv4) -allip 测速全部的IP;对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 IP 段随机测速一个 IP) -v @@ -219,11 +217,11 @@ D:\ABC\CloudflareST\CloudflareST.exe -n 500 -t 4 -dn 20 -dt 5 -o " " **** ``` bash -# 测速 IPv4 时,需要指定 IPv4 数据文件(-f 默认值就是 ip.txt,所以该参数可以省略) +# 测速 IPv4 时,需要指定 IPv4 数据文件(-f 默认值就是 ip.txt,所以该参数可省略) CloudflareST.exe -f ip.txt -# 测速 IPv6 时,需要指定 IPv6 数据文件( ipv6.txt ) 的同时再加上 -ipv6 参数 -CloudflareST.exe -f ipv6.txt -ipv6 +# 测速 IPv6 时,需要指定 IPv6 数据文件( ipv6.txt ) +CloudflareST.exe -f ipv6.txt ``` > 测速 IPv6 时,可能会注意到每次测速数量都不一样,了解原因: [#120](https://github.com/XIU2/CloudflareSpeedTest/issues/120) diff --git a/main.go b/main.go index e1bfc07..73168fb 100644 --- a/main.go +++ b/main.go @@ -52,8 +52,6 @@ https://github.com/XIU2/CloudflareSpeedTest 写入结果文件;如路径含有空格请加上引号;值为空时不写入文件 [-o ""];(默认 result.csv) -dd 禁用下载测速;禁用后测速结果会按延迟排序 (默认按下载速度排序);(默认 启用) - -ipv6 - IPv6测速模式;确保 IP 段数据文件内只包含 IPv6 IP段,软件不支持同时测速 IPv4+IPv6;(默认 IPv4) -allip 测速全部的IP;对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 IP 段随机测速一个 IP) -v @@ -73,7 +71,6 @@ https://github.com/XIU2/CloudflareSpeedTest flag.IntVar(&task.TestCount, "dn", 10, "下载测速数量") flag.StringVar(&task.URL, "url", "https://cf.xiu2.xyz/url", "下载测速地址") flag.BoolVar(&task.Disable, "dd", false, "禁用下载测速") - flag.BoolVar(&task.IPv6, "ipv6", false, "启用IPv6") flag.BoolVar(&task.TestAll, "allip", false, "测速全部 IP") flag.StringVar(&task.IPFile, "f", "ip.txt", "IP 数据文件") flag.Float64Var(&task.MinSpeed, "sl", 0, "下载速度下限") @@ -118,8 +115,8 @@ func main() { pingData := task.NewPing().Run().FilterDelay() // 开始下载测速 speedData := task.TestDownloadSpeed(pingData) - utils.ExportCsv(speedData) - speedData.Print(task.IPv6) + utils.ExportCsv(speedData) // 输出文件 + speedData.Print() // 打印结果 if versionNew != "" { fmt.Printf("\n*** 发现新版本 [%s]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新! ***\n", versionNew) diff --git a/task/download.go b/task/download.go index f9624cc..3a80479 100644 --- a/task/download.go +++ b/task/download.go @@ -91,9 +91,11 @@ func TestDownloadSpeed(ipSet utils.PingDelaySet) (speedSet utils.DownloadSpeedSe } func getDialContext(ip *net.IPAddr) func(ctx context.Context, network, address string) (net.Conn, error) { - fakeSourceAddr := ip.String() + ":" + fmt.Sprintf("%d", TCPPort) - if IPv6 { // IPv6 需要加上 [] - fakeSourceAddr = "[" + ip.String() + "]:" + fmt.Sprintf("%d", TCPPort) + var fakeSourceAddr string + if isIPv4(ip.String()) { + fakeSourceAddr = fmt.Sprintf("%s:%d", ip.String(), TCPPort) + } else { + fakeSourceAddr = fmt.Sprintf("[%s]:%d", ip.String(), TCPPort) } return func(ctx context.Context, network, address string) (net.Conn, error) { return (&net.Dialer{}).DialContext(ctx, network, fakeSourceAddr) diff --git a/task/ip.go b/task/ip.go index 02d3359..0d1b3d0 100644 --- a/task/ip.go +++ b/task/ip.go @@ -14,8 +14,6 @@ import ( const defaultInputFile = "ip.txt" var ( - // IPv6 IP version is 6 - IPv6 = false // TestAll test all ip TestAll = false // IPFile is the filename of IP Rangs @@ -26,6 +24,10 @@ func InitRandSeed() { rand.Seed(time.Now().UnixNano()) } +func isIPv4(ip string) bool { + return strings.Contains(ip, ".") +} + func randIPEndWith(num byte) byte { if num == 0 { // 对于 /32 这种单独的 IP return byte(0) @@ -49,8 +51,9 @@ func newIPRanges() *IPRanges { func (r *IPRanges) fixIP(ip string) string { // 如果不含有 '/' 则代表不是 IP 段,而是一个单独的 IP,因此需要加上 /32 /128 子网掩码 if i := strings.IndexByte(ip, '/'); i < 0 { - r.mask = "/32" - if IPv6 { + if isIPv4(ip) { + r.mask = "/32" + } else { r.mask = "/128" } ip += r.mask @@ -116,8 +119,6 @@ func (r *IPRanges) chooseIPv4() { func (r *IPRanges) chooseIPv6() { var tempIP uint8 for r.ipNet.Contains(r.firstIP) { - //fmt.Println(firstIP) - //fmt.Println(firstIP[0], firstIP[1], firstIP[2], firstIP[3], firstIP[4], firstIP[5], firstIP[6], firstIP[7], firstIP[8], firstIP[9], firstIP[10], firstIP[11], firstIP[12], firstIP[13], firstIP[14], firstIP[15]) if r.mask != "/128" { r.firstIP[15] = randIPEndWith(255) // 随机 IP 的最后一段 r.firstIP[14] = randIPEndWith(255) // 随机 IP 的最后一段 @@ -148,11 +149,11 @@ func loadIPRanges() []*net.IPAddr { scanner := bufio.NewScanner(file) for scanner.Scan() { ranges.parseCIDR(scanner.Text()) - if IPv6 { + if isIPv4(scanner.Text()) { + ranges.chooseIPv4() + } else { ranges.chooseIPv6() - continue } - ranges.chooseIPv4() } return ranges.ips } diff --git a/task/tcping.go b/task/tcping.go index 08a64df..81d63cb 100644 --- a/task/tcping.go +++ b/task/tcping.go @@ -62,11 +62,7 @@ func (p *Ping) Run() utils.PingDelaySet { if len(p.ips) == 0 { return p.csv } - ipVersion := "IPv4" - if IPv6 { // IPv6 模式判断 - ipVersion = "IPv6" - } - fmt.Printf("开始延迟测速(模式:TCP %s,端口:%d,平均延迟上限:%v ms,平均延迟下限:%v ms)\n", ipVersion, TCPPort, utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds()) + fmt.Printf("开始延迟测速(模式:TCP,端口:%d,平均延迟上限:%v ms,平均延迟下限:%v ms)\n", TCPPort, utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds()) for _, ip := range p.ips { p.wg.Add(1) p.control <- false @@ -87,9 +83,10 @@ func (p *Ping) start(ip *net.IPAddr) { //bool connectionSucceed float32 time func (p *Ping) tcping(ip *net.IPAddr) (bool, time.Duration) { startTime := time.Now() - fullAddress := fmt.Sprintf("%s:%d", ip.String(), TCPPort) - //fmt.Println(ip.String()) - if IPv6 { // IPv6 需要加上 [] + var fullAddress string + if isIPv4(ip.String()) { + fullAddress = fmt.Sprintf("%s:%d", ip.String(), TCPPort) + } else { fullAddress = fmt.Sprintf("[%s]:%d", ip.String(), TCPPort) } conn, err := net.DialTimeout("tcp", fullAddress, tcpConnectTimeout) diff --git a/utils/csv.go b/utils/csv.go index 0f32a62..0af80fd 100644 --- a/utils/csv.go +++ b/utils/csv.go @@ -138,7 +138,7 @@ func (s DownloadSpeedSet) Swap(i, j int) { s[i], s[j] = s[j], s[i] } -func (s DownloadSpeedSet) Print(ipv6 bool) { +func (s DownloadSpeedSet) Print() { if NoPrintResult() { return } @@ -152,9 +152,12 @@ func (s DownloadSpeedSet) Print(ipv6 bool) { } headFormat := "%-16s%-5s%-5s%-5s%-6s%-11s\n" dataFormat := "%-18s%-8s%-8s%-8s%-10s%-15s\n" - if ipv6 { // IPv6 太长了,所以需要调整一下间隔 - headFormat = "%-40s%-5s%-5s%-5s%-6s%-11s\n" - dataFormat = "%-42s%-8s%-8s%-8s%-10s%-15s\n" + for i := 0; i < PrintNum; i++ { // 如果要输出的 IP 中包含 IPv6,那么就需要调整一下间隔 + if len(dateString[i][0]) > 15 { + headFormat = "%-40s%-5s%-5s%-5s%-6s%-11s\n" + dataFormat = "%-42s%-8s%-8s%-8s%-10s%-15s\n" + break + } } fmt.Printf(headFormat, "IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)") for i := 0; i < PrintNum; i++ {