2020-06-21 12:33:43 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-21 01:30:03 +01:00
|
|
|
"fmt"
|
2021-02-20 23:57:06 +01:00
|
|
|
"io"
|
2020-06-21 12:33:43 +02:00
|
|
|
"log"
|
2021-02-20 23:57:06 +01:00
|
|
|
"os"
|
2020-06-21 12:33:43 +02:00
|
|
|
|
|
|
|
"github.com/juanfont/headscale"
|
2021-02-21 01:30:03 +01:00
|
|
|
"github.com/spf13/cobra"
|
2020-06-21 12:33:43 +02:00
|
|
|
"github.com/spf13/viper"
|
2021-02-20 23:57:06 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"tailscale.com/tailcfg"
|
2020-06-21 12:33:43 +02:00
|
|
|
)
|
|
|
|
|
2021-02-21 01:30:03 +01:00
|
|
|
const version = "0.1"
|
|
|
|
|
|
|
|
var versionCmd = &cobra.Command{
|
|
|
|
Use: "version",
|
|
|
|
Short: "Print the version.",
|
|
|
|
Long: "The version of headscale.",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
fmt.Println(version)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var headscaleCmd = &cobra.Command{
|
|
|
|
Use: "headscale",
|
|
|
|
Short: "headscale - a Tailscale control server",
|
|
|
|
Long: fmt.Sprintf(`
|
|
|
|
headscale is an open source implementation of the Tailscale control server
|
|
|
|
|
|
|
|
Juan Font Alonso <juanfontalonso@gmail.com> - 2021
|
|
|
|
https://gitlab.com/juanfont/headscale`),
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
h.Serve()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var registerCmd = &cobra.Command{
|
2021-02-28 00:58:09 +01:00
|
|
|
Use: "register machineID namespace",
|
2021-02-21 01:30:03 +01:00
|
|
|
Short: "Registers a machine to your network",
|
2021-02-28 00:58:09 +01:00
|
|
|
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)
|
|
|
|
}
|
2021-02-28 20:29:31 +01:00
|
|
|
err = h.RegisterMachine(args[0], args[1])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println("Ook.")
|
2021-02-28 00:58:09 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var namespaceCmd = &cobra.Command{
|
|
|
|
Use: "namespace",
|
|
|
|
Short: "Manage the namespaces of Headscale",
|
|
|
|
}
|
|
|
|
|
|
|
|
var createNamespaceCmd = &cobra.Command{
|
|
|
|
Use: "create NAME",
|
|
|
|
Short: "Creates a new namespace",
|
2021-02-21 01:30:03 +01:00
|
|
|
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)
|
|
|
|
}
|
2021-02-28 00:58:09 +01:00
|
|
|
_, err = h.CreateNamespace(args[0])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("Ook.\n")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var listNamespacesCmd = &cobra.Command{
|
|
|
|
Use: "list",
|
2021-03-14 11:38:42 +01:00
|
|
|
Short: "List all the namespaces",
|
2021-02-28 00:58:09 +01:00
|
|
|
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)
|
|
|
|
}
|
2021-02-21 01:30:03 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-14 11:38:42 +01:00
|
|
|
var nodeCmd = &cobra.Command{
|
|
|
|
Use: "node",
|
|
|
|
Short: "Manage the nodes of Headscale",
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
err = h.ListNodeRoutes(args[0], args[1])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-21 12:33:43 +02:00
|
|
|
func main() {
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Fatal error config file: %s \n", err)
|
|
|
|
}
|
|
|
|
|
2021-02-21 01:30:03 +01:00
|
|
|
headscaleCmd.AddCommand(versionCmd)
|
|
|
|
headscaleCmd.AddCommand(serveCmd)
|
|
|
|
headscaleCmd.AddCommand(registerCmd)
|
2021-02-28 00:58:09 +01:00
|
|
|
headscaleCmd.AddCommand(namespaceCmd)
|
|
|
|
namespaceCmd.AddCommand(createNamespaceCmd)
|
|
|
|
namespaceCmd.AddCommand(listNamespacesCmd)
|
2021-02-21 01:30:03 +01:00
|
|
|
|
2021-03-14 11:38:42 +01:00
|
|
|
headscaleCmd.AddCommand(nodeCmd)
|
|
|
|
nodeCmd.AddCommand(listRoutesCmd)
|
|
|
|
nodeCmd.AddCommand(enableRouteCmd)
|
|
|
|
|
2021-02-21 01:30:03 +01:00
|
|
|
if err := headscaleCmd.Execute(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHeadscaleApp() (*headscale.Headscale, error) {
|
2021-02-20 23:57:06 +01:00
|
|
|
derpMap, err := loadDerpMap(viper.GetString("derp_map_path"))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not load DERP servers map file: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-06-21 12:33:43 +02:00
|
|
|
cfg := headscale.Config{
|
|
|
|
ServerURL: viper.GetString("server_url"),
|
|
|
|
Addr: viper.GetString("listen_addr"),
|
|
|
|
PrivateKeyPath: viper.GetString("private_key_path"),
|
2021-02-20 23:57:06 +01:00
|
|
|
DerpMap: derpMap,
|
2020-06-21 12:33:43 +02:00
|
|
|
|
|
|
|
DBhost: viper.GetString("db_host"),
|
|
|
|
DBport: viper.GetInt("db_port"),
|
|
|
|
DBname: viper.GetString("db_name"),
|
|
|
|
DBuser: viper.GetString("db_user"),
|
|
|
|
DBpass: viper.GetString("db_pass"),
|
|
|
|
}
|
|
|
|
h, err := headscale.NewHeadscale(cfg)
|
|
|
|
if err != nil {
|
2021-02-21 01:30:03 +01:00
|
|
|
return nil, err
|
2020-06-21 12:33:43 +02:00
|
|
|
}
|
2021-02-21 01:30:03 +01:00
|
|
|
return h, nil
|
2020-06-21 12:33:43 +02:00
|
|
|
}
|
2021-02-20 23:57:06 +01:00
|
|
|
|
|
|
|
func loadDerpMap(path string) (*tailcfg.DERPMap, error) {
|
|
|
|
derpFile, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer derpFile.Close()
|
|
|
|
var derpMap tailcfg.DERPMap
|
|
|
|
b, err := io.ReadAll(derpFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(b, &derpMap)
|
|
|
|
return &derpMap, err
|
|
|
|
}
|