-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.go
164 lines (154 loc) · 3.82 KB
/
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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package netut
import (
"errors"
"fmt"
"net"
"os/exec"
"runtime"
"strings"
log "github.com/colt3k/nglog/ng"
)
//RetrieveIP retrieve system ipv4 address
func RetrieveIP() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("are you connected to the network")
}
// Parse and set Domain/IP's passed in
func ParseIPs(ipAddresses []string) ([]string, []net.IP, error) {
var parsed []net.IP
var domains []string
for _, s := range ipAddresses {
domainIp := strings.Split(s, "/")
for _, j := range domainIp {
if ip := net.ParseIP(j); ip != nil {
parsed = append(parsed, ip)
} else {
domains = append(domains, j)
}
}
}
return domains, parsed, nil
}
func GetLocalIP(doesNotStartWith []string) string {
ifaces, err := net.Interfaces()
if err != nil {
log.Logf(log.FATAL, "issue retrieving network interfaces\n%+v", err)
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
log.Logf(log.FATAL, "issue getting address\n%+v", err)
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
// process IP address
// Compare against all prefixes
var found bool
ipStr := ip.String()
for _, d := range doesNotStartWith {
if strings.HasPrefix(ipStr, d) {
found = true
}
}
if !found && strings.Index(ipStr, ":") == -1 {
return ip.String()
}
}
}
return ""
}
func Ping(server string) (bool, error) {
var valid bool
var err error
log.Logln(log.DBGL3, "******************** PING - resolution ****************************")
ip, err := net.ResolveIPAddr("ip4:icmp", server)
if err != nil {
log.Logln(log.DBGL3, "******************** END PING - resolution *************************")
return valid, err
}
log.Logf(log.DBGL3, "%s resolved to IP %s", server, ip.IP.String())
addr := net.ParseIP(ip.IP.String())
if addr == nil {
valid = false
err = fmt.Errorf("invalid ip address")
}
if isIPV4(addr) || isIPv6(addr) {
valid = call(addr.String())
err = nil
} else {
valid = false
err = fmt.Errorf("invalid format for ipv4 or ipv6")
}
log.Logln(log.DBGL3, "******************** END PING - resolution *************************")
return valid, err
}
func buildPingCmd(addr string) []string {
if runtime.GOOS == "darwin" {
return []string{addr, "-c 1", "-t 3"}
} else {
return []string{addr, "-c 1", "-W 3"}
}
}
func call(addr string) bool {
var available bool
parms := buildPingCmd(addr)
log.Logln(log.DBGL3, "******************** START PING ****************************")
log.Logln(log.DBGL3, "executing ping ", parms)
out, _ := exec.Command("ping", parms...).Output()
log.Logf(log.DBGL3, "ping output |%s|", string(out))
if strings.Contains(string(out), "Destination Host Unreachable") {
log.Logln(log.DBGL3, "destination host unreachable")
available = false
} else if strings.Contains(string(out), "100.0% packet loss") {
log.Logln(log.DBGL3, "100.0% packet lost")
available = false
} else {
available = true
}
log.Logln(log.DBGL3, "******************** END PING ****************************")
return available
}
func isIPV4(ip net.IP) bool {
return len(ip.To4()) == net.IPv4len
}
func isIPv6(ip net.IP) bool {
return len(ip) == net.IPv6len
}