/
ip.go
52 lines (47 loc) · 905 Bytes
/
ip.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
package tools
import (
"bytes"
"io/ioutil"
"net"
"net/http"
"strings"
"github.com/xxjwxc/public/mylog"
)
// GetWwwIP 获取公网IP地址
func GetWwwIP() (exip string) {
resp, err := http.Get("http://myexternalip.com/raw")
if err != nil {
return ""
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ""
}
return string(bytes.TrimSpace(b))
}
// GetLocalIP 获取内网ip
func GetLocalIP() (ip string) {
addrs, err := net.InterfaceAddrs()
if err != nil {
mylog.Error(err)
return
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ip = ipnet.IP.String()
break
}
}
}
return
}
// GetClientIP 获取用户ip
func GetClientIP(r *http.Request) (ip string) {
ip = r.Header.Get("X-Real-Ip")
if ip == "" {
ip = strings.Split(r.RemoteAddr, ":")[0]
}
return
}