测试IP延迟完成
This commit is contained in:
@@ -2,29 +2,29 @@ package task
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/schollz/progressbar/v3"
|
||||
)
|
||||
|
||||
var (
|
||||
TcpPort = 443
|
||||
PingTimes = 4
|
||||
Routines = 200
|
||||
PingTimes int
|
||||
Routines int
|
||||
TcpPort int
|
||||
TcpConnectTimeOut = time.Second * 1
|
||||
)
|
||||
|
||||
type IPDelay struct {
|
||||
IP *net.IPAddr
|
||||
Delay time.Duration
|
||||
IP *net.IPAddr
|
||||
Delay time.Duration
|
||||
DownloadSpeed float64
|
||||
}
|
||||
|
||||
func (p *IPRangeList) Run() *IPRangeList {
|
||||
var wg sync.WaitGroup
|
||||
ch := make(chan struct{}, Routines)
|
||||
|
||||
fmt.Println("正在测试IP", "协议为:", "TCP", "端口为:", TcpPort, "并发数为:", Routines, "Ping次数为:", PingTimes)
|
||||
// 创建进度条
|
||||
bar := progressbar.NewOptions(len(p.Ips),
|
||||
progressbar.OptionSetWidth(50),
|
||||
@@ -32,7 +32,6 @@ func (p *IPRangeList) Run() *IPRangeList {
|
||||
progressbar.OptionShowCount(),
|
||||
progressbar.OptionShowIts(),
|
||||
)
|
||||
|
||||
for _, ip := range p.Ips {
|
||||
wg.Add(1)
|
||||
ch <- struct{}{} // 控制最大并发数
|
||||
@@ -41,7 +40,7 @@ func (p *IPRangeList) Run() *IPRangeList {
|
||||
defer func() { <-ch }() // 释放并发控制
|
||||
success, duration := TCPing(ip)
|
||||
if success && duration > 0 {
|
||||
p.delays = append(p.delays, IPDelay{IP: ip, Delay: duration})
|
||||
p.Delays = append(p.Delays, IPDelay{IP: ip, Delay: duration, DownloadSpeed: 0})
|
||||
}
|
||||
err := bar.Add(1)
|
||||
if err != nil {
|
||||
@@ -54,27 +53,42 @@ func (p *IPRangeList) Run() *IPRangeList {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("调用成功,可用的IP数量: %d\n", len(p.delays))
|
||||
fmt.Printf("调用成功,可用的IP数量: %d\n", len(p.Delays))
|
||||
return p
|
||||
}
|
||||
|
||||
// TCPing 通过TCP连接测试IP是否可用
|
||||
func TCPing(ip *net.IPAddr) (bool, time.Duration) {
|
||||
startTime := time.Now()
|
||||
var fullAddress string
|
||||
if IsIpv4(ip.String()) {
|
||||
fullAddress = fmt.Sprintf("%s:%d", ip.String(), TcpPort)
|
||||
} else {
|
||||
fullAddress = fmt.Sprintf("[%s]:%d", ip.String(), TcpPort)
|
||||
var totalDuration time.Duration
|
||||
var successCount int
|
||||
for i := 0; i < 4; i++ { // 重试4次
|
||||
func() {
|
||||
startTime := time.Now()
|
||||
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)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
err = conn.Close()
|
||||
if err != nil {
|
||||
}
|
||||
}()
|
||||
duration := time.Since(startTime)
|
||||
totalDuration += duration
|
||||
successCount++
|
||||
}()
|
||||
}
|
||||
conn, err := net.DialTimeout("tcp", fullAddress, TcpConnectTimeOut)
|
||||
if err != nil {
|
||||
|
||||
if successCount == 0 { // 如果所有尝试都失败,返回 false 和 0
|
||||
return false, 0
|
||||
}
|
||||
defer func(conn net.Conn) {
|
||||
err = conn.Close()
|
||||
if err != nil {
|
||||
}
|
||||
}(conn)
|
||||
duration := time.Since(startTime)
|
||||
return true, duration
|
||||
|
||||
averageDuration := totalDuration / time.Duration(successCount) // 计算平均持续时间
|
||||
return true, averageDuration
|
||||
}
|
||||
|
Reference in New Issue
Block a user