From a6100dc4d0bcb451139cbccf29f3e3f27112e736 Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Sat, 20 Feb 2021 23:57:06 +0100 Subject: [PATCH] Load DERP servers from file --- app.go | 2 ++ cmd/headscale/headscale.go | 25 +++++++++++++++++ config.json.example | 2 +- derp.yaml | 57 ++++++++++++++++++++++++++++++++++++++ go.mod | 1 + handlers.go | 2 +- 6 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 derp.yaml diff --git a/app.go b/app.go index 95d820b6..3545cd83 100644 --- a/app.go +++ b/app.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "github.com/gin-gonic/gin" + "tailscale.com/tailcfg" "tailscale.com/wgengine/wgcfg" ) @@ -12,6 +13,7 @@ type Config struct { ServerURL string Addr string PrivateKeyPath string + DerpMap *tailcfg.DERPMap DBhost string DBport int diff --git a/cmd/headscale/headscale.go b/cmd/headscale/headscale.go index a2423c43..ef895bf9 100644 --- a/cmd/headscale/headscale.go +++ b/cmd/headscale/headscale.go @@ -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 +} diff --git a/config.json.example b/config.json.example index 069b20b2..5495bd64 100644 --- a/config.json.example +++ b/config.json.example @@ -2,7 +2,7 @@ "server_url": "http://192.168.1.12:8000", "listen_addr": "0.0.0.0:8000", "private_key_path": "private.key", - "public_key_path": "public.key", + "derp_map_path": "./derp.yaml", "db_host": "localhost", "db_port": 5432, "db_name": "headscale", diff --git a/derp.yaml b/derp.yaml new file mode 100644 index 00000000..d992de82 --- /dev/null +++ b/derp.yaml @@ -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 \ No newline at end of file diff --git a/go.mod b/go.mod index 586f092c..2be6955f 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/klauspost/compress v1.11.7 github.com/spf13/viper v1.7.1 golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 + gopkg.in/yaml.v2 v2.2.8 inet.af/netaddr v0.0.0-20210203214853-aa702495c49b tailscale.com v1.1.1-0.20210220175347-39f7a61e9c1f diff --git a/handlers.go b/handlers.go index 0f24bd81..a7036041 100644 --- a/handlers.go +++ b/handlers.go @@ -201,7 +201,7 @@ func (h *Headscale) getMapResponse(mKey wgcfg.Key, req tailcfg.MapRequest, m Mac SearchPaths: []string{}, Domain: "foobar@example.com", PacketFilter: tailcfg.FilterAllowAll, - DERPMap: &tailcfg.DERPMap{}, + DERPMap: h.cfg.DerpMap, UserProfiles: []tailcfg.UserProfile{}, Roles: []tailcfg.Role{}}