mirror of
https://github.com/XIU2/CloudflareSpeedTest.git
synced 2026-03-02 11:57:05 +08:00
新增 HTTPing 延迟测速模式(beta); 新增 IP 机场三字码 colo 筛选功能(beta) (#282)
这两个功能目前仅为测试版,后续会大幅改动,以最终成品为准~
This commit is contained in:
@@ -68,13 +68,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 := utils.NewBar(TestCount, "", "")
|
||||
for i := 0; i < testNum; i++ {
|
||||
speed := downloadHandler(ipSet[i].IP)
|
||||
ipSet[i].DownloadSpeed = speed
|
||||
// 在每个 IP 下载测速后,以 [下载速度下限] 条件过滤结果
|
||||
if speed >= MinSpeed*1024*1024 {
|
||||
bar.Grow(1)
|
||||
bar.Grow(1, "")
|
||||
speedSet = append(speedSet, ipSet[i]) // 高于下载速度下限时,添加到新数组中
|
||||
if len(speedSet) == TestCount { // 凑够满足条件的 IP 时(下载测速数量 -dn),就跳出循环
|
||||
break
|
||||
|
||||
140
task/httping.go
Normal file
140
task/httping.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"CloudflareSpeedTest/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
Httping bool //是否启用httping
|
||||
HttpingTimeout int //设置超时时间,单位毫秒
|
||||
HttpingColo string //有值代表筛选机场三字码区域
|
||||
)
|
||||
|
||||
var (
|
||||
HttpingColomap *sync.Map
|
||||
HttpingRequest *http.Request
|
||||
)
|
||||
|
||||
// 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,
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
} // #nosec
|
||||
|
||||
traceURL := fmt.Sprintf("http://%s/cdn-cgi/trace",
|
||||
fullAddress)
|
||||
|
||||
// for connect and get colo
|
||||
{
|
||||
requ, err := http.NewRequest(http.MethodHead, traceURL, nil)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
requ.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36")
|
||||
resp, err := hc.Do(requ)
|
||||
if err != nil {
|
||||
return 0, 0
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
log.Fatal("意外的错误,情报告:", err)
|
||||
return 0, 0
|
||||
}
|
||||
requ.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36")
|
||||
if i == PingTimes-1 {
|
||||
requ.Header.Set("Connection", "close")
|
||||
}
|
||||
startTime := time.Now()
|
||||
resp, err := hc.Do(requ)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
success++
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
duration := time.Since(startTime)
|
||||
delay += duration
|
||||
|
||||
}
|
||||
|
||||
return success, delay
|
||||
|
||||
}
|
||||
|
||||
func MapColoMap() *sync.Map {
|
||||
if HttpingColo == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
colos := strings.Split(HttpingColo, ",")
|
||||
colomap := &sync.Map{}
|
||||
for _, colo := range colos {
|
||||
colomap.Store(colo, colo)
|
||||
}
|
||||
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 ""
|
||||
}
|
||||
idColo := strings.Split(b, "-")
|
||||
|
||||
out := idColo[1]
|
||||
|
||||
utils.ColoAble.Store(out, out)
|
||||
|
||||
if HttpingColomap == nil {
|
||||
return out
|
||||
}
|
||||
|
||||
_, ok := HttpingColomap.Load(out)
|
||||
if ok {
|
||||
return out
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -54,7 +55,7 @@ func NewPing() *Ping {
|
||||
ips: ips,
|
||||
csv: make(utils.PingDelaySet, 0),
|
||||
control: make(chan bool, Routines),
|
||||
bar: utils.NewBar(len(ips)),
|
||||
bar: utils.NewBar(len(ips), "可用IP:", ""),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +63,11 @@ func (p *Ping) Run() utils.PingDelaySet {
|
||||
if len(p.ips) == 0 {
|
||||
return p.csv
|
||||
}
|
||||
fmt.Printf("开始延迟测速(模式:TCP,端口:%d,平均延迟上限:%v ms,平均延迟下限:%v ms)\n", TCPPort, utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds())
|
||||
if Httping {
|
||||
fmt.Printf("开始延迟测速(模式:HTTP,端口:80,平均延迟上限:%v ms,平均延迟下限:%v ms)\n", utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds())
|
||||
} else {
|
||||
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
|
||||
@@ -80,7 +85,7 @@ func (p *Ping) start(ip *net.IPAddr) {
|
||||
<-p.control
|
||||
}
|
||||
|
||||
//bool connectionSucceed float32 time
|
||||
// bool connectionSucceed float32 time
|
||||
func (p *Ping) tcping(ip *net.IPAddr) (bool, time.Duration) {
|
||||
startTime := time.Now()
|
||||
var fullAddress string
|
||||
@@ -98,8 +103,12 @@ func (p *Ping) tcping(ip *net.IPAddr) (bool, time.Duration) {
|
||||
return true, duration
|
||||
}
|
||||
|
||||
//pingReceived pingTotalTime
|
||||
// pingReceived pingTotalTime
|
||||
func (p *Ping) checkConnection(ip *net.IPAddr) (recv int, totalDelay time.Duration) {
|
||||
if Httping {
|
||||
recv, totalDelay = p.httping(ip)
|
||||
return
|
||||
}
|
||||
for i := 0; i < PingTimes; i++ {
|
||||
if ok, delay := p.tcping(ip); ok {
|
||||
recv++
|
||||
@@ -120,7 +129,11 @@ func (p *Ping) appendIPData(data *utils.PingData) {
|
||||
// handle tcping
|
||||
func (p *Ping) tcpingHandler(ip *net.IPAddr) {
|
||||
recv, totalDlay := p.checkConnection(ip)
|
||||
p.bar.Grow(1)
|
||||
nowAble := len(p.csv)
|
||||
if recv != 0 {
|
||||
nowAble++
|
||||
}
|
||||
p.bar.Grow(1, strconv.Itoa(nowAble))
|
||||
if recv == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user