diff --git a/README.md b/README.md index e720b00..4247802 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ go install github.com/mstxq17/MoreFind@latest ``` 方式二: 直接安装二进制文件 ```bash -wget --no-check-certificate https://ghproxy.com/https://github.com/mstxq17/MoreFind/releases/download/v1.4.2/MoreFind_`uname -s`_`uname -m`.tar.gz +wget --no-check-certificate https://ghproxy.com/https://github.com/mstxq17/MoreFind/releases/download/v1.4.3/MoreFind_`uname -s`_`uname -m`.tar.gz tar -xzvf MoreFind_`uname -s`_`uname -m`.tar.gz sudo mv ./MoreFind /usr/bin/MoreFind && chmod +x /usr/bin/MoreFind ``` @@ -136,6 +136,7 @@ MoreFind -l 0-35 ```bash MoreFind -c="192.168.0.1/24" +MoreFind -c="192.168.0.1-192.168.2.254" echo -e "192.168.4.1/24\n192.168.1.1/24"|./MoreFind --cidr ``` diff --git a/cmd/root.go b/cmd/root.go index 3d8fe19..18413f7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -193,12 +193,45 @@ func inc(ip net.IP) { } func genIP(cidr string) { - ip, ipnet, err := net.ParseCIDR(cidr) - if err != nil { - fmt.Println("无法解析CIDR地址:", err) + // fix parse error because of \n in window env + // 修复 window 因为多了换行符导致的错误 + cidr = strings.TrimSpace(cidr) + if strings.Contains(cidr, "/") { + ip, ipnet, err := net.ParseCIDR(cidr) + if err != nil { + logger.Println("无法解析CIDR地址:", err) + } else { + for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) { + fmt.Println(ip) + } + } + } + if strings.Contains(cidr, "-") { + var ipRange []string + for _, ipstr := range strings.Split(cidr, "-") { + ipRange = append(ipRange, strings.TrimSpace(ipstr)) + } + if len(ipRange) != 2 { + logger.Println("无法解析给定的IP段: " + cidr) + return + } + + startIPStr := ipRange[0] + endIPStr := ipRange[1] + errStart := net.ParseIP(startIPStr) + errEnd := net.ParseIP(endIPStr) + if errStart == nil || errEnd == nil { + logger.Println("无法解析给定的IP段: " + cidr) + return + } + ipList := core.IPRange(startIPStr, endIPStr) + for _, ip := range ipList { + fmt.Println(ip) + } } - for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) { - fmt.Println(ip) + if !strings.Contains(cidr, "/") && !strings.Contains(cidr, "-") { + cidr = cidr + "/24" + genIP(cidr) } } diff --git a/cmd/version.go b/cmd/version.go index b0e7450..a2b819b 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -14,6 +14,6 @@ var versionCmd = &cobra.Command{ Short: "Print the semantic version number of MoreFind", Long: `All software has versions. This is MoreFind's`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("MoreFind v1.4.2") + fmt.Println("MoreFind v1.4.3") }, } diff --git a/core/iprange.go b/core/iprange.go index 9a8bc95..34b172f 100644 --- a/core/iprange.go +++ b/core/iprange.go @@ -1 +1,62 @@ package core + +import ( + "math/big" + "net" +) + +func IPRange(startIPStr, endIPStr string) []string { + var ipList []string + startIPInt := ipToUInt32(startIPStr) + endIPInt := ipToUInt32(endIPStr) + for currIPInt := new(big.Int).Set(startIPInt); currIPInt.Cmp(endIPInt) <= 0; incIP(currIPInt) { + ip := intToIP(currIPInt) + ipList = append(ipList, ip) + } + + return ipList +} + +func intToIP(ipInt *big.Int) string { + ipBytes := ipInt.Bytes() + if len(ipBytes) < 4 { + // 补齐 4 个字节 + padBytes := make([]byte, 4-len(ipBytes)) + ipBytes = append(padBytes, ipBytes...) + } + return net.IP(ipBytes).String() +} + +func ipToUInt32(ipStr string) *big.Int { + ip := net.ParseIP(ipStr) + if ip == nil { + return nil + } + + // 将 net.IP 转换为 4 字节的大整数 + ipInt := new(big.Int) + ipInt.SetBytes(ip.To4()) + return ipInt +} + +func incIP(ipInt *big.Int) { + ipInt.Add(ipInt, big.NewInt(1)) +} + +//func check +//func main() { +//startIPStr := "192.168.1.10" +//endIPStr := "192.168.10.15" +// +//startIPInt := ipToUInt32(startIPStr) +//endIPInt := ipToUInt32(endIPStr) +// +//if startIPInt != nil && endIPInt != nil { +// ipList := ipRange(startIPInt, endIPInt) +// for _, ip := range ipList { +// fmt.Println(ip) +// } +//} else { +// fmt.Println("无效的IP地址") +//} +//}