util.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. import (
  3. "encoding/csv"
  4. "log"
  5. "math/rand"
  6. "net"
  7. "os"
  8. "strconv"
  9. "time"
  10. "github.com/cheggaaa/pb/v3"
  11. )
  12. type CloudflareIPData struct {
  13. ip net.IPAddr
  14. pingCount int
  15. pingReceived int
  16. recvRate float32
  17. downloadSpeed float32
  18. pingTime float32
  19. }
  20. func (cf *CloudflareIPData) getRecvRate() float32 {
  21. if cf.recvRate == 0 {
  22. pingLost := cf.pingCount - cf.pingReceived
  23. cf.recvRate = float32(pingLost) / float32(cf.pingCount)
  24. }
  25. return cf.recvRate
  26. }
  27. func ExportCsv(filePath string, data []CloudflareIPData) {
  28. fp, err := os.Create(filePath)
  29. if err != nil {
  30. log.Fatalf("创建文件["+filePath+"]句柄失败,%v", err)
  31. return
  32. }
  33. defer fp.Close()
  34. w := csv.NewWriter(fp) //创建一个新的写入文件流
  35. w.Write([]string{"IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)"})
  36. w.WriteAll(convertToString(data))
  37. w.Flush()
  38. }
  39. func (cf *CloudflareIPData) toString() []string {
  40. result := make([]string, 6)
  41. result[0] = cf.ip.String()
  42. result[1] = strconv.Itoa(cf.pingCount)
  43. result[2] = strconv.Itoa(cf.pingReceived)
  44. result[3] = strconv.FormatFloat(float64(cf.getRecvRate()), 'f', 2, 32)
  45. result[4] = strconv.FormatFloat(float64(cf.pingTime), 'f', 2, 32)
  46. result[5] = strconv.FormatFloat(float64(cf.downloadSpeed)/1024/1024, 'f', 2, 32)
  47. return result
  48. }
  49. func convertToString(data []CloudflareIPData) [][]string {
  50. result := make([][]string, 0)
  51. for _, v := range data {
  52. result = append(result, v.toString())
  53. }
  54. return result
  55. }
  56. var pingTime int
  57. var pingRoutine int
  58. var ipEndWith uint8 = 0
  59. type progressEvent int
  60. const (
  61. NoAvailableIPFound progressEvent = iota
  62. AvailableIPFound
  63. NormalPing
  64. )
  65. var url string
  66. var downloadTestTime time.Duration
  67. const downloadBufferSize = 1024
  68. var downloadTestCount int
  69. //const defaultTcpPort = 443
  70. const tcpConnectTimeout = time.Second * 1
  71. var failTime int
  72. type CloudflareIPDataSet []CloudflareIPData
  73. func initRandSeed() {
  74. rand.Seed(time.Now().UnixNano())
  75. }
  76. func randipEndWith() {
  77. ipEndWith = uint8(rand.Intn(254) + 1)
  78. }
  79. func ipPadding(ip string) string {
  80. var ipLength int
  81. var ipPrint string
  82. ipPrint = ip
  83. ipLength = len(ipPrint)
  84. if ipLength < 15 {
  85. for i := 0; i <= 15-ipLength; i++ {
  86. ipPrint += " "
  87. }
  88. }
  89. return ipPrint
  90. }
  91. func handleProgressGenerator(pb *pb.ProgressBar) func(e progressEvent) {
  92. return func(e progressEvent) {
  93. switch e {
  94. case NoAvailableIPFound:
  95. pb.Add(pingTime)
  96. case AvailableIPFound:
  97. pb.Add(failTime)
  98. case NormalPing:
  99. pb.Increment()
  100. }
  101. }
  102. }
  103. func (cfs CloudflareIPDataSet) Len() int {
  104. return len(cfs)
  105. }
  106. func (cfs CloudflareIPDataSet) Less(i, j int) bool {
  107. if (cfs)[i].getRecvRate() != cfs[j].getRecvRate() {
  108. return cfs[i].getRecvRate() < cfs[j].getRecvRate()
  109. }
  110. return cfs[i].pingTime < cfs[j].pingTime
  111. }
  112. func (cfs CloudflareIPDataSet) Swap(i, j int) {
  113. cfs[i], cfs[j] = cfs[j], cfs[i]
  114. }