This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addr.go
134 lines (123 loc) · 2.78 KB
/
addr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package gopsu
import (
"fmt"
"net"
"strconv"
"strings"
)
// IPUint2String change ip int64 data to string format
func IPUint2String(ipnr uint) string {
return fmt.Sprintf("%d.%d.%d.%d", (ipnr>>24)&0xFF, (ipnr>>16)&0xFF, (ipnr>>8)&0xFF, ipnr&0xFF)
}
// IPInt642String change ip int64 data to string format
func IPInt642String(ipnr int64) string {
return fmt.Sprintf("%d.%d.%d.%d", (ipnr)&0xFF, (ipnr>>8)&0xFF, (ipnr>>16)&0xFF, (ipnr>>24)&0xFF)
}
// IPInt642Bytes change ip int64 data to string format
func IPInt642Bytes(ipnr int64) []byte {
return []byte{byte((ipnr) & 0xFF), byte((ipnr >> 8) & 0xFF), byte((ipnr >> 16) & 0xFF), byte((ipnr >> 24) & 0xFF)}
}
// IPUint2Bytes change ip int64 data to string format
func IPUint2Bytes(ipnr int64) []byte {
return []byte{byte((ipnr >> 24) & 0xFF), byte((ipnr >> 16) & 0xFF), byte((ipnr >> 8) & 0xFF), byte((ipnr) & 0xFF)}
}
// IP2Uint change ip string data to int64 format
func IP2Uint(ipnr string) uint {
// ex := errors.New("wrong ip address format")
bits := strings.Split(ipnr, ".")
if len(bits) != 4 {
return 0
}
var intip uint
for k, v := range bits {
i, ex := strconv.Atoi(v)
if ex != nil || i > 255 || i < 0 {
return 0
}
intip += uint(i) << uint(8*(3-k))
}
return intip
}
// IP2Int64 change ip string data to int64 format
func IP2Int64(ipnr string) int64 {
// ex := errors.New("wrong ip address format"
bits := strings.Split(ipnr, ".")
if len(bits) != 4 {
return 0
}
var intip uint
for k, v := range bits {
i, ex := strconv.Atoi(v)
if ex != nil || i > 255 || i < 0 {
return 0
}
intip += uint(i) << uint(8*(k))
}
return int64(intip)
}
// RealIP 返回本机的v4或v6ip
func RealIP(v6first bool) string {
if v6first {
if ip := ExternalIPV6(); ip != "" {
return ip
}
}
return ExternalIP()
}
// ExternalIP 返回v4地址
func ExternalIP() string {
v4, v6, err := GlobalIPs()
if err != nil {
return ""
}
if len(v4) > 0 {
return v4[0]
}
if len(v6) > 0 {
return v6[0]
}
return ""
}
// ExternalIPV6 返回v6地址
func ExternalIPV6() string {
_, v6, err := GlobalIPs()
if err != nil {
return ""
}
if len(v6) > 0 {
return v6[0]
}
return ""
}
// GlobalIPs 返回所有可访问ip
// ipv4 list,ipv6 list
func GlobalIPs() ([]string, []string, error) {
var v4, v6 = make([]string, 0), make([]string, 0)
s, err := net.InterfaceAddrs()
if err != nil {
return v4, v6, err
}
for _, a := range s {
var ip net.IP
switch addr := a.(type) {
case *net.IPAddr:
ip = addr.IP
case *net.IPNet:
ip = addr.IP
default:
continue
}
if ip.To4().String() != "<nil>" {
if ip.IsGlobalUnicast() {
v4 = append(v4, ip.To4().String())
}
continue
}
if ip.To16().String() != "<nil>" {
if ip.IsGlobalUnicast() {
v6 = append(v6, "["+ip.To16().String()+"]")
}
}
}
return v4, v6, nil
}