-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add an embedded DERP server to Headscale #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 20 commits
897d480
9d43f58
23cde84
607c1eb
22d2443
09d78c7
758b1ba
df37d1a
b742379
88378c2
e9eb90f
992efbd
237f7f1
e78c002
54c3e00
70910c4
dc909ba
eb50015
eb06054
de2ea83
e1fcf0d
b47de07
580db9b
a27b386
b3fa66d
05df8e9
15ed713
03452a8
dd26cbd
cc0c88a
b41d899
05c5e22
e5d22b8
bdbf620
b803240
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,10 +120,16 @@ type OIDCConfig struct { | |
| } | ||
|
|
||
| type DERPConfig struct { | ||
| URLs []url.URL | ||
| Paths []string | ||
| AutoUpdate bool | ||
| UpdateFrequency time.Duration | ||
| ServerEnabled bool | ||
| ServerRegionID int | ||
| ServerRegionCode string | ||
| ServerRegionName string | ||
| STUNEnabled bool | ||
| STUNAddr string | ||
|
Comment on lines
+123
to
+128
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we split this into At some point I want to have a go at getting rid of all the manual reading of options from viper, I think it can do that... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe look into using HCL struct based parsing? |
||
| URLs []url.URL | ||
| Paths []string | ||
| AutoUpdate bool | ||
| UpdateFrequency time.Duration | ||
| } | ||
|
|
||
| type CLIConfig struct { | ||
|
|
@@ -142,7 +148,8 @@ type Headscale struct { | |
| dbDebug bool | ||
| privateKey *key.MachinePrivate | ||
|
|
||
| DERPMap *tailcfg.DERPMap | ||
| DERPMap *tailcfg.DERPMap | ||
| DERPServer *DERPServer | ||
|
|
||
| aclPolicy *ACLPolicy | ||
| aclRules []tailcfg.FilterRule | ||
|
|
@@ -178,7 +185,6 @@ func LookupTLSClientAuthMode(mode string) (tls.ClientAuthType, bool) { | |
| } | ||
| } | ||
|
|
||
| // NewHeadscale returns the Headscale app. | ||
| func NewHeadscale(cfg Config) (*Headscale, error) { | ||
| privKey, err := readOrCreatePrivateKey(cfg.PrivateKeyPath) | ||
| if err != nil { | ||
|
|
@@ -239,6 +245,14 @@ func NewHeadscale(cfg Config) (*Headscale, error) { | |
| } | ||
| } | ||
|
|
||
| if cfg.DERP.ServerEnabled { | ||
| embeddedDERPServer, err := app.NewDERPServer() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| app.DERPServer = embeddedDERPServer | ||
| } | ||
|
|
||
| return &app, nil | ||
| } | ||
|
|
||
|
|
@@ -463,6 +477,12 @@ func (h *Headscale) createRouter(grpcMux *runtime.ServeMux) *gin.Engine { | |
| router.GET("/swagger", SwaggerUI) | ||
| router.GET("/swagger/v1/openapiv2.json", SwaggerAPIv1) | ||
|
|
||
| if h.cfg.DERP.ServerEnabled { | ||
| router.Any("/derp", h.DERPHandler) | ||
| router.Any("/derp/probe", h.DERPProbeHandler) | ||
| router.Any("/bootstrap-dns", h.DERPBootstrapDNSHandler) | ||
| } | ||
|
|
||
| api := router.Group("/api") | ||
| api.Use(h.httpAuthenticationMiddleware) | ||
| { | ||
|
|
@@ -481,6 +501,13 @@ func (h *Headscale) Serve() error { | |
| // Fetch an initial DERP Map before we start serving | ||
| h.DERPMap = GetDERPMap(h.cfg.DERP) | ||
|
|
||
| if h.cfg.DERP.ServerEnabled { | ||
| h.DERPMap.Regions[h.DERPServer.region.RegionID] = &h.DERPServer.region | ||
| if h.cfg.DERP.STUNEnabled { | ||
| go h.ServeSTUN() | ||
| } | ||
| } | ||
|
|
||
| if h.cfg.DERP.AutoUpdate { | ||
| derpMapCancelChannel := make(chan struct{}) | ||
| defer func() { derpMapCancelChannel <- struct{}{} }() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,26 @@ ip_prefixes: | |
| # headscale needs a list of DERP servers that can be presented | ||
| # to the clients. | ||
| derp: | ||
| server: | ||
| # If enabled, runs the embedded DERP server and merges it into the rest of the DERP config | ||
| # The Headscale server_url defined above MUST be using https, DERP requires TLS to be in place | ||
| enabled: false | ||
|
|
||
| # Region ID to use for the embedded DERP server. | ||
| # The local DERP prevails if the region ID collides with other region ID coming from | ||
| # the regular DERP config. | ||
| region_id: 999 | ||
|
|
||
| # Region code and name are displayed in the Tailscale UI to identify a DERP region | ||
| region_code: "headscale" | ||
| region_name: "Headscale Embedded DERP" | ||
|
|
||
| # If enabled, also listens in the configured address for STUN connections to help on NAT traversal | ||
| # For more details on how this works, check this great article: https://tailscale.com/blog/how-tailscale-works/ | ||
| stun: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could possible call out that this is UDP, so people know what to open in firewall. |
||
| enabled: false | ||
| listen_addr: "0.0.0.0:3478" | ||
|
|
||
| # List of externally available DERP maps encoded in JSON | ||
| urls: | ||
| - https://controlplane.tailscale.com/derpmap/default | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.