-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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地址") | ||
//} | ||
//} |