tcping.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package task
  2. import (
  3. "fmt"
  4. "net"
  5. "sort"
  6. "sync"
  7. "time"
  8. "CloudflareSpeedTest/utils"
  9. )
  10. const (
  11. tcpConnectTimeout = time.Second * 1
  12. maxRoutine = 1000
  13. defaultRoutines = 200
  14. defaultPort = 443
  15. defaultPingTimes = 4
  16. )
  17. var (
  18. Routines = defaultRoutines
  19. TCPPort int = defaultPort
  20. PingTimes int = defaultPingTimes
  21. )
  22. type Ping struct {
  23. wg *sync.WaitGroup
  24. m *sync.Mutex
  25. ips []*net.IPAddr
  26. csv utils.PingDelaySet
  27. control chan bool
  28. bar *utils.Bar
  29. }
  30. func checkPingDefault() {
  31. if Routines <= 0 {
  32. Routines = defaultRoutines
  33. }
  34. if TCPPort <= 0 || TCPPort >= 65535 {
  35. TCPPort = defaultPort
  36. }
  37. if PingTimes <= 0 {
  38. PingTimes = defaultPingTimes
  39. }
  40. }
  41. func NewPing() *Ping {
  42. checkPingDefault()
  43. ips := loadIPRanges()
  44. return &Ping{
  45. wg: &sync.WaitGroup{},
  46. m: &sync.Mutex{},
  47. ips: ips,
  48. csv: make(utils.PingDelaySet, 0),
  49. control: make(chan bool, Routines),
  50. bar: utils.NewBar(len(ips)),
  51. }
  52. }
  53. func (p *Ping) Run() utils.PingDelaySet {
  54. if len(p.ips) == 0 {
  55. return p.csv
  56. }
  57. fmt.Printf("开始延迟测速(模式:TCP,端口:%d,平均延迟上限:%v ms,平均延迟下限:%v ms)\n", TCPPort, utils.InputMaxDelay.Milliseconds(), utils.InputMinDelay.Milliseconds())
  58. for _, ip := range p.ips {
  59. p.wg.Add(1)
  60. p.control <- false
  61. go p.start(ip)
  62. }
  63. p.wg.Wait()
  64. p.bar.Done()
  65. sort.Sort(p.csv)
  66. return p.csv
  67. }
  68. func (p *Ping) start(ip *net.IPAddr) {
  69. defer p.wg.Done()
  70. p.tcpingHandler(ip)
  71. <-p.control
  72. }
  73. //bool connectionSucceed float32 time
  74. func (p *Ping) tcping(ip *net.IPAddr) (bool, time.Duration) {
  75. startTime := time.Now()
  76. var fullAddress string
  77. if isIPv4(ip.String()) {
  78. fullAddress = fmt.Sprintf("%s:%d", ip.String(), TCPPort)
  79. } else {
  80. fullAddress = fmt.Sprintf("[%s]:%d", ip.String(), TCPPort)
  81. }
  82. conn, err := net.DialTimeout("tcp", fullAddress, tcpConnectTimeout)
  83. if err != nil {
  84. return false, 0
  85. }
  86. defer conn.Close()
  87. duration := time.Since(startTime)
  88. return true, duration
  89. }
  90. //pingReceived pingTotalTime
  91. func (p *Ping) checkConnection(ip *net.IPAddr) (recv int, totalDelay time.Duration) {
  92. for i := 0; i < PingTimes; i++ {
  93. if ok, delay := p.tcping(ip); ok {
  94. recv++
  95. totalDelay += delay
  96. }
  97. }
  98. return
  99. }
  100. func (p *Ping) appendIPData(data *utils.PingData) {
  101. p.m.Lock()
  102. defer p.m.Unlock()
  103. p.csv = append(p.csv, utils.CloudflareIPData{
  104. PingData: data,
  105. })
  106. }
  107. // handle tcping
  108. func (p *Ping) tcpingHandler(ip *net.IPAddr) {
  109. recv, totalDlay := p.checkConnection(ip)
  110. p.bar.Grow(1)
  111. if recv == 0 {
  112. return
  113. }
  114. data := &utils.PingData{
  115. IP: ip,
  116. Sended: PingTimes,
  117. Received: recv,
  118. Delay: totalDlay / time.Duration(recv),
  119. }
  120. p.appendIPData(data)
  121. }