-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.go
More file actions
130 lines (115 loc) · 3.12 KB
/
net.go
File metadata and controls
130 lines (115 loc) · 3.12 KB
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
package crab
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strconv"
"strings"
)
const ipv4_regex = `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`
// SubNetMaskToLen ipv4 子网掩码长度换算
// 如 255.255.255.0 对应的网络位长度为 24
func SubNetMaskToLen(netmask string) (int, error) {
ipSplitArr := strings.Split(netmask, ".")
if len(ipSplitArr) != 4 {
return 0, fmt.Errorf("netmask:%v is not valid, pattern should like: 255.255.255.0", netmask)
}
ipv4MaskArr := make([]byte, 4)
for i, value := range ipSplitArr {
intValue, err := strconv.Atoi(value)
if err != nil {
return 0, fmt.Errorf("ipMaskToInt call strconv.Atoi error:[%v] string value is: [%s]", err, value)
}
if intValue > 255 {
return 0, fmt.Errorf("netmask cannot greater than 255, current value is: [%s]", value)
}
ipv4MaskArr[i] = byte(intValue)
}
ones, _ := net.IPv4Mask(ipv4MaskArr[0], ipv4MaskArr[1], ipv4MaskArr[2], ipv4MaskArr[3]).Size()
return ones, nil
}
// LenToSubNetMask ipv4 网络位长度转换为子网掩码地址
// 如 24 对应的子网掩码地址为 255.255.255.0
func LenToSubNetMask(subnet int) string {
var buff bytes.Buffer
for i := 0; i < subnet; i++ {
buff.WriteString("1")
}
for i := subnet; i < 32; i++ {
buff.WriteString("0")
}
masker := buff.String()
a, _ := strconv.ParseUint(masker[:8], 2, 64)
b, _ := strconv.ParseUint(masker[8:16], 2, 64)
c, _ := strconv.ParseUint(masker[16:24], 2, 64)
d, _ := strconv.ParseUint(masker[24:32], 2, 64)
resultMask := fmt.Sprintf("%v.%v.%v.%v", a, b, c, d)
return resultMask
}
// IsPublicIPv4 ipv4 判断是否是公网ip
func IsPublicIPv4(IP net.IP) bool {
if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
return false
}
if ip4 := IP.To4(); ip4 != nil {
switch true {
case ip4[0] == 10:
return false
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
return false
case ip4[0] == 192 && ip4[1] == 168:
return false
default:
return true
}
}
return false
}
// IPGet 返回客户端 IP
func IPGet(req *http.Request) string {
remoteAddr := req.RemoteAddr
if ip := req.Header.Get("X-Real-IP"); ip != "" {
remoteAddr = ip
} else if ip = req.Header.Get("X-Forwarded-For"); ip != "" {
remoteAddr = ip
} else {
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
}
if remoteAddr == "::1" {
remoteAddr = "127.0.0.1"
}
return remoteAddr
}
type Location struct {
IP string `json:"ip"`
Hostname string `json:"hostname"`
City string `json:"city"`
Region string `json:"region"`
Country string `json:"country"`
Loc string `json:"loc"`
Org string `json:"org"`
Postal string `json:"postal"`
Timezone string `json:"timezone"`
Readme string `json:"readme"`
}
var MyIPLocationURL = "https://ipinfo.io"
func SetIPLocationURL(ipURL string) {
MyIPLocationURL = ipURL
}
func GetMyIPLocation() (lo *Location, err error) {
lo = &Location{}
resp, err := http.Get(MyIPLocationURL)
if err != nil {
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(body, lo)
return
}