Skip to content

Commit 252c68c

Browse files
committed
Add HTTPS support for the web endpoint with manually configured
certificate/key files.
1 parent c5a3d0b commit 252c68c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

app.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package headscale
33
import (
44
"fmt"
55
"os"
6+
"strings"
67
"sync"
78

89
"github.com/gin-gonic/gin"
@@ -22,6 +23,9 @@ type Config struct {
2223
DBname string
2324
DBuser string
2425
DBpass string
26+
27+
TLSCertPath string
28+
TLSKeyPath string
2529
}
2630

2731
// Headscale represents the base app of the service
@@ -68,6 +72,17 @@ func (h *Headscale) Serve() error {
6872
r.GET("/register", h.RegisterWebAPI)
6973
r.POST("/machine/:id/map", h.PollNetMapHandler)
7074
r.POST("/machine/:id", h.RegistrationHandler)
71-
err := r.Run(h.cfg.Addr)
75+
var err error
76+
if h.cfg.TLSCertPath == "" {
77+
if !strings.HasPrefix(h.cfg.ServerURL, "http://") {
78+
fmt.Println("WARNING: listening without TLS but ServerURL does not start with http://")
79+
}
80+
err = r.Run(h.cfg.Addr)
81+
} else {
82+
if !strings.HasPrefix(h.cfg.ServerURL, "https://") {
83+
fmt.Println("WARNING: listening with TLS but ServerURL does not start with https://")
84+
}
85+
err = r.RunTLS(h.cfg.Addr, h.cfg.TLSCertPath, h.cfg.TLSKeyPath)
86+
}
7287
return err
7388
}

cmd/headscale/headscale.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ func getHeadscaleApp() (*headscale.Headscale, error) {
311311
DBname: viper.GetString("db_name"),
312312
DBuser: viper.GetString("db_user"),
313313
DBpass: viper.GetString("db_pass"),
314+
315+
TLSCertPath: absPath(viper.GetString("tls_cert_path")),
316+
TLSKeyPath: absPath(viper.GetString("tls_key_path")),
314317
}
318+
315319
h, err := headscale.NewHeadscale(cfg)
316320
if err != nil {
317321
return nil, err

config.json.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
"db_port": 5432,
88
"db_name": "headscale",
99
"db_user": "foo",
10-
"db_pass": "bar"
10+
"db_pass": "bar",
11+
"tls_cert_path": "",
12+
"tls_key_path": ""
1113
}

0 commit comments

Comments
 (0)