Skip to content

Commit 4183039

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 ccc895b commit 4183039

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
@@ -454,10 +454,21 @@ func (h *Headscale) ensureUnixSocketIsAbsent() error {
454454
return os.Remove(h.cfg.UnixSocket)
455455
}
456456

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

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

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

hscontrol/types/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ type Config struct {
6363
Log LogConfig
6464
DisableUpdateCheck bool
6565

66+
AccessControlAllowOrigins string
67+
6668
Database DatabaseConfig
6769

6870
DERP DERPConfig
@@ -303,6 +305,8 @@ func LoadConfig(path string, isFile bool) error {
303305
viper.SetDefault("tuning.batch_change_delay", "800ms")
304306
viper.SetDefault("tuning.node_mapsession_buffered_chan_size", 30)
305307

308+
viper.SetDefault("Access-Control-Allow-Origin", "")
309+
306310
viper.SetDefault("prefixes.allocation", string(IPAllocationStrategySequential))
307311

308312
if err := viper.ReadInConfig(); err != nil {
@@ -868,6 +872,8 @@ func LoadServerConfig() (*Config, error) {
868872
GRPCAllowInsecure: viper.GetBool("grpc_allow_insecure"),
869873
DisableUpdateCheck: false,
870874

875+
AccessControlAllowOrigins: viper.GetString("Access-Control-Allow-Origin"),
876+
871877
PrefixV4: prefix4,
872878
PrefixV6: prefix6,
873879
IPAllocation: IPAllocationStrategy(alloc),

0 commit comments

Comments
 (0)