package ip

import "net"

var PRIVATE_NETWORKS = [...]string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "fd00::/8", "127.0.0.0/8"}

type IP struct {
	IP               net.IP
	private_networks []*net.IPNet
}

func NewIP() *IP {
	var ipnet *net.IPNet
	privnets := make([]*net.IPNet, len(PRIVATE_NETWORKS), len(PRIVATE_NETWORKS))
	for i, n := range PRIVATE_NETWORKS {
		_, ipnet, _ = net.ParseCIDR(n)
		privnets[i] = ipnet
	}

	return &IP{private_networks: privnets}
}

func (i *IP) IsPrivate(ip net.IP) bool {
	for _, net := range i.private_networks {
		if net.Contains(ip) {
			return true
		}
	}
	return false
}

func (i *IP) Network(ip net.IP) *net.IPNet {
	for _, n := range i.private_networks {
		if n.Contains(ip) {
			return n
		}
	}
	return nil
}
