1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00
juanfont.headscale/cmd/headscale/cli/routes.go

168 lines
4.1 KiB
Go
Raw Normal View History

package cli
import (
2021-11-04 23:44:59 +01:00
"context"
"fmt"
"log"
2021-11-04 23:44:59 +01:00
"strconv"
"time"
2021-11-04 23:44:59 +01:00
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
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")
if err != nil {
log.Fatalf(err.Error())
}
2021-11-04 23:44:59 +01:00
routesCmd.AddCommand(listRoutesCmd)
2021-11-04 23:44:59 +01:00
enableRouteCmd.Flags().StringSliceP("route", "r", []string{}, "List (or repeated flags) of routes to enable")
enableRouteCmd.Flags().Uint64P("identifier", "i", 0, "Node identifier (ID)")
err = enableRouteCmd.MarkFlagRequired("identifier")
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
nodeCmd.AddCommand(routesCmd)
2021-07-25 15:08:40 +02:00
}
var routesCmd = &cobra.Command{
Use: "routes",
Short: "Manage the routes of Headscale",
}
2021-07-25 15:08:40 +02:00
var listRoutesCmd = &cobra.Command{
2021-11-04 23:44:59 +01:00
Use: "list",
Short: "List routes advertised and enabled by a given node",
Run: func(cmd *cobra.Command, args []string) {
2021-11-04 23:44:59 +01:00
output, _ := cmd.Flags().GetString("output")
machineId, err := cmd.Flags().GetUint64("identifier")
if err != nil {
2021-11-04 23:44:59 +01:00
ErrorOutput(err, fmt.Sprintf("Error getting machine id from flag: %s", err), output)
return
}
2021-11-04 23:44:59 +01:00
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
client, conn := getHeadscaleGRPCClient(ctx)
defer conn.Close()
request := &v1.GetMachineRouteRequest{
MachineId: machineId,
}
2021-05-08 13:58:51 +02:00
2021-11-04 23:44:59 +01:00
response, err := client.GetMachineRoute(ctx, request)
if err != nil {
2021-11-04 23:44:59 +01:00
ErrorOutput(err, fmt.Sprintf("Cannot get nodes: %s", err), output)
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)
return
}
2021-05-08 13:58:51 +02:00
2021-11-04 23:44:59 +01:00
d := routesToPtables(response.Routes)
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output)
return
}
err = pterm.DefaultTable.WithHasHeader().WithData(d).Render()
if err != nil {
2021-11-04 23:44:59 +01:00
ErrorOutput(err, fmt.Sprintf("Failed to render pterm table: %s", err), output)
return
}
},
}
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")
machineId, err := cmd.Flags().GetUint64("identifier")
if err != nil {
2021-11-04 23:44:59 +01:00
ErrorOutput(err, fmt.Sprintf("Error getting machine id from flag: %s", err), output)
return
}
2021-11-04 23:44:59 +01:00
routes, err := cmd.Flags().GetStringSlice("route")
if err != nil {
2021-11-04 23:44:59 +01:00
ErrorOutput(err, fmt.Sprintf("Error getting routes from flag: %s", err), output)
return
}
2021-11-04 23:44:59 +01:00
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
client, conn := getHeadscaleGRPCClient(ctx)
defer conn.Close()
request := &v1.EnableMachineRoutesRequest{
MachineId: machineId,
Routes: routes,
}
2021-11-04 23:44:59 +01:00
response, err := client.EnableMachineRoutes(ctx, request)
if err != nil {
2021-11-04 23:44:59 +01:00
ErrorOutput(err, fmt.Sprintf("Cannot register machine: %s\n", err), output)
return
}
2021-11-04 23:44:59 +01:00
if output != "" {
SuccessOutput(response.Routes, "", output)
return
}
d := routesToPtables(response.Routes)
if err != nil {
2021-11-04 23:44:59 +01:00
ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output)
return
}
2021-11-04 23:44:59 +01:00
err = pterm.DefaultTable.WithHasHeader().WithData(d).Render()
if err != nil {
ErrorOutput(err, fmt.Sprintf("Failed to render pterm table: %s", err), output)
return
}
},
}
2021-11-04 23:44:59 +01:00
// routesToPtables converts the list of routes to a nice table
func routesToPtables(routes *v1.Routes) pterm.TableData {
d := pterm.TableData{{"Route", "Enabled"}}
for _, route := range routes.GetAdvertisedRoutes() {
enabled := isStringInSlice(routes.EnabledRoutes, route)
d = append(d, []string{route, strconv.FormatBool(enabled)})
}
return d
}
func isStringInSlice(strs []string, s string) bool {
for _, s2 := range strs {
if s == s2 {
return true
}
}
return false
}