1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
net.go 4.52 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-08-08 23:44 . IP
package system
import (
"fmt"
"io"
"net"
"net/http"
"sync"
"time"
)
var (
resolveIp = net.IP{}
publicIp = net.IP{}
rm sync.RWMutex
)
const (
KLocalHost = "127.0.0.1"
url = "https://myexternalip.com/raw"
)
func init() {
resolveIp, _ = GetResolveIPV2()
publicIp, _ = GetPublicIP()
}
func GetPublicIP() (net.IP, error) {
return GetPublicIPWithUrl(url)
}
func GetPublicIPIf() net.IP {
rm.RLock()
if len(publicIp) > 0 {
rm.RUnlock()
return publicIp
}
rm.RUnlock()
ip, err := GetPublicIPWithUrl(url)
if err == nil {
rm.Lock()
publicIp = ip
rm.Unlock()
}
return ip
}
func GetPublicIPWithUrl(url string) (net.IP, error) {
resp, err := http.Get(url)
if err != nil {
return net.IP{}, err
}
defer resp.Body.Close()
ip, err := io.ReadAll(resp.Body)
if err != nil {
return net.IP{}, err
}
return net.ParseIP(string(ip)), nil
}
func PublicIPValid() bool {
pip := GetPublicIPIf()
return len(pip) > 0
}
// SelfExternalIP 自己外部IP,可能是局域网IP,公网IP
// lan 局域网优先
func SelfExternalIP(lan bool) string {
pip := GetPublicIPIf()
rip := GetResolveIPIf()
if lan && IsLanIP(rip) {
return rip.String()
}
if IsPublicIP(pip) {
return pip.String()
}
return rip.String()
}
// IPIsSelf 是否是自己
func IPIsSelf(ip string) string {
pip := GetPublicIPIf()
rip := GetResolveIPIf()
if pip.String() == ip {
return KLocalHost
}
if rip.String() == ip {
return KLocalHost
}
return ip
}
// IsLanIP 是否局域网 192.168.x.x
func IsLanIP(IP net.IP) bool {
return GetIPType(IP) == IptPriC
}
func IsPublicIP(IP net.IP) bool {
return GetIPType(IP) == IptPub
}
func GetResolveIPIf() net.IP {
rm.RLock()
if len(resolveIp) > 0 {
rm.RUnlock()
return resolveIp
}
ip, err := GetResolveIPV2()
if err == nil {
rm.Lock()
resolveIp = ip
rm.Unlock()
}
return ip
}
func GetResolveIP() (net.IP, error) {
adds, err := net.InterfaceAddrs()
if err != nil {
return net.IP{}, err
}
return parseAddress(adds), nil
}
func GetResolveIPV2() (net.IP, error) {
var ips, err = GetResolveIPs(func(ip net.IP) bool {
t := GetIPType(ip)
return t == IptUnk || t == IptPub
})
if err != nil {
return nil, err
}
if len(ips) > 0 {
return ips[0], nil
}
return nil, fmt.Errorf("not exist")
}
func GetResolveIPV3(tt IPType) (net.IP, error) {
var ips, err = GetResolveIPs(func(ip net.IP) bool {
t := GetIPType(ip)
return t != tt
})
if err != nil {
return nil, err
}
if len(ips) > 0 {
return ips[0], nil
}
return nil, fmt.Errorf("not exist")
}
func GetResolveIPs(filter func(ip net.IP) bool) ([]net.IP, error) {
var ips []net.IP
var ip, err = GetClientIP()
if err != nil {
return nil, err
}
for i := range ip {
if filter(ip[i]) {
continue
}
ips = append(ips, ip[i])
}
return ips, nil
}
func GetClientIP() ([]net.IP, error) {
var ips []net.IP
faces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, face := range faces {
if face.Flags&net.FlagUp == 0 {
continue
}
if face.Flags&net.FlagLoopback != 0 {
continue
}
adds, er := face.Addrs()
if er != nil {
return nil, er
}
ip := parseAddress(adds)
if len(ip) > 0 {
ips = append(ips, ip)
}
}
return ips, nil
}
func parseAddress(adds []net.Addr) net.IP {
for _, addr := range adds {
ip := parseResolveIP(addr)
if len(ip) > 0 {
return ip
}
}
return net.IP{}
}
func parseResolveIP(addr net.Addr) net.IP {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
return nil
}
return ip.To4()
}
type IPType int8
const (
IptUnk = IPType(0)
IptPub = IPType(1)
IptPriA = IPType(2)
IptPriB = IPType(3)
IptPriC = IPType(4)
)
func GetIPType(ip net.IP) IPType {
if ip4 := ip.To4(); ip4 != nil {
switch true {
case ip4[0] == 10:
return IptPriA
case ip4[0] == 172 && ip4[1]&0xf0 == 16:
return IptPriB
case ip4[0] == 192 && ip4[1] == 168:
return IptPriC
default:
return IptPub
}
}
if len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc {
return IptPriC
}
return IptPub
}
/**
* 私有IP
A 类 10.0.0.0/8:10.0.0.0~10.255.255.255
B 类 172.16.0.0/12:172.16.0.0~172.31.255.255
C 192.168.0.0/16:192.168.0.0~192.168.255.255
*/
func PortsUsed(ports []int) []bool {
var used = make([]bool, len(ports))
for i := range ports {
used[i] = PortUsed(ports[i])
}
return used
}
func PortUsed(port int) bool {
var host = fmt.Sprintf(":%d", port)
if conn, _ := net.DialTimeout("tcp", host, 3*time.Second); conn != nil {
_ = conn.Close()
return true
}
return false
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.9.11

搜索帮助