File tree 3 files changed +24
-0
lines changed
3 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -40,6 +40,13 @@ grpc_listen_addr: 127.0.0.1:50443
40
40
# are doing.
41
41
grpc_allow_insecure : false
42
42
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
+
43
50
# The Noise section includes specific configuration for the
44
51
# TS2021 Noise protocol
45
52
noise :
Original file line number Diff line number Diff line change @@ -455,10 +455,21 @@ func (h *Headscale) ensureUnixSocketIsAbsent() error {
455
455
return os .Remove (h .cfg .UnixSocket )
456
456
}
457
457
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
+
458
465
func (h * Headscale ) createRouter (grpcMux * grpcRuntime.ServeMux ) * mux.Router {
459
466
router := mux .NewRouter ()
460
467
router .Use (prometheusMiddleware )
461
468
469
+ if h .cfg .AccessControlAllowOrigins != "" {
470
+ router .Use (h .corsHeadersMiddleware )
471
+ }
472
+
462
473
router .HandleFunc (ts2021UpgradePath , h .NoiseUpgradeHandler ).Methods (http .MethodPost , http .MethodGet )
463
474
464
475
router .HandleFunc ("/health" , h .HealthHandler ).Methods (http .MethodGet )
Original file line number Diff line number Diff line change @@ -66,6 +66,8 @@ type Config struct {
66
66
Log LogConfig
67
67
DisableUpdateCheck bool
68
68
69
+ AccessControlAllowOrigins string
70
+
69
71
Database DatabaseConfig
70
72
71
73
DERP DERPConfig
@@ -332,6 +334,8 @@ func LoadConfig(path string, isFile bool) error {
332
334
viper .SetDefault ("tuning.batch_change_delay" , "800ms" )
333
335
viper .SetDefault ("tuning.node_mapsession_buffered_chan_size" , 30 )
334
336
337
+ viper .SetDefault ("access_control_allow_origin" , "" )
338
+
335
339
viper .SetDefault ("prefixes.allocation" , string (IPAllocationStrategySequential ))
336
340
337
341
if err := viper .ReadInConfig (); err != nil {
@@ -903,6 +907,8 @@ func LoadServerConfig() (*Config, error) {
903
907
GRPCAllowInsecure : viper .GetBool ("grpc_allow_insecure" ),
904
908
DisableUpdateCheck : false ,
905
909
910
+ AccessControlAllowOrigins : viper .GetString ("access_control_allow_origin" ),
911
+
906
912
PrefixV4 : prefix4 ,
907
913
PrefixV6 : prefix6 ,
908
914
IPAllocation : IPAllocationStrategy (alloc ),
You can’t perform that action at this time.
0 commit comments