2021-04-28 16:15:45 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-01-29 12:55:29 +01:00
|
|
|
"net/netip"
|
2021-11-04 23:44:59 +01:00
|
|
|
"strconv"
|
2021-04-28 16:15:45 +02:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2023-05-21 18:37:59 +02:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2021-08-21 15:49:46 +02:00
|
|
|
"github.com/pterm/pterm"
|
2021-04-28 16:15:45 +02:00
|
|
|
"github.com/spf13/cobra"
|
2021-11-07 11:15:32 +01:00
|
|
|
"google.golang.org/grpc/status"
|
2021-04-28 16:15:45 +02:00
|
|
|
)
|
|
|
|
|
2022-11-26 10:59:56 +01:00
|
|
|
const (
|
|
|
|
Base10 = 10
|
|
|
|
)
|
|
|
|
|
2021-07-25 15:08:40 +02:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(routesCmd)
|
2021-11-04 23:44:59 +01:00
|
|
|
listRoutesCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
|
|
|
|
routesCmd.AddCommand(listRoutesCmd)
|
2021-08-21 16:04:30 +02:00
|
|
|
|
2022-11-26 01:04:02 +01:00
|
|
|
enableRouteCmd.Flags().Uint64P("route", "r", 0, "Route identifier (ID)")
|
|
|
|
err := enableRouteCmd.MarkFlagRequired("route")
|
2021-11-04 23:44:59 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-07-25 15:08:40 +02:00
|
|
|
routesCmd.AddCommand(enableRouteCmd)
|
2021-11-04 23:44:59 +01:00
|
|
|
|
2022-11-26 01:04:02 +01:00
|
|
|
disableRouteCmd.Flags().Uint64P("route", "r", 0, "Route identifier (ID)")
|
|
|
|
err = disableRouteCmd.MarkFlagRequired("route")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
routesCmd.AddCommand(disableRouteCmd)
|
2023-03-06 09:05:40 +01:00
|
|
|
|
|
|
|
deleteRouteCmd.Flags().Uint64P("route", "r", 0, "Route identifier (ID)")
|
|
|
|
err = deleteRouteCmd.MarkFlagRequired("route")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
routesCmd.AddCommand(deleteRouteCmd)
|
2021-07-25 15:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var routesCmd = &cobra.Command{
|
2022-03-02 05:20:04 +01:00
|
|
|
Use: "routes",
|
|
|
|
Short: "Manage the routes of Headscale",
|
|
|
|
Aliases: []string{"r", "route"},
|
2021-04-30 00:23:26 +02:00
|
|
|
}
|
|
|
|
|
2021-07-25 15:08:40 +02:00
|
|
|
var listRoutesCmd = &cobra.Command{
|
2022-03-02 05:20:04 +01:00
|
|
|
Use: "list",
|
2022-11-26 01:04:02 +01:00
|
|
|
Short: "List all routes",
|
2022-03-02 05:20:04 +01:00
|
|
|
Aliases: []string{"ls", "show"},
|
2021-04-28 16:15:45 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-11-04 23:44:59 +01:00
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
2021-11-15 18:24:24 +01:00
|
|
|
machineID, err := cmd.Flags().GetUint64("identifier")
|
2021-04-30 00:23:26 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error getting machine id from flag: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-04-30 00:23:26 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 09:23:58 +02:00
|
|
|
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
|
2021-11-04 23:44:59 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
2022-11-26 01:04:02 +01:00
|
|
|
var routes []*v1.Route
|
2021-05-08 13:58:51 +02:00
|
|
|
|
2022-11-26 01:04:02 +01:00
|
|
|
if machineID == 0 {
|
|
|
|
response, err := client.GetRoutes(ctx, &v1.GetRoutesRequest{})
|
|
|
|
if err != nil {
|
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Cannot get nodes: %s", status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
|
|
|
}
|
2021-05-08 17:06:36 +02:00
|
|
|
|
2022-11-28 16:54:23 +01:00
|
|
|
if output != "" {
|
2023-11-23 08:31:33 +01:00
|
|
|
SuccessOutput(response.GetRoutes(), "", output)
|
2022-11-28 16:54:23 +01:00
|
|
|
}
|
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
routes = response.GetRoutes()
|
2022-11-26 01:04:02 +01:00
|
|
|
} else {
|
2023-09-24 13:42:05 +02:00
|
|
|
response, err := client.GetNodeRoutes(ctx, &v1.GetNodeRoutesRequest{
|
|
|
|
NodeId: machineID,
|
2022-11-26 01:04:02 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ErrorOutput(
|
|
|
|
err,
|
2023-09-24 13:42:05 +02:00
|
|
|
fmt.Sprintf("Cannot get routes for node %d: %s", machineID, status.Convert(err).Message()),
|
2022-11-26 01:04:02 +01:00
|
|
|
output,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:54:23 +01:00
|
|
|
if output != "" {
|
2023-11-23 08:31:33 +01:00
|
|
|
SuccessOutput(response.GetRoutes(), "", output)
|
2022-11-28 16:54:23 +01:00
|
|
|
}
|
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
routes = response.GetRoutes()
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2021-05-08 13:58:51 +02:00
|
|
|
|
2022-11-26 01:04:02 +01:00
|
|
|
tableData := routesToPtables(routes)
|
2021-11-04 23:44:59 +01:00
|
|
|
if err != nil {
|
|
|
|
ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output)
|
|
|
|
}
|
2021-08-21 15:49:46 +02:00
|
|
|
|
2021-11-14 20:32:03 +01:00
|
|
|
err = pterm.DefaultTable.WithHasHeader().WithData(tableData).Render()
|
2021-08-21 15:49:46 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Failed to render pterm table: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-08-21 15:49:46 +02:00
|
|
|
}
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:08:40 +02:00
|
|
|
var enableRouteCmd = &cobra.Command{
|
2021-11-04 23:44:59 +01:00
|
|
|
Use: "enable",
|
2022-11-26 01:04:02 +01:00
|
|
|
Short: "Set a route as enabled",
|
|
|
|
Long: `This command will make as enabled a given route.`,
|
2021-11-04 23:44:59 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
output, _ := cmd.Flags().GetString("output")
|
2021-11-15 18:24:24 +01:00
|
|
|
|
2022-11-26 01:04:02 +01:00
|
|
|
routeID, err := cmd.Flags().GetUint64("route")
|
2021-08-21 16:04:30 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error getting machine id from flag: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-08-21 16:04:30 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 09:23:58 +02:00
|
|
|
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
|
2021-11-04 23:44:59 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
2022-11-26 01:04:02 +01:00
|
|
|
response, err := client.EnableRoute(ctx, &v1.EnableRouteRequest{
|
|
|
|
RouteId: routeID,
|
|
|
|
})
|
2021-08-21 16:04:30 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
2022-11-26 01:04:02 +01:00
|
|
|
fmt.Sprintf("Cannot enable route %d: %s", routeID, status.Convert(err).Message()),
|
2021-11-13 09:36:45 +01:00
|
|
|
output,
|
|
|
|
)
|
2021-08-21 16:04:30 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
if output != "" {
|
2022-11-26 01:04:02 +01:00
|
|
|
SuccessOutput(response, "", output)
|
2021-11-04 23:44:59 +01:00
|
|
|
}
|
2022-11-26 01:04:02 +01:00
|
|
|
},
|
|
|
|
}
|
2021-11-04 23:44:59 +01:00
|
|
|
|
2022-11-26 01:04:02 +01:00
|
|
|
var disableRouteCmd = &cobra.Command{
|
|
|
|
Use: "disable",
|
|
|
|
Short: "Set as disabled a given route",
|
|
|
|
Long: `This command will make as disabled a given route.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
|
|
|
routeID, err := cmd.Flags().GetUint64("route")
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
2022-11-26 01:04:02 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error getting machine id from flag: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2021-08-21 15:49:46 +02:00
|
|
|
|
2024-09-07 09:23:58 +02:00
|
|
|
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
|
2022-11-26 01:04:02 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
response, err := client.DisableRoute(ctx, &v1.DisableRouteRequest{
|
|
|
|
RouteId: routeID,
|
|
|
|
})
|
2021-11-04 23:44:59 +01:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
2023-03-06 09:05:40 +01:00
|
|
|
fmt.Sprintf("Cannot disable route %d: %s", routeID, status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if output != "" {
|
|
|
|
SuccessOutput(response, "", output)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var deleteRouteCmd = &cobra.Command{
|
|
|
|
Use: "delete",
|
|
|
|
Short: "Delete a given route",
|
|
|
|
Long: `This command will delete a given route.`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
output, _ := cmd.Flags().GetString("output")
|
|
|
|
|
|
|
|
routeID, err := cmd.Flags().GetUint64("route")
|
|
|
|
if err != nil {
|
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Error getting machine id from flag: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-09-07 09:23:58 +02:00
|
|
|
ctx, client, conn, cancel := newHeadscaleCLIWithConfig()
|
2023-03-06 09:05:40 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
response, err := client.DeleteRoute(ctx, &v1.DeleteRouteRequest{
|
|
|
|
RouteId: routeID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Cannot delete route %d: %s", routeID, status.Convert(err).Message()),
|
2021-11-13 09:36:45 +01:00
|
|
|
output,
|
|
|
|
)
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2022-11-26 01:04:02 +01:00
|
|
|
|
|
|
|
if output != "" {
|
|
|
|
SuccessOutput(response, "", output)
|
|
|
|
}
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
2021-11-04 23:44:59 +01:00
|
|
|
|
2021-11-13 09:39:04 +01:00
|
|
|
// routesToPtables converts the list of routes to a nice table.
|
2022-11-26 01:04:02 +01:00
|
|
|
func routesToPtables(routes []*v1.Route) pterm.TableData {
|
2023-09-24 13:42:05 +02:00
|
|
|
tableData := pterm.TableData{{"ID", "Node", "Prefix", "Advertised", "Enabled", "Primary"}}
|
2022-11-26 01:04:02 +01:00
|
|
|
|
|
|
|
for _, route := range routes {
|
2023-01-29 12:55:29 +01:00
|
|
|
var isPrimaryStr string
|
2023-11-23 08:31:33 +01:00
|
|
|
prefix, err := netip.ParsePrefix(route.GetPrefix())
|
2023-01-29 12:55:29 +01:00
|
|
|
if err != nil {
|
2023-11-23 08:31:33 +01:00
|
|
|
log.Printf("Error parsing prefix %s: %s", route.GetPrefix(), err)
|
2023-01-29 12:55:29 +01:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2023-05-21 18:37:59 +02:00
|
|
|
if prefix == types.ExitRouteV4 || prefix == types.ExitRouteV6 {
|
2023-01-29 12:55:29 +01:00
|
|
|
isPrimaryStr = "-"
|
|
|
|
} else {
|
2023-11-23 08:31:33 +01:00
|
|
|
isPrimaryStr = strconv.FormatBool(route.GetIsPrimary())
|
2023-01-29 12:55:29 +01:00
|
|
|
}
|
|
|
|
|
2022-11-26 01:04:02 +01:00
|
|
|
tableData = append(tableData,
|
|
|
|
[]string{
|
2023-11-23 08:31:33 +01:00
|
|
|
strconv.FormatUint(route.GetId(), Base10),
|
|
|
|
route.GetNode().GetGivenName(),
|
|
|
|
route.GetPrefix(),
|
|
|
|
strconv.FormatBool(route.GetAdvertised()),
|
|
|
|
strconv.FormatBool(route.GetEnabled()),
|
2023-01-29 12:55:29 +01:00
|
|
|
isPrimaryStr,
|
2022-11-26 01:04:02 +01:00
|
|
|
})
|
2021-11-04 23:44:59 +01:00
|
|
|
}
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-14 20:32:03 +01:00
|
|
|
return tableData
|
2021-11-04 23:44:59 +01:00
|
|
|
}
|