IPRangeLoader.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "bufio"
  4. "log"
  5. "net"
  6. "os"
  7. )
  8. func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr {
  9. file, err := os.Open(ipFile)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. firstIPs := make([]net.IPAddr, 0)
  14. scanner := bufio.NewScanner(file)
  15. scanner.Split(bufio.ScanLines)
  16. for scanner.Scan() {
  17. IPString := scanner.Text()
  18. firstIP, IPRange, err := net.ParseCIDR(IPString)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. if ipv6Mode { // IPv6
  23. var tempIP uint8
  24. for IPRange.Contains(firstIP) {
  25. //fmt.Println(firstIP)
  26. //fmt.Println(firstIP[0], firstIP[1], firstIP[2], firstIP[3], firstIP[4], firstIP[5], firstIP[6], firstIP[7], firstIP[8], firstIP[9], firstIP[10], firstIP[11], firstIP[12], firstIP[13], firstIP[14], firstIP[15])
  27. firstIP[15] = randipEndWith() // 随机 IP 的最后一段
  28. firstIP[14] = randipEndWith() // 随机 IP 的最后一段
  29. firstIPCopy := make([]byte, len(firstIP))
  30. copy(firstIPCopy, firstIP)
  31. firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
  32. tempIP = firstIP[13]
  33. firstIP[13] += randipEndWith()
  34. if firstIP[13] < tempIP {
  35. tempIP = firstIP[12]
  36. firstIP[12] += randipEndWith()
  37. if firstIP[12] < tempIP {
  38. tempIP = firstIP[11]
  39. firstIP[11] += randipEndWith()
  40. if firstIP[11] < tempIP {
  41. tempIP = firstIP[10]
  42. firstIP[10] += randipEndWith()
  43. if firstIP[10] < tempIP {
  44. tempIP = firstIP[9]
  45. firstIP[9] += randipEndWith()
  46. if firstIP[9] < tempIP {
  47. tempIP = firstIP[8]
  48. firstIP[8] += randipEndWith()
  49. if firstIP[8] < tempIP {
  50. tempIP = firstIP[7]
  51. firstIP[7] += randipEndWith()
  52. if firstIP[7] < tempIP {
  53. tempIP = firstIP[6]
  54. firstIP[6] += randipEndWith()
  55. if firstIP[6] < tempIP {
  56. tempIP = firstIP[5]
  57. firstIP[5] += randipEndWith()
  58. if firstIP[5] < tempIP {
  59. tempIP = firstIP[4]
  60. firstIP[4] += randipEndWith()
  61. if firstIP[4] < tempIP {
  62. tempIP = firstIP[3]
  63. firstIP[3] += randipEndWith()
  64. if firstIP[3] < tempIP {
  65. tempIP = firstIP[2]
  66. firstIP[2] += randipEndWith()
  67. if firstIP[2] < tempIP {
  68. tempIP = firstIP[1]
  69. firstIP[1] += randipEndWith()
  70. if firstIP[1] < tempIP {
  71. tempIP = firstIP[0]
  72. firstIP[0] += randipEndWith()
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. } else { //IPv4
  88. for IPRange.Contains(firstIP) {
  89. firstIP[15] = randipEndWith() // 随机 IP 的最后一段 0.0.0.X
  90. firstIPCopy := make([]byte, len(firstIP))
  91. copy(firstIPCopy, firstIP)
  92. firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
  93. firstIP[14]++ // 0.0.(X+1).X
  94. if firstIP[14] == 0 {
  95. firstIP[13]++ // 0.(X+1).X.X
  96. if firstIP[13] == 0 {
  97. firstIP[12]++ // (X+1).X.X.X
  98. }
  99. }
  100. }
  101. }
  102. }
  103. return firstIPs
  104. }