-
Notifications
You must be signed in to change notification settings - Fork 10
/
helpers.go
69 lines (64 loc) · 1.18 KB
/
helpers.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
package ntlm_proxy
import (
"net"
"os"
"strings"
log "github.com/sirupsen/logrus"
)
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func isLocalHost(host string) bool {
if strings.ToLower(host) == "localhost" {
return true
}
addr := net.ParseIP(host)
if addr.IsLoopback() || addr.IsLinkLocalUnicast() {
return true
}
ips := []net.IP{addr}
if addr == nil {
ipstrings, err := net.LookupHost(host)
if err != nil {
log.Fatal(err)
}
for _, ipString := range ipstrings {
ips = append(ips, net.ParseIP(ipString))
}
}
for _, ip := range ips {
if ip.IsLoopback() || ip.IsLinkLocalUnicast() {
return true
}
for _, localIP := range localIPs() {
if localIP.Equal(ip) {
return true
}
}
}
return false
}
func localIPs() (ips []net.IP) {
ifaces, err := net.Interfaces()
if err != nil {
log.Fatal(err)
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
log.Fatal(err)
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
ips = append(ips, v.IP)
case *net.IPAddr:
ips = append(ips, v.IP)
}
}
}
return
}