Skip to content

Commit

Permalink
新增 支持 IPv4 与 IPv6 混合测速; 移除 -ipv6 参数(已无用)
Browse files Browse the repository at this point in the history
  • Loading branch information
XIU2 committed Nov 8, 2022
1 parent ce22f6b commit 021914f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 34 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, "下载速度下限")
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions task/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 10 additions & 9 deletions task/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 的最后一段
Expand Down Expand Up @@ -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
}
13 changes: 5 additions & 8 deletions task/tcping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions utils/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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++ {
Expand Down

0 comments on commit 021914f

Please sign in to comment.