]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20240823183215
authorqydysky <qydysky@foxmail.com>
Fri, 23 Aug 2024 18:26:48 +0000 (02:26 +0800)
committerqydysky <qydysky@foxmail.com>
Fri, 23 Aug 2024 18:26:48 +0000 (02:26 +0800)
sys/Sys.go
sys/Sys_test.go

index 4cc40936f43ac0de0f0f03d6892537f1a94cb634..ff007ac3f7b07b9152050f812d24935bad0d204c 100644 (file)
@@ -3,6 +3,7 @@ package part
 import (
        "errors"
        "fmt"
+       "iter"
        "net"
        "os"
        "path/filepath"
@@ -181,40 +182,42 @@ func GetIntranetIp(cidr string) (ips []string) {
        return []string{"127.0.0.1"}
 }
 
-func GetIpByCidr(cidr ...string) (ips []string, e error) {
-       netInterfaces, err := net.Interfaces()
-       if err != nil {
-               return nil, err
-       }
-
-       var cidrN *net.IPNet
-       if len(cidr) == 0 {
-               cidr = append(cidr, `0.0.0.0/0`)
-               cidr = append(cidr, `::/0`)
-       }
-
-       for i := 0; i < len(cidr); i++ {
-               _, cidrN, err = net.ParseCIDR(cidr[i])
+func GetIpByCidr(cidr ...string) (seq iter.Seq[net.IP]) {
+       return func(yield func(net.IP) bool) {
+               netInterfaces, err := net.Interfaces()
                if err != nil {
-                       return nil, err
+                       return
                }
 
-               for i := 0; i < len(netInterfaces); i++ {
-                       if (netInterfaces[i].Flags & net.FlagUp) != 0 {
-                               addrs, _ := netInterfaces[i].Addrs()
+               var cidrN *net.IPNet
+               if len(cidr) == 0 {
+                       cidr = append(cidr, `0.0.0.0/0`)
+                       cidr = append(cidr, `::/0`)
+               }
 
-                               for _, address := range addrs {
-                                       if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
-                                               if cidrN != nil && cidrN.Contains(ipnet.IP) {
-                                                       ips = append(ips, ipnet.IP.String())
+               for i := 0; i < len(cidr); i++ {
+                       _, cidrN, err = net.ParseCIDR(cidr[i])
+                       if err != nil {
+                               return
+                       }
+
+                       for i := 0; i < len(netInterfaces); i++ {
+                               if (netInterfaces[i].Flags & net.FlagUp) != 0 {
+                                       addrs, _ := netInterfaces[i].Addrs()
+
+                                       for _, address := range addrs {
+                                               if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
+                                                       if cidrN != nil && cidrN.Contains(ipnet.IP) {
+                                                               if !yield(ipnet.IP) {
+                                                                       return
+                                                               }
+                                                       }
                                                }
                                        }
                                }
                        }
                }
        }
-
-       return
 }
 
 func (t *sys) CheckProgram(pros ...string) []int {
index 5c697072592b25b4946647fbe89178ef995844bc..d6e064551234b8f5c6c0c05bb6ddd882aff7c960 100644 (file)
@@ -6,5 +6,7 @@ import (
 
 func Test_customMap(t *testing.T) {
        t.Log(GetIntranetIp(`192.168.0.0/16`))
-       t.Log(GetIpByCidr())
+       for ip := range GetIpByCidr() {
+               t.Log(ip.String())
+       }
 }