From 600dc236ddabc8c2fbb1c7a0c4fa4c93c009f1ee Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Sat, 26 Oct 2024 18:43:42 +0200 Subject: [PATCH] Get status of TLS from server service (#629) * Add IsTLSEnabled function to server and add it to the API endpoint, getServers * Ignore error * Initialize variable and check for nil before returning * Initialize variable --- api/api.go | 1 + network/server.go | 29 +++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/api/api.go b/api/api.go index d394752a..e7235676 100644 --- a/api/api.go +++ b/api/api.go @@ -289,6 +289,7 @@ func (a *API) GetServers(context.Context, *emptypb.Empty) (*structpb.Struct, err "status": uint(server.Status), "tickInterval": server.TickInterval.Nanoseconds(), "loadBalancer": map[string]any{"strategy": server.LoadbalancerStrategyName}, + "isTLSEnabled": server.IsTLSEnabled(), } } diff --git a/network/server.go b/network/server.go index a6e98b88..03aa046b 100644 --- a/network/server.go +++ b/network/server.go @@ -34,6 +34,7 @@ const ( Shutdown ) +//nolint:interfacebloat type IServer interface { OnBoot() Action OnOpen(conn *ConnWrapper) ([]byte, Action) @@ -44,7 +45,10 @@ type IServer interface { Run() *gerr.GatewayDError Shutdown() IsRunning() bool + IsTLSEnabled() bool CountConnections() int + GetProxyForConnection(conn *ConnWrapper) (IProxy, bool) + RemoveConnectionFromMap(conn *ConnWrapper) } type Server struct { @@ -69,12 +73,13 @@ type Server struct { KeyFile string HandshakeTimeout time.Duration - listener net.Listener - host string - port int - connections uint32 - running *atomic.Bool - stopServer chan struct{} + listener net.Listener + host string + port int + connections uint32 + running *atomic.Bool + isTLSEnabled *atomic.Bool + stopServer chan struct{} // loadbalancer loadbalancerStrategy LoadBalancerStrategy @@ -543,6 +548,9 @@ func (s *Server) Run() *gerr.GatewayDError { return gerr.ErrCastFailed.Wrap(origErr) } + s.isTLSEnabled = &atomic.Bool{} + s.running = &atomic.Bool{} + go func(server *Server) { <-server.stopServer server.OnShutdown() @@ -582,6 +590,7 @@ func (s *Server) Run() *gerr.GatewayDError { return gerr.ErrGetTLSConfigFailed.Wrap(origErr) } s.Logger.Info().Msg("TLS is enabled") + s.isTLSEnabled.Store(true) } else { s.Logger.Debug().Msg("TLS is disabled") } @@ -767,6 +776,14 @@ func (s *Server) CountConnections() int { return int(s.connections) } +// IsTLSEnabled returns true if TLS is enabled. +func (s *Server) IsTLSEnabled() bool { + if s.isTLSEnabled == nil { + return false + } + return s.isTLSEnabled.Load() +} + // GetProxyForConnection returns the proxy associated with the given connection. func (s *Server) GetProxyForConnection(conn *ConnWrapper) (IProxy, bool) { proxy, exists := s.connectionToProxyMap.Load(conn)