2022-02-23 14:34:16 +00:00
|
|
|
package server
|
2022-02-10 17:19:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-06-15 14:49:31 +00:00
|
|
|
"net/url"
|
2022-02-10 17:19:34 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2024-09-26 22:37:32 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/common"
|
2022-02-10 17:19:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2022-10-12 10:00:14 +00:00
|
|
|
isRunning bool
|
|
|
|
server *http.Server
|
|
|
|
logger *zap.Logger
|
|
|
|
cert *tls.Certificate
|
|
|
|
hostname string
|
|
|
|
handlers HandlerPatternMap
|
2023-02-15 15:50:30 +00:00
|
|
|
|
2022-10-12 10:00:14 +00:00
|
|
|
portManger
|
2023-02-15 15:50:30 +00:00
|
|
|
*timeoutManager
|
2022-02-10 17:19:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 12:15:39 +00:00
|
|
|
func NewServer(cert *tls.Certificate, hostname string, afterPortChanged func(int), logger *zap.Logger) Server {
|
2022-10-12 10:00:14 +00:00
|
|
|
return Server{
|
2023-02-15 15:50:30 +00:00
|
|
|
logger: logger,
|
|
|
|
cert: cert,
|
|
|
|
hostname: hostname,
|
|
|
|
portManger: newPortManager(logger.Named("Server"), afterPortChanged),
|
|
|
|
timeoutManager: newTimeoutManager(),
|
2022-10-12 10:00:14 +00:00
|
|
|
}
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
2022-02-10 17:19:34 +00:00
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
func (s *Server) getHost() string {
|
2022-10-12 12:51:13 +00:00
|
|
|
return fmt.Sprintf("%s:%d", s.hostname, s.GetPort())
|
|
|
|
}
|
|
|
|
|
2023-01-06 12:21:14 +00:00
|
|
|
func (s *Server) GetHostname() string {
|
|
|
|
return s.hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetCert() *tls.Certificate {
|
|
|
|
return s.cert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetLogger() *zap.Logger {
|
|
|
|
return s.logger
|
|
|
|
}
|
|
|
|
|
2022-10-12 12:51:13 +00:00
|
|
|
func (s *Server) mustGetHost() string {
|
|
|
|
return fmt.Sprintf("%s:%d", s.hostname, s.MustGetPort())
|
2022-02-10 17:19:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) listenAndServe() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2022-02-10 17:19:34 +00:00
|
|
|
|
2024-11-19 09:31:29 +00:00
|
|
|
var listener net.Listener
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if s.cert != nil {
|
|
|
|
// HTTPS mode
|
|
|
|
cfg := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{*s.cert},
|
|
|
|
ServerName: s.hostname,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
listener, err = tls.Listen("tcp", s.getHost(), cfg)
|
|
|
|
} else {
|
|
|
|
// HTTP mode
|
|
|
|
listener, err = net.Listen("tcp", s.getHost())
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:19:34 +00:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("failed to start server, retrying", zap.Error(err))
|
2022-10-12 10:00:14 +00:00
|
|
|
s.ResetPort()
|
2022-02-10 17:19:34 +00:00
|
|
|
err = s.Start()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("server start failed, giving up", zap.Error(err))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-12 10:00:14 +00:00
|
|
|
err = s.SetPort(listener.Addr().(*net.TCPAddr).Port)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("failed to set Server.port", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.isRunning = true
|
2022-06-15 14:49:31 +00:00
|
|
|
|
2023-02-15 15:50:30 +00:00
|
|
|
s.StartTimeout(func() {
|
|
|
|
err := s.Stop()
|
|
|
|
if err != nil {
|
2023-03-21 12:36:34 +00:00
|
|
|
s.logger.Error("server termination fail", zap.Error(err))
|
2023-02-15 15:50:30 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-02-10 17:19:34 +00:00
|
|
|
err = s.server.Serve(listener)
|
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
s.logger.Error("server failed unexpectedly, restarting", zap.Error(err))
|
|
|
|
err = s.Start()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("server start failed, giving up", zap.Error(err))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-10-12 10:00:14 +00:00
|
|
|
s.isRunning = false
|
2022-02-10 17:19:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
func (s *Server) resetServer() {
|
2023-02-15 15:50:30 +00:00
|
|
|
s.StopTimeout()
|
2022-06-15 14:49:31 +00:00
|
|
|
s.server = new(http.Server)
|
2022-10-12 10:00:14 +00:00
|
|
|
s.ResetPort()
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
2022-05-09 13:07:57 +00:00
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
func (s *Server) applyHandlers() {
|
|
|
|
if s.server == nil {
|
|
|
|
s.server = new(http.Server)
|
|
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
2022-02-10 17:19:34 +00:00
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
for p, h := range s.handlers {
|
|
|
|
mux.HandleFunc(p, h)
|
|
|
|
}
|
|
|
|
s.server.Handler = mux
|
|
|
|
}
|
2022-02-10 17:19:34 +00:00
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
func (s *Server) Start() error {
|
|
|
|
// Once Shutdown has been called on a server, it may not be reused;
|
|
|
|
s.resetServer()
|
|
|
|
s.applyHandlers()
|
|
|
|
go s.listenAndServe()
|
2022-02-10 17:19:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
2023-02-15 15:50:30 +00:00
|
|
|
s.StopTimeout()
|
2022-02-10 17:19:34 +00:00
|
|
|
if s.server != nil {
|
|
|
|
return s.server.Shutdown(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-15 15:50:30 +00:00
|
|
|
func (s *Server) IsRunning() bool {
|
|
|
|
return s.isRunning
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:19:34 +00:00
|
|
|
func (s *Server) ToForeground() {
|
2022-10-12 10:00:14 +00:00
|
|
|
if !s.isRunning && (s.server != nil) {
|
2022-02-10 17:19:34 +00:00
|
|
|
err := s.Start()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("server start failed during foreground transition", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ToBackground() {
|
2022-10-12 10:00:14 +00:00
|
|
|
if s.isRunning {
|
2022-02-10 17:19:34 +00:00
|
|
|
err := s.Stop()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("server stop failed during background transition", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 14:49:31 +00:00
|
|
|
|
|
|
|
func (s *Server) SetHandlers(handlers HandlerPatternMap) {
|
|
|
|
s.handlers = handlers
|
|
|
|
}
|
|
|
|
|
2022-08-06 13:26:16 +00:00
|
|
|
func (s *Server) AddHandlers(handlers HandlerPatternMap) {
|
|
|
|
if s.handlers == nil {
|
|
|
|
s.handlers = make(HandlerPatternMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name := range handlers {
|
|
|
|
s.handlers[name] = handlers[name]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
func (s *Server) MakeBaseURL() *url.URL {
|
|
|
|
return &url.URL{
|
|
|
|
Scheme: "https",
|
2022-10-12 12:51:13 +00:00
|
|
|
Host: s.mustGetHost(),
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
}
|