2021-04-28 16:15:45 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
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"
|
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
|
|
|
)
|
|
|
|
|
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)")
|
|
|
|
err := listRoutesCmd.MarkFlagRequired("identifier")
|
2021-07-25 16:26:15 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-11-04 23:44:59 +01:00
|
|
|
routesCmd.AddCommand(listRoutesCmd)
|
2021-08-21 16:04:30 +02:00
|
|
|
|
2021-11-13 09:36:45 +01:00
|
|
|
enableRouteCmd.Flags().
|
|
|
|
StringSliceP("route", "r", []string{}, "List (or repeated flags) of routes to enable")
|
2021-11-04 23:44:59 +01:00
|
|
|
enableRouteCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
|
|
|
|
err = enableRouteCmd.MarkFlagRequired("identifier")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-08-21 16:04:30 +02:00
|
|
|
|
2021-07-25 15:08:40 +02:00
|
|
|
routesCmd.AddCommand(enableRouteCmd)
|
2021-11-04 23:44:59 +01:00
|
|
|
|
|
|
|
nodeCmd.AddCommand(routesCmd)
|
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",
|
|
|
|
Short: "List routes advertised and enabled by a given node",
|
|
|
|
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-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
return
|
2021-04-30 00:23:26 +02:00
|
|
|
}
|
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:44:59 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
request := &v1.GetMachineRouteRequest{
|
2021-11-15 18:24:24 +01:00
|
|
|
MachineId: machineID,
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2021-05-08 13:58:51 +02:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
response, err := client.GetMachineRoute(ctx, request)
|
2021-08-21 15:49:46 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Cannot get nodes: %s", status.Convert(err).Message()),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-05-08 13:58:51 +02:00
|
|
|
return
|
|
|
|
}
|
2021-05-08 17:06:36 +02:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
if output != "" {
|
|
|
|
SuccessOutput(response.Routes, "", output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
return
|
|
|
|
}
|
2021-05-08 13:58:51 +02:00
|
|
|
|
2021-11-14 20:32:03 +01:00
|
|
|
tableData := routesToPtables(response.Routes)
|
2021-11-04 23:44:59 +01:00
|
|
|
if err != nil {
|
|
|
|
ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
return
|
|
|
|
}
|
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-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
return
|
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",
|
|
|
|
Short: "Set the enabled routes for a given node",
|
|
|
|
Long: `This command will take a list of routes that will _replace_
|
|
|
|
the current set of routes on a given node.
|
|
|
|
If you would like to disable a route, simply run the command again, but
|
|
|
|
omit the route you do not want to enable.
|
|
|
|
`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
output, _ := cmd.Flags().GetString("output")
|
2021-11-15 18:24:24 +01:00
|
|
|
|
|
|
|
machineID, err := cmd.Flags().GetUint64("identifier")
|
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-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
return
|
2021-08-21 16:04:30 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
routes, err := cmd.Flags().GetStringSlice("route")
|
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 routes from flag: %s", err),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
return
|
2021-04-30 00:23:26 +02:00
|
|
|
}
|
2021-08-21 16:04:30 +02:00
|
|
|
|
2021-11-07 10:41:14 +01:00
|
|
|
ctx, client, conn, cancel := getHeadscaleCLIClient()
|
2021-11-04 23:44:59 +01:00
|
|
|
defer cancel()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
request := &v1.EnableMachineRoutesRequest{
|
2021-11-15 18:24:24 +01:00
|
|
|
MachineId: machineID,
|
2021-11-04 23:44:59 +01:00
|
|
|
Routes: routes,
|
|
|
|
}
|
2021-04-30 00:23:26 +02:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
response, err := client.EnableMachineRoutes(ctx, request)
|
2021-08-21 16:04:30 +02:00
|
|
|
if err != nil {
|
2021-11-13 09:36:45 +01:00
|
|
|
ErrorOutput(
|
|
|
|
err,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Cannot register machine: %s\n",
|
|
|
|
status.Convert(err).Message(),
|
|
|
|
),
|
|
|
|
output,
|
|
|
|
)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
return
|
2021-08-21 16:04:30 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
if output != "" {
|
|
|
|
SuccessOutput(response.Routes, "", output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-14 20:32:03 +01:00
|
|
|
tableData := routesToPtables(response.Routes)
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
2021-11-04 23:44:59 +01:00
|
|
|
ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output)
|
2021-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
return
|
2021-04-28 16:15:45 +02:00
|
|
|
}
|
2021-08-21 15:49:46 +02:00
|
|
|
|
2021-11-14 20:32:03 +01:00
|
|
|
err = pterm.DefaultTable.WithHasHeader().WithData(tableData).Render()
|
2021-11-04 23:44:59 +01: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-11-14 16:46:09 +01:00
|
|
|
|
2021-11-04 23:44:59 +01:00
|
|
|
return
|
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.
|
2021-11-04 23:44:59 +01:00
|
|
|
func routesToPtables(routes *v1.Routes) pterm.TableData {
|
2021-11-14 20:32:03 +01:00
|
|
|
tableData := pterm.TableData{{"Route", "Enabled"}}
|
2021-11-04 23:44:59 +01:00
|
|
|
|
|
|
|
for _, route := range routes.GetAdvertisedRoutes() {
|
|
|
|
enabled := isStringInSlice(routes.EnabledRoutes, route)
|
|
|
|
|
2021-11-14 20:32:03 +01:00
|
|
|
tableData = append(tableData, []string{route, strconv.FormatBool(enabled)})
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func isStringInSlice(strs []string, s string) bool {
|
|
|
|
for _, s2 := range strs {
|
|
|
|
if s == s2 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|