You've already forked ionscale
mirror of
https://github.com/jsiebens/ionscale.git
synced 2026-04-05 12:32:58 +01:00
feat: view and enable routes on machines
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
"inet.af/netaddr"
|
||||
)
|
||||
|
||||
func (s *Service) ListMachines(ctx context.Context, req *api.ListMachinesRequest) (*api.ListMachinesResponse, error) {
|
||||
@@ -75,3 +76,78 @@ func (s *Service) DeleteMachine(ctx context.Context, req *api.DeleteMachineReque
|
||||
|
||||
return &api.DeleteMachineResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetMachineRoutes(ctx context.Context, req *api.GetMachineRoutesRequest) (*api.GetMachineRoutesResponse, error) {
|
||||
|
||||
m, err := s.repository.GetMachine(ctx, req.MachineId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if m == nil {
|
||||
return nil, status.Error(codes.NotFound, "machine does not exist")
|
||||
}
|
||||
|
||||
var routes []*api.RoutableIP
|
||||
for _, r := range m.HostInfo.RoutableIPs {
|
||||
routes = append(routes, &api.RoutableIP{
|
||||
Advertised: r.String(),
|
||||
Allowed: m.IsAllowedIP(r),
|
||||
})
|
||||
}
|
||||
|
||||
response := api.GetMachineRoutesResponse{
|
||||
Routes: routes,
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func (s *Service) SetMachineRoutes(ctx context.Context, req *api.SetMachineRoutesRequest) (*api.GetMachineRoutesResponse, error) {
|
||||
m, err := s.repository.GetMachine(ctx, req.MachineId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if m == nil {
|
||||
return nil, status.Error(codes.NotFound, "machine does not exist")
|
||||
}
|
||||
|
||||
var allowedIps []netaddr.IPPrefix
|
||||
for _, r := range req.AllowedIps {
|
||||
prefix, err := netaddr.ParseIPPrefix(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
allowedIps = append(allowedIps, prefix)
|
||||
}
|
||||
|
||||
m.AllowIPs = allowedIps
|
||||
if err := s.repository.SaveMachine(ctx, m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.brokers(m.TailnetID).SignalPeerUpdated(m.ID)
|
||||
|
||||
var routes []*api.RoutableIP
|
||||
for _, r := range m.HostInfo.RoutableIPs {
|
||||
routes = append(routes, &api.RoutableIP{
|
||||
Advertised: r.String(),
|
||||
Allowed: m.IsAllowedIP(r),
|
||||
})
|
||||
}
|
||||
|
||||
response := api.GetMachineRoutesResponse{
|
||||
Routes: routes,
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func mapIp(ip []netaddr.IPPrefix) []string {
|
||||
var x = []string{}
|
||||
for _, i := range ip {
|
||||
x = append(x, i.String())
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user