2024-04-23 14:42:53 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-04-29 18:43:14 +02:00
|
|
|
"net/netip"
|
2024-04-23 14:42:53 +02:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"golang.org/x/exp/maps"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/proto"
|
2024-06-13 13:24:24 +02:00
|
|
|
"github.com/netbirdio/netbird/management/domain"
|
2024-05-06 14:47:49 +02:00
|
|
|
"github.com/netbirdio/netbird/route"
|
2024-04-23 14:42:53 +02:00
|
|
|
)
|
|
|
|
|
2024-04-29 18:43:14 +02:00
|
|
|
type selectRoute struct {
|
2024-05-06 14:47:49 +02:00
|
|
|
NetID route.NetID
|
2024-04-29 18:43:14 +02:00
|
|
|
Network netip.Prefix
|
2024-06-13 13:24:24 +02:00
|
|
|
Domains domain.List
|
2024-04-29 18:43:14 +02:00
|
|
|
Selected bool
|
|
|
|
}
|
|
|
|
|
2024-04-23 14:42:53 +02:00
|
|
|
// ListRoutes returns a list of all available routes.
|
2024-06-13 13:24:24 +02:00
|
|
|
func (s *Server) ListRoutes(context.Context, *proto.ListRoutesRequest) (*proto.ListRoutesResponse, error) {
|
2024-04-23 14:42:53 +02:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
2024-05-10 10:47:16 +02:00
|
|
|
if s.connectClient == nil {
|
2024-04-23 14:42:53 +02:00
|
|
|
return nil, fmt.Errorf("not connected")
|
|
|
|
}
|
|
|
|
|
2024-05-10 10:47:16 +02:00
|
|
|
engine := s.connectClient.Engine()
|
|
|
|
if engine == nil {
|
|
|
|
return nil, fmt.Errorf("not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
routesMap := engine.GetClientRoutesWithNetID()
|
|
|
|
routeSelector := engine.GetRouteManager().GetRouteSelector()
|
2024-04-23 14:42:53 +02:00
|
|
|
|
2024-04-29 18:43:14 +02:00
|
|
|
var routes []*selectRoute
|
2024-04-23 14:42:53 +02:00
|
|
|
for id, rt := range routesMap {
|
|
|
|
if len(rt) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2024-04-29 18:43:14 +02:00
|
|
|
route := &selectRoute{
|
|
|
|
NetID: id,
|
|
|
|
Network: rt[0].Network,
|
2024-06-13 13:24:24 +02:00
|
|
|
Domains: rt[0].Domains,
|
2024-04-29 18:43:14 +02:00
|
|
|
Selected: routeSelector.IsSelected(id),
|
|
|
|
}
|
|
|
|
routes = append(routes, route)
|
2024-04-23 14:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(routes, func(i, j int) bool {
|
|
|
|
iPrefix := routes[i].Network.Bits()
|
|
|
|
jPrefix := routes[j].Network.Bits()
|
|
|
|
|
|
|
|
if iPrefix == jPrefix {
|
|
|
|
iAddr := routes[i].Network.Addr()
|
|
|
|
jAddr := routes[j].Network.Addr()
|
|
|
|
if iAddr == jAddr {
|
2024-04-29 18:43:14 +02:00
|
|
|
return routes[i].NetID < routes[j].NetID
|
2024-04-23 14:42:53 +02:00
|
|
|
}
|
|
|
|
return iAddr.String() < jAddr.String()
|
|
|
|
}
|
|
|
|
return iPrefix < jPrefix
|
|
|
|
})
|
|
|
|
|
2024-06-13 13:24:24 +02:00
|
|
|
resolvedDomains := s.statusRecorder.GetResolvedDomainsStates()
|
2024-04-23 14:42:53 +02:00
|
|
|
var pbRoutes []*proto.Route
|
|
|
|
for _, route := range routes {
|
2024-06-13 13:24:24 +02:00
|
|
|
pbRoute := &proto.Route{
|
|
|
|
ID: string(route.NetID),
|
|
|
|
Network: route.Network.String(),
|
|
|
|
Domains: route.Domains.ToSafeStringList(),
|
|
|
|
ResolvedIPs: map[string]*proto.IPList{},
|
|
|
|
Selected: route.Selected,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, domain := range route.Domains {
|
|
|
|
if prefixes, exists := resolvedDomains[domain]; exists {
|
|
|
|
var ipStrings []string
|
|
|
|
for _, prefix := range prefixes {
|
|
|
|
ipStrings = append(ipStrings, prefix.Addr().String())
|
|
|
|
}
|
|
|
|
pbRoute.ResolvedIPs[string(domain)] = &proto.IPList{
|
|
|
|
Ips: ipStrings,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pbRoutes = append(pbRoutes, pbRoute)
|
2024-04-23 14:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.ListRoutesResponse{
|
|
|
|
Routes: pbRoutes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectRoutes selects specific routes based on the client request.
|
|
|
|
func (s *Server) SelectRoutes(_ context.Context, req *proto.SelectRoutesRequest) (*proto.SelectRoutesResponse, error) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
2024-05-10 10:47:16 +02:00
|
|
|
if s.connectClient == nil {
|
|
|
|
return nil, fmt.Errorf("not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
engine := s.connectClient.Engine()
|
|
|
|
if engine == nil {
|
|
|
|
return nil, fmt.Errorf("not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
routeManager := engine.GetRouteManager()
|
2024-04-23 14:42:53 +02:00
|
|
|
routeSelector := routeManager.GetRouteSelector()
|
|
|
|
if req.GetAll() {
|
|
|
|
routeSelector.SelectAllRoutes()
|
|
|
|
} else {
|
2024-05-06 14:47:49 +02:00
|
|
|
routes := toNetIDs(req.GetRouteIDs())
|
2024-05-10 10:47:16 +02:00
|
|
|
if err := routeSelector.SelectRoutes(routes, req.GetAppend(), maps.Keys(engine.GetClientRoutesWithNetID())); err != nil {
|
2024-04-23 14:42:53 +02:00
|
|
|
return nil, fmt.Errorf("select routes: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2024-05-10 10:47:16 +02:00
|
|
|
routeManager.TriggerSelection(engine.GetClientRoutes())
|
2024-04-23 14:42:53 +02:00
|
|
|
|
|
|
|
return &proto.SelectRoutesResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeselectRoutes deselects specific routes based on the client request.
|
|
|
|
func (s *Server) DeselectRoutes(_ context.Context, req *proto.SelectRoutesRequest) (*proto.SelectRoutesResponse, error) {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
2024-05-10 10:47:16 +02:00
|
|
|
if s.connectClient == nil {
|
|
|
|
return nil, fmt.Errorf("not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
engine := s.connectClient.Engine()
|
|
|
|
if engine == nil {
|
|
|
|
return nil, fmt.Errorf("not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
routeManager := engine.GetRouteManager()
|
2024-04-23 14:42:53 +02:00
|
|
|
routeSelector := routeManager.GetRouteSelector()
|
|
|
|
if req.GetAll() {
|
|
|
|
routeSelector.DeselectAllRoutes()
|
|
|
|
} else {
|
2024-05-06 14:47:49 +02:00
|
|
|
routes := toNetIDs(req.GetRouteIDs())
|
2024-05-10 10:47:16 +02:00
|
|
|
if err := routeSelector.DeselectRoutes(routes, maps.Keys(engine.GetClientRoutesWithNetID())); err != nil {
|
2024-04-23 14:42:53 +02:00
|
|
|
return nil, fmt.Errorf("deselect routes: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2024-05-10 10:47:16 +02:00
|
|
|
routeManager.TriggerSelection(engine.GetClientRoutes())
|
2024-04-23 14:42:53 +02:00
|
|
|
|
|
|
|
return &proto.SelectRoutesResponse{}, nil
|
|
|
|
}
|
2024-05-06 14:47:49 +02:00
|
|
|
|
|
|
|
func toNetIDs(routes []string) []route.NetID {
|
|
|
|
var netIDs []route.NetID
|
|
|
|
for _, rt := range routes {
|
|
|
|
netIDs = append(netIDs, route.NetID(rt))
|
|
|
|
}
|
|
|
|
return netIDs
|
|
|
|
}
|