2020-06-21 10:33:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-20 22:57:06 +00:00
|
|
|
"io"
|
2020-06-21 10:33:43 +00:00
|
|
|
"log"
|
2021-02-20 22:57:06 +00:00
|
|
|
"os"
|
2020-06-21 10:33:43 +00:00
|
|
|
|
|
|
|
"github.com/juanfont/headscale"
|
|
|
|
"github.com/spf13/viper"
|
2021-02-20 22:57:06 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"tailscale.com/tailcfg"
|
2020-06-21 10:33:43 +00: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-20 22:57:06 +00: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 10:33:43 +00:00
|
|
|
cfg := headscale.Config{
|
|
|
|
ServerURL: viper.GetString("server_url"),
|
|
|
|
Addr: viper.GetString("listen_addr"),
|
|
|
|
PrivateKeyPath: viper.GetString("private_key_path"),
|
2021-02-20 22:57:06 +00:00
|
|
|
DerpMap: derpMap,
|
2020-06-21 10:33:43 +00: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 {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
h.Serve()
|
|
|
|
}
|
2021-02-20 22:57:06 +00: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
|
|
|
|
}
|