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

CLI stuff should be in the cmd/ path

This commit is contained in:
Juan Font Alonso 2021-04-28 16:55:29 +02:00
parent 77e5255fdd
commit 7646bd8fd4
3 changed files with 71 additions and 65 deletions

64
cli.go
View File

@ -1,13 +1,9 @@
package headscale package headscale
import ( import (
"encoding/json"
"errors"
"fmt" "fmt"
"log" "log"
"github.com/jinzhu/gorm/dialects/postgres"
"inet.af/netaddr"
"tailscale.com/wgengine/wgcfg" "tailscale.com/wgengine/wgcfg"
) )
@ -51,63 +47,3 @@ func (h *Headscale) RegisterMachine(key string, namespace string) error {
fmt.Println("Machine registered 🎉") fmt.Println("Machine registered 🎉")
return nil 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")
}

View File

@ -21,11 +21,12 @@ var ListRoutesCmd = &cobra.Command{
if err != nil { if err != nil {
log.Fatalf("Error initializing: %s", err) log.Fatalf("Error initializing: %s", err)
} }
err = h.ListNodeRoutes(args[0], args[1]) routes, err := h.GetNodeRoutes(args[0], args[1])
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
fmt.Println(routes)
}, },
} }

69
routes.go Normal file
View File

@ -0,0 +1,69 @@
package headscale
import (
"encoding/json"
"errors"
"log"
"github.com/jinzhu/gorm/dialects/postgres"
"inet.af/netaddr"
)
// GetNodeRoutes returns the subnet routes advertised by a node (identified by
// namespace and node name)
func (h *Headscale) GetNodeRoutes(namespace string, nodeName string) (*[]netaddr.IPPrefix, error) {
m, err := h.GetMachine(namespace, nodeName)
if err != nil {
return nil, err
}
hi, err := m.GetHostInfo()
if err != nil {
return nil, err
}
return &hi.RoutableIPs, 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")
}