Skip to content

Commit

Permalink
Get status of TLS from server service (#629)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mostafa authored Oct 26, 2024
1 parent 37fec61 commit 600dc23
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
29 changes: 23 additions & 6 deletions network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
Shutdown
)

//nolint:interfacebloat
type IServer interface {
OnBoot() Action
OnOpen(conn *ConnWrapper) ([]byte, Action)
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 600dc23

Please sign in to comment.