Load DERP servers from file

This commit is contained in:
Juan Font Alonso 2021-02-20 23:57:06 +01:00
parent c5fbc5baa8
commit a6100dc4d0
6 changed files with 87 additions and 2 deletions

View file

@ -1,10 +1,14 @@
package main
import (
"io"
"log"
"os"
"github.com/juanfont/headscale"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"tailscale.com/tailcfg"
)
func main() {
@ -16,10 +20,16 @@ func main() {
log.Fatalf("Fatal error config file: %s \n", err)
}
derpMap, err := loadDerpMap(viper.GetString("derp_map_path"))
if err != nil {
log.Printf("Could not load DERP servers map file: %s", err)
}
cfg := headscale.Config{
ServerURL: viper.GetString("server_url"),
Addr: viper.GetString("listen_addr"),
PrivateKeyPath: viper.GetString("private_key_path"),
DerpMap: derpMap,
DBhost: viper.GetString("db_host"),
DBport: viper.GetInt("db_port"),
@ -33,3 +43,18 @@ func main() {
}
h.Serve()
}
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
}