1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
net.go 3.74 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-04-05 21:56 . 检测本地端口是否被占用
package system
import (
"fmt"
"io"
"net"
"net/http"
"time"
)
var (
ResolveIp = net.IP{}
PublicIp = net.IP{}
)
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 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 {
return len(PublicIp) > 0
}
// SelfExternalIP 自己外部IP,可能是局域网IP,公网IP
// lan 局域网优先
func SelfExternalIP(lan bool) string {
if lan && IsLanIP(ResolveIp) {
return ResolveIp.String()
}
if IsPublicIP(PublicIp) {
return PublicIp.String()
}
return ResolveIp.String()
}
// IPIsSelf 是否是自己
func IPIsSelf(ip string) string {
if PublicIp.String() == ip {
return KLocalHost
}
if ResolveIp.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 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 ip, err = GetClientIP()
if err != nil {
return nil, err
}
for i := range ip {
t := GetIPType(ip[i])
if t == IptUnk || t == IptPub {
continue
}
return ip[i], nil
}
return nil, fmt.Errorf("not exist")
}
func GetResolveIPV3(tt IPType) (net.IP, error) {
var ips, err = GetClientIP()
if err != nil {
return nil, err
}
for i := range ips {
t := GetIPType(ips[i])
if t == tt {
return ips[i], nil
}
}
return nil, fmt.Errorf("not exist")
}
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 IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
return IptPub
}
if ip4 := IP.To4(); ip4 != nil {
switch true {
case ip4[0] == 10:
return IptPriA
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
return IptPriB
case ip4[0] == 192 && ip4[1] == 168:
return IptPriC
default:
return IptPub
}
}
return IptUnk
}
/**
* 私有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.6.6

搜索帮助