From 33ae56acfaf96d6e4d3e8d15c91868b47ba5ed6c Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sun, 18 Sep 2022 11:36:35 +0200 Subject: [PATCH] Add primary routes to node Signed-off-by: Kristoffer Dalby --- machine.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/machine.go b/machine.go index 9fe450a..b43c457 100644 --- a/machine.go +++ b/machine.go @@ -35,6 +35,11 @@ const ( maxHostnameLength = 255 ) +var ( + ExitRouteV4 = netip.MustParsePrefix("0.0.0.0/0") + ExitRouteV6 = netip.MustParsePrefix("::/0") +) + // Machine is a Headscale client. type Machine struct { ID uint64 `gorm:"primary_key"` @@ -633,10 +638,17 @@ func (machine Machine) toNode( []netip.Prefix{}, addrs...) // we append the node own IP, as it is required by the clients - // TODO(kradalby): Needs investigation, We probably dont need this condition - // now that we dont have shared nodes - if includeRoutes { - allowedIPs = append(allowedIPs, machine.EnabledRoutes...) + allowedIPs = append(allowedIPs, machine.EnabledRoutes...) + + primaryRoutes := []netip.Prefix{} + if len(machine.EnabledRoutes) > 0 { + for _, route := range machine.EnabledRoutes { + if route == ExitRouteV4 || route == ExitRouteV6 { + continue + } + + primaryRoutes = append(primaryRoutes, route) + } } var derp string @@ -691,6 +703,7 @@ func (machine Machine) toNode( DiscoKey: discoKey, Addresses: addrs, AllowedIPs: allowedIPs, + PrimaryRoutes: primaryRoutes, Endpoints: machine.Endpoints, DERP: derp,