CasaOS/pkg/utils/ip_helper/ip.go
a624669980 2c1ca2b095 暂存
2021-09-27 14:17:36 +08:00

42 lines
787 B
Go

package ip_helper
import (
httper2 "github.com/IceWhaleTech/CasaOS/pkg/utils/httper"
"net"
"strings"
)
func IsIPv4(address string) bool {
return strings.Count(address, ":") < 2
}
func IsIPv6(address string) bool {
return strings.Count(address, ":") >= 2
}
//获取外网ip
func GetExternalIPV4() string {
return httper2.Get("https://api.ipify.org", nil)
}
//获取外网ip
func GetExternalIPV6() string {
return httper2.Get("https://api6.ipify.org", nil)
}
//获取本地ip
func GetLoclIp() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "127.0.0.1"
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return "127.0.0.1"
}