修复初始化完成跳转与服务退出竞态
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-08-30 20:26:19 +08:00
1 parent e86f60a429
commit 78ae5cb17b
4 files changed
+202 -96

No files matched your search

+35 -9
View File
@@ -40,9 +40,40 @@ func AcmeChallenge(w http.ResponseWriter, r *http.Request) {
}
type sslResponse struct {
Port int `json:"port"`
Type string `json:"type"`
Ready bool `json:"ready"`
Port int `json:"port"`
HTTPPort int `json:"httpPort"`
HTTPSPort int `json:"httpsPort"`
TLSMode string `json:"tlsMode"`
HTTPSEnabled bool `json:"httpsEnabled"`
WebDomain string `json:"webDomain"`
Type string `json:"type"`
Ready bool `json:"ready"`
}
func buildSetupSSLResponse(sslType string, cfg *config.Config, httpConfig config.HTTPConfig, webDomain string, ready bool) sslResponse {
httpPort := httpConfig.Port
if httpPort <= 0 && cfg != nil {
httpPort = cfg.GetSetupPort()
}
httpsPort := httpConfig.HTTPSPort
if httpsPort <= 0 {
httpsPort = 443
}
return sslResponse{
Port: httpPort,
HTTPPort: httpPort,
HTTPSPort: httpsPort,
TLSMode: httpConfig.TLSMode,
HTTPSEnabled: httpConfig.HTTPSEnabled,
WebDomain: webDomain,
Type: sslType,
Ready: ready,
}
}
func currentSetupSSLResponse(sslType string) sslResponse {
cfg := config.CurrentConfig()
return buildSetupSSLResponse(sslType, cfg, config.CurrentHTTPConfig(), config.CurrentWebDomain(), config.IsSetupReady())
}
func writeSetupError(ctx *context.Context, w http.ResponseWriter, err error) {
@@ -163,12 +194,7 @@ func Setup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
writeSetupError(ctx, w, err)
return
}
res := sslResponse{
Type: sslType,
Port: config.CurrentConfig().GetSetupPort(),
Ready: config.IsSetupReady(),
}
response.NewSuccessResponse(res).FPrint(w)
response.NewSuccessResponse(currentSetupSSLResponse(sslType)).FPrint(w)
return
}
+40
View File
@@ -6,6 +6,7 @@ import (
"strings"
"testing"
"git.pcux.cn/xuans/Smail/config"
appcontext "git.pcux.cn/xuans/Smail/utils/context"
)
@@ -32,3 +33,42 @@ func TestSetupReturnsJSONForUnsupportedOperation(t *testing.T) {
t.Fatalf("unexpected setup response: %+v", payload)
}
}
func TestBuildSetupSSLResponseUsesConfiguredProxyPorts(t *testing.T) {
cfg := &config.Config{HttpPort: 9001}
response := buildSetupSSLResponse(
config.SSLTypeUser,
cfg,
config.HTTPConfig{
Port: 9001,
HTTPSPort: 9002,
TLSMode: config.HTTPSTLSModeProxy,
HTTPSEnabled: false,
},
"xuans.com",
true,
)
if response.HTTPPort != 9001 || response.HTTPSPort != 9002 {
t.Fatalf("unexpected configured ports: http=%d https=%d", response.HTTPPort, response.HTTPSPort)
}
if response.TLSMode != config.HTTPSTLSModeProxy || response.HTTPSEnabled {
t.Fatalf("unexpected proxy settings: mode=%q enabled=%v", response.TLSMode, response.HTTPSEnabled)
}
if response.WebDomain != "xuans.com" || !response.Ready {
t.Fatalf("unexpected setup response identity: domain=%q ready=%v", response.WebDomain, response.Ready)
}
}
func TestBuildSetupSSLResponseDefaultsHTTPSPort(t *testing.T) {
response := buildSetupSSLResponse(
config.SSLTypeAutoHTTP,
&config.Config{HttpPort: 9001},
config.HTTPConfig{Port: 9001},
"mail.example.com",
false,
)
if response.HTTPPort != 9001 || response.HTTPSPort != 443 {
t.Fatalf("unexpected default ports: http=%d https=%d", response.HTTPPort, response.HTTPSPort)
}
}