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