代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。