diff --git a/IPRangeLoader.go b/IPRangeLoader.go index 9b89cd44..a17f71b9 100644 --- a/IPRangeLoader.go +++ b/IPRangeLoader.go @@ -9,18 +9,49 @@ import ( "strings" ) +// 根据子网掩码获取主机数量 func getCidrHostNum(maskLen int) int { - cidrIpNum := int(0) + cidrIPNum := int(0) if maskLen < 32 { var i int = int(32 - maskLen - 1) for ; i >= 1; i-- { - cidrIpNum += 1 << i + cidrIPNum += 1 << i } - cidrIpNum += 2 + cidrIPNum += 2 } else { - cidrIpNum = 1 + cidrIPNum = 1 } - return cidrIpNum + if cidrIPNum > 255 { + cidrIPNum = 255 + } + return cidrIPNum +} + +// 获取 IP 最后一段最小值和最大值 +func getCidrIPRange(cidr string) (uint8, uint8) { + ip := strings.Split(cidr, "/")[0] + ipSegs := strings.Split(ip, ".") + maskLen, _ := strconv.Atoi(strings.Split(cidr, "/")[1]) + seg4MinIP, seg4MaxIP := getIPSeg4Range(ipSegs, maskLen) + //ipPrefix := ipSegs[0] + "." + ipSegs[1] + "." + ipSegs[2] + "." + + return seg4MinIP, + seg4MaxIP +} + +// 获取 IP 最后一段的区间 +func getIPSeg4Range(ipSegs []string, maskLen int) (uint8, uint8) { + ipSeg, _ := strconv.Atoi(ipSegs[3]) + return getIPSegRange(uint8(ipSeg), uint8(32-maskLen)) +} + +// 根据输入的基础IP地址和CIDR掩码计算一个IP片段的区间 +func getIPSegRange(userSegIP, offset uint8) (uint8, uint8) { + var ipSegMax uint8 = 255 + netSegIP := ipSegMax << offset + segMinIP := netSegIP & userSegIP + segMaxIP := userSegIP&(255< 255 { - MaxIPNum = 255 - } - //fmt.Println(MaxIPNum) if err != nil { log.Fatal(err) } - if ipv6Mode { // IPv6 + if !ipv6Mode { // IPv4 + minIP, maxIP := getCidrIPRange(scanner.Text()) // 获取 IP 最后一段最小值和最大值 + Mask, _ := strconv.Atoi(strings.Split(scanner.Text(), "/")[1]) // 获取子网掩码 + MaxIPNum := getCidrHostNum(Mask) // 根据子网掩码获取主机数量 + for IPRange.Contains(firstIP) { + if allip { // 如果是测速全部 IP + for i := int(minIP); i <= int(maxIP); i++ { // 遍历 IP 最后一段最小值到最大值 + firstIP[15] = uint8(i) + firstIPCopy := make([]byte, len(firstIP)) + copy(firstIPCopy, firstIP) + firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy}) + } + } else { // 随机 IP 的最后一段 0.0.0.X + firstIP[15] = minIP + randipEndWith(MaxIPNum) + firstIPCopy := make([]byte, len(firstIP)) + copy(firstIPCopy, firstIP) + firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy}) + } + firstIP[14]++ // 0.0.(X+1).X + if firstIP[14] == 0 { + firstIP[13]++ // 0.(X+1).X.X + if firstIP[13] == 0 { + firstIP[12]++ // (X+1).X.X.X + } + } + } + } else { //IPv6 var tempIP uint8 - MaxIPNum = 255 for IPRange.Contains(firstIP) { //fmt.Println(firstIP) //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]) - firstIP[15] = randipEndWith(MaxIPNum) // 随机 IP 的最后一段 - firstIP[14] = randipEndWith(MaxIPNum) // 随机 IP 的最后一段 + firstIP[15] = randipEndWith(255) // 随机 IP 的最后一段 + firstIP[14] = randipEndWith(255) // 随机 IP 的最后一段 firstIPCopy := make([]byte, len(firstIP)) copy(firstIPCopy, firstIP) firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy}) tempIP = firstIP[13] - firstIP[13] += randipEndWith(MaxIPNum) + firstIP[13] += randipEndWith(255) if firstIP[13] < tempIP { tempIP = firstIP[12] - firstIP[12] += randipEndWith(MaxIPNum) + firstIP[12] += randipEndWith(255) if firstIP[12] < tempIP { tempIP = firstIP[11] - firstIP[11] += randipEndWith(MaxIPNum) + firstIP[11] += randipEndWith(255) if firstIP[11] < tempIP { tempIP = firstIP[10] - firstIP[10] += randipEndWith(MaxIPNum) + firstIP[10] += randipEndWith(255) if firstIP[10] < tempIP { tempIP = firstIP[9] - firstIP[9] += randipEndWith(MaxIPNum) + firstIP[9] += randipEndWith(255) if firstIP[9] < tempIP { tempIP = firstIP[8] - firstIP[8] += randipEndWith(MaxIPNum) + firstIP[8] += randipEndWith(255) if firstIP[8] < tempIP { tempIP = firstIP[7] - firstIP[7] += randipEndWith(MaxIPNum) + firstIP[7] += randipEndWith(255) if firstIP[7] < tempIP { tempIP = firstIP[6] - firstIP[6] += randipEndWith(MaxIPNum) + firstIP[6] += randipEndWith(255) if firstIP[6] < tempIP { tempIP = firstIP[5] - firstIP[5] += randipEndWith(MaxIPNum) + firstIP[5] += randipEndWith(255) if firstIP[5] < tempIP { tempIP = firstIP[4] - firstIP[4] += randipEndWith(MaxIPNum) + firstIP[4] += randipEndWith(255) if firstIP[4] < tempIP { tempIP = firstIP[3] - firstIP[3] += randipEndWith(MaxIPNum) + firstIP[3] += randipEndWith(255) if firstIP[3] < tempIP { tempIP = firstIP[2] - firstIP[2] += randipEndWith(MaxIPNum) + firstIP[2] += randipEndWith(255) if firstIP[2] < tempIP { tempIP = firstIP[1] - firstIP[1] += randipEndWith(MaxIPNum) + firstIP[1] += randipEndWith(255) if firstIP[1] < tempIP { tempIP = firstIP[0] - firstIP[0] += randipEndWith(MaxIPNum) + firstIP[0] += randipEndWith(255) } } } @@ -111,41 +161,6 @@ func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr { } } } - } else { //IPv4 - for IPRange.Contains(firstIP) { - //fmt.Println(firstIP) - //fmt.Println(firstIP[15]) - if allip { - if firstIP[15] == 0 { // 当 IP 最后一段为 0 时会按顺序生成 IP - for i := 0; i < MaxIPNum; i++ { - firstIP[15] = uint8(i) // 按顺序生成 IP 的最后一段 0.0.0.X - //fmt.Println(firstIP) - firstIPCopy := make([]byte, len(firstIP)) - copy(firstIPCopy, firstIP) - firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy}) - } - } else { // 当 IP 最后一段不为 0 时,则保留 IP 最后一段 - firstIPCopy := make([]byte, len(firstIP)) - copy(firstIPCopy, firstIP) - firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy}) - } - } else { - if firstIP[15] == 0 { - firstIP[15] = randipEndWith(MaxIPNum) // 随机 IP 的最后一段 0.0.0.X - } - firstIPCopy := make([]byte, len(firstIP)) - copy(firstIPCopy, firstIP) - firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy}) - } - firstIP[15] = 0 - firstIP[14]++ // 0.0.(X+1).X - if firstIP[14] == 0 { - firstIP[13]++ // 0.(X+1).X.X - if firstIP[13] == 0 { - firstIP[12]++ // (X+1).X.X.X - } - } - } } } return firstIPs diff --git a/ip.txt b/ip.txt index ce040793..ffc7dc45 100644 --- a/ip.txt +++ b/ip.txt @@ -1,3 +1,5 @@ +1.1.1.0/24 +1.0.0.0/24 173.245.48.0/20 103.21.244.0/22 103.22.200.0/22