From d21a70a56d0c6630cce463d4fb53879b6dbaa26a Mon Sep 17 00:00:00 2001 From: n-holmstedt Date: Wed, 12 Jun 2024 14:18:11 +0200 Subject: [PATCH 1/3] Add kick leafnode client functionality --- server/events.go | 7 ++++++- server/server.go | 11 +++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/server/events.go b/server/events.go index a2f6c570646..4295e1ffd1e 100644 --- a/server/events.go +++ b/server/events.go @@ -2833,8 +2833,10 @@ func (s *Server) reloadConfig(sub *subscription, c *client, _ *Account, subject, }) } + type KickClientReq struct { CID uint64 `json:"cid"` + Kind string `json:"kind"` } type LDMClientReq struct { @@ -2851,10 +2853,13 @@ func (s *Server) kickClient(_ *subscription, c *client, _ *Account, subject, rep s.sys.client.Errorf("Error unmarshalling kick client request: %v", err) return } + if req.Kind == "" { + req.Kind = "Client" + } optz := &EventFilterOptions{} s.zReq(c, reply, hdr, msg, optz, optz, func() (any, error) { - return nil, s.DisconnectClientByID(req.CID) + return nil, s.DisconnectClientByID(req.CID, req.Kind) }) } diff --git a/server/server.go b/server/server.go index 426d96208fa..e8f2a70275f 100644 --- a/server/server.go +++ b/server/server.go @@ -4464,11 +4464,18 @@ func (s *Server) changeRateLimitLogInterval(d time.Duration) { } // DisconnectClientByID disconnects a client by connection ID -func (s *Server) DisconnectClientByID(id uint64) error { +func (s *Server) DisconnectClientByID(id uint64, kind string) error { if s == nil { return ErrServerNotRunning } - if client := s.getClient(id); client != nil { + var client *client + if kind == "Client" { + client = s.getClient(id) + } + if kind == "Leafnode" { + client = s.GetLeafNode(id) + } + if client != nil { client.closeConnection(Kicked) return nil } From f3f2ec7ce50bb3825bdcde57922257aac15099ef Mon Sep 17 00:00:00 2001 From: n-holmstedt Date: Wed, 12 Jun 2024 15:18:18 +0200 Subject: [PATCH 2/3] Fix gofmt --- server/events.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/events.go b/server/events.go index 4295e1ffd1e..1b99953b02f 100644 --- a/server/events.go +++ b/server/events.go @@ -2833,9 +2833,8 @@ func (s *Server) reloadConfig(sub *subscription, c *client, _ *Account, subject, }) } - type KickClientReq struct { - CID uint64 `json:"cid"` + CID uint64 `json:"cid"` Kind string `json:"kind"` } From b89e22f76ff4d22fef9c9269ea30e605007c9f2d Mon Sep 17 00:00:00 2001 From: n-holmstedt Date: Thu, 13 Jun 2024 08:57:18 +0200 Subject: [PATCH 3/3] Fallback to kicking leafnode client instead of per kind --- server/events.go | 8 ++------ server/server.go | 13 +++++-------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/server/events.go b/server/events.go index 1b99953b02f..a2f6c570646 100644 --- a/server/events.go +++ b/server/events.go @@ -2834,8 +2834,7 @@ func (s *Server) reloadConfig(sub *subscription, c *client, _ *Account, subject, } type KickClientReq struct { - CID uint64 `json:"cid"` - Kind string `json:"kind"` + CID uint64 `json:"cid"` } type LDMClientReq struct { @@ -2852,13 +2851,10 @@ func (s *Server) kickClient(_ *subscription, c *client, _ *Account, subject, rep s.sys.client.Errorf("Error unmarshalling kick client request: %v", err) return } - if req.Kind == "" { - req.Kind = "Client" - } optz := &EventFilterOptions{} s.zReq(c, reply, hdr, msg, optz, optz, func() (any, error) { - return nil, s.DisconnectClientByID(req.CID, req.Kind) + return nil, s.DisconnectClientByID(req.CID) }) } diff --git a/server/server.go b/server/server.go index e8f2a70275f..f6acf2773b7 100644 --- a/server/server.go +++ b/server/server.go @@ -4464,18 +4464,15 @@ func (s *Server) changeRateLimitLogInterval(d time.Duration) { } // DisconnectClientByID disconnects a client by connection ID -func (s *Server) DisconnectClientByID(id uint64, kind string) error { +func (s *Server) DisconnectClientByID(id uint64) error { if s == nil { return ErrServerNotRunning } - var client *client - if kind == "Client" { - client = s.getClient(id) - } - if kind == "Leafnode" { - client = s.GetLeafNode(id) + if client := s.getClient(id); client != nil { + client.closeConnection(Kicked) + return nil } - if client != nil { + if client := s.GetLeafNode(id); client != nil { client.closeConnection(Kicked) return nil }