yao/network/ip.go
2021-11-28 23:06:32 +08:00

40 lines
766 B
Go

package network
import (
"net"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/exception"
)
// IP 读取IP地址
func IP() map[string]string {
res := map[string]string{}
ifaces, err := net.Interfaces()
if err != nil {
exception.New("读取网卡失败 %s", 500, err.Error()).Throw()
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
exception.New("读取IP地址失败 %s", 500, err.Error()).Throw()
}
// handle err
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
res[i.Name] = ip.String()
}
}
return res
}
// ProcessIP xiang.network.IP IP地址
func ProcessIP(process *gou.Process) interface{} {
return IP()
}