mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-02 03:29:07 +02:00
expose resource type consts
This commit is contained in:
@@ -20,9 +20,9 @@ import (
|
|||||||
type NetworkResourceType string
|
type NetworkResourceType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
host NetworkResourceType = "host"
|
Host NetworkResourceType = "host"
|
||||||
subnet NetworkResourceType = "subnet"
|
Subnet NetworkResourceType = "subnet"
|
||||||
domain NetworkResourceType = "domain"
|
Domain NetworkResourceType = "domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p NetworkResourceType) String() string {
|
func (p NetworkResourceType) String() string {
|
||||||
@@ -66,7 +66,7 @@ func NewNetworkResource(accountID, networkID, name, description, address string,
|
|||||||
|
|
||||||
func (n *NetworkResource) ToAPIResponse(groups []api.GroupMinimum) *api.NetworkResource {
|
func (n *NetworkResource) ToAPIResponse(groups []api.GroupMinimum) *api.NetworkResource {
|
||||||
addr := n.Prefix.String()
|
addr := n.Prefix.String()
|
||||||
if n.Type == domain {
|
if n.Type == Domain {
|
||||||
addr = n.Domain
|
addr = n.Domain
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +125,7 @@ func (n *NetworkResource) ToRoute(peer *nbpeer.Peer, router *routerTypes.Network
|
|||||||
AccessControlGroups: nil,
|
AccessControlGroups: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.Type == host || n.Type == subnet {
|
if n.Type == Host || n.Type == Subnet {
|
||||||
r.Network = n.Prefix
|
r.Network = n.Prefix
|
||||||
|
|
||||||
r.NetworkType = route.IPv4Network
|
r.NetworkType = route.IPv4Network
|
||||||
@@ -134,7 +134,7 @@ func (n *NetworkResource) ToRoute(peer *nbpeer.Peer, router *routerTypes.Network
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if n.Type == domain {
|
if n.Type == Domain {
|
||||||
domainList, err := nbDomain.FromStringList([]string{n.Domain})
|
domainList, err := nbDomain.FromStringList([]string{n.Domain})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -157,18 +157,18 @@ func (n *NetworkResource) EventMeta(network *networkTypes.Network) map[string]an
|
|||||||
func GetResourceType(address string) (NetworkResourceType, string, netip.Prefix, error) {
|
func GetResourceType(address string) (NetworkResourceType, string, netip.Prefix, error) {
|
||||||
if prefix, err := netip.ParsePrefix(address); err == nil {
|
if prefix, err := netip.ParsePrefix(address); err == nil {
|
||||||
if prefix.Bits() == 32 || prefix.Bits() == 128 {
|
if prefix.Bits() == 32 || prefix.Bits() == 128 {
|
||||||
return host, "", prefix, nil
|
return Host, "", prefix, nil
|
||||||
}
|
}
|
||||||
return subnet, "", prefix, nil
|
return Subnet, "", prefix, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if ip, err := netip.ParseAddr(address); err == nil {
|
if ip, err := netip.ParseAddr(address); err == nil {
|
||||||
return host, "", netip.PrefixFrom(ip, ip.BitLen()), nil
|
return Host, "", netip.PrefixFrom(ip, ip.BitLen()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
domainRegex := regexp.MustCompile(`^(\*\.)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$`)
|
domainRegex := regexp.MustCompile(`^(\*\.)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$`)
|
||||||
if domainRegex.MatchString(address) {
|
if domainRegex.MatchString(address) {
|
||||||
return domain, address, netip.Prefix{}, nil
|
return Domain, address, netip.Prefix{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", "", netip.Prefix{}, errors.New("not a valid host, subnet, or domain")
|
return "", "", netip.Prefix{}, errors.New("not a valid host, subnet, or domain")
|
||||||
|
|||||||
@@ -14,15 +14,15 @@ func TestGetResourceType(t *testing.T) {
|
|||||||
expectedPrefix netip.Prefix
|
expectedPrefix netip.Prefix
|
||||||
}{
|
}{
|
||||||
// Valid host IPs
|
// Valid host IPs
|
||||||
{"1.1.1.1", host, false, "", netip.MustParsePrefix("1.1.1.1/32")},
|
{"1.1.1.1", Host, false, "", netip.MustParsePrefix("1.1.1.1/32")},
|
||||||
{"1.1.1.1/32", host, false, "", netip.MustParsePrefix("1.1.1.1/32")},
|
{"1.1.1.1/32", Host, false, "", netip.MustParsePrefix("1.1.1.1/32")},
|
||||||
// Valid subnets
|
// Valid subnets
|
||||||
{"192.168.1.0/24", subnet, false, "", netip.MustParsePrefix("192.168.1.0/24")},
|
{"192.168.1.0/24", Subnet, false, "", netip.MustParsePrefix("192.168.1.0/24")},
|
||||||
{"10.0.0.0/16", subnet, false, "", netip.MustParsePrefix("10.0.0.0/16")},
|
{"10.0.0.0/16", Subnet, false, "", netip.MustParsePrefix("10.0.0.0/16")},
|
||||||
// Valid domains
|
// Valid domains
|
||||||
{"example.com", domain, false, "example.com", netip.Prefix{}},
|
{"example.com", Domain, false, "example.com", netip.Prefix{}},
|
||||||
{"*.example.com", domain, false, "*.example.com", netip.Prefix{}},
|
{"*.example.com", Domain, false, "*.example.com", netip.Prefix{}},
|
||||||
{"sub.example.com", domain, false, "sub.example.com", netip.Prefix{}},
|
{"sub.example.com", Domain, false, "sub.example.com", netip.Prefix{}},
|
||||||
// Invalid inputs
|
// Invalid inputs
|
||||||
{"invalid", "", true, "", netip.Prefix{}},
|
{"invalid", "", true, "", netip.Prefix{}},
|
||||||
{"1.1.1.1/abc", "", true, "", netip.Prefix{}},
|
{"1.1.1.1/abc", "", true, "", netip.Prefix{}},
|
||||||
|
|||||||
Reference in New Issue
Block a user