@@ -140,8 +140,10 @@ type Server struct {
140140 currentPath string
141141
142142 // wsRoutes tracks registered websocket upgrade paths.
143- wsRouteMu sync.Mutex
144- wsRoutes map [string ]struct {}
143+ wsRouteMu sync.Mutex
144+ wsRoutes map [string ]struct {}
145+ wsAuthChanged func (bool , bool )
146+ wsAuthEnabled atomic.Bool
145147
146148 // management handler
147149 mgmt * managementHandlers.Handler
@@ -235,6 +237,7 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk
235237 envManagementSecret : envManagementSecret ,
236238 wsRoutes : make (map [string ]struct {}),
237239 }
240+ s .wsAuthEnabled .Store (cfg .WebsocketAuth )
238241 // Save initial YAML snapshot
239242 s .oldConfigYaml , _ = yaml .Marshal (cfg )
240243 s .applyAccessConfig (nil , cfg )
@@ -398,10 +401,20 @@ func (s *Server) AttachWebsocketRoute(path string, handler http.Handler) {
398401 s .wsRoutes [trimmed ] = struct {}{}
399402 s .wsRouteMu .Unlock ()
400403
401- s .engine .GET (trimmed , func (c * gin.Context ) {
404+ authMiddleware := AuthMiddleware (s .accessManager )
405+ conditionalAuth := func (c * gin.Context ) {
406+ if ! s .wsAuthEnabled .Load () {
407+ c .Next ()
408+ return
409+ }
410+ authMiddleware (c )
411+ }
412+ finalHandler := func (c * gin.Context ) {
402413 handler .ServeHTTP (c .Writer , c .Request )
403414 c .Abort ()
404- })
415+ }
416+
417+ s .engine .GET (trimmed , conditionalAuth , finalHandler )
405418}
406419
407420func (s * Server ) registerManagementRoutes () {
@@ -803,6 +816,10 @@ func (s *Server) UpdateClients(cfg *config.Config) {
803816
804817 s .applyAccessConfig (oldCfg , cfg )
805818 s .cfg = cfg
819+ s .wsAuthEnabled .Store (cfg .WebsocketAuth )
820+ if oldCfg != nil && s .wsAuthChanged != nil && oldCfg .WebsocketAuth != cfg .WebsocketAuth {
821+ s .wsAuthChanged (oldCfg .WebsocketAuth , cfg .WebsocketAuth )
822+ }
806823 managementasset .SetCurrentConfig (cfg )
807824 // Save YAML snapshot for next comparison
808825 s .oldConfigYaml , _ = yaml .Marshal (cfg )
@@ -843,6 +860,13 @@ func (s *Server) UpdateClients(cfg *config.Config) {
843860 )
844861}
845862
863+ func (s * Server ) SetWebsocketAuthChangeHandler (fn func (bool , bool )) {
864+ if s == nil {
865+ return
866+ }
867+ s .wsAuthChanged = fn
868+ }
869+
846870// (management handlers moved to internal/api/handlers/management)
847871
848872// AuthMiddleware returns a Gin middleware handler that authenticates requests
0 commit comments