1
0
mirror of https://github.com/sairson/Yasso.git synced 2026-02-06 20:14:09 +08:00
Files
Yasso/pkg/netspy/example/spy_test.go

77 lines
2.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package example
import (
"fmt"
"strconv"
"strings"
"testing"
)
func Test1(t *testing.T) {
minIp, maxIp := getCidrIpRange("192.168.248.1/22")
fmt.Println("CIDR最小IP", minIp, " CIDR最大IP", maxIp)
fmt.Println("掩码:", getCidrIpMask(24))
fmt.Println("主机数量", getCidrHostNum(24))
}
func getCidrIpRange(cidr string) (string, string) {
ip := strings.Split(cidr, "/")[0]
ipSegs := strings.Split(ip, ".")
maskLen, _ := strconv.Atoi(strings.Split(cidr, "/")[1])
seg3MinIp, seg3MaxIp := getIpSeg3Range(ipSegs, maskLen)
seg4MinIp, seg4MaxIp := getIpSeg4Range(ipSegs, maskLen)
ipPrefix := ipSegs[0] + "." + ipSegs[1] + "."
return ipPrefix + strconv.Itoa(seg3MinIp) + "." + strconv.Itoa(seg4MinIp),
ipPrefix + strconv.Itoa(seg3MaxIp) + "." + strconv.Itoa(seg4MaxIp)
}
//计算得到CIDR地址范围内可拥有的主机数量
func getCidrHostNum(maskLen int) uint {
cidrIpNum := uint(0)
var i uint = uint(32 - maskLen - 1)
for ; i >= 1; i-- {
cidrIpNum += 1 << i
}
return cidrIpNum
}
//获取Cidr的掩码
func getCidrIpMask(maskLen int) string {
// ^uint32(0)二进制为32个比特1通过向左位移得到CIDR掩码的二进制
cidrMask := ^uint32(0) << uint(32-maskLen)
fmt.Println(fmt.Sprintf("%b \n", cidrMask))
//计算CIDR掩码的四个片段将想要得到的片段移动到内存最低8位后将其强转为8位整型从而得到
cidrMaskSeg1 := uint8(cidrMask >> 24)
cidrMaskSeg2 := uint8(cidrMask >> 16)
cidrMaskSeg3 := uint8(cidrMask >> 8)
cidrMaskSeg4 := uint8(cidrMask & uint32(255))
return fmt.Sprint(cidrMaskSeg1) + "." + fmt.Sprint(cidrMaskSeg2) + "." + fmt.Sprint(cidrMaskSeg3) + "." + fmt.Sprint(cidrMaskSeg4)
}
//得到第三段IP的区间第一片段.第二片段.第三片段.第四片段)
func getIpSeg3Range(ipSegs []string, maskLen int) (int, int) {
if maskLen > 24 {
segIp, _ := strconv.Atoi(ipSegs[2])
return segIp, segIp
}
ipSeg, _ := strconv.Atoi(ipSegs[2])
return getIpSegRange(uint8(ipSeg), uint8(24-maskLen))
}
//得到第四段IP的区间第一片段.第二片段.第三片段.第四片段)
func getIpSeg4Range(ipSegs []string, maskLen int) (int, int) {
ipSeg, _ := strconv.Atoi(ipSegs[3])
segMinIp, segMaxIp := getIpSegRange(uint8(ipSeg), uint8(32-maskLen))
return segMinIp + 1, segMaxIp
}
//根据用户输入的基础IP地址和CIDR掩码计算一个IP片段的区间
func getIpSegRange(userSegIp, offset uint8) (int, int) {
var ipSegMax uint8 = 255
netSegIp := ipSegMax << offset
segMinIp := netSegIp & userSegIp
segMaxIp := userSegIp&(255<<offset) | ^(255 << offset)
return int(segMinIp), int(segMaxIp)
}