2021-04-28 16:15:45 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-07-17 00:23:12 +02:00
|
|
|
"strconv"
|
2021-05-08 13:28:22 +02:00
|
|
|
"strings"
|
Fix nil dereference in nodes list command.
Fixes a nil pointer dereference observed when listing nodes that have
not yet connected.
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xb931a4]
goroutine 1 [running]:
github.com/juanfont/headscale/cmd/headscale/cli.glob..func8(0x13c93e0, 0xc0004c4220, 0x0, 0x2)
/go/src/headscale/cmd/headscale/cli/nodes.go:74 +0x364
github.com/spf13/cobra.(*Command).execute(0x13c93e0, 0xc0004c41e0, 0x2, 0x2, 0x13c93e0, 0xc0004c41e0)
/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:856 +0x2c2
github.com/spf13/cobra.(*Command).ExecuteC(0x13ca2e0, 0xc000497110, 0xe76416, 0x6)
/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:960 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:897
main.main()
/go/src/headscale/cmd/headscale/headscale.go:89 +0x805
command terminated with exit code 2
```
2021-06-20 01:18:13 +02:00
|
|
|
"time"
|
2021-04-28 16:15:45 +02:00
|
|
|
|
2021-07-17 11:09:42 +02:00
|
|
|
survey "github.com/AlecAivazis/survey/v2"
|
2021-04-28 16:15:45 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2021-06-28 20:04:05 +02:00
|
|
|
var NodeCmd = &cobra.Command{
|
|
|
|
Use: "nodes",
|
|
|
|
Short: "Manage the nodes of Headscale",
|
|
|
|
}
|
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
var RegisterCmd = &cobra.Command{
|
2021-04-30 00:23:26 +02:00
|
|
|
Use: "register machineID",
|
2021-04-28 16:15:45 +02:00
|
|
|
Short: "Registers a machine to your network",
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
2021-04-30 00:23:26 +02:00
|
|
|
if len(args) < 1 {
|
2021-04-28 16:15:45 +02:00
|
|
|
return fmt.Errorf("Missing parameters")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-04-30 00:23:26 +02:00
|
|
|
n, err := cmd.Flags().GetString("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error getting namespace: %s", err)
|
|
|
|
}
|
2021-05-08 13:28:22 +02:00
|
|
|
o, _ := cmd.Flags().GetString("output")
|
2021-04-30 00:23:26 +02:00
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
h, err := getHeadscaleApp()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing: %s", err)
|
|
|
|
}
|
2021-05-08 13:28:22 +02:00
|
|
|
m, err := h.RegisterMachine(args[0], n)
|
|
|
|
if strings.HasPrefix(o, "json") {
|
2021-05-08 17:06:36 +02:00
|
|
|
JsonOutput(m, err, o)
|
2021-05-08 13:28:22 +02:00
|
|
|
return
|
|
|
|
}
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
2021-05-08 13:28:22 +02:00
|
|
|
fmt.Printf("Cannot register machine: %s\n", err)
|
2021-04-28 16:15:45 +02:00
|
|
|
return
|
|
|
|
}
|
2021-05-08 13:28:22 +02:00
|
|
|
fmt.Printf("Machine registered\n")
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-05-01 20:00:25 +02:00
|
|
|
var ListNodesCmd = &cobra.Command{
|
|
|
|
Use: "list",
|
|
|
|
Short: "List the nodes in a given namespace",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
n, err := cmd.Flags().GetString("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error getting namespace: %s", err)
|
|
|
|
}
|
2021-05-08 13:58:51 +02:00
|
|
|
o, _ := cmd.Flags().GetString("output")
|
2021-05-01 20:00:25 +02:00
|
|
|
|
|
|
|
h, err := getHeadscaleApp()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing: %s", err)
|
|
|
|
}
|
|
|
|
machines, err := h.ListMachinesInNamespace(n)
|
2021-05-08 13:58:51 +02:00
|
|
|
if strings.HasPrefix(o, "json") {
|
2021-05-08 17:06:36 +02:00
|
|
|
JsonOutput(machines, err, o)
|
2021-05-08 13:58:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-01 20:00:25 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error getting nodes: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-07-17 00:23:12 +02:00
|
|
|
fmt.Printf("ID\tname\t\tlast seen\t\tephemeral\n")
|
2021-05-01 20:00:25 +02:00
|
|
|
for _, m := range *machines {
|
2021-05-23 15:55:15 +02:00
|
|
|
var ephemeral bool
|
|
|
|
if m.AuthKey != nil && m.AuthKey.Ephemeral {
|
|
|
|
ephemeral = true
|
|
|
|
}
|
Fix nil dereference in nodes list command.
Fixes a nil pointer dereference observed when listing nodes that have
not yet connected.
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xb931a4]
goroutine 1 [running]:
github.com/juanfont/headscale/cmd/headscale/cli.glob..func8(0x13c93e0, 0xc0004c4220, 0x0, 0x2)
/go/src/headscale/cmd/headscale/cli/nodes.go:74 +0x364
github.com/spf13/cobra.(*Command).execute(0x13c93e0, 0xc0004c41e0, 0x2, 0x2, 0x13c93e0, 0xc0004c41e0)
/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:856 +0x2c2
github.com/spf13/cobra.(*Command).ExecuteC(0x13ca2e0, 0xc000497110, 0xe76416, 0x6)
/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:960 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:897
main.main()
/go/src/headscale/cmd/headscale/headscale.go:89 +0x805
command terminated with exit code 2
```
2021-06-20 01:18:13 +02:00
|
|
|
var lastSeen time.Time
|
|
|
|
if m.LastSeen != nil {
|
|
|
|
lastSeen = *m.LastSeen
|
|
|
|
}
|
2021-07-17 00:23:12 +02:00
|
|
|
fmt.Printf("%d\t%s\t%s\t%t\n", m.ID, m.Name, lastSeen.Format("2006-01-02 15:04:05"), ephemeral)
|
2021-05-01 20:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
2021-07-17 00:23:12 +02:00
|
|
|
|
|
|
|
var DeleteCmd = &cobra.Command{
|
|
|
|
Use: "delete ID",
|
|
|
|
Short: "Delete a node",
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) < 1 {
|
|
|
|
return fmt.Errorf("Missing parameters")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
h, err := getHeadscaleApp()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing: %s", err)
|
|
|
|
}
|
|
|
|
id, err := strconv.Atoi(args[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error converting ID to integer: %s", err)
|
|
|
|
}
|
|
|
|
m, err := h.GetMachineByID(uint64(id))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error getting node: %s", err)
|
|
|
|
}
|
2021-07-17 11:09:42 +02:00
|
|
|
|
|
|
|
confirm := false
|
|
|
|
prompt := &survey.Confirm{
|
|
|
|
Message: fmt.Sprintf("Do you want to remove the node %s?", m.Name),
|
|
|
|
}
|
2021-07-17 11:17:42 +02:00
|
|
|
err = survey.AskOne(prompt, &confirm)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-07-17 11:09:42 +02:00
|
|
|
|
|
|
|
if confirm {
|
|
|
|
err = h.DeleteMachine(m)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error deleting node: %s", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Node deleted\n")
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Node not deleted\n")
|
2021-07-17 00:23:12 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|