Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions cli.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package headscale

import (
"encoding/json"
"errors"
"fmt"
"log"

"github.com/jinzhu/gorm/dialects/postgres"
"inet.af/netaddr"
"tailscale.com/wgengine/wgcfg"
)

Expand Down Expand Up @@ -51,63 +47,3 @@ func (h *Headscale) RegisterMachine(key string, namespace string) error {
fmt.Println("Machine registered 🎉")
return nil
}

// ListNodeRoutes prints the subnet routes advertised by a node (identified by
// namespace and node name)
func (h *Headscale) ListNodeRoutes(namespace string, nodeName string) error {
m, err := h.GetMachine(namespace, nodeName)
if err != nil {
return err
}

hi, err := m.GetHostInfo()
if err != nil {
return err
}
fmt.Println(hi.RoutableIPs)
return nil
}

// EnableNodeRoute enables a subnet route advertised by a node (identified by
// namespace and node name)
func (h *Headscale) EnableNodeRoute(namespace string, nodeName string, routeStr string) error {
m, err := h.GetMachine(namespace, nodeName)
if err != nil {
return err
}
hi, err := m.GetHostInfo()
if err != nil {
return err
}
route, err := netaddr.ParseIPPrefix(routeStr)
if err != nil {
return err
}

for _, rIP := range hi.RoutableIPs {
if rIP == route {
db, err := h.db()
if err != nil {
log.Printf("Cannot open DB: %s", err)
return err
}

routes, _ := json.Marshal([]string{routeStr}) // TODO: only one for the time being, so overwriting the rest
m.EnabledRoutes = postgres.Jsonb{RawMessage: json.RawMessage(routes)}
db.Save(&m)
db.Close()

peers, _ := h.getPeers(*m)
h.pollMu.Lock()
for _, p := range *peers {
if pUp, ok := h.clientsPolling[uint64(p.ID)]; ok {
pUp <- []byte{}
}
}
h.pollMu.Unlock()
return nil
}
}

return errors.New("could not find routable range")
}
56 changes: 56 additions & 0 deletions cmd/headscale/cli/namespaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cli

import (
"fmt"
"log"

"github.com/spf13/cobra"
)

var NamespaceCmd = &cobra.Command{
Use: "namespace",
Short: "Manage the namespaces of Headscale",
}

var CreateNamespaceCmd = &cobra.Command{
Use: "create NAME",
Short: "Creates a new namespace",
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)
}
_, err = h.CreateNamespace(args[0])
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Ook.\n")
},
}

var ListNamespacesCmd = &cobra.Command{
Use: "list",
Short: "List all the namespaces",
Run: func(cmd *cobra.Command, args []string) {
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
ns, err := h.ListNamespaces()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("ID\tName\n")
for _, n := range *ns {
fmt.Printf("%d\t%s\n", n.ID, n.Name)
}
},
}
36 changes: 36 additions & 0 deletions cmd/headscale/cli/nodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cli

import (
"fmt"
"log"

"github.com/spf13/cobra"
)

var RegisterCmd = &cobra.Command{
Use: "register machineID namespace",
Short: "Registers a machine to your network",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
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)
}
err = h.RegisterMachine(args[0], args[1])
if err != nil {
fmt.Printf("Error: %s", err)
return
}
fmt.Println("Ook.")
},
}

var NodeCmd = &cobra.Command{
Use: "node",
Short: "Manage the nodes of Headscale",
}
83 changes: 83 additions & 0 deletions cmd/headscale/cli/preauthkeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cli

import (
"fmt"
"log"
"time"

"github.com/hako/durafmt"
"github.com/spf13/cobra"
)

var PreauthkeysCmd = &cobra.Command{
Use: "preauthkey",
Short: "Handle the preauthkeys in Headscale",
}

var ListPreAuthKeys = &cobra.Command{
Use: "list NAMESPACE",
Short: "List the preauthkeys for this namespace",
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)
}
keys, err := h.GetPreAuthKeys(args[0])
if err != nil {
fmt.Println(err)
return
}
for _, k := range *keys {
fmt.Printf(
"key: %s, namespace: %s, reusable: %v, expiration: %s, created_at: %s\n",
k.Key,
k.Namespace.Name,
k.Reusable,
k.Expiration.Format("2006-01-02 15:04:05"),
k.CreatedAt.Format("2006-01-02 15:04:05"),
)
}
},
}

var CreatePreAuthKeyCmd = &cobra.Command{
Use: "create NAMESPACE",
Short: "Creates a new preauthkey in the specified namespace",
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)
}
reusable, _ := cmd.Flags().GetBool("reusable")

e, _ := cmd.Flags().GetString("expiration")
var expiration *time.Time
if e != "" {
duration, err := durafmt.ParseStringShort(e)
if err != nil {
log.Fatalf("Error parsing expiration: %s", err)
}
exp := time.Now().UTC().Add(duration.Duration())
expiration = &exp
}

_, err = h.CreatePreAuthKey(args[0], reusable, expiration)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Ook.\n")
},
}
53 changes: 53 additions & 0 deletions cmd/headscale/cli/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cli

import (
"fmt"
"log"

"github.com/spf13/cobra"
)

var ListRoutesCmd = &cobra.Command{
Use: "list-routes NAMESPACE NODE",
Short: "List the routes exposed by this node",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
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)
}
routes, err := h.GetNodeRoutes(args[0], args[1])
if err != nil {
fmt.Println(err)
return
}
fmt.Println(routes)
},
}

var EnableRouteCmd = &cobra.Command{
Use: "enable-route",
Short: "Allows exposing a route declared by this node to the rest of the nodes",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 3 {
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)
}
err = h.EnableNodeRoute(args[0], args[1], args[2])
if err != nil {
fmt.Println(err)
return
}
},
}
25 changes: 25 additions & 0 deletions cmd/headscale/cli/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cli

import (
"log"

"github.com/spf13/cobra"
)

var ServeCmd = &cobra.Command{
Use: "serve",
Short: "Launches the headscale server",
Args: func(cmd *cobra.Command, args []string) error {
return nil
},
Run: func(cmd *cobra.Command, args []string) {
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
err = h.Serve()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
},
}
Loading