新增 指定IP段数据(-ip) 参数; 新增 延迟测速时显示可用 IP 数量; 新增 有效状态代码(HTTPing 模式所用) 参数; 优化 HTTPing 延迟测速模式; 优化 匹配指定地区 功能

This commit is contained in:
xiu2
2023-01-31 20:11:13 +08:00
parent f4f1fdcd80
commit a1ae4f8e45
7 changed files with 248 additions and 248 deletions

51
main.go
View File

@@ -26,18 +26,24 @@ https://github.com/XIU2/CloudflareSpeedTest
参数: 参数:
-n 200 -n 200
测速线程数量;越多测速越快,性能弱的设备 (如路由器) 请勿太高;(默认 200 最多 1000) 延迟测速线程;越多延迟测速越快,性能弱的设备 (如路由器) 请勿太高;(默认 200 最多 1000)
-t 4 -t 4
延迟测速次数;单个 IP 延迟测速次数,为 1 时将过滤丢包的IP(默认 4 次) 延迟测速次数;单个 IP 延迟测速次数,为 1 时将过滤丢包的IP(默认 4 次)
-tp 443
指定测速端口;延迟测速/下载测速时使用的端口;(默认 443 端口httping模式下该参数无效)
-dn 10 -dn 10
下载测速数量;延迟测速并排序后,从最低延迟起下载测速的数量;(默认 10 个) 下载测速数量;延迟测速并排序后,从最低延迟起下载测速的数量;(默认 10 个)
-dt 10 -dt 10
下载测速时间;单个 IP 下载测速最长时间,不能太短;(默认 10 秒) 下载测速时间;单个 IP 下载测速最长时间,不能太短;(默认 10 秒)
-tp 443
指定测速端口;延迟测速/下载测速时使用的端口;(默认 443 端口)
-url https://cf.xiu2.xyz/url -url https://cf.xiu2.xyz/url
下载测速地址;用来下载测速的 Cloudflare CDN 文件地址,默认地址不保证可用性,建议自建; 指定测速地址;延迟测速(HTTPing)/下载测速时使用的地址,默认地址不保证可用性,建议自建;
-httping
切换测速模式;延迟测速模式改为 HTTP 协议,所用测试地址为 [-url] 参数;(默认 TCPing)
-httping-code 200
有效状态代码HTTPing 延迟测速时网页返回的有效 HTTP 状态码,仅限一个;(默认 200 301 302)
-cfcolo HKG,KHH,NRT,LAX,SEA,SJC,FRA,MAD
匹配指定地区;地区名为当地机场三字码,英文逗号分隔,仅 HTTPing 模式可用;(默认 所有地区)
-tl 200 -tl 200
平均延迟上限;只输出低于指定平均延迟的 IP可与其他上限/下限搭配;(默认 9999 ms) 平均延迟上限;只输出低于指定平均延迟的 IP可与其他上限/下限搭配;(默认 9999 ms)
@@ -50,46 +56,46 @@ https://github.com/XIU2/CloudflareSpeedTest
显示结果数量;测速后直接显示指定数量的结果,为 0 时不显示结果直接退出;(默认 10 个) 显示结果数量;测速后直接显示指定数量的结果,为 0 时不显示结果直接退出;(默认 10 个)
-f ip.txt -f ip.txt
IP段数据文件如路径含有空格请加上引号支持其他 CDN IP段(默认 ip.txt) IP段数据文件如路径含有空格请加上引号支持其他 CDN IP段(默认 ip.txt)
-ip 1.1.1.1,2.2.2.2/24,2606:4700::/32
指定IP段数据直接通过参数指定要测速的 IP 段数据,英文逗号分隔;(默认 空)
-o result.csv -o result.csv
写入结果文件;如路径含有空格请加上引号;值为空时不写入文件 [-o ""](默认 result.csv) 写入结果文件;如路径含有空格请加上引号;值为空时不写入文件 [-o ""](默认 result.csv)
-dd -dd
禁用下载测速;禁用后测速结果会按延迟排序 (默认按下载速度排序)(默认 启用) 禁用下载测速;禁用后测速结果会按延迟排序 (默认按下载速度排序)(默认 启用)
-allip -allip
测速全部的IP对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 IP 段随机测速一个 IP) 测速全部的IP对 IP 段中的每个 IP (仅支持 IPv4) 进行测速;(默认 每个 /24 段随机测速一个 IP)
-Httping
启用HTTP ping启用后会将tcping换成httping模式(默认 不启用)
-HttpingColo DFW,LAX,SEA,SJC,FRA,MAD
匹配机场三字码;需要匹配多个请使用英文逗号分割;(默认 匹配全部机场码需要启用HTTP ping)
目前已知区域KIX,HKG,SIN,NRT,ICN,DFW,LAX,SEA,SJC,FRA,MAD
目前已知大概率能扫描到美/法区域
-HttpingTimeout 2000
指定httping超时时间httping超时毫秒(默认 2000 ms需要启用HTTP ping)
-v -v
打印程序版本 + 检查版本更新 打印程序版本 + 检查版本更新
-h -h
打印帮助说明 打印帮助说明
` `
var minDelay, maxDelay, downloadTime int var minDelay, maxDelay, downloadTime int
flag.IntVar(&task.Routines, "n", 200, "测速线程数量") flag.IntVar(&task.Routines, "n", 200, "延迟测速线程")
flag.IntVar(&task.PingTimes, "t", 4, "延迟测速次数") flag.IntVar(&task.PingTimes, "t", 4, "延迟测速次数")
flag.IntVar(&task.TCPPort, "tp", 443, "指定测速端口")
flag.IntVar(&task.TestCount, "dn", 10, "下载测速数量") flag.IntVar(&task.TestCount, "dn", 10, "下载测速数量")
flag.IntVar(&downloadTime, "dt", 10, "下载测速时间") flag.IntVar(&downloadTime, "dt", 10, "下载测速时间")
flag.StringVar(&task.URL, "url", "https://cf.xiu2.xyz/url", "下载测速地址") flag.IntVar(&task.TCPPort, "tp", 443, "指定测速端口")
flag.StringVar(&task.URL, "url", "https://cf.xiu2.xyz/url", "指定测速地址")
flag.BoolVar(&task.Httping, "httping", false, "切换测速模式")
flag.IntVar(&task.HttpingStatusCode, "httping-code", 0, "有效状态代码")
flag.StringVar(&task.HttpingCFColo, "cfcolo", "", "匹配指定地区")
flag.IntVar(&maxDelay, "tl", 9999, "平均延迟上限") flag.IntVar(&maxDelay, "tl", 9999, "平均延迟上限")
flag.IntVar(&minDelay, "tll", 0, "平均延迟下限") flag.IntVar(&minDelay, "tll", 0, "平均延迟下限")
flag.Float64Var(&task.MinSpeed, "sl", 0, "下载速度下限") flag.Float64Var(&task.MinSpeed, "sl", 0, "下载速度下限")
flag.IntVar(&utils.PrintNum, "p", 10, "显示结果数量") flag.IntVar(&utils.PrintNum, "p", 10, "显示结果数量")
flag.StringVar(&task.IPFile, "f", "ip.txt", "IP 数据文件") flag.StringVar(&task.IPFile, "f", "ip.txt", "IP数据文件")
flag.StringVar(&task.IPText, "ip", "", "指定IP段数据")
flag.StringVar(&utils.Output, "o", "result.csv", "输出结果文件") flag.StringVar(&utils.Output, "o", "result.csv", "输出结果文件")
flag.BoolVar(&task.Disable, "dd", false, "禁用下载测速") flag.BoolVar(&task.Disable, "dd", false, "禁用下载测速")
flag.BoolVar(&task.TestAll, "allip", false, "测速全部 IP") flag.BoolVar(&task.TestAll, "allip", false, "测速全部 IP")
flag.BoolVar(&printVersion, "v", false, "打印程序版本") flag.BoolVar(&printVersion, "v", false, "打印程序版本")
flag.BoolVar(&task.Httping, "Httping", false, "启用HTTP ping")
flag.StringVar(&task.HttpingColo, "HttpingColo", "", "匹配机场三字码")
flag.IntVar(&task.HttpingTimeout, "HttpingTimeout", 2000, "指定httping超时时间")
flag.Usage = func() { fmt.Print(help) } flag.Usage = func() { fmt.Print(help) }
flag.Parse() flag.Parse()
@@ -99,8 +105,7 @@ https://github.com/XIU2/CloudflareSpeedTest
utils.InputMaxDelay = time.Duration(maxDelay) * time.Millisecond utils.InputMaxDelay = time.Duration(maxDelay) * time.Millisecond
utils.InputMinDelay = time.Duration(minDelay) * time.Millisecond utils.InputMinDelay = time.Duration(minDelay) * time.Millisecond
task.Timeout = time.Duration(downloadTime) * time.Second task.Timeout = time.Duration(downloadTime) * time.Second
task.HttpingColomap = task.MapColoMap() task.HttpingCFColomap = task.MapColoMap()
task.HttpingRequest = task.GetRequest()
if printVersion { if printVersion {
println(version) println(version)

View File

@@ -7,6 +7,7 @@ import (
"net" "net"
"net/http" "net/http"
"sort" "sort"
"strconv"
"time" "time"
"CloudflareSpeedTest/utils" "CloudflareSpeedTest/utils"
@@ -24,11 +25,8 @@ const (
) )
var ( var (
// download test url
URL = defaultURL URL = defaultURL
// download timeout
Timeout = defaultTimeout Timeout = defaultTimeout
// disable download
Disable = defaultDisableDownload Disable = defaultDisableDownload
TestCount = defaultTestNum TestCount = defaultTestNum
@@ -68,7 +66,13 @@ func TestDownloadSpeed(ipSet utils.PingDelaySet) (speedSet utils.DownloadSpeedSe
} }
fmt.Printf("开始下载测速(下载速度下限:%.2f MB/s下载测速数量%d下载测速队列%d\n", MinSpeed, TestCount, testNum) fmt.Printf("开始下载测速(下载速度下限:%.2f MB/s下载测速数量%d下载测速队列%d\n", MinSpeed, TestCount, testNum)
bar := utils.NewBar(TestCount, "", "") // 控制 下载测速进度条 与 延迟测速进度条 长度一致(强迫症)
bar_a := len(strconv.Itoa(len(ipSet)))
bar_b := " "
for i := 0; i < bar_a; i++ {
bar_b += " "
}
bar := utils.NewBar(TestCount, bar_b, "")
for i := 0; i < testNum; i++ { for i := 0; i < testNum; i++ {
speed := downloadHandler(ipSet[i].IP) speed := downloadHandler(ipSet[i].IP)
ipSet[i].DownloadSpeed = speed ipSet[i].DownloadSpeed = speed

View File

@@ -1,8 +1,8 @@
package task package task
import ( import (
"crypto/tls" //"crypto/tls"
"fmt" //"fmt"
"io" "io"
"log" "log"
"net" "net"
@@ -10,42 +10,31 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"CloudflareSpeedTest/utils"
) )
var ( var (
Httping bool //是否启用httping Httping bool
HttpingTimeout int //设置超时时间,单位毫秒 HttpingStatusCode int
HttpingColo string //有值代表筛选机场三字码区域 HttpingCFColo string
) HttpingCFColomap *sync.Map
var (
HttpingColomap *sync.Map
HttpingRequest *http.Request
) )
// pingReceived pingTotalTime // pingReceived pingTotalTime
func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration) { func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration) {
var fullAddress string
if isIPv4(ip.String()) {
fullAddress = fmt.Sprintf("%s", ip.String())
} else {
fullAddress = fmt.Sprintf("[%s]", ip.String())
}
hc := http.Client{ hc := http.Client{
Timeout: time.Duration(HttpingTimeout) * time.Millisecond, Timeout: time.Second * 2,
Transport: &http.Transport{ Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, DialContext: getDialContext(ip),
//TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 跳过证书验证
}, },
} // #nosec CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse // 阻止重定向
},
}
traceURL := fmt.Sprintf("http://%s/cdn-cgi/trace", // 先访问一次获得 HTTP 状态码 及 Cloudflare Colo
fullAddress)
// for connect and get colo
{ {
requ, err := http.NewRequest(http.MethodHead, traceURL, nil) requ, err := http.NewRequest(http.MethodHead, URL, nil)
if err != nil { if err != nil {
return 0, 0 return 0, 0
} }
@@ -55,22 +44,35 @@ func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration) {
return 0, 0 return 0, 0
} }
defer resp.Body.Close() defer resp.Body.Close()
//fmt.Println("IP:", ip, "StatusCode:", resp.StatusCode, resp.Request.URL)
if HttpingStatusCode == 0 || HttpingStatusCode < 100 && HttpingStatusCode > 599 {
if resp.StatusCode != 200 && resp.StatusCode != 301 && resp.StatusCode != 302 {
return 0, 0
}
} else {
if resp.StatusCode != HttpingStatusCode {
return 0, 0
}
}
io.Copy(io.Discard, resp.Body) io.Copy(io.Discard, resp.Body)
if HttpingCFColo != "" {
cfRay := resp.Header.Get("CF-RAY") cfRay := resp.Header.Get("CF-RAY")
colo := p.getColo(cfRay) colo := p.getColo(cfRay)
if colo == "" { if colo == "" {
return 0, 0 return 0, 0
} }
}
} }
// for test delay // 循环测速计算延迟
success := 0 success := 0
var delay time.Duration var delay time.Duration
for i := 0; i < PingTimes; i++ { for i := 0; i < PingTimes; i++ {
requ, err := http.NewRequest(http.MethodHead, traceURL, nil) requ, err := http.NewRequest(http.MethodHead, URL, nil)
if err != nil { if err != nil {
log.Fatal("意外的错误,情报告:", err) log.Fatal("意外的错误,情报告:", err)
return 0, 0 return 0, 0
@@ -97,11 +99,11 @@ func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration) {
} }
func MapColoMap() *sync.Map { func MapColoMap() *sync.Map {
if HttpingColo == "" { if HttpingCFColo == "" {
return nil return nil
} }
colos := strings.Split(HttpingColo, ",") colos := strings.Split(HttpingCFColo, ",")
colomap := &sync.Map{} colomap := &sync.Map{}
for _, colo := range colos { for _, colo := range colos {
colomap.Store(colo, colo) colomap.Store(colo, colo)
@@ -109,14 +111,6 @@ func MapColoMap() *sync.Map {
return colomap return colomap
} }
func GetRequest() *http.Request {
req, err := http.NewRequest("GET", URL, nil)
if err != nil {
log.Fatal(err)
}
return req
}
func (p *Ping) getColo(b string) string { func (p *Ping) getColo(b string) string {
if b == "" { if b == "" {
return "" return ""
@@ -125,13 +119,11 @@ func (p *Ping) getColo(b string) string {
out := idColo[1] out := idColo[1]
utils.ColoAble.Store(out, out) if HttpingCFColomap == nil {
if HttpingColomap == nil {
return out return out
} }
_, ok := HttpingColomap.Load(out) _, ok := HttpingCFColomap.Load(out)
if ok { if ok {
return out return out
} }

View File

@@ -18,6 +18,7 @@ var (
TestAll = false TestAll = false
// IPFile is the filename of IP Rangs // IPFile is the filename of IP Rangs
IPFile = defaultInputFile IPFile = defaultInputFile
IPText string
) )
func InitRandSeed() { func InitRandSeed() {
@@ -137,6 +138,18 @@ func (r *IPRanges) chooseIPv6() {
} }
func loadIPRanges() []*net.IPAddr { func loadIPRanges() []*net.IPAddr {
ranges := newIPRanges()
if IPText != "" { // 从参数中获取 IP 段数据
IPs := strings.Split(IPText, ",")
for _, IP := range IPs {
ranges.parseCIDR(IP)
if isIPv4(IP) {
ranges.chooseIPv4()
} else {
ranges.chooseIPv6()
}
}
} else { // 从文件中获取 IP 段数据
if IPFile == "" { if IPFile == "" {
IPFile = defaultInputFile IPFile = defaultInputFile
} }
@@ -145,7 +158,6 @@ func loadIPRanges() []*net.IPAddr {
log.Fatal(err) log.Fatal(err)
} }
defer file.Close() defer file.Close()
ranges := newIPRanges()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
ranges.parseCIDR(scanner.Text()) ranges.parseCIDR(scanner.Text())
@@ -155,5 +167,6 @@ func loadIPRanges() []*net.IPAddr {
ranges.chooseIPv6() ranges.chooseIPv6()
} }
} }
}
return ranges.ips return ranges.ips
} }

View File

@@ -55,7 +55,7 @@ func NewPing() *Ping {
ips: ips, ips: ips,
csv: make(utils.PingDelaySet, 0), csv: make(utils.PingDelaySet, 0),
control: make(chan bool, Routines), control: make(chan bool, Routines),
bar: utils.NewBar(len(ips), "可用IP:", ""), bar: utils.NewBar(len(ips), "可用:", ""),
} }
} }
@@ -64,7 +64,7 @@ func (p *Ping) Run() utils.PingDelaySet {
return p.csv return p.csv
} }
if Httping { if Httping {
fmt.Printf("开始延迟测速模式HTTP端口80,平均延迟上限:%v ms平均延迟下限%v ms)\n", utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds()) fmt.Printf("开始延迟测速模式HTTP端口%d,平均延迟上限:%v ms平均延迟下限%v ms)\n", TCPPort, utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds())
} else { } else {
fmt.Printf("开始延迟测速模式TCP端口%d平均延迟上限%v ms平均延迟下限%v ms)\n", TCPPort, utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds()) fmt.Printf("开始延迟测速模式TCP端口%d平均延迟上限%v ms平均延迟下限%v ms)\n", TCPPort, utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds())
} }

View File

@@ -6,10 +6,7 @@ import (
"log" "log"
"net" "net"
"os" "os"
"sort"
"strconv" "strconv"
"strings"
"sync"
"time" "time"
) )
@@ -24,7 +21,6 @@ var (
InputMinDelay = minDelay InputMinDelay = minDelay
Output = defaultOutput Output = defaultOutput
PrintNum = 10 PrintNum = 10
ColoAble sync.Map
) )
// 是否打印测试结果 // 是否打印测试结果
@@ -143,16 +139,6 @@ func (s DownloadSpeedSet) Swap(i, j int) {
} }
func (s DownloadSpeedSet) Print() { func (s DownloadSpeedSet) Print() {
var colos []string
ColoAble.Range(func(key, value interface{}) bool {
colos = append(colos, key.(string))
return true
})
if len(colos) != 0 {
sort.Strings(colos)
colostrings := strings.Join(colos, ",")
fmt.Println("\n下次可以考虑机场三字码参数" + colostrings + "\n")
}
if NoPrintResult() { if NoPrintResult() {
return return
} }

View File

@@ -11,7 +11,7 @@ type Bar struct {
} }
func NewBar(count int, MyStrStart, MyStrEnd string) *Bar { func NewBar(count int, MyStrStart, MyStrEnd string) *Bar {
tmpl := fmt.Sprintf(`{{counters . }}{{ bar . "[" "-" (cycle . "↖" "↗" "↘" "↙" ) "_" "]"}} %s {{string . "MyStr" | green}} %s `, MyStrStart, MyStrEnd) tmpl := fmt.Sprintf(`{{counters . }} {{ bar . "[" "-" (cycle . "↖" "↗" "↘" "↙" ) "_" "]"}} %s {{string . "MyStr" | green}} %s `, MyStrStart, MyStrEnd)
bar := pb.ProgressBarTemplate(tmpl).Start(count) bar := pb.ProgressBarTemplate(tmpl).Start(count)
return &Bar{pb: bar} return &Bar{pb: bar}
} }