From be70aa82d29b899bf143a7b162c222672791828c Mon Sep 17 00:00:00 2001 From: Shadowobj Date: Mon, 12 Aug 2024 18:26:15 +0800 Subject: [PATCH 1/8] custom port supported --- main.go | 6 ++++-- task/download.go | 12 +++++------ task/httping.go | 5 ++--- task/ip.go | 54 +++++++++++++++++++++++++++++++++++++++++------- task/tcping.go | 21 ++++++++++--------- utils/csv.go | 1 + 6 files changed, 70 insertions(+), 29 deletions(-) diff --git a/main.go b/main.go index 9ae5b26a..3c60868a 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,8 @@ https://github.com/XIU2/CloudflareSpeedTest -dt 10 下载测速时间;单个 IP 下载测速最长时间,不能太短;(默认 10 秒) -tp 443 - 指定测速端口;延迟测速/下载测速时使用的端口;(默认 443 端口) + 指定所有测速端口;即延迟测速/下载测速时使用的端口;(默认 443 端口) + (也可以在IP段数据文件里指定,在每个IP段后加上 ,端口 即可) -url https://cf.xiu2.xyz/url 指定测速地址;延迟测速(HTTPing)/下载测速时使用的地址,默认地址不保证可用性,建议自建; @@ -115,7 +116,8 @@ https://github.com/XIU2/CloudflareSpeedTest if printVersion { println(version) fmt.Println("检查版本更新中...") - checkUpdate() + //checkUpdate() + versionNew = "" if versionNew != "" { fmt.Printf("*** 发现新版本 [%s]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新! ***", versionNew) } else { diff --git a/task/download.go b/task/download.go index 26eac418..0adafad6 100644 --- a/task/download.go +++ b/task/download.go @@ -74,7 +74,7 @@ func TestDownloadSpeed(ipSet utils.PingDelaySet) (speedSet utils.DownloadSpeedSe } bar := utils.NewBar(TestCount, bar_b, "") for i := 0; i < testNum; i++ { - speed := downloadHandler(ipSet[i].IP) + speed := downloadHandler(ipSet[i].IP, ipSet[i].Port) ipSet[i].DownloadSpeed = speed // 在每个 IP 下载测速后,以 [下载速度下限] 条件过滤结果 if speed >= MinSpeed*1024*1024 { @@ -94,12 +94,12 @@ func TestDownloadSpeed(ipSet utils.PingDelaySet) (speedSet utils.DownloadSpeedSe return } -func getDialContext(ip *net.IPAddr) func(ctx context.Context, network, address string) (net.Conn, error) { +func getDialContext(ip *net.IPAddr, port int) func(ctx context.Context, network, address string) (net.Conn, error) { var fakeSourceAddr string if isIPv4(ip.String()) { - fakeSourceAddr = fmt.Sprintf("%s:%d", ip.String(), TCPPort) + fakeSourceAddr = fmt.Sprintf("%s:%d", ip.String(), port) } else { - fakeSourceAddr = fmt.Sprintf("[%s]:%d", ip.String(), TCPPort) + fakeSourceAddr = fmt.Sprintf("[%s]:%d", ip.String(), port) } return func(ctx context.Context, network, address string) (net.Conn, error) { return (&net.Dialer{}).DialContext(ctx, network, fakeSourceAddr) @@ -107,9 +107,9 @@ func getDialContext(ip *net.IPAddr) func(ctx context.Context, network, address s } // return download Speed -func downloadHandler(ip *net.IPAddr) float64 { +func downloadHandler(ip *net.IPAddr, port int) float64 { client := &http.Client{ - Transport: &http.Transport{DialContext: getDialContext(ip)}, + Transport: &http.Transport{DialContext: getDialContext(ip, port)}, Timeout: Timeout, CheckRedirect: func(req *http.Request, via []*http.Request) error { if len(via) > 10 { // 限制最多重定向 10 次 diff --git a/task/httping.go b/task/httping.go index 783a8830..e1ce789a 100644 --- a/task/httping.go +++ b/task/httping.go @@ -5,7 +5,6 @@ import ( //"fmt" "io" "log" - "net" "net/http" "regexp" "strings" @@ -22,11 +21,11 @@ var ( ) // pingReceived pingTotalTime -func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration) { +func (p *Ping) httping(ip *dest) (int, time.Duration) { hc := http.Client{ Timeout: time.Second * 2, Transport: &http.Transport{ - DialContext: getDialContext(ip), + DialContext: getDialContext(ip.IPAddr, ip.Port), //TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 跳过证书验证 }, CheckRedirect: func(req *http.Request, via []*http.Request) error { diff --git a/task/ip.go b/task/ip.go index 48f27158..ef61fa57 100644 --- a/task/ip.go +++ b/task/ip.go @@ -37,15 +37,21 @@ func randIPEndWith(num byte) byte { } type IPRanges struct { - ips []*net.IPAddr + ips []*dest + port int mask string firstIP net.IP ipNet *net.IPNet } +type dest struct { + *net.IPAddr + Port int +} + func newIPRanges() *IPRanges { return &IPRanges{ - ips: make([]*net.IPAddr, 0), + ips: make([]*dest, 0), } } @@ -73,12 +79,42 @@ func (r *IPRanges) parseCIDR(ip string) { } } +// 解析自定义的 IP 段,获得 IP、端口、IP 范围、子网掩码 +func (r *IPRanges) parseCIDRcustom(ip string) { + var ( + err error + port int64 + ) + + arr := strings.Split(ip, ",") + if len(arr) > 1 { + ip = arr[0] + if port, err = strconv.ParseInt(arr[1], 10, 0); err != nil { + r.port = TCPPort + } else { + if port <= 0 || port >= 65535 { + r.port = TCPPort + } else { + r.port = int(port) + } + } + } else { + r.port = TCPPort + } + if r.firstIP, r.ipNet, err = net.ParseCIDR(r.fixIP(ip)); err != nil { + log.Fatalln("ParseCIDRcustom err", err) + } +} + func (r *IPRanges) appendIPv4(d byte) { r.appendIP(net.IPv4(r.firstIP[12], r.firstIP[13], r.firstIP[14], d)) } func (r *IPRanges) appendIP(ip net.IP) { - r.ips = append(r.ips, &net.IPAddr{IP: ip}) + r.ips = append(r.ips, &dest{ + &net.IPAddr{IP: ip}, + r.port, + }) } // 返回第四段 ip 的最小值及可用数目 @@ -147,7 +183,7 @@ func (r *IPRanges) chooseIPv6() { } } -func loadIPRanges() []*net.IPAddr { +func loadIPRanges() []*dest { ranges := newIPRanges() if IPText != "" { // 从参数中获取 IP 段数据 IPs := strings.Split(IPText, ",") // 以逗号分隔为数组并循环遍历 @@ -156,8 +192,9 @@ func loadIPRanges() []*net.IPAddr { if IP == "" { // 跳过空的(即开头、结尾或连续多个 ,, 的情况) continue } - ranges.parseCIDR(IP) // 解析 IP 段,获得 IP、IP 范围、子网掩码 - if isIPv4(IP) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) + //ranges.parseCIDR(IP) // 解析 IP 段,获得 IP、IP 范围、子网掩码 + ranges.parseCIDRcustom(IP) // 解析 IP 段,获得 IP、端口、IP 范围、子网掩码 + if isIPv4(IP) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) ranges.chooseIPv4() } else { ranges.chooseIPv6() @@ -178,8 +215,9 @@ func loadIPRanges() []*net.IPAddr { if line == "" { // 跳过空行 continue } - ranges.parseCIDR(line) // 解析 IP 段,获得 IP、IP 范围、子网掩码 - if isIPv4(line) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) + //ranges.parseCIDR(IP) // 解析 IP 段,获得 IP、IP 范围、子网掩码 + ranges.parseCIDRcustom(line) // 解析 IP 段,获得 IP、端口、IP 范围、子网掩码 + if isIPv4(line) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) ranges.chooseIPv4() } else { ranges.chooseIPv6() diff --git a/task/tcping.go b/task/tcping.go index 511e688c..0b336399 100644 --- a/task/tcping.go +++ b/task/tcping.go @@ -28,7 +28,7 @@ var ( type Ping struct { wg *sync.WaitGroup m *sync.Mutex - ips []*net.IPAddr + ips []*dest csv utils.PingDelaySet control chan bool bar *utils.Bar @@ -64,9 +64,9 @@ func (p *Ping) Run() utils.PingDelaySet { return p.csv } if Httping { - fmt.Printf("开始延迟测速(模式:HTTP, 端口:%d, 范围:%v ~ %v ms, 丢包:%.2f)\n", TCPPort, utils.InputMinDelay.Milliseconds(), utils.InputMaxDelay.Milliseconds(), utils.InputMaxLossRate) + fmt.Printf("开始延迟测速(模式:HTTP, 默认端口:%d, 范围:%v ~ %v ms, 丢包:%.2f)\n", TCPPort, utils.InputMinDelay.Milliseconds(), utils.InputMaxDelay.Milliseconds(), utils.InputMaxLossRate) } else { - fmt.Printf("开始延迟测速(模式:TCP, 端口:%d, 范围:%v ~ %v ms, 丢包:%.2f)\n", TCPPort, utils.InputMinDelay.Milliseconds(), utils.InputMaxDelay.Milliseconds(), utils.InputMaxLossRate) + fmt.Printf("开始延迟测速(模式:TCP, 默认端口:%d, 范围:%v ~ %v ms, 丢包:%.2f)\n", TCPPort, utils.InputMinDelay.Milliseconds(), utils.InputMaxDelay.Milliseconds(), utils.InputMaxLossRate) } for _, ip := range p.ips { p.wg.Add(1) @@ -79,20 +79,20 @@ func (p *Ping) Run() utils.PingDelaySet { return p.csv } -func (p *Ping) start(ip *net.IPAddr) { +func (p *Ping) start(ip *dest) { defer p.wg.Done() p.tcpingHandler(ip) <-p.control } // bool connectionSucceed float32 time -func (p *Ping) tcping(ip *net.IPAddr) (bool, time.Duration) { +func (p *Ping) tcping(ip *dest) (bool, time.Duration) { startTime := time.Now() var fullAddress string if isIPv4(ip.String()) { - fullAddress = fmt.Sprintf("%s:%d", ip.String(), TCPPort) + fullAddress = fmt.Sprintf("%s:%d", ip.IPAddr.String(), ip.Port) } else { - fullAddress = fmt.Sprintf("[%s]:%d", ip.String(), TCPPort) + fullAddress = fmt.Sprintf("[%s]:%d", ip.IPAddr.String(), ip.Port) } conn, err := net.DialTimeout("tcp", fullAddress, tcpConnectTimeout) if err != nil { @@ -104,7 +104,7 @@ func (p *Ping) tcping(ip *net.IPAddr) (bool, time.Duration) { } // pingReceived pingTotalTime -func (p *Ping) checkConnection(ip *net.IPAddr) (recv int, totalDelay time.Duration) { +func (p *Ping) checkConnection(ip *dest) (recv int, totalDelay time.Duration) { if Httping { recv, totalDelay = p.httping(ip) return @@ -127,7 +127,7 @@ func (p *Ping) appendIPData(data *utils.PingData) { } // handle tcping -func (p *Ping) tcpingHandler(ip *net.IPAddr) { +func (p *Ping) tcpingHandler(ip *dest) { recv, totalDlay := p.checkConnection(ip) nowAble := len(p.csv) if recv != 0 { @@ -138,7 +138,8 @@ func (p *Ping) tcpingHandler(ip *net.IPAddr) { return } data := &utils.PingData{ - IP: ip, + IP: ip.IPAddr, + Port: ip.Port, Sended: PingTimes, Received: recv, Delay: totalDlay / time.Duration(recv), diff --git a/utils/csv.go b/utils/csv.go index ac48e27e..c6a5078b 100644 --- a/utils/csv.go +++ b/utils/csv.go @@ -37,6 +37,7 @@ func noOutput() bool { type PingData struct { IP *net.IPAddr + Port int Sended int Received int Delay time.Duration From afac9043f6dd82ac3b796bdf7ae7cb1a6acf8f14 Mon Sep 17 00:00:00 2001 From: Shadowobj Date: Mon, 12 Aug 2024 18:59:02 +0800 Subject: [PATCH 2/8] fixed --- .gitignore | 1 + build_linux.bat | 3 +++ build_linux.sh | 3 +++ build_win.bat | 2 ++ build_win.sh | 4 ++++ main.go | 2 +- task/ip.go | 2 +- 7 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 build_linux.bat create mode 100644 build_linux.sh create mode 100644 build_win.bat create mode 100644 build_win.sh diff --git a/.gitignore b/.gitignore index 7970fb16..ccce46f0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ dist Releases *.exe *.csv +.vscode/ \ No newline at end of file diff --git a/build_linux.bat b/build_linux.bat new file mode 100644 index 00000000..e32957fb --- /dev/null +++ b/build_linux.bat @@ -0,0 +1,3 @@ +go env -w GOOS=linux +go build -ldflags "-w -s" -o dist/CloudflareSpeedTest . +go env -w GOOS=windows \ No newline at end of file diff --git a/build_linux.sh b/build_linux.sh new file mode 100644 index 00000000..961f06e2 --- /dev/null +++ b/build_linux.sh @@ -0,0 +1,3 @@ +#!/bin/bash +go env -w GOOS=linux +go build -ldflags "-w -s" -o dist/CloudflareSpeedTest . \ No newline at end of file diff --git a/build_win.bat b/build_win.bat new file mode 100644 index 00000000..7b18e438 --- /dev/null +++ b/build_win.bat @@ -0,0 +1,2 @@ +go env -w GOOS=windows +go build -ldflags "-w -s" -o dist/CloudflareSpeedTest.exe . diff --git a/build_win.sh b/build_win.sh new file mode 100644 index 00000000..574d6b1b --- /dev/null +++ b/build_win.sh @@ -0,0 +1,4 @@ +#!/bin/bash +go env -w GOOS=windows +go build -ldflags "-w -s" -o dist/CloudflareSpeedTest.exe . +go env -w GOOS=linux \ No newline at end of file diff --git a/main.go b/main.go index 3c60868a..b22c2a57 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ https://github.com/XIU2/CloudflareSpeedTest 下载测速时间;单个 IP 下载测速最长时间,不能太短;(默认 10 秒) -tp 443 指定所有测速端口;即延迟测速/下载测速时使用的端口;(默认 443 端口) - (也可以在IP段数据文件里指定,在每个IP段后加上 ,端口 即可) + (也可以在IP段数据文件里指定,在每个IP段后加上 ;端口 即可) -url https://cf.xiu2.xyz/url 指定测速地址;延迟测速(HTTPing)/下载测速时使用的地址,默认地址不保证可用性,建议自建; diff --git a/task/ip.go b/task/ip.go index ef61fa57..c3fcab21 100644 --- a/task/ip.go +++ b/task/ip.go @@ -86,7 +86,7 @@ func (r *IPRanges) parseCIDRcustom(ip string) { port int64 ) - arr := strings.Split(ip, ",") + arr := strings.Split(ip, ";") if len(arr) > 1 { ip = arr[0] if port, err = strconv.ParseInt(arr[1], 10, 0); err != nil { From 86cc65b2593542a489b6784af7dc4614b64a45d9 Mon Sep 17 00:00:00 2001 From: Shadowobj Date: Tue, 13 Aug 2024 11:55:48 +0800 Subject: [PATCH 3/8] script removed & updated ip/mask:port input form & fixed csv output --- build_linux.bat | 3 --- build_linux.sh | 3 --- build_win.bat | 2 -- build_win.sh | 4 ---- main.go | 4 +--- task/ip.go | 55 ++++++++++++++++++++++++++++--------------------- utils/csv.go | 2 +- 7 files changed, 34 insertions(+), 39 deletions(-) delete mode 100644 build_linux.bat delete mode 100644 build_linux.sh delete mode 100644 build_win.bat delete mode 100644 build_win.sh diff --git a/build_linux.bat b/build_linux.bat deleted file mode 100644 index e32957fb..00000000 --- a/build_linux.bat +++ /dev/null @@ -1,3 +0,0 @@ -go env -w GOOS=linux -go build -ldflags "-w -s" -o dist/CloudflareSpeedTest . -go env -w GOOS=windows \ No newline at end of file diff --git a/build_linux.sh b/build_linux.sh deleted file mode 100644 index 961f06e2..00000000 --- a/build_linux.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -go env -w GOOS=linux -go build -ldflags "-w -s" -o dist/CloudflareSpeedTest . \ No newline at end of file diff --git a/build_win.bat b/build_win.bat deleted file mode 100644 index 7b18e438..00000000 --- a/build_win.bat +++ /dev/null @@ -1,2 +0,0 @@ -go env -w GOOS=windows -go build -ldflags "-w -s" -o dist/CloudflareSpeedTest.exe . diff --git a/build_win.sh b/build_win.sh deleted file mode 100644 index 574d6b1b..00000000 --- a/build_win.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -go env -w GOOS=windows -go build -ldflags "-w -s" -o dist/CloudflareSpeedTest.exe . -go env -w GOOS=linux \ No newline at end of file diff --git a/main.go b/main.go index b22c2a57..71aa9353 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ https://github.com/XIU2/CloudflareSpeedTest 下载测速时间;单个 IP 下载测速最长时间,不能太短;(默认 10 秒) -tp 443 指定所有测速端口;即延迟测速/下载测速时使用的端口;(默认 443 端口) - (也可以在IP段数据文件里指定,在每个IP段后加上 ;端口 即可) + (会被-ip参数或IP段文件中指定的 ip/mask:port 覆盖) -url https://cf.xiu2.xyz/url 指定测速地址;延迟测速(HTTPing)/下载测速时使用的地址,默认地址不保证可用性,建议自建; @@ -128,8 +128,6 @@ https://github.com/XIU2/CloudflareSpeedTest } func main() { - task.InitRandSeed() // 置随机数种子 - fmt.Printf("# XIU2/CloudflareSpeedTest %s \n\n", version) // 开始延迟测速 + 过滤延迟/丢包 diff --git a/task/ip.go b/task/ip.go index c3fcab21..b519fac5 100644 --- a/task/ip.go +++ b/task/ip.go @@ -18,13 +18,10 @@ var ( TestAll = false // IPFile is the filename of IP Rangs IPFile = defaultInputFile + seed = rand.Source(rand.NewSource(time.Now().UnixNano())) IPText string ) -func InitRandSeed() { - rand.Seed(time.Now().UnixNano()) -} - func isIPv4(ip string) bool { return strings.Contains(ip, ".") } @@ -33,7 +30,7 @@ func randIPEndWith(num byte) byte { if num == 0 { // 对于 /32 这种单独的 IP return byte(0) } - return byte(rand.Intn(int(num))) + return byte(seed.Intn(int(num))) } type IPRanges struct { @@ -80,29 +77,41 @@ func (r *IPRanges) parseCIDR(ip string) { } // 解析自定义的 IP 段,获得 IP、端口、IP 范围、子网掩码 -func (r *IPRanges) parseCIDRcustom(ip string) { +func (r *IPRanges) parseCIDRwithport(ip string) { var ( - err error - port int64 + err error + port int64 + isIPv4bool bool + sep string ) - arr := strings.Split(ip, ";") + if isIPv4(ip) { + isIPv4bool = true + sep = ":" + } else { + isIPv4bool = false + sep = "]:" + } + + arr := strings.Split(ip, sep) if len(arr) > 1 { - ip = arr[0] - if port, err = strconv.ParseInt(arr[1], 10, 0); err != nil { - r.port = TCPPort + if isIPv4bool { + ip = arr[0] } else { - if port <= 0 || port >= 65535 { - r.port = TCPPort - } else { - r.port = int(port) - } + ip = arr[0][1:] } - } else { + port, err = strconv.ParseInt(arr[1], 10, 0) + if (err == nil) && (port > 0) && (port < 65535) { + r.port = int(port) + } + } + + if err != nil || port == 0 { r.port = TCPPort } + if r.firstIP, r.ipNet, err = net.ParseCIDR(r.fixIP(ip)); err != nil { - log.Fatalln("ParseCIDRcustom err", err) + log.Fatalln("ParseCIDRwithport err", err) } } @@ -193,8 +202,8 @@ func loadIPRanges() []*dest { continue } //ranges.parseCIDR(IP) // 解析 IP 段,获得 IP、IP 范围、子网掩码 - ranges.parseCIDRcustom(IP) // 解析 IP 段,获得 IP、端口、IP 范围、子网掩码 - if isIPv4(IP) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) + ranges.parseCIDRwithport(IP) // 解析 IP 段,获得 IP、端口、IP 范围、子网掩码 + if isIPv4(IP) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) ranges.chooseIPv4() } else { ranges.chooseIPv6() @@ -216,8 +225,8 @@ func loadIPRanges() []*dest { continue } //ranges.parseCIDR(IP) // 解析 IP 段,获得 IP、IP 范围、子网掩码 - ranges.parseCIDRcustom(line) // 解析 IP 段,获得 IP、端口、IP 范围、子网掩码 - if isIPv4(line) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) + ranges.parseCIDRwithport(line) // 解析 IP 段,获得 IP、端口、IP 范围、子网掩码 + if isIPv4(line) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) ranges.chooseIPv4() } else { ranges.chooseIPv6() diff --git a/utils/csv.go b/utils/csv.go index c6a5078b..77f5f86a 100644 --- a/utils/csv.go +++ b/utils/csv.go @@ -60,7 +60,7 @@ func (cf *CloudflareIPData) getLossRate() float32 { func (cf *CloudflareIPData) toString() []string { result := make([]string, 6) - result[0] = cf.IP.String() + result[0] = fmt.Sprintf("%s:%d", cf.IP.String(), cf.Port) result[1] = strconv.Itoa(cf.Sended) result[2] = strconv.Itoa(cf.Received) result[3] = strconv.FormatFloat(float64(cf.getLossRate()), 'f', 2, 32) From 295ab1553d8d5a6490325459524ae441439884b8 Mon Sep 17 00:00:00 2001 From: Shadowobj Date: Tue, 13 Aug 2024 12:01:49 +0800 Subject: [PATCH 4/8] script removed & updated ip/mask:port input form & fixed csv output --- build_linux.bat | 3 --- build_linux.sh | 3 --- build_win.bat | 2 -- build_win.sh | 4 ---- main.go | 2 +- task/ip.go | 53 ++++++++++++++++++++++++++++++------------------- utils/csv.go | 2 +- 7 files changed, 35 insertions(+), 34 deletions(-) delete mode 100644 build_linux.bat delete mode 100644 build_linux.sh delete mode 100644 build_win.bat delete mode 100644 build_win.sh diff --git a/build_linux.bat b/build_linux.bat deleted file mode 100644 index e32957fb..00000000 --- a/build_linux.bat +++ /dev/null @@ -1,3 +0,0 @@ -go env -w GOOS=linux -go build -ldflags "-w -s" -o dist/CloudflareSpeedTest . -go env -w GOOS=windows \ No newline at end of file diff --git a/build_linux.sh b/build_linux.sh deleted file mode 100644 index 961f06e2..00000000 --- a/build_linux.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -go env -w GOOS=linux -go build -ldflags "-w -s" -o dist/CloudflareSpeedTest . \ No newline at end of file diff --git a/build_win.bat b/build_win.bat deleted file mode 100644 index 7b18e438..00000000 --- a/build_win.bat +++ /dev/null @@ -1,2 +0,0 @@ -go env -w GOOS=windows -go build -ldflags "-w -s" -o dist/CloudflareSpeedTest.exe . diff --git a/build_win.sh b/build_win.sh deleted file mode 100644 index 574d6b1b..00000000 --- a/build_win.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -go env -w GOOS=windows -go build -ldflags "-w -s" -o dist/CloudflareSpeedTest.exe . -go env -w GOOS=linux \ No newline at end of file diff --git a/main.go b/main.go index b22c2a57..07dbfc2e 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ https://github.com/XIU2/CloudflareSpeedTest 下载测速时间;单个 IP 下载测速最长时间,不能太短;(默认 10 秒) -tp 443 指定所有测速端口;即延迟测速/下载测速时使用的端口;(默认 443 端口) - (也可以在IP段数据文件里指定,在每个IP段后加上 ;端口 即可) + (会被-ip参数或IP段文件中指定的 ip/mask:port 覆盖) -url https://cf.xiu2.xyz/url 指定测速地址;延迟测速(HTTPing)/下载测速时使用的地址,默认地址不保证可用性,建议自建; diff --git a/task/ip.go b/task/ip.go index c3fcab21..768a451c 100644 --- a/task/ip.go +++ b/task/ip.go @@ -18,11 +18,12 @@ var ( TestAll = false // IPFile is the filename of IP Rangs IPFile = defaultInputFile + seed *rand.Rand IPText string ) func InitRandSeed() { - rand.Seed(time.Now().UnixNano()) + seed = rand.New(rand.NewSource(time.Now().UnixNano())) } func isIPv4(ip string) bool { @@ -33,7 +34,7 @@ func randIPEndWith(num byte) byte { if num == 0 { // 对于 /32 这种单独的 IP return byte(0) } - return byte(rand.Intn(int(num))) + return byte(seed.Intn(int(num))) } type IPRanges struct { @@ -80,29 +81,41 @@ func (r *IPRanges) parseCIDR(ip string) { } // 解析自定义的 IP 段,获得 IP、端口、IP 范围、子网掩码 -func (r *IPRanges) parseCIDRcustom(ip string) { +func (r *IPRanges) parseCIDRwithport(ip string) { var ( - err error - port int64 + err error + port int64 + isIPv4bool bool + sep string ) - arr := strings.Split(ip, ";") + if isIPv4(ip) { + isIPv4bool = true + sep = ":" + } else { + isIPv4bool = false + sep = "]:" + } + + arr := strings.Split(ip, sep) if len(arr) > 1 { - ip = arr[0] - if port, err = strconv.ParseInt(arr[1], 10, 0); err != nil { - r.port = TCPPort + if isIPv4bool { + ip = arr[0] } else { - if port <= 0 || port >= 65535 { - r.port = TCPPort - } else { - r.port = int(port) - } + ip = arr[0][1:] } - } else { + port, err = strconv.ParseInt(arr[1], 10, 0) + if (err == nil) && (port > 0) && (port < 65535) { + r.port = int(port) + } + } + + if err != nil || port == 0 { r.port = TCPPort } + if r.firstIP, r.ipNet, err = net.ParseCIDR(r.fixIP(ip)); err != nil { - log.Fatalln("ParseCIDRcustom err", err) + log.Fatalln("ParseCIDRwithport err", err) } } @@ -193,8 +206,8 @@ func loadIPRanges() []*dest { continue } //ranges.parseCIDR(IP) // 解析 IP 段,获得 IP、IP 范围、子网掩码 - ranges.parseCIDRcustom(IP) // 解析 IP 段,获得 IP、端口、IP 范围、子网掩码 - if isIPv4(IP) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) + ranges.parseCIDRwithport(IP) // 解析 IP 段,获得 IP、端口、IP 范围、子网掩码 + if isIPv4(IP) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) ranges.chooseIPv4() } else { ranges.chooseIPv6() @@ -216,8 +229,8 @@ func loadIPRanges() []*dest { continue } //ranges.parseCIDR(IP) // 解析 IP 段,获得 IP、IP 范围、子网掩码 - ranges.parseCIDRcustom(line) // 解析 IP 段,获得 IP、端口、IP 范围、子网掩码 - if isIPv4(line) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) + ranges.parseCIDRwithport(line) // 解析 IP 段,获得 IP、端口、IP 范围、子网掩码 + if isIPv4(line) { // 生成要测速的所有 IPv4 / IPv6 地址(单个/随机/全部) ranges.chooseIPv4() } else { ranges.chooseIPv6() diff --git a/utils/csv.go b/utils/csv.go index c6a5078b..77f5f86a 100644 --- a/utils/csv.go +++ b/utils/csv.go @@ -60,7 +60,7 @@ func (cf *CloudflareIPData) getLossRate() float32 { func (cf *CloudflareIPData) toString() []string { result := make([]string, 6) - result[0] = cf.IP.String() + result[0] = fmt.Sprintf("%s:%d", cf.IP.String(), cf.Port) result[1] = strconv.Itoa(cf.Sended) result[2] = strconv.Itoa(cf.Received) result[3] = strconv.FormatFloat(float64(cf.getLossRate()), 'f', 2, 32) From f2cf5bb43b5e97636cb7a5aee31b167eeb7d0757 Mon Sep 17 00:00:00 2001 From: Shadowobj Date: Tue, 13 Aug 2024 12:24:32 +0800 Subject: [PATCH 5/8] .gitignore updated --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ccce46f0..f964d988 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ dist Releases *.exe *.csv -.vscode/ \ No newline at end of file +.vscode/ +*.bat +*.sh \ No newline at end of file From ca648fa3d266d414267735ddbee043a336a97bdc Mon Sep 17 00:00:00 2001 From: Shadowobj Date: Tue, 13 Aug 2024 12:26:39 +0800 Subject: [PATCH 6/8] fixed updater --- main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.go b/main.go index 71aa9353..36b73f7d 100644 --- a/main.go +++ b/main.go @@ -116,8 +116,7 @@ https://github.com/XIU2/CloudflareSpeedTest if printVersion { println(version) fmt.Println("检查版本更新中...") - //checkUpdate() - versionNew = "" + checkUpdate() if versionNew != "" { fmt.Printf("*** 发现新版本 [%s]!请前往 [https://github.com/XIU2/CloudflareSpeedTest] 更新! ***", versionNew) } else { From e2b4ceca965c860a11de55bbd1c86bc89d3719e2 Mon Sep 17 00:00:00 2001 From: Shadowobj Date: Tue, 13 Aug 2024 12:49:33 +0800 Subject: [PATCH 7/8] add OnlyIP support --- main.go | 3 +++ utils/csv.go | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 36b73f7d..923a06dd 100644 --- a/main.go +++ b/main.go @@ -68,6 +68,8 @@ https://github.com/XIU2/CloudflareSpeedTest 禁用下载测速;禁用后测速结果会按延迟排序 (默认按下载速度排序);(默认 启用) -allip 测速全部的IP;对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 /24 段随机测速一个 IP) + -onlyip + 命令行输出和结果文件仅输出 IP,不附带端口信息 -v 打印程序版本 + 检查版本更新 @@ -99,6 +101,7 @@ https://github.com/XIU2/CloudflareSpeedTest flag.BoolVar(&task.Disable, "dd", false, "禁用下载测速") flag.BoolVar(&task.TestAll, "allip", false, "测速全部 IP") + flag.BoolVar(&utils.OnlyIP, "onlyip", false, "结果仅输出 IP") flag.BoolVar(&printVersion, "v", false, "打印程序版本") flag.Usage = func() { fmt.Print(help) } diff --git a/utils/csv.go b/utils/csv.go index 77f5f86a..b2d4b6be 100644 --- a/utils/csv.go +++ b/utils/csv.go @@ -23,6 +23,7 @@ var ( InputMaxLossRate = maxLossRate Output = defaultOutput PrintNum = 10 + OnlyIP = false ) // 是否打印测试结果 @@ -60,7 +61,11 @@ func (cf *CloudflareIPData) getLossRate() float32 { func (cf *CloudflareIPData) toString() []string { result := make([]string, 6) - result[0] = fmt.Sprintf("%s:%d", cf.IP.String(), cf.Port) + if OnlyIP { + result[0] = cf.IP.String() + } else { + result[0] = fmt.Sprintf("%s:%d", cf.IP.String(), cf.Port) + } result[1] = strconv.Itoa(cf.Sended) result[2] = strconv.Itoa(cf.Received) result[3] = strconv.FormatFloat(float64(cf.getLossRate()), 'f', 2, 32) From 2031c2b5804436c38f54c09679dfc9cfddfb1ee6 Mon Sep 17 00:00:00 2001 From: Shadowobj Date: Tue, 13 Aug 2024 12:53:14 +0800 Subject: [PATCH 8/8] fixed --- main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.go b/main.go index 923a06dd..225082b9 100644 --- a/main.go +++ b/main.go @@ -130,6 +130,8 @@ https://github.com/XIU2/CloudflareSpeedTest } func main() { + task.InitRandSeed() // 置随机数种子 + fmt.Printf("# XIU2/CloudflareSpeedTest %s \n\n", version) // 开始延迟测速 + 过滤延迟/丢包