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

2
app.go
View File

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"tailscale.com/tailcfg"
"tailscale.com/wgengine/wgcfg" "tailscale.com/wgengine/wgcfg"
) )
@ -12,6 +13,7 @@ type Config struct {
ServerURL string ServerURL string
Addr string Addr string
PrivateKeyPath string PrivateKeyPath string
DerpMap *tailcfg.DERPMap
DBhost string DBhost string
DBport int DBport int

View File

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

View File

@ -2,7 +2,7 @@
"server_url": "http://192.168.1.12:8000", "server_url": "http://192.168.1.12:8000",
"listen_addr": "0.0.0.0:8000", "listen_addr": "0.0.0.0:8000",
"private_key_path": "private.key", "private_key_path": "private.key",
"public_key_path": "public.key", "derp_map_path": "./derp.yaml",
"db_host": "localhost", "db_host": "localhost",
"db_port": 5432, "db_port": 5432,
"db_name": "headscale", "db_name": "headscale",

57
derp.yaml Normal file
View File

@ -0,0 +1,57 @@
# This file contains some of the official Tailscale DERP servers,
# shamelessly taken from https://github.com/tailscale/tailscale/blob/main/derp/derpmap/derpmap.go
#
# If you plan to somehow use headscale, please deploy your own DERP infra
regions:
1:
regionid: 1
regioncode: nyc
regionname: New York City
nodes:
- name: 1a
regionid: 1
hostname: derp1.tailscale.com
ipv4: 159.89.225.99
ipv6: "2604:a880:400:d1::828:b001"
stunport: 0
stunonly: false
derptestport: 0
2:
regionid: 2
regioncode: sfo
regionname: San Francisco
nodes:
- name: 2a
regionid: 2
hostname: derp2.tailscale.com
ipv4: 167.172.206.31
ipv6: "2604:a880:2:d1::c5:7001"
stunport: 0
stunonly: false
derptestport: 0
3:
regionid: 3
regioncode: sin
regionname: Singapore
nodes:
- name: 3a
regionid: 3
hostname: derp3.tailscale.com
ipv4: 68.183.179.66
ipv6: "2400:6180:0:d1::67d:8001"
stunport: 0
stunonly: false
derptestport: 0
4:
regionid: 4
regioncode: fra
regionname: Frankfurt
nodes:
- name: 4a
regionid: 4
hostname: derp4.tailscale.com
ipv4: 167.172.182.26
ipv6: "2a03:b0c0:3:e0::36e:900"
stunport: 0
stunonly: false
derptestport: 0

1
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/klauspost/compress v1.11.7 github.com/klauspost/compress v1.11.7
github.com/spf13/viper v1.7.1 github.com/spf13/viper v1.7.1
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
gopkg.in/yaml.v2 v2.2.8
inet.af/netaddr v0.0.0-20210203214853-aa702495c49b inet.af/netaddr v0.0.0-20210203214853-aa702495c49b
tailscale.com v1.1.1-0.20210220175347-39f7a61e9c1f tailscale.com v1.1.1-0.20210220175347-39f7a61e9c1f

View File

@ -201,7 +201,7 @@ func (h *Headscale) getMapResponse(mKey wgcfg.Key, req tailcfg.MapRequest, m Mac
SearchPaths: []string{}, SearchPaths: []string{},
Domain: "foobar@example.com", Domain: "foobar@example.com",
PacketFilter: tailcfg.FilterAllowAll, PacketFilter: tailcfg.FilterAllowAll,
DERPMap: &tailcfg.DERPMap{}, DERPMap: h.cfg.DerpMap,
UserProfiles: []tailcfg.UserProfile{}, UserProfiles: []tailcfg.UserProfile{},
Roles: []tailcfg.Role{}} Roles: []tailcfg.Role{}}