From b00ab7833a06af1e5a4c725f2e025398e076440a Mon Sep 17 00:00:00 2001 From: luliang Date: Sun, 12 May 2024 19:36:36 +0800 Subject: [PATCH] =?UTF-8?q?###=20=E6=B7=BB=E5=8A=A0=E5=8F=96=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E7=BB=91=E5=AE=9A=E7=9A=84IP=E5=9C=B0=E5=9D=80=20###?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/common.go | 78 ++++++++++++++++++++++++++++++++++++++++++++ utils/common_test.go | 17 ++++++++++ 2 files changed, 95 insertions(+) create mode 100644 utils/common_test.go diff --git a/utils/common.go b/utils/common.go index c403ea3..12dcd37 100644 --- a/utils/common.go +++ b/utils/common.go @@ -1,9 +1,14 @@ package utils import ( + "context" + "fmt" + "github.com/nildebug/tools/log" + "net" "os" "os/signal" "syscall" + "time" ) // ListenExitChan @@ -14,3 +19,76 @@ func ListenExitChan() { signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) <-sigCh } + +var LocalIPs []string + +// GetLocalIPs +// +// @Description: 获取本地IP地址 +func GetLocalIPs() { + + interfaces, err := net.Interfaces() + if err != nil { + log.ZError(context.TODO(), "获取本地绑定IP地址失败", err) + return + } + + for _, iface := range interfaces { + //只取运行状态的接口 + if iface.Flags&net.FlagRunning == 0 { + continue + } + addrs, err := iface.Addrs() + if err != nil { + log.ZError(context.TODO(), "获取本地绑定IP地址失败", err) + continue + } + + for _, addr := range addrs { + ip, ok := addr.(*net.IPNet) + if !ok || ip.IP.IsLoopback() { + continue + } + if ip.IP.To4() != nil { + LocalIPs = append(LocalIPs, ip.IP.String()) + log.ZDebug(context.TODO(), "localIP", "ip", ip.IP.String(), "name", iface.Name, "Flags", iface.Flags) + } + } + } +} + +// IsPrivateIP +// +// @Description: 绑定IP地址是否为10 172 192 段 +// @param ip +// @return bool +func IsPrivateIP(ip net.IP) bool { + privateIPBlocks := []*net.IPNet{ + // 10.0.0.0/8 + {IP: net.IPv4(10, 0, 0, 0), Mask: net.CIDRMask(8, 32)}, + // 172.16.0.0/12 + {IP: net.IPv4(172, 16, 0, 0), Mask: net.CIDRMask(12, 32)}, + // 192.168.0.0/16 + {IP: net.IPv4(192, 168, 0, 0), Mask: net.CIDRMask(16, 32)}, + } + + for _, block := range privateIPBlocks { + if block.Contains(ip) { + return true + } + } + return false +} + +// GetCostTime +// +// @Description: 获取耗时 保留最后两位小数 +// @param startTime +// @return string +func GetCostTime(startTime time.Time) string { + duration := time.Since(startTime) + if duration.Seconds() >= 1 { + return fmt.Sprintf("%.2f(s)", duration.Seconds()) + } + return fmt.Sprintf("%.2f(ms)", float64(duration.Nanoseconds())/1e6) +} diff --git a/utils/common_test.go b/utils/common_test.go new file mode 100644 index 0000000..2bee58b --- /dev/null +++ b/utils/common_test.go @@ -0,0 +1,17 @@ +package utils + +import ( + "context" + "github.com/nildebug/tools/log" + "testing" + "time" +) + +func TestGetLocalIPs(t *testing.T) { + log.InitFromConfig("log", "debug", 5, true, true, "", 3) + startTime := time.Now() + GetLocalIPs() + //time.Sleep(1 * time.Second) + log.ZDebug(context.TODO(), GetCostTime(startTime)) + +}