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 @@ -454,10 +454,21 @@ func (h *Headscale) ensureUnixSocketIsAbsent() error {
454
454
return os .Remove (h .cfg .UnixSocket )
455
455
}
456
456
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
+
457
464
func (h * Headscale ) createRouter (grpcMux * grpcRuntime.ServeMux ) * mux.Router {
458
465
router := mux .NewRouter ()
459
466
router .Use (prometheusMiddleware )
460
467
468
+ if h .cfg .AccessControlAllowOrigins != "" {
469
+ router .Use (h .corsHeadersMiddleware )
470
+ }
471
+
461
472
router .HandleFunc (ts2021UpgradePath , h .NoiseUpgradeHandler ).Methods (http .MethodPost , http .MethodGet )
462
473
463
474
router .HandleFunc ("/health" , h .HealthHandler ).Methods (http .MethodGet )
Original file line number Diff line number Diff line change @@ -63,6 +63,8 @@ type Config struct {
63
63
Log LogConfig
64
64
DisableUpdateCheck bool
65
65
66
+ AccessControlAllowOrigins string
67
+
66
68
Database DatabaseConfig
67
69
68
70
DERP DERPConfig
@@ -303,6 +305,8 @@ func LoadConfig(path string, isFile bool) error {
303
305
viper .SetDefault ("tuning.batch_change_delay" , "800ms" )
304
306
viper .SetDefault ("tuning.node_mapsession_buffered_chan_size" , 30 )
305
307
308
+ viper .SetDefault ("Access-Control-Allow-Origin" , "" )
309
+
306
310
viper .SetDefault ("prefixes.allocation" , string (IPAllocationStrategySequential ))
307
311
308
312
if err := viper .ReadInConfig (); err != nil {
@@ -868,6 +872,8 @@ func LoadServerConfig() (*Config, error) {
868
872
GRPCAllowInsecure : viper .GetBool ("grpc_allow_insecure" ),
869
873
DisableUpdateCheck : false ,
870
874
875
+ AccessControlAllowOrigins : viper .GetString ("Access-Control-Allow-Origin" ),
876
+
871
877
PrefixV4 : prefix4 ,
872
878
PrefixV6 : prefix6 ,
873
879
IPAllocation : IPAllocationStrategy (alloc ),
You can’t perform that action at this time.
0 commit comments