tcping.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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(ips []*net.IPAddr) *Ping {
  42. checkPingDefault()
  43. return &Ping{
  44. wg: &sync.WaitGroup{},
  45. m: &sync.Mutex{},
  46. ips: ips,
  47. csv: make(utils.PingDelaySet, 0),
  48. control: make(chan bool, Routines),
  49. bar: utils.NewBar(len(ips)),
  50. }
  51. }
  52. func (p *Ping) Run() utils.PingDelaySet {
  53. for _, ip := range p.ips {
  54. p.wg.Add(1)
  55. p.control <- false
  56. go p.start(ip)
  57. }
  58. p.wg.Wait()
  59. p.bar.Done()
  60. sort.Sort(p.csv)
  61. return p.csv
  62. }
  63. func (p *Ping) start(ip *net.IPAddr) {
  64. defer p.wg.Done()
  65. p.tcpingHandler(ip)
  66. <-p.control
  67. }
  68. //bool connectionSucceed float32 time
  69. func (p *Ping) tcping(ip *net.IPAddr) (bool, time.Duration) {
  70. startTime := time.Now()
  71. fullAddress := fmt.Sprintf("%s:%d", ip.String(), TCPPort)
  72. //fmt.Println(ip.String())
  73. if IPv6 { // IPv6 需要加上 []
  74. fullAddress = fmt.Sprintf("[%s]:%d", ip.String(), TCPPort)
  75. }
  76. conn, err := net.DialTimeout("tcp", fullAddress, tcpConnectTimeout)
  77. if err != nil {
  78. return false, 0
  79. }
  80. defer conn.Close()
  81. duration := time.Since(startTime)
  82. return true, duration
  83. }
  84. //pingReceived pingTotalTime
  85. func (p *Ping) checkConnection(ip *net.IPAddr) (recv int, totalDelay time.Duration) {
  86. for i := 0; i < PingTimes; i++ {
  87. if ok, delay := p.tcping(ip); ok {
  88. recv++
  89. totalDelay += delay
  90. }
  91. }
  92. return
  93. }
  94. func (p *Ping) appendIPData(data *utils.PingData) {
  95. p.m.Lock()
  96. defer p.m.Unlock()
  97. p.csv = append(p.csv, utils.CloudflareIPData{
  98. PingData: data,
  99. })
  100. }
  101. //return Success packetRecv averagePingTime specificIPAddr
  102. func (p *Ping) tcpingHandler(ip *net.IPAddr) {
  103. ipCanConnect := false
  104. pingRecv := 0
  105. var delay time.Duration
  106. for !ipCanConnect {
  107. recv, totalDlay := p.checkConnection(ip)
  108. if recv > 0 {
  109. ipCanConnect = true
  110. pingRecv = recv
  111. delay = totalDlay
  112. } else {
  113. ip.IP[15]++
  114. if ip.IP[15] == 0 {
  115. break
  116. }
  117. break
  118. }
  119. }
  120. p.bar.Grow(1)
  121. if !ipCanConnect {
  122. return
  123. }
  124. // for i := 0; i < PingTimes; i++ {
  125. // pingSuccess, pingTimeCurrent := p.tcping(ip)
  126. // progressHandler(utils.NormalPing)
  127. // if pingSuccess {
  128. // pingRecv++
  129. // pingTime += pingTimeCurrent
  130. // }
  131. // }
  132. data := &utils.PingData{
  133. IP: ip,
  134. Sended: PingTimes,
  135. Received: pingRecv,
  136. Delay: delay / time.Duration(pingRecv),
  137. }
  138. p.appendIPData(data)
  139. return
  140. }