tcping.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package main
  2. import (
  3. "context"
  4. "io"
  5. "net"
  6. "net/http"
  7. "strconv"
  8. "sync"
  9. "time"
  10. "github.com/VividCortex/ewma"
  11. )
  12. //bool connectionSucceed float32 time
  13. func tcping(ip net.IPAddr, tcpPort int) (bool, float32) {
  14. startTime := time.Now()
  15. conn, err := net.DialTimeout("tcp", ip.String()+":"+strconv.Itoa(tcpPort), tcpConnectTimeout)
  16. if err != nil {
  17. return false, 0
  18. } else {
  19. var endTime = time.Since(startTime)
  20. var duration = float32(endTime.Microseconds()) / 1000.0
  21. _ = conn.Close()
  22. return true, duration
  23. }
  24. }
  25. //pingReceived pingTotalTime
  26. func checkConnection(ip net.IPAddr, tcpPort int) (int, float32) {
  27. pingRecv := 0
  28. var pingTime float32 = 0.0
  29. for i := 1; i <= failTime; i++ {
  30. pingSucceed, pingTimeCurrent := tcping(ip, tcpPort)
  31. if pingSucceed {
  32. pingRecv++
  33. pingTime += pingTimeCurrent
  34. }
  35. }
  36. return pingRecv, pingTime
  37. }
  38. //return Success packetRecv averagePingTime specificIPAddr
  39. func tcpingHandler(ip net.IPAddr, tcpPort int, pingCount int, progressHandler func(e progressEvent)) (bool, int, float32, net.IPAddr) {
  40. ipCanConnect := false
  41. pingRecv := 0
  42. var pingTime float32 = 0.0
  43. for !ipCanConnect {
  44. pingRecvCurrent, pingTimeCurrent := checkConnection(ip, tcpPort)
  45. if pingRecvCurrent != 0 {
  46. ipCanConnect = true
  47. pingRecv = pingRecvCurrent
  48. pingTime = pingTimeCurrent
  49. } else {
  50. ip.IP[15]++
  51. if ip.IP[15] == 0 {
  52. break
  53. }
  54. break
  55. }
  56. }
  57. if ipCanConnect {
  58. progressHandler(AvailableIPFound)
  59. for i := failTime; i < pingCount; i++ {
  60. pingSuccess, pingTimeCurrent := tcping(ip, tcpPort)
  61. progressHandler(NormalPing)
  62. if pingSuccess {
  63. pingRecv++
  64. pingTime += pingTimeCurrent
  65. }
  66. }
  67. return true, pingRecv, pingTime / float32(pingRecv), ip
  68. } else {
  69. progressHandler(NoAvailableIPFound)
  70. return false, 0, 0, net.IPAddr{}
  71. }
  72. }
  73. func tcpingGoroutine(wg *sync.WaitGroup, mutex *sync.Mutex, ip net.IPAddr, tcpPort int, pingCount int, csv *[]CloudflareIPData, control chan bool, progressHandler func(e progressEvent)) {
  74. defer wg.Done()
  75. success, pingRecv, pingTimeAvg, currentIP := tcpingHandler(ip, tcpPort, pingCount, progressHandler)
  76. if success {
  77. mutex.Lock()
  78. var cfdata CloudflareIPData
  79. cfdata.ip = currentIP
  80. cfdata.pingReceived = pingRecv
  81. cfdata.pingTime = pingTimeAvg
  82. cfdata.pingCount = pingCount
  83. *csv = append(*csv, cfdata)
  84. mutex.Unlock()
  85. }
  86. <-control
  87. }
  88. func GetDialContextByAddr(fakeSourceAddr string) func(ctx context.Context, network, address string) (net.Conn, error) {
  89. return func(ctx context.Context, network, address string) (net.Conn, error) {
  90. c, e := (&net.Dialer{}).DialContext(ctx, network, fakeSourceAddr)
  91. return c, e
  92. }
  93. }
  94. //bool : can download,float32 downloadSpeed
  95. func DownloadSpeedHandler(ip net.IPAddr) (bool, float32) {
  96. var client = http.Client{
  97. Transport: nil,
  98. CheckRedirect: nil,
  99. Jar: nil,
  100. Timeout: 0,
  101. }
  102. client.Transport = &http.Transport{
  103. DialContext: GetDialContextByAddr(ip.String() + ":443"),
  104. }
  105. response, err := client.Get(url)
  106. if err != nil {
  107. return false, 0
  108. } else {
  109. defer func() { _ = response.Body.Close() }()
  110. if response.StatusCode == 200 {
  111. timeStart := time.Now()
  112. timeEnd := timeStart.Add(downloadTestTime)
  113. contentLength := response.ContentLength
  114. buffer := make([]byte, downloadBufferSize)
  115. var contentRead int64 = 0
  116. var timeSlice = downloadTestTime / 100
  117. var timeCounter = 1
  118. var lastContentRead int64 = 0
  119. var nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
  120. e := ewma.NewMovingAverage()
  121. for contentLength != contentRead {
  122. var currentTime = time.Now()
  123. if currentTime.After(nextTime) {
  124. timeCounter += 1
  125. nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
  126. e.Add(float64(contentRead - lastContentRead))
  127. lastContentRead = contentRead
  128. }
  129. if currentTime.After(timeEnd) {
  130. break
  131. }
  132. bufferRead, err := response.Body.Read(buffer)
  133. contentRead += int64(bufferRead)
  134. if err != nil {
  135. if err != io.EOF {
  136. break
  137. } else {
  138. e.Add(float64(contentRead-lastContentRead) / (float64(nextTime.Sub(currentTime)) / float64(timeSlice)))
  139. }
  140. }
  141. }
  142. return true, float32(e.Value()) / (float32(downloadTestTime.Seconds()) / 100)
  143. } else {
  144. return false, 0
  145. }
  146. }
  147. }