-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnet.go
80 lines (74 loc) · 1.82 KB
/
net.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
package util
import (
"fmt"
"log"
"net"
"net/url"
"strings"
)
// Performs a logical UDP connection to an endpoint
// that does not need to exist and returns the local
// IP address that was used to perform the connection.
func PreferredIPv4() net.IP {
conn, err := net.Dial("udp", "1.2.3.4:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}
// Performs a logical UDP connection to an endpoint
// that does not need to exist and returns the local
// IP address that was used to perform the connection.
func PreferredIPv6() net.IP {
conn, err := net.Dial("udp", "[::1]:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}
// Parses a list of usable local IP addresses
func LocalAddresses() ([]string, error) {
ips := make([]string, 0)
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
ips = append(ips, ipNet.IP.String())
}
}
return ips, nil
}
// Returns true if the provided IP falls within a private address range
func IsPrivateSubnet(ip net.IP) bool {
privateRanges := []string{
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
}
for _, cidr := range privateRanges {
_, subnet, _ := net.ParseCIDR(cidr)
if subnet.Contains(ip) {
return true
}
}
return false
}
// Returns the Fully Qualified Domain Name for a given URL
func ParseFQDN(anyURL string) (string, error) {
parsedURL, err := url.Parse(anyURL)
if err != nil {
return "", fmt.Errorf("invalid URL: %w", err)
}
host := parsedURL.Host
if strings.Contains(host, ":") {
host = strings.Split(host, ":")[0] // Remove port if present
}
return host, nil
}