mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-23 18:15:26 +00:00
CLI stuff should be in the cmd/ path
This commit is contained in:
parent
77e5255fdd
commit
7646bd8fd4
64
cli.go
64
cli.go
@ -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"
|
||||
)
|
||||
|
||||
@ -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")
|
||||
}
|
||||
|
@ -21,11 +21,12 @@ var ListRoutesCmd = &cobra.Command{
|
||||
if err != nil {
|
||||
log.Fatalf("Error initializing: %s", err)
|
||||
}
|
||||
err = h.ListNodeRoutes(args[0], args[1])
|
||||
routes, err := h.GetNodeRoutes(args[0], args[1])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(routes)
|
||||
},
|
||||
}
|
||||
|
||||
|
69
routes.go
Normal file
69
routes.go
Normal 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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user