1 Star 0 Fork 0

younland / brick

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ip.go 661 Bytes
一键复制 编辑 原始数据 按行查看 历史
严宇川 提交于 2023-04-30 14:22 . v0.0.6
package xutil
import (
"io"
"net"
"net/http"
)
// GetIntranetIP 获取内网 IP
func GetIntranetIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, addr := range addrs {
// 检查ip地址判断是否回环地址
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
// GetExternalIP 获取公网 IP
func GetExternalIP() string {
// http://icanhazip.com/
resp, err := http.Get("http://myexternalip.com/raw")
if err != nil {
return ""
}
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
return string(b)
}
1
https://gitee.com/younland/brick.git
git@gitee.com:younland/brick.git
younland
brick
brick
v1.0.1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891