~tsileo/blobstash

ref: b178995e346d blobstash/pkg/iputil/iputil.go -rw-r--r-- 1.2 KiB
b178995eThomas Sileo vendor: update deps 2 years ago
                                                                                
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
// Package iputil implements IP address related utils.
package iputil // import "a4.io/blobstash/pkg/iputil"
import (
	"net"
	"net/url"
	"strings"
)

var privateIPNets [3]*net.IPNet

func init() {
	var err error
	for i, cidr := range []string{"192.168.0.0/16", "10.0.0.0/8", "176.16.0.0/12"} {
		_, privateIPNets[i], err = net.ParseCIDR(cidr)
		if err != nil {
			panic(err)
		}
	}
}

// IsIPPrivate retrurns true if the given IP address is part of a private network
func IsIPPrivate(ip net.IP) bool {
	for _, ipnet := range privateIPNets {
		if ipnet.Contains(ip) {
			return true
		}
	}
	return false
}

// IsPrivate returns true if the given host revolve to a private IP address (or if a private address is passed)
func IsPrivate(host string) (bool, error) {
	if strings.HasPrefix(host, "http") {
		u, err := url.Parse(host)
		if err != nil {
			return false, err
		}
		host = u.Hostname()
	}
	if ip := net.ParseIP(host); ip != nil {
		return IsIPPrivate(ip), nil
	}
	ips, err := net.LookupIP(host)
	if err != nil {
		return false, err
	}
	if len(ips) == 0 {
		return false, err
	}
	for _, ip := range ips {
		if !IsIPPrivate(ip) {
			return false, nil
		}
	}
	return true, nil
}