mirror of
https://github.com/juanfont/headscale.git
synced 2025-03-12 16:30:53 +00:00

* utility iterator for ipset Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * split policy -> policy and v1 This commit split out the common policy logic and policy implementation into separate packages. policy contains functions that are independent of the policy implementation, this typically means logic that works on tailcfg types and generic formats. In addition, it defines the PolicyManager interface which the v1 implements. v1 is a subpackage which implements the PolicyManager using the "original" policy implementation. Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * use polivyv1 definitions in integration tests These can be marshalled back into JSON, which the new format might not be able to. Also, just dont change it all to JSON strings for now. Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * formatter: breaks lines Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * remove compareprefix, use tsaddr version Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * remove getacl test, add back autoapprover Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * use policy manager tag handling Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * rename display helper for user Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * introduce policy v2 package policy v2 is built from the ground up to be stricter and follow the same pattern for all types of resolvers. TODO introduce aliass resolver Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * wire up policyv2 in integration testing Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * split policy v2 tests into seperate workflow to work around github limit Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * add policy manager output to /debug Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> * update changelog Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com> --------- Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
128 lines
2.9 KiB
Go
128 lines
2.9 KiB
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"iter"
|
|
"net/netip"
|
|
"strings"
|
|
|
|
"go4.org/netipx"
|
|
)
|
|
|
|
// This is borrowed from, and updated to use IPSet
|
|
// https://github.com/tailscale/tailscale/blob/71029cea2ddf82007b80f465b256d027eab0f02d/wgengine/filter/tailcfg.go#L97-L162
|
|
// TODO(kradalby): contribute upstream and make public.
|
|
var (
|
|
zeroIP4 = netip.AddrFrom4([4]byte{})
|
|
zeroIP6 = netip.AddrFrom16([16]byte{})
|
|
)
|
|
|
|
// parseIPSet parses arg as one:
|
|
//
|
|
// - an IP address (IPv4 or IPv6)
|
|
// - the string "*" to match everything (both IPv4 & IPv6)
|
|
// - a CIDR (e.g. "192.168.0.0/16")
|
|
// - a range of two IPs, inclusive, separated by hyphen ("2eff::1-2eff::0800")
|
|
//
|
|
// bits, if non-nil, is the legacy SrcBits CIDR length to make a IP
|
|
// address (without a slash) treated as a CIDR of *bits length.
|
|
// nolint
|
|
func ParseIPSet(arg string, bits *int) (*netipx.IPSet, error) {
|
|
var ipSet netipx.IPSetBuilder
|
|
if arg == "*" {
|
|
ipSet.AddPrefix(netip.PrefixFrom(zeroIP4, 0))
|
|
ipSet.AddPrefix(netip.PrefixFrom(zeroIP6, 0))
|
|
|
|
return ipSet.IPSet()
|
|
}
|
|
if strings.Contains(arg, "/") {
|
|
pfx, err := netip.ParsePrefix(arg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if pfx != pfx.Masked() {
|
|
return nil, fmt.Errorf("%v contains non-network bits set", pfx)
|
|
}
|
|
|
|
ipSet.AddPrefix(pfx)
|
|
|
|
return ipSet.IPSet()
|
|
}
|
|
if strings.Count(arg, "-") == 1 {
|
|
ip1s, ip2s, _ := strings.Cut(arg, "-")
|
|
|
|
ip1, err := netip.ParseAddr(ip1s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ip2, err := netip.ParseAddr(ip2s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
r := netipx.IPRangeFrom(ip1, ip2)
|
|
if !r.IsValid() {
|
|
return nil, fmt.Errorf("invalid IP range %q", arg)
|
|
}
|
|
|
|
for _, prefix := range r.Prefixes() {
|
|
ipSet.AddPrefix(prefix)
|
|
}
|
|
|
|
return ipSet.IPSet()
|
|
}
|
|
ip, err := netip.ParseAddr(arg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid IP address %q", arg)
|
|
}
|
|
bits8 := uint8(ip.BitLen())
|
|
if bits != nil {
|
|
if *bits < 0 || *bits > int(bits8) {
|
|
return nil, fmt.Errorf("invalid CIDR size %d for IP %q", *bits, arg)
|
|
}
|
|
bits8 = uint8(*bits)
|
|
}
|
|
|
|
ipSet.AddPrefix(netip.PrefixFrom(ip, int(bits8)))
|
|
|
|
return ipSet.IPSet()
|
|
}
|
|
|
|
func GetIPPrefixEndpoints(na netip.Prefix) (netip.Addr, netip.Addr) {
|
|
var network, broadcast netip.Addr
|
|
ipRange := netipx.RangeOfPrefix(na)
|
|
network = ipRange.From()
|
|
broadcast = ipRange.To()
|
|
|
|
return network, broadcast
|
|
}
|
|
|
|
func StringToIPPrefix(prefixes []string) ([]netip.Prefix, error) {
|
|
result := make([]netip.Prefix, len(prefixes))
|
|
|
|
for index, prefixStr := range prefixes {
|
|
prefix, err := netip.ParsePrefix(prefixStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result[index] = prefix
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// IPSetAddrIter returns a function that iterates over all the IPs in the IPSet.
|
|
func IPSetAddrIter(ipSet *netipx.IPSet) iter.Seq[netip.Addr] {
|
|
return func(yield func(netip.Addr) bool) {
|
|
for _, rng := range ipSet.Ranges() {
|
|
for ip := rng.From(); ip.Compare(rng.To()) <= 0; ip = ip.Next() {
|
|
if !yield(ip) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|