tcping.go 2.9 KB

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