-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
67 lines (61 loc) · 1.25 KB
/
utils.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
package go_websocket
import (
"fmt"
"io"
"math/big"
"net"
"net/http"
"net/netip"
)
// 获取内网IP
func GetInternalIP() (string, error) {
conn, err := net.Dial("udp", "114.114.114.114:80")
if err != nil {
return "", err
}
defer conn.Close()
addrPort, err := netip.ParseAddrPort(conn.LocalAddr().String())
if err != nil {
return "", err
}
return addrPort.Addr().String(), nil
}
// 获取外网IP
func GetExternalIP() (string, error) {
resp, err := http.Get("http://myexternalip.com/raw")
if err != nil {
return "", err
}
defer resp.Body.Close()
all, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(all), nil
}
// 获取远程IP
func RemoteIp(req *http.Request) string {
ip := req.Header.Get("X-Real-IP")
if ip == "" {
ip = req.Header.Get("X-Forwarded-For")
}
if ip == "" {
addr := req.RemoteAddr
addrPort, err := netip.ParseAddrPort(addr)
if err != nil {
return ""
}
ip = addrPort.Addr().String()
}
return ip
}
// 整数IP转字符串
func InetNtoA(ip int64) string {
return fmt.Sprintf("%d.%d.%d.%d", byte(ip>>24), byte(ip>>16), byte(ip>>8), byte(ip))
}
// 字符串IP转整数
func InetAtoN(ip string) int64 {
ret := big.NewInt(0)
ret.SetBytes(net.ParseIP(ip).To4())
return ret.Int64()
}