2022-08-18 18:22:15 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/netbirdio/netbird/management/proto"
|
2023-01-25 16:29:59 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
2022-11-11 20:36:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2022-08-18 18:22:15 +02:00
|
|
|
"github.com/netbirdio/netbird/route"
|
|
|
|
"github.com/rs/xid"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"net/netip"
|
|
|
|
"strconv"
|
2022-08-22 14:10:24 +02:00
|
|
|
"unicode/utf8"
|
2022-08-18 18:22:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// UpdateRouteDescription indicates a route description update operation
|
|
|
|
UpdateRouteDescription RouteUpdateOperationType = iota
|
2022-08-22 14:10:24 +02:00
|
|
|
// UpdateRouteNetwork indicates a route IP update operation
|
|
|
|
UpdateRouteNetwork
|
2022-08-18 18:22:15 +02:00
|
|
|
// UpdateRoutePeer indicates a route peer update operation
|
|
|
|
UpdateRoutePeer
|
|
|
|
// UpdateRouteMetric indicates a route metric update operation
|
|
|
|
UpdateRouteMetric
|
|
|
|
// UpdateRouteMasquerade indicates a route masquerade update operation
|
|
|
|
UpdateRouteMasquerade
|
|
|
|
// UpdateRouteEnabled indicates a route enabled update operation
|
|
|
|
UpdateRouteEnabled
|
2022-08-22 14:10:24 +02:00
|
|
|
// UpdateRouteNetworkIdentifier indicates a route net ID update operation
|
|
|
|
UpdateRouteNetworkIdentifier
|
2022-12-06 10:11:57 +01:00
|
|
|
// UpdateRouteGroups indicates a group list update operation
|
|
|
|
UpdateRouteGroups
|
2022-08-18 18:22:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// RouteUpdateOperationType operation type
|
|
|
|
type RouteUpdateOperationType int
|
|
|
|
|
|
|
|
func (t RouteUpdateOperationType) String() string {
|
|
|
|
switch t {
|
|
|
|
case UpdateRouteDescription:
|
|
|
|
return "UpdateRouteDescription"
|
2022-08-22 14:10:24 +02:00
|
|
|
case UpdateRouteNetwork:
|
|
|
|
return "UpdateRouteNetwork"
|
2022-08-18 18:22:15 +02:00
|
|
|
case UpdateRoutePeer:
|
|
|
|
return "UpdateRoutePeer"
|
|
|
|
case UpdateRouteMetric:
|
|
|
|
return "UpdateRouteMetric"
|
|
|
|
case UpdateRouteMasquerade:
|
|
|
|
return "UpdateRouteMasquerade"
|
|
|
|
case UpdateRouteEnabled:
|
|
|
|
return "UpdateRouteEnabled"
|
2022-08-22 14:10:24 +02:00
|
|
|
case UpdateRouteNetworkIdentifier:
|
|
|
|
return "UpdateRouteNetworkIdentifier"
|
2022-12-06 10:11:57 +01:00
|
|
|
case UpdateRouteGroups:
|
|
|
|
return "UpdateRouteGroups"
|
2022-08-18 18:22:15 +02:00
|
|
|
default:
|
|
|
|
return "InvalidOperation"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouteUpdateOperation operation object with type and values to be applied
|
|
|
|
type RouteUpdateOperation struct {
|
|
|
|
Type RouteUpdateOperationType
|
|
|
|
Values []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRoute gets a route object from account and route IDs
|
2022-11-05 10:24:50 +01:00
|
|
|
func (am *DefaultAccountManager) GetRoute(accountID, routeID, userID string) (*route.Route, error) {
|
2022-11-07 17:52:23 +01:00
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
user, err := account.FindUser(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.IsAdmin() {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.PermissionDenied, "Only administrators can view Network Routes")
|
2022-11-05 10:24:50 +01:00
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
wantedRoute, found := account.Routes[routeID]
|
|
|
|
if found {
|
|
|
|
return wantedRoute, nil
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.NotFound, "route with ID %s not found", routeID)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 14:10:24 +02:00
|
|
|
// checkPrefixPeerExists checks the combination of prefix and peer id, if it exists returns an error, otherwise returns nil
|
2023-02-03 10:33:28 +01:00
|
|
|
func (am *DefaultAccountManager) checkPrefixPeerExists(accountID, peerID string, prefix netip.Prefix) error {
|
2022-08-23 11:09:56 +02:00
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
if peerID == "" {
|
2022-08-23 11:09:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:10:56 +01:00
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
routesWithPrefix := account.GetRoutesByPrefix(prefix)
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
if s, ok := status.FromError(err); ok && s.Type() == status.NotFound {
|
2022-08-18 18:22:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "failed to parse prefix %s", prefix.String())
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
for _, prefixRoute := range routesWithPrefix {
|
2023-02-03 10:33:28 +01:00
|
|
|
if prefixRoute.Peer == peerID {
|
|
|
|
return status.Errorf(status.AlreadyExists, "failed to add route with prefix %s - peer already has this route", prefix.String())
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRoute creates and saves a new route
|
2023-02-03 10:33:28 +01:00
|
|
|
func (am *DefaultAccountManager) CreateRoute(accountID string, network, peerID, description, netID string, masquerade bool, metric int, groups []string, enabled bool, userID string) (*route.Route, error) {
|
2022-11-07 17:52:23 +01:00
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
if peerID != "" {
|
|
|
|
peer := account.GetPeer(peerID)
|
2023-01-25 16:29:59 +01:00
|
|
|
if peer == nil {
|
2023-02-03 10:33:28 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "peer with ID %s not found", peerID)
|
2023-01-25 16:29:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
var newRoute route.Route
|
2022-08-22 14:10:24 +02:00
|
|
|
prefixType, newPrefix, err := route.ParseNetwork(network)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "failed to parse IP %s", network)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
2023-02-03 10:33:28 +01:00
|
|
|
err = am.checkPrefixPeerExists(accountID, peerID, newPrefix)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if metric < route.MinMetric || metric > route.MaxMetric {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "metric should be between %d and %d", route.MinMetric, route.MaxMetric)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 14:10:24 +02:00
|
|
|
if utf8.RuneCountInString(netID) > route.MaxNetIDChar || netID == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "identifier should be between 1 and %d", route.MaxNetIDChar)
|
2022-08-22 14:10:24 +02:00
|
|
|
}
|
|
|
|
|
2022-12-06 10:11:57 +01:00
|
|
|
err = validateGroups(groups, account.Groups)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
newRoute.Peer = peerID
|
2022-08-18 18:22:15 +02:00
|
|
|
newRoute.ID = xid.New().String()
|
2022-08-22 14:10:24 +02:00
|
|
|
newRoute.Network = newPrefix
|
|
|
|
newRoute.NetworkType = prefixType
|
2022-08-18 18:22:15 +02:00
|
|
|
newRoute.Description = description
|
2022-08-22 14:10:24 +02:00
|
|
|
newRoute.NetID = netID
|
2022-08-18 18:22:15 +02:00
|
|
|
newRoute.Masquerade = masquerade
|
|
|
|
newRoute.Metric = metric
|
|
|
|
newRoute.Enabled = enabled
|
2022-12-06 10:11:57 +01:00
|
|
|
newRoute.Groups = groups
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
if account.Routes == nil {
|
|
|
|
account.Routes = make(map[string]*route.Route)
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Routes[newRoute.ID] = &newRoute
|
|
|
|
|
|
|
|
account.Network.IncSerial()
|
|
|
|
if err = am.Store.SaveAccount(account); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.updateAccountPeers(account)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2022-11-11 20:36:45 +01:00
|
|
|
return &newRoute, status.Errorf(status.Internal, "failed to update peers after create route %s", newPrefix)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
2023-01-25 16:29:59 +01:00
|
|
|
|
|
|
|
am.storeEvent(userID, newRoute.ID, accountID, activity.RouteCreated, newRoute.EventMeta())
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
return &newRoute, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveRoute saves route
|
2023-01-25 16:29:59 +01:00
|
|
|
func (am *DefaultAccountManager) SaveRoute(accountID, userID string, routeToSave *route.Route) error {
|
2022-11-07 17:52:23 +01:00
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
if routeToSave == nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "route provided is nil")
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 14:10:24 +02:00
|
|
|
if !routeToSave.Network.IsValid() {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "invalid Prefix %s", routeToSave.Network.String())
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if routeToSave.Metric < route.MinMetric || routeToSave.Metric > route.MaxMetric {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "metric should be between %d and %d", route.MinMetric, route.MaxMetric)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 14:10:24 +02:00
|
|
|
if utf8.RuneCountInString(routeToSave.NetID) > route.MaxNetIDChar || routeToSave.NetID == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
return status.Errorf(status.InvalidArgument, "identifier should be between 1 and %d", route.MaxNetIDChar)
|
2022-08-22 14:10:24 +02:00
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return err
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if routeToSave.Peer != "" {
|
2023-02-03 10:33:28 +01:00
|
|
|
peer := account.GetPeer(routeToSave.Peer)
|
|
|
|
if peer == nil {
|
|
|
|
return status.Errorf(status.InvalidArgument, "peer with ID %s not found", routeToSave.Peer)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-06 10:11:57 +01:00
|
|
|
err = validateGroups(routeToSave.Groups, account.Groups)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
account.Routes[routeToSave.ID] = routeToSave
|
|
|
|
|
|
|
|
account.Network.IncSerial()
|
|
|
|
if err = am.Store.SaveAccount(account); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
err = am.updateAccountPeers(account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-25 16:29:59 +01:00
|
|
|
am.storeEvent(userID, routeToSave.ID, accountID, activity.RouteUpdated, routeToSave.EventMeta())
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
return nil
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRoute updates existing route with set of operations
|
|
|
|
func (am *DefaultAccountManager) UpdateRoute(accountID, routeID string, operations []RouteUpdateOperation) (*route.Route, error) {
|
2022-11-07 17:52:23 +01:00
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
routeToUpdate, ok := account.Routes[routeID]
|
|
|
|
if !ok {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.NotFound, "route %s no longer exists", routeID)
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
newRoute := routeToUpdate.Copy()
|
|
|
|
|
|
|
|
for _, operation := range operations {
|
|
|
|
|
|
|
|
if len(operation.Values) != 1 {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "operation %s contains invalid number of values, it should be 1", operation.Type.String())
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch operation.Type {
|
|
|
|
case UpdateRouteDescription:
|
|
|
|
newRoute.Description = operation.Values[0]
|
2022-08-22 14:10:24 +02:00
|
|
|
case UpdateRouteNetworkIdentifier:
|
|
|
|
if utf8.RuneCountInString(operation.Values[0]) > route.MaxNetIDChar || operation.Values[0] == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "identifier should be between 1 and %d", route.MaxNetIDChar)
|
2022-08-22 14:10:24 +02:00
|
|
|
}
|
|
|
|
newRoute.NetID = operation.Values[0]
|
|
|
|
case UpdateRouteNetwork:
|
|
|
|
prefixType, prefix, err := route.ParseNetwork(operation.Values[0])
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "failed to parse IP %s", operation.Values[0])
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
err = am.checkPrefixPeerExists(accountID, routeToUpdate.Peer, prefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-22 14:10:24 +02:00
|
|
|
newRoute.Network = prefix
|
|
|
|
newRoute.NetworkType = prefixType
|
2022-08-18 18:22:15 +02:00
|
|
|
case UpdateRoutePeer:
|
|
|
|
if operation.Values[0] != "" {
|
2023-02-03 10:33:28 +01:00
|
|
|
peer := account.GetPeer(operation.Values[0])
|
|
|
|
if peer == nil {
|
|
|
|
return nil, status.Errorf(status.InvalidArgument, "peer with ID %s not found", operation.Values[0])
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 14:10:24 +02:00
|
|
|
err = am.checkPrefixPeerExists(accountID, operation.Values[0], routeToUpdate.Network)
|
2022-08-18 18:22:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newRoute.Peer = operation.Values[0]
|
|
|
|
case UpdateRouteMetric:
|
|
|
|
metric, err := strconv.Atoi(operation.Values[0])
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "failed to parse metric %s, not int", operation.Values[0])
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
if metric < route.MinMetric || metric > route.MaxMetric {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "failed to parse metric %s, value should be %d > N < %d",
|
2022-08-18 18:22:15 +02:00
|
|
|
operation.Values[0],
|
|
|
|
route.MinMetric,
|
|
|
|
route.MaxMetric,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
newRoute.Metric = metric
|
|
|
|
case UpdateRouteMasquerade:
|
|
|
|
masquerade, err := strconv.ParseBool(operation.Values[0])
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "failed to parse masquerade %s, not boolean", operation.Values[0])
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
newRoute.Masquerade = masquerade
|
|
|
|
case UpdateRouteEnabled:
|
|
|
|
enabled, err := strconv.ParseBool(operation.Values[0])
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "failed to parse enabled %s, not boolean", operation.Values[0])
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
newRoute.Enabled = enabled
|
2022-12-06 10:11:57 +01:00
|
|
|
case UpdateRouteGroups:
|
|
|
|
err = validateGroups(operation.Values, account.Groups)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newRoute.Groups = operation.Values
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Routes[routeID] = newRoute
|
|
|
|
|
|
|
|
account.Network.IncSerial()
|
|
|
|
if err = am.Store.SaveAccount(account); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.updateAccountPeers(account)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.Internal, "failed to update account peers")
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
return newRoute, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRoute deletes route with routeID
|
2023-01-25 16:29:59 +01:00
|
|
|
func (am *DefaultAccountManager) DeleteRoute(accountID, routeID, userID string) error {
|
2022-11-07 17:52:23 +01:00
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return err
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-25 16:29:59 +01:00
|
|
|
routy := account.Routes[routeID]
|
|
|
|
if routy == nil {
|
|
|
|
return status.Errorf(status.NotFound, "route with ID %s doesn't exist", routeID)
|
|
|
|
}
|
2022-08-18 18:22:15 +02:00
|
|
|
delete(account.Routes, routeID)
|
|
|
|
|
|
|
|
account.Network.IncSerial()
|
|
|
|
if err = am.Store.SaveAccount(account); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-25 16:29:59 +01:00
|
|
|
am.storeEvent(userID, routy.ID, accountID, activity.RouteRemoved, routy.EventMeta())
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
return am.updateAccountPeers(account)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRoutes returns a list of routes from account
|
2022-11-05 10:24:50 +01:00
|
|
|
func (am *DefaultAccountManager) ListRoutes(accountID, userID string) ([]*route.Route, error) {
|
2022-11-07 17:52:23 +01:00
|
|
|
unlock := am.Store.AcquireAccountLock(accountID)
|
|
|
|
defer unlock()
|
2022-08-18 18:22:15 +02:00
|
|
|
|
|
|
|
account, err := am.Store.GetAccount(accountID)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
user, err := account.FindUser(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.IsAdmin() {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.PermissionDenied, "Only administrators can view Network Routes")
|
2022-11-05 10:24:50 +01:00
|
|
|
}
|
|
|
|
|
2022-08-18 18:22:15 +02:00
|
|
|
routes := make([]*route.Route, 0, len(account.Routes))
|
|
|
|
for _, item := range account.Routes {
|
|
|
|
routes = append(routes, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toProtocolRoute(route *route.Route) *proto.Route {
|
|
|
|
return &proto.Route{
|
2022-08-22 14:10:24 +02:00
|
|
|
ID: route.ID,
|
|
|
|
NetID: route.NetID,
|
|
|
|
Network: route.Network.String(),
|
|
|
|
NetworkType: int64(route.NetworkType),
|
|
|
|
Peer: route.Peer,
|
|
|
|
Metric: int64(route.Metric),
|
|
|
|
Masquerade: route.Masquerade,
|
2022-08-18 18:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toProtocolRoutes(routes []*route.Route) []*proto.Route {
|
|
|
|
protoRoutes := make([]*proto.Route, 0)
|
|
|
|
for _, r := range routes {
|
|
|
|
protoRoutes = append(protoRoutes, toProtocolRoute(r))
|
|
|
|
}
|
|
|
|
return protoRoutes
|
|
|
|
}
|