Skip to content

Commit 68ce697

Browse files
committed
Headscale: Added an option to set an Access-Control-Allow-Origin response header to enable Cross-Origin Resource Sharing (CORS)
1 parent e172c29 commit 68ce697

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

config-example.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ grpc_listen_addr: 127.0.0.1:50443
4040
# are doing.
4141
grpc_allow_insecure: false
4242

43+
# The Access-Control-Allow-Origin header specifies which origins are allowed to access resources.
44+
# Options:
45+
# - "*" to allow access from any origin (not recommended for sensitive data).
46+
# - "http://example.com" to only allow access from a specific origin.
47+
# - "" to disable Cross-Origin Resource Sharing (CORS).
48+
access_control_allow_origin: ""
49+
4350
# The Noise section includes specific configuration for the
4451
# TS2021 Noise protocol
4552
noise:

hscontrol/app.go

+11
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,21 @@ func (h *Headscale) ensureUnixSocketIsAbsent() error {
455455
return os.Remove(h.cfg.UnixSocket)
456456
}
457457

458+
func (h *Headscale) corsHeadersMiddleware(next http.Handler) http.Handler {
459+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
460+
w.Header().Set("Access-Control-Allow-Origin", h.cfg.AccessControlAllowOrigins)
461+
next.ServeHTTP(w, r)
462+
})
463+
}
464+
458465
func (h *Headscale) createRouter(grpcMux *grpcRuntime.ServeMux) *mux.Router {
459466
router := mux.NewRouter()
460467
router.Use(prometheusMiddleware)
461468

469+
if h.cfg.AccessControlAllowOrigins != "" {
470+
router.Use(h.corsHeadersMiddleware)
471+
}
472+
462473
router.HandleFunc(ts2021UpgradePath, h.NoiseUpgradeHandler).Methods(http.MethodPost, http.MethodGet)
463474

464475
router.HandleFunc("/health", h.HealthHandler).Methods(http.MethodGet)

hscontrol/types/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ type Config struct {
6666
Log LogConfig
6767
DisableUpdateCheck bool
6868

69+
AccessControlAllowOrigins string
70+
6971
Database DatabaseConfig
7072

7173
DERP DERPConfig
@@ -332,6 +334,8 @@ func LoadConfig(path string, isFile bool) error {
332334
viper.SetDefault("tuning.batch_change_delay", "800ms")
333335
viper.SetDefault("tuning.node_mapsession_buffered_chan_size", 30)
334336

337+
viper.SetDefault("access_control_allow_origin", "")
338+
335339
viper.SetDefault("prefixes.allocation", string(IPAllocationStrategySequential))
336340

337341
if err := viper.ReadInConfig(); err != nil {
@@ -903,6 +907,8 @@ func LoadServerConfig() (*Config, error) {
903907
GRPCAllowInsecure: viper.GetBool("grpc_allow_insecure"),
904908
DisableUpdateCheck: false,
905909

910+
AccessControlAllowOrigins: viper.GetString("access_control_allow_origin"),
911+
906912
PrefixV4: prefix4,
907913
PrefixV6: prefix6,
908914
IPAllocation: IPAllocationStrategy(alloc),

0 commit comments

Comments
 (0)