修复安装入口与监听启停竞态并记录清库验收
This commit is contained in:
1 parent
d2839f0f91
commit
c5ce5f06bb
9 files changed
+291
-27
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
@@ -20,8 +21,11 @@ import (
|
||||
|
||||
// 这个服务只负责 HTTP 到 HTTPS 的安全跳转,不承载业务会话。
|
||||
var httpServer *http.Server
|
||||
var httpServerMu sync.Mutex
|
||||
|
||||
func HttpStop() {
|
||||
httpServerMu.Lock()
|
||||
defer httpServerMu.Unlock()
|
||||
if httpServer != nil {
|
||||
if err := httpServer.Close(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.WithError(err).Warn("close HTTP server failed")
|
||||
@@ -276,8 +280,29 @@ func allowRouteMethod(w http.ResponseWriter, r *http.Request, allowed ...string)
|
||||
}
|
||||
|
||||
func HttpStart() error {
|
||||
server, err := prepareHTTPServer()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return serveHTTPServer(server)
|
||||
}
|
||||
|
||||
func serveHTTPServer(server *http.Server) error {
|
||||
log.WithFields(log.Fields{"action": "启动 HTTP 监听", "object": server.Addr, "result": "started"}).Info("开始绑定监听端口")
|
||||
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
return fmt.Errorf("serve HTTP: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func prepareHTTPServer() (*http.Server, error) {
|
||||
httpServerMu.Lock()
|
||||
defer httpServerMu.Unlock()
|
||||
if httpServer != nil {
|
||||
return nil, fmt.Errorf("HTTP server is already started")
|
||||
}
|
||||
if config.CurrentConfig() == nil && config.CurrentManager() == nil {
|
||||
return fmt.Errorf("runtime configuration is unavailable")
|
||||
return nil, fmt.Errorf("runtime configuration is unavailable")
|
||||
}
|
||||
cfg := config.CurrentHTTPConfig()
|
||||
httpPort := 80
|
||||
@@ -287,7 +312,7 @@ func HttpStart() error {
|
||||
|
||||
handler, err := newHTTPHandler(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
httpServer = &http.Server{
|
||||
@@ -299,11 +324,7 @@ func HttpStart() error {
|
||||
IdleTimeout: time.Second * 30,
|
||||
MaxHeaderBytes: 16 << 10,
|
||||
}
|
||||
log.WithFields(log.Fields{"action": "启动 HTTP 监听", "object": fmt.Sprintf(":%d", httpPort), "result": "started"}).Info("开始绑定监听端口")
|
||||
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
return fmt.Errorf("serve HTTP: %w", err)
|
||||
}
|
||||
return nil
|
||||
return httpServer, nil
|
||||
}
|
||||
|
||||
func newHTTPHandler(cfg config.HTTPConfig) (http.Handler, error) {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
@@ -32,6 +33,7 @@ import (
|
||||
var local embed.FS
|
||||
|
||||
var httpsServer *http.Server
|
||||
var httpsServerMu sync.Mutex
|
||||
|
||||
type nullWrite struct{}
|
||||
|
||||
@@ -41,25 +43,53 @@ func (w *nullWrite) Write(p []byte) (int, error) {
|
||||
|
||||
// HttpsStart 启动仅承载业务的 HTTPS 服务。
|
||||
func HttpsStart() error {
|
||||
server, err := prepareHTTPSServer()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return serveHTTPSServer(server)
|
||||
}
|
||||
|
||||
func serveHTTPSServer(server *http.Server) error {
|
||||
if server == nil {
|
||||
return nil
|
||||
}
|
||||
log.WithFields(log.Fields{"action": "启动 HTTPS 监听", "object": server.Addr, "result": "started"}).Info("开始绑定监听端口")
|
||||
if err := server.ListenAndServeTLS("", ""); err != nil {
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
log.WithFields(log.Fields{"action": "停止 HTTPS 监听", "object": server.Addr, "result": "success"}).Info("监听已正常关闭")
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("serve HTTPS on %s: %w", server.Addr, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func prepareHTTPSServer() (*http.Server, error) {
|
||||
httpsServerMu.Lock()
|
||||
defer httpsServerMu.Unlock()
|
||||
if httpsServer != nil {
|
||||
return nil, fmt.Errorf("HTTPS server is already started")
|
||||
}
|
||||
legacyCfg := config.CurrentConfig()
|
||||
if legacyCfg == nil && config.CurrentManager() == nil {
|
||||
return fmt.Errorf("runtime configuration is unavailable")
|
||||
return nil, fmt.Errorf("runtime configuration is unavailable")
|
||||
}
|
||||
httpCfg := config.CurrentHTTPConfig()
|
||||
if httpCfg.TLSMode == config.HTTPSTLSModeProxy {
|
||||
log.WithFields(log.Fields{"action": "启动内置 HTTPS 监听", "result": "skipped"}).Info("HTTPS 由反向代理处理,跳过内置监听")
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
if err := router(mux); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
publicRecovery, err := newPublicRecoveryHandler()
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if session.Instance == nil {
|
||||
return fmt.Errorf("session manager is unavailable")
|
||||
return nil, fmt.Errorf("session manager is unavailable")
|
||||
}
|
||||
|
||||
nullLog := olog.New(&nullWrite{}, "", olog.Ldate)
|
||||
@@ -69,14 +99,14 @@ func HttpsStart() error {
|
||||
}
|
||||
|
||||
if config.DevelopmentMode && legacyCfg != nil && legacyCfg.HttpsEnabled == 2 {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
if legacyCfg == nil {
|
||||
return fmt.Errorf("legacy SSL configuration is unavailable")
|
||||
return nil, fmt.Errorf("legacy SSL configuration is unavailable")
|
||||
}
|
||||
tlsConfig, err := config.NewRuntimeTLSConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve HTTPS certificate: %w", err)
|
||||
return nil, fmt.Errorf("resolve HTTPS certificate: %w", err)
|
||||
}
|
||||
|
||||
httpsServer = &http.Server{
|
||||
@@ -90,18 +120,12 @@ func HttpsStart() error {
|
||||
MaxHeaderBytes: 16 << 10,
|
||||
ErrorLog: nullLog,
|
||||
}
|
||||
log.WithFields(log.Fields{"action": "启动 HTTPS 监听", "object": httpsServer.Addr, "result": "started"}).Info("开始绑定监听端口")
|
||||
if err := httpsServer.ListenAndServeTLS("", ""); err != nil {
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
log.WithFields(log.Fields{"action": "停止 HTTPS 监听", "object": httpsServer.Addr, "result": "success"}).Info("监听已正常关闭")
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("serve HTTPS on port %d: %w", httpsPort, err)
|
||||
}
|
||||
return nil
|
||||
return httpsServer, nil
|
||||
}
|
||||
|
||||
func HttpsStop() {
|
||||
httpsServerMu.Lock()
|
||||
defer httpsServerMu.Unlock()
|
||||
if httpsServer != nil {
|
||||
if err := httpsServer.Close(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.WithError(err).Warn("close HTTPS server failed")
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package http_server
|
||||
|
||||
// PrepareWebServers 在提交监听任务前同步创建实例,防止停止后才发布新的监听器。
|
||||
func PrepareWebServers() (httpRun, httpsRun func() error, err error) {
|
||||
httpInstance, err := prepareHTTPServer()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
httpsInstance, err := prepareHTTPSServer()
|
||||
if err != nil {
|
||||
HttpStop()
|
||||
return nil, nil, err
|
||||
}
|
||||
return func() error { return serveHTTPServer(httpInstance) },
|
||||
func() error { return serveHTTPSServer(httpsInstance) }, nil
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package http_server
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
)
|
||||
|
||||
func TestWebListenersStopBeforeServe(t *testing.T) {
|
||||
certificateServer := httptest.NewTLSServer(http.NotFoundHandler())
|
||||
tlsConfig := certificateServer.TLS.Clone()
|
||||
certificateServer.Close()
|
||||
for _, secure := range []bool{false, true} {
|
||||
server := &http.Server{Addr: "127.0.0.1:0", TLSConfig: tlsConfig.Clone()}
|
||||
var stop func()
|
||||
var serve func(*http.Server) error
|
||||
if secure {
|
||||
httpsServer = server
|
||||
stop, serve = HttpsStop, serveHTTPSServer
|
||||
} else {
|
||||
httpServer = server
|
||||
stop, serve = HttpStop, serveHTTPServer
|
||||
}
|
||||
stop()
|
||||
if err := serve(server); err != nil {
|
||||
t.Fatalf("secure=%v: stopped listener returned %v", secure, err)
|
||||
}
|
||||
stop()
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebListenersConcurrentStop(t *testing.T) {
|
||||
certificateServer := httptest.NewTLSServer(http.NotFoundHandler())
|
||||
certificates := append([]tls.Certificate(nil), certificateServer.TLS.Certificates...)
|
||||
certificateServer.Close()
|
||||
for _, secure := range []bool{false, true} {
|
||||
server := &http.Server{Addr: "127.0.0.1:0", TLSConfig: &tls.Config{Certificates: certificates}}
|
||||
var stop func()
|
||||
var serve func(*http.Server) error
|
||||
if secure {
|
||||
httpsServer = server
|
||||
stop, serve = HttpsStop, serveHTTPSServer
|
||||
} else {
|
||||
httpServer = server
|
||||
stop, serve = HttpStop, serveHTTPServer
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- serve(server) }()
|
||||
var workers sync.WaitGroup
|
||||
for range 10 {
|
||||
workers.Add(1)
|
||||
go func() { defer workers.Done(); stop() }()
|
||||
}
|
||||
workers.Wait()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("listener did not stop")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareWebServersClosesHTTPOnHTTPSFailure(t *testing.T) {
|
||||
oldConfig, oldManager, oldDevelopment := config.Instance, config.CurrentManager(), config.DevelopmentMode
|
||||
config.SetRuntimeManager(nil)
|
||||
config.Instance = &config.Config{WebDomain: "example.com", HttpsEnabled: 1}
|
||||
config.DevelopmentMode = false
|
||||
t.Cleanup(func() {
|
||||
HttpStop()
|
||||
HttpsStop()
|
||||
config.Instance = oldConfig
|
||||
config.SetRuntimeManager(oldManager)
|
||||
config.DevelopmentMode = oldDevelopment
|
||||
})
|
||||
if _, _, err := PrepareWebServers(); err == nil {
|
||||
t.Fatal("missing HTTPS dependencies were accepted")
|
||||
}
|
||||
if httpServer != nil {
|
||||
t.Fatal("HTTP listener was retained after HTTPS preparation failed")
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package http_server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -8,8 +9,50 @@ import (
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
)
|
||||
|
||||
func TestSetupAuthenticationStatusRequiresBootstrapAuthorization(t *testing.T) {
|
||||
const token = "0123456789abcdefghijklmnop"
|
||||
for _, endpoint := range []string{"/api/user/info", "/api/v1/user/info"} {
|
||||
t.Run(endpoint, func(t *testing.T) {
|
||||
security := withSetupSecurity(t, token)
|
||||
mux := http.NewServeMux()
|
||||
registerSetupAPIRoutes(mux, security, func(http.ResponseWriter, *http.Request) { t.Fatal("setup mutation invoked") })
|
||||
for _, scenario := range []struct {
|
||||
method, source, token string
|
||||
ready bool
|
||||
status int
|
||||
}{
|
||||
{http.MethodPost, "127.0.0.1:1234", token, false, http.StatusOK},
|
||||
{http.MethodPost, "127.0.0.1:1234", "", false, http.StatusForbidden},
|
||||
{http.MethodPost, "192.0.2.1:1234", token, false, http.StatusForbidden},
|
||||
{http.MethodGet, "127.0.0.1:1234", token, false, http.StatusMethodNotAllowed},
|
||||
{http.MethodPost, "127.0.0.1:1234", token, true, http.StatusForbidden},
|
||||
} {
|
||||
config.SetInitState(scenario.ready)
|
||||
request := httptest.NewRequest(scenario.method, endpoint, nil)
|
||||
request.RemoteAddr = scenario.source
|
||||
request.AddCookie(&http.Cookie{Name: setupCookieName, Value: scenario.token})
|
||||
recorder := httptest.NewRecorder()
|
||||
mux.ServeHTTP(recorder, request)
|
||||
if recorder.Code != scenario.status {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, scenario.status)
|
||||
}
|
||||
if recorder.Code == http.StatusOK {
|
||||
var result response.Response
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &result); err != nil || result.ErrorNo != response.NeedSetup {
|
||||
t.Fatalf("setup state response = %+v, error = %v", result, err)
|
||||
}
|
||||
if recorder.Header().Get("Cache-Control") != "no-store" {
|
||||
t.Fatal("setup state can be cached")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func withSetupSecurity(t *testing.T, token string) *setupSecurity {
|
||||
t.Helper()
|
||||
oldToken := os.Getenv("SMAIL_SETUP_TOKEN")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/controllers"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/utils/ip"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cast"
|
||||
@@ -89,6 +90,14 @@ func registerSetupAPIRoutes(mux *http.ServeMux, security *setupSecurity, next ht
|
||||
authorized := security.authorize(next)
|
||||
mux.HandleFunc("/api/setup", authorized)
|
||||
mux.HandleFunc("/api/v1/setup", authorized)
|
||||
// 安装阶段尚无业务会话,通过受保护的状态接口引导前端进入安装向导。
|
||||
status := security.authorize(setupOnlyPost(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
response.NewErrorResponse(response.NeedSetup, "请先完成初始化", "").FPrint(w)
|
||||
}))
|
||||
mux.HandleFunc("/api/user/info", status)
|
||||
mux.HandleFunc("/api/v1/user/info", status)
|
||||
}
|
||||
|
||||
func setupOnlyPost(next http.HandlerFunc) http.HandlerFunc {
|
||||
|
||||
@@ -215,6 +215,11 @@ func Init(serverVersion string) error {
|
||||
}()
|
||||
}
|
||||
hooks.Init(serverVersion)
|
||||
httpRun, httpsRun, err := http_server.PrepareWebServers()
|
||||
if err != nil {
|
||||
stopServices()
|
||||
return fmt.Errorf("prepare web services: %w", err)
|
||||
}
|
||||
mailSecurity := config.CurrentMailSecurity()
|
||||
// SMTP 25 用于服务器间投递,始终保留并支持机会性 STARTTLS。
|
||||
startService(serviceErrors, "SMTP", smtp_server.Start)
|
||||
@@ -225,8 +230,8 @@ func Init(serverVersion string) error {
|
||||
startService(serviceErrors, "SMTP STARTTLS", smtp_server.StartWithTLSNew)
|
||||
}
|
||||
// http server start
|
||||
startService(serviceErrors, "HTTPS", http_server.HttpsStart)
|
||||
startService(serviceErrors, "HTTP", http_server.HttpStart)
|
||||
startService(serviceErrors, "HTTPS", httpsRun)
|
||||
startService(serviceErrors, "HTTP", httpRun)
|
||||
|
||||
// 等待 HTTP 服务启动后再执行需要 HTTP 回调的 ACME 操作。
|
||||
if err := waitHTTPReady(serviceErrors); err != nil {
|
||||
|
||||
Reference in new issue
Block a user