2020-06-21 10:33:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-27 00:30:06 +00:00
|
|
|
"errors"
|
2021-02-21 00:30:03 +00:00
|
|
|
"fmt"
|
2020-06-21 10:33:43 +00:00
|
|
|
"log"
|
2021-02-20 22:57:06 +00:00
|
|
|
"os"
|
2021-04-23 01:10:50 +00:00
|
|
|
"strings"
|
2020-06-21 10:33:43 +00:00
|
|
|
|
2021-04-28 14:15:45 +00:00
|
|
|
"github.com/juanfont/headscale/cmd/headscale/cli"
|
2021-02-21 00:30:03 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-06-21 10:33:43 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2021-04-25 14:21:04 +00:00
|
|
|
var version = "dev"
|
2021-02-21 00:30:03 +00:00
|
|
|
|
|
|
|
var versionCmd = &cobra.Command{
|
|
|
|
Use: "version",
|
|
|
|
Short: "Print the version.",
|
|
|
|
Long: "The version of headscale.",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-05-08 11:28:22 +00:00
|
|
|
o, _ := cmd.Flags().GetString("output")
|
2021-05-08 15:06:36 +00:00
|
|
|
if strings.HasPrefix(o, "json") {
|
|
|
|
cli.JsonOutput(map[string]string{"version": version}, nil, o)
|
|
|
|
return
|
2021-05-08 11:28:22 +00:00
|
|
|
}
|
2021-05-08 15:06:36 +00:00
|
|
|
fmt.Println(version)
|
2021-02-21 00:30:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var headscaleCmd = &cobra.Command{
|
|
|
|
Use: "headscale",
|
|
|
|
Short: "headscale - a Tailscale control server",
|
2021-04-24 15:41:29 +00:00
|
|
|
Long: `
|
2021-02-21 00:30:03 +00:00
|
|
|
headscale is an open source implementation of the Tailscale control server
|
|
|
|
|
|
|
|
Juan Font Alonso <juanfontalonso@gmail.com> - 2021
|
2021-04-24 15:41:29 +00:00
|
|
|
https://gitlab.com/juanfont/headscale`,
|
2021-02-21 00:30:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 00:30:06 +00:00
|
|
|
func loadConfig(path string) error {
|
2020-06-21 10:33:43 +00:00
|
|
|
viper.SetConfigName("config")
|
2021-04-25 15:24:42 +00:00
|
|
|
if path == "" {
|
|
|
|
viper.AddConfigPath("/etc/headscale/")
|
|
|
|
viper.AddConfigPath("$HOME/.headscale")
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
} else {
|
|
|
|
// For testing
|
|
|
|
viper.AddConfigPath(path)
|
|
|
|
}
|
2020-06-21 10:33:43 +00:00
|
|
|
viper.AutomaticEnv()
|
2021-04-24 02:54:15 +00:00
|
|
|
|
|
|
|
viper.SetDefault("tls_letsencrypt_cache_dir", "/var/www/.cache")
|
|
|
|
viper.SetDefault("tls_letsencrypt_challenge_type", "HTTP-01")
|
|
|
|
|
2020-06-21 10:33:43 +00:00
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
2021-04-30 01:03:15 +00:00
|
|
|
return fmt.Errorf("Fatal error reading config file: %s \n", err)
|
2020-06-21 10:33:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 00:30:06 +00:00
|
|
|
// Collect any validation errors and return them all at once
|
|
|
|
var errorText string
|
2021-04-24 02:54:15 +00:00
|
|
|
if (viper.GetString("tls_letsencrypt_hostname") != "") && ((viper.GetString("tls_cert_path") != "") || (viper.GetString("tls_key_path") != "")) {
|
2021-04-27 00:30:06 +00:00
|
|
|
errorText += "Fatal config error: set either tls_letsencrypt_hostname or tls_cert_path/tls_key_path, not both\n"
|
2021-04-24 02:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (viper.GetString("tls_letsencrypt_hostname") != "") && (viper.GetString("tls_letsencrypt_challenge_type") == "TLS-ALPN-01") && (!strings.HasSuffix(viper.GetString("listen_addr"), ":443")) {
|
2021-04-27 00:30:06 +00:00
|
|
|
errorText += "Fatal config error: when using tls_letsencrypt_hostname with TLS-ALPN-01 as challenge type, listen_addr must end in :443\n"
|
2021-04-24 02:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (viper.GetString("tls_letsencrypt_challenge_type") != "HTTP-01") && (viper.GetString("tls_letsencrypt_challenge_type") != "TLS-ALPN-01") {
|
2021-04-27 00:30:06 +00:00
|
|
|
errorText += "Fatal config error: the only supported values for tls_letsencrypt_challenge_type are HTTP-01 and TLS-ALPN-01\n"
|
2021-04-24 02:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(viper.GetString("server_url"), "http://") && !strings.HasPrefix(viper.GetString("server_url"), "https://") {
|
2021-04-27 00:30:06 +00:00
|
|
|
errorText += "Fatal config error: server_url must start with https:// or http://\n"
|
|
|
|
}
|
|
|
|
if errorText != "" {
|
|
|
|
return errors.New(strings.TrimSuffix(errorText, "\n"))
|
|
|
|
} else {
|
|
|
|
return nil
|
2021-04-24 02:54:15 +00:00
|
|
|
}
|
2021-04-25 15:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-04-27 00:30:06 +00:00
|
|
|
err := loadConfig("")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-04-24 02:54:15 +00:00
|
|
|
|
2021-04-28 14:15:45 +00:00
|
|
|
headscaleCmd.AddCommand(cli.NamespaceCmd)
|
|
|
|
headscaleCmd.AddCommand(cli.NodeCmd)
|
2021-04-29 22:23:26 +00:00
|
|
|
headscaleCmd.AddCommand(cli.PreauthkeysCmd)
|
|
|
|
headscaleCmd.AddCommand(cli.RoutesCmd)
|
|
|
|
headscaleCmd.AddCommand(cli.ServeCmd)
|
|
|
|
headscaleCmd.AddCommand(versionCmd)
|
|
|
|
|
|
|
|
cli.NodeCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace")
|
2021-04-30 07:55:39 +00:00
|
|
|
err = cli.NodeCmd.MarkPersistentFlagRequired("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-04-29 22:23:26 +00:00
|
|
|
|
|
|
|
cli.PreauthkeysCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace")
|
2021-04-30 07:55:39 +00:00
|
|
|
err = cli.PreauthkeysCmd.MarkPersistentFlagRequired("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-04-29 22:23:26 +00:00
|
|
|
|
|
|
|
cli.RoutesCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace")
|
2021-04-30 07:55:39 +00:00
|
|
|
err = cli.RoutesCmd.MarkPersistentFlagRequired("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-04-22 22:25:01 +00:00
|
|
|
|
2021-04-28 14:15:45 +00:00
|
|
|
cli.NamespaceCmd.AddCommand(cli.CreateNamespaceCmd)
|
|
|
|
cli.NamespaceCmd.AddCommand(cli.ListNamespacesCmd)
|
2021-05-09 15:12:05 +00:00
|
|
|
cli.NamespaceCmd.AddCommand(cli.DestroyNamespaceCmd)
|
2021-02-21 00:30:03 +00:00
|
|
|
|
2021-05-01 18:05:10 +00:00
|
|
|
cli.NodeCmd.AddCommand(cli.ListNodesCmd)
|
|
|
|
cli.NodeCmd.AddCommand(cli.RegisterCmd)
|
|
|
|
|
2021-04-29 22:23:26 +00:00
|
|
|
cli.RoutesCmd.AddCommand(cli.ListRoutesCmd)
|
|
|
|
cli.RoutesCmd.AddCommand(cli.EnableRouteCmd)
|
2021-03-14 10:38:42 +00:00
|
|
|
|
2021-04-28 14:15:45 +00:00
|
|
|
cli.PreauthkeysCmd.AddCommand(cli.ListPreAuthKeys)
|
|
|
|
cli.PreauthkeysCmd.AddCommand(cli.CreatePreAuthKeyCmd)
|
2021-04-29 22:23:26 +00:00
|
|
|
|
2021-04-28 14:15:45 +00:00
|
|
|
cli.CreatePreAuthKeyCmd.PersistentFlags().Bool("reusable", false, "Make the preauthkey reusable")
|
2021-05-23 00:15:29 +00:00
|
|
|
cli.CreatePreAuthKeyCmd.PersistentFlags().Bool("ephemeral", false, "Preauthkey for ephemeral nodes")
|
2021-04-28 14:15:45 +00:00
|
|
|
cli.CreatePreAuthKeyCmd.Flags().StringP("expiration", "e", "", "Human-readable expiration of the key (30m, 24h, 365d...)")
|
2021-04-22 22:25:01 +00:00
|
|
|
|
2021-05-08 11:28:22 +00:00
|
|
|
headscaleCmd.PersistentFlags().StringP("output", "o", "", "Output format. Empty for human-readable, 'json' or 'json-line'")
|
|
|
|
|
2021-02-21 00:30:03 +00:00
|
|
|
if err := headscaleCmd.Execute(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
}
|