Skip to content

Commit

Permalink
perf: generate ip range enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
mstxq17 committed Oct 19, 2023
1 parent 77d7d6f commit df14b06
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -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
```

Expand Down
43 changes: 38 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
}
61 changes: 61 additions & 0 deletions core/iprange.go
Original file line number Diff line number Diff line change
@@ -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地址")
//}
//}

0 comments on commit df14b06

Please sign in to comment.