2021-04-28 16:15:45 +02:00
package cli
import (
2021-05-08 13:28:22 +02:00
"encoding/json"
"fmt"
2021-04-28 16:15:45 +02:00
"io"
"log"
"os"
"path/filepath"
"strings"
2021-05-23 02:15:29 +02:00
"time"
2021-04-28 16:15:45 +02:00
"github.com/juanfont/headscale"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"tailscale.com/tailcfg"
)
2021-05-08 13:28:22 +02:00
type ErrorOutput struct {
Error string
}
2021-04-28 16:15:45 +02:00
func absPath ( path string ) string {
// If a relative path is provided, prefix it with the the directory where
// the config file was found.
2021-05-18 23:33:08 +02:00
if ( path != "" ) && ! strings . HasPrefix ( path , string ( os . PathSeparator ) ) {
2021-04-28 16:15:45 +02:00
dir , _ := filepath . Split ( viper . ConfigFileUsed ( ) )
if dir != "" {
2021-05-18 23:33:08 +02:00
path = filepath . Join ( dir , path )
2021-04-28 16:15:45 +02:00
}
}
return path
}
func getHeadscaleApp ( ) ( * headscale . Headscale , error ) {
derpMap , err := loadDerpMap ( absPath ( viper . GetString ( "derp_map_path" ) ) )
if err != nil {
log . Printf ( "Could not load DERP servers map file: %s" , err )
}
2021-05-23 02:15:29 +02:00
// Minimum inactivity time out is keepalive timeout (60s) plus a few seconds
// to avoid races
minInactivityTimeout , _ := time . ParseDuration ( "65s" )
if viper . GetDuration ( "ephemeral_node_inactivity_timeout" ) <= minInactivityTimeout {
err = fmt . Errorf ( "ephemeral_node_inactivity_timeout (%s) is set too low, must be more than %s\n" , viper . GetString ( "ephemeral_node_inactivity_timeout" ) , minInactivityTimeout )
return nil , err
}
2021-04-28 16:15:45 +02:00
cfg := headscale . Config {
ServerURL : viper . GetString ( "server_url" ) ,
Addr : viper . GetString ( "listen_addr" ) ,
PrivateKeyPath : absPath ( viper . GetString ( "private_key_path" ) ) ,
DerpMap : derpMap ,
2021-05-23 02:15:29 +02:00
EphemeralNodeInactivityTimeout : viper . GetDuration ( "ephemeral_node_inactivity_timeout" ) ,
2021-05-15 14:32:26 +02:00
DBtype : viper . GetString ( "db_type" ) ,
2021-05-19 01:28:47 +02:00
DBpath : absPath ( viper . GetString ( "db_path" ) ) ,
2021-04-28 16:15:45 +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" ) ,
TLSLetsEncryptHostname : viper . GetString ( "tls_letsencrypt_hostname" ) ,
TLSLetsEncryptCacheDir : absPath ( viper . GetString ( "tls_letsencrypt_cache_dir" ) ) ,
TLSLetsEncryptChallengeType : viper . GetString ( "tls_letsencrypt_challenge_type" ) ,
TLSCertPath : absPath ( viper . GetString ( "tls_cert_path" ) ) ,
TLSKeyPath : absPath ( viper . GetString ( "tls_key_path" ) ) ,
}
h , err := headscale . NewHeadscale ( cfg )
if err != nil {
return nil , err
}
return h , nil
}
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
}
2021-05-08 13:28:22 +02:00
2021-05-08 17:06:36 +02:00
func JsonOutput ( result interface { } , errResult error , outputFormat string ) {
2021-05-08 13:28:22 +02:00
var j [ ] byte
var err error
switch outputFormat {
case "json" :
if errResult != nil {
j , err = json . MarshalIndent ( ErrorOutput { errResult . Error ( ) } , "" , "\t" )
if err != nil {
log . Fatalln ( err )
}
} else {
j , err = json . MarshalIndent ( result , "" , "\t" )
if err != nil {
log . Fatalln ( err )
}
}
case "json-line" :
if errResult != nil {
j , err = json . Marshal ( ErrorOutput { errResult . Error ( ) } )
if err != nil {
log . Fatalln ( err )
}
} else {
j , err = json . Marshal ( result )
if err != nil {
log . Fatalln ( err )
}
}
}
fmt . Println ( string ( j ) )
}