mirror of
https://github.com/XIU2/CloudflareSpeedTest.git
synced 2026-03-11 17:35:57 +08:00
新增 指定IP段数据(-ip) 参数; 新增 延迟测速时显示可用 IP 数量; 新增 有效状态代码(HTTPing 模式所用) 参数; 优化 HTTPing 延迟测速模式; 优化 匹配指定地区 功能
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"CloudflareSpeedTest/utils"
|
||||
@@ -24,11 +25,8 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
// download test url
|
||||
URL = defaultURL
|
||||
// download timeout
|
||||
URL = defaultURL
|
||||
Timeout = defaultTimeout
|
||||
// disable download
|
||||
Disable = defaultDisableDownload
|
||||
|
||||
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)
|
||||
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++ {
|
||||
speed := downloadHandler(ipSet[i].IP)
|
||||
ipSet[i].DownloadSpeed = speed
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
//"crypto/tls"
|
||||
//"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
@@ -10,42 +10,31 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"CloudflareSpeedTest/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
Httping bool //是否启用httping
|
||||
HttpingTimeout int //设置超时时间,单位毫秒
|
||||
HttpingColo string //有值代表筛选机场三字码区域
|
||||
)
|
||||
|
||||
var (
|
||||
HttpingColomap *sync.Map
|
||||
HttpingRequest *http.Request
|
||||
Httping bool
|
||||
HttpingStatusCode int
|
||||
HttpingCFColo string
|
||||
HttpingCFColomap *sync.Map
|
||||
)
|
||||
|
||||
// pingReceived pingTotalTime
|
||||
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{
|
||||
Timeout: time.Duration(HttpingTimeout) * time.Millisecond,
|
||||
Timeout: time.Second * 2,
|
||||
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",
|
||||
fullAddress)
|
||||
|
||||
// for connect and get colo
|
||||
// 先访问一次获得 HTTP 状态码 及 Cloudflare Colo
|
||||
{
|
||||
requ, err := http.NewRequest(http.MethodHead, traceURL, nil)
|
||||
requ, err := http.NewRequest(http.MethodHead, URL, nil)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
@@ -55,22 +44,35 @@ func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration) {
|
||||
return 0, 0
|
||||
}
|
||||
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)
|
||||
|
||||
cfRay := resp.Header.Get("CF-RAY")
|
||||
|
||||
colo := p.getColo(cfRay)
|
||||
if colo == "" {
|
||||
return 0, 0
|
||||
if HttpingCFColo != "" {
|
||||
cfRay := resp.Header.Get("CF-RAY")
|
||||
colo := p.getColo(cfRay)
|
||||
if colo == "" {
|
||||
return 0, 0
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// for test delay
|
||||
// 循环测速计算延迟
|
||||
success := 0
|
||||
var delay time.Duration
|
||||
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 {
|
||||
log.Fatal("意外的错误,情报告:", err)
|
||||
return 0, 0
|
||||
@@ -97,11 +99,11 @@ func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration) {
|
||||
}
|
||||
|
||||
func MapColoMap() *sync.Map {
|
||||
if HttpingColo == "" {
|
||||
if HttpingCFColo == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
colos := strings.Split(HttpingColo, ",")
|
||||
colos := strings.Split(HttpingCFColo, ",")
|
||||
colomap := &sync.Map{}
|
||||
for _, colo := range colos {
|
||||
colomap.Store(colo, colo)
|
||||
@@ -109,14 +111,6 @@ func MapColoMap() *sync.Map {
|
||||
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 {
|
||||
if b == "" {
|
||||
return ""
|
||||
@@ -125,13 +119,11 @@ func (p *Ping) getColo(b string) string {
|
||||
|
||||
out := idColo[1]
|
||||
|
||||
utils.ColoAble.Store(out, out)
|
||||
|
||||
if HttpingColomap == nil {
|
||||
if HttpingCFColomap == nil {
|
||||
return out
|
||||
}
|
||||
|
||||
_, ok := HttpingColomap.Load(out)
|
||||
_, ok := HttpingCFColomap.Load(out)
|
||||
if ok {
|
||||
return out
|
||||
}
|
||||
|
||||
43
task/ip.go
43
task/ip.go
@@ -18,6 +18,7 @@ var (
|
||||
TestAll = false
|
||||
// IPFile is the filename of IP Rangs
|
||||
IPFile = defaultInputFile
|
||||
IPText string
|
||||
)
|
||||
|
||||
func InitRandSeed() {
|
||||
@@ -137,22 +138,34 @@ func (r *IPRanges) chooseIPv6() {
|
||||
}
|
||||
|
||||
func loadIPRanges() []*net.IPAddr {
|
||||
if IPFile == "" {
|
||||
IPFile = defaultInputFile
|
||||
}
|
||||
file, err := os.Open(IPFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
ranges := newIPRanges()
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
ranges.parseCIDR(scanner.Text())
|
||||
if isIPv4(scanner.Text()) {
|
||||
ranges.chooseIPv4()
|
||||
} else {
|
||||
ranges.chooseIPv6()
|
||||
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 == "" {
|
||||
IPFile = defaultInputFile
|
||||
}
|
||||
file, err := os.Open(IPFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
ranges.parseCIDR(scanner.Text())
|
||||
if isIPv4(scanner.Text()) {
|
||||
ranges.chooseIPv4()
|
||||
} else {
|
||||
ranges.chooseIPv6()
|
||||
}
|
||||
}
|
||||
}
|
||||
return ranges.ips
|
||||
|
||||
@@ -55,7 +55,7 @@ func NewPing() *Ping {
|
||||
ips: ips,
|
||||
csv: make(utils.PingDelaySet, 0),
|
||||
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
|
||||
}
|
||||
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 {
|
||||
fmt.Printf("开始延迟测速(模式:TCP,端口:%d,平均延迟上限:%v ms,平均延迟下限:%v ms)\n", TCPPort, utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user