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 @@ -440,10 +440,21 @@ func (h *Headscale) ensureUnixSocketIsAbsent() error {
440
440
return os .Remove (h .cfg .UnixSocket )
441
441
}
442
442
443
+ func (h * Headscale ) corsHeadersMiddleware (next http.Handler ) http.Handler {
444
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
445
+ w .Header ().Set ("Access-Control-Allow-Origin" , h .cfg .AccessControlAllowOrigins )
446
+ next .ServeHTTP (w , r )
447
+ })
448
+ }
449
+
443
450
func (h * Headscale ) createRouter (grpcMux * grpcRuntime.ServeMux ) * mux.Router {
444
451
router := mux .NewRouter ()
445
452
router .Use (prometheusMiddleware )
446
453
454
+ if h .cfg .AccessControlAllowOrigins != "" {
455
+ router .Use (h .corsHeadersMiddleware )
456
+ }
457
+
447
458
router .HandleFunc (ts2021UpgradePath , h .NoiseUpgradeHandler ).Methods (http .MethodPost , http .MethodGet )
448
459
449
460
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
@@ -292,6 +294,8 @@ func LoadConfig(path string, isFile bool) error {
292
294
viper .SetDefault ("tuning.batch_change_delay" , "800ms" )
293
295
viper .SetDefault ("tuning.node_mapsession_buffered_chan_size" , 30 )
294
296
297
+ viper .SetDefault ("Access-Control-Allow-Origin" , "" )
298
+
295
299
viper .SetDefault ("prefixes.allocation" , string (IPAllocationStrategySequential ))
296
300
297
301
if err := viper .ReadInConfig (); err != nil {
@@ -852,6 +856,8 @@ func LoadServerConfig() (*Config, error) {
852
856
GRPCAllowInsecure : viper .GetBool ("grpc_allow_insecure" ),
853
857
DisableUpdateCheck : false ,
854
858
859
+ AccessControlAllowOrigins : viper .GetString ("Access-Control-Allow-Origin" ),
860
+
855
861
PrefixV4 : prefix4 ,
856
862
PrefixV6 : prefix6 ,
857
863
IPAllocation : IPAllocationStrategy (alloc ),
You can’t perform that action at this time.
0 commit comments