Skip to content

Commit

Permalink
优化 IP 段子网掩码解析
Browse files Browse the repository at this point in the history
  • Loading branch information
XIU2 committed Dec 11, 2020
1 parent c8ef175 commit 976dd79
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 64 deletions.
143 changes: 79 additions & 64 deletions IPRangeLoader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<<offset) | ^(255 << offset)
return uint8(segMinIP), uint8(segMaxIP)
}

func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr {
Expand All @@ -36,67 +67,86 @@ func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr {
firstIP, IPRange, err := net.ParseCIDR(IPString)
//fmt.Println(firstIP)
//fmt.Println(IPRange)
Mask, _ := strconv.Atoi(strings.Split(scanner.Text(), "/")[1])
MaxIPNum := getCidrHostNum(Mask)
if MaxIPNum > 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)
}
}
}
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions ip.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 976dd79

Please sign in to comment.