反代模式停用自动证书签发续期并完善初始化提示与测试
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-06 13:27:06 +08:00
1 parent 7ffb1e751d
commit 369f502c10
20 files changed
+599 -56

No files matched your search

+2
View File
@@ -31,6 +31,7 @@ type PublicIssue struct {
type ConfigStatus struct {
SetupState SetupState `json:"setupState"`
TLSMode string `json:"tlsMode"`
ActiveGeneration uint64 `json:"activeGeneration"`
LastValidAt *time.Time `json:"lastValidAt,omitempty"`
PendingRestart []ServiceName `json:"pendingRestart,omitempty"`
@@ -248,6 +249,7 @@ func (m *ConfigManager) Status() ConfigStatus {
m.mu.RLock()
defer m.mu.RUnlock()
status := m.status
status.TLSMode = m.current.Root.HTTP.TLSMode
status.PendingRestart = append([]ServiceName(nil), status.PendingRestart...)
status.DegradedReasons = append([]PublicIssue(nil), status.DegradedReasons...)
if status.LastValidAt != nil {
+2 -1
View File
@@ -145,7 +145,8 @@ func sameSSLPolicy(left, right *Config) bool {
return reflect.DeepEqual(left.SSL, right.SSL) && left.SSLType == right.SSLType &&
left.SSLPrivateKeyPath == right.SSLPrivateKeyPath && left.SSLPublicKeyPath == right.SSLPublicKeyPath &&
left.Domain == right.Domain && left.WebDomain == right.WebDomain &&
reflect.DeepEqual(left.Domains, right.Domains) && left.HttpsEnabled == right.HttpsEnabled
reflect.DeepEqual(left.Domains, right.Domains) && left.HttpsEnabled == right.HttpsEnabled &&
left.TLSMode == right.TLSMode
}
func mergeServices(previous, next []ServiceName) []ServiceName {
+13
View File
@@ -0,0 +1,13 @@
package config
import "strings"
// UsesProxyHTTPS 表示网页 HTTPS 由反向代理终止,不代表邮件协议可以关闭 TLS。
func (c *Config) UsesProxyHTTPS() bool {
return c != nil && strings.EqualFold(strings.TrimSpace(c.TLSMode), HTTPSTLSModeProxy)
}
// AutomaticSSLEnabled 保留原证书策略,但反代模式下禁止 Smail 自动签发和续期。
func (c *Config) AutomaticSSLEnabled() bool {
return c != nil && !c.UsesProxyHTTPS() && (c.SSLType == SSLTypeAutoHTTP || c.SSLType == SSLTypeAutoDNS)
}
+1
View File
@@ -33,6 +33,7 @@ func ConfigStatus(ctx *appcontext.Context, w http.ResponseWriter, req *http.Requ
response.NewSuccessResponse(map[string]any{
"available": true,
"setupState": status.SetupState,
"tlsMode": status.TLSMode,
"activeGeneration": status.ActiveGeneration,
"lastValidAt": status.LastValidAt,
"pendingRestart": status.PendingRestart,
+38 -35
View File
@@ -28,64 +28,63 @@ func Start() {
}
}
cfg := config.CurrentConfig()
if cfg != nil && (cfg.SSLType == config.SSLTypeAutoHTTP || cfg.SSLType == config.SSLTypeAutoDNS) {
go sslUpdateLoop()
} else {
go sslCheck()
}
go sslUpdateLoop()
}
// 每天检查一遍SSL证书是否更新,更新就重启
// 只检查外部维护的本地邮件证书是否更新,不触发 ACME。
func sslCheck() {
var err error
_, expiredTime, _, err = ssl.CheckSSLCrtInfo()
_, newExpTime, _, err := ssl.CheckSSLCrtInfo()
if err != nil {
log.Errorf("SSL Check Error! %+v", err)
expiredTime = time.Time{}
return
}
for {
time.Sleep(24 * time.Hour)
_, newExpTime, _, err := ssl.CheckSSLCrtInfo()
if err != nil {
log.Errorf("SSL Check Error! %+v", err)
continue
}
if expiredTime.IsZero() {
expiredTime = newExpTime
continue
}
if newExpTime != expiredTime {
expiredTime = newExpTime
log.Infoln("SSL certificate had update! restarting")
signal.RestartChan <- true
}
if expiredTime.IsZero() {
expiredTime = newExpTime
return
}
if newExpTime != expiredTime {
expiredTime = newExpTime
log.Infoln("SSL certificate had update! restarting")
signal.RestartChan <- true
}
}
// 每天检查一遍SSL证书是否即将过期,即将过期就重新生成
// 每轮读取最新策略,切换反代模式后不再等待 HTTP 挑战或执行自动续期。
func maintainSSL() {
cfg := config.CurrentConfig()
if cfg == nil || !cfg.IsSetupReady() {
return
}
if !cfg.AutomaticSSLEnabled() {
sslCheck()
return
}
expiredTime = time.Time{}
if cfg.SSLType == config.SSLTypeAutoHTTP && !waitHTTPReady() {
return
}
ssl.Update(true)
}
// 每天检查证书,外部管理模式仅保留本地证书变更检查。
func sslUpdateLoop() {
for {
// 等待 HTTP 就绪后再执行 ACME 续期,避免挑战失败
waitHTTPReady()
ssl.Update(true)
maintainSSL()
// 每24小时检测一次证书有效期
time.Sleep(24 * time.Hour)
}
}
// 新增:HTTP 就绪探测(最多等待 ~90 秒)
func waitHTTPReady() {
func waitHTTPReady() bool {
port := 80
if cfg := config.CurrentConfig(); cfg != nil && cfg.HttpPort > 0 {
port = cfg.HttpPort
}
if port < 1 || port > 65535 {
log.Warnf("invalid HTTP port %d, skipping SSL update this round", port)
return
return false
}
target := &url.URL{
@@ -96,6 +95,9 @@ func waitHTTPReady() {
client := &http.Client{Timeout: 2 * time.Second}
for i := 0; i < 90; i++ {
if !config.CurrentConfig().AutomaticSSLEnabled() {
return false
}
requestContext, cancel := context.WithTimeout(context.Background(), 2*time.Second)
request := (&http.Request{
Method: http.MethodGet,
@@ -112,9 +114,10 @@ func waitHTTPReady() {
cancel()
if statusOK {
log.Infof("HTTP ready: %s", target.String())
return
return true
}
time.Sleep(1 * time.Second)
}
log.Warnf("HTTP not ready after 90s, skipping SSL update this round")
return false
}
+19 -3
View File
@@ -86,6 +86,9 @@ func GetSSL() (string, error) {
if err != nil {
return "", errors.Wrap(err)
}
if cfg.UsesProxyHTTPS() {
return config.SSLTypeUser, nil
}
if cfg.SSLType == "" {
return config.SSLTypeAutoHTTP, nil
}
@@ -104,6 +107,9 @@ func SetSSL(sslType, priKey, crtKey string) error {
} else {
return errors.New("SSL 类型错误")
}
if cfg.UsesProxyHTTPS() && cfg.SSLType != config.SSLTypeUser {
return newUserCertificateError("HTTPS 反代模式由反向代理管理证书,Smail 不自动申请或续期;邮件服务请导入外部维护的证书")
}
if cfg.SSLType != config.SSLTypeUser {
clearLegacySSLPaths(cfg)
@@ -138,8 +144,12 @@ func SetSSL(sslType, priKey, crtKey string) error {
return normalizeUserCertificateImportError(err)
}
applyManagedSSLReference(cfg, reference)
// 手动设置证书的情况下后台地址默认不启用https
cfg.HttpsEnabled = 1
// 导入邮件服务证书不能重新启用反代模式下的内置 HTTPS。
if cfg.UsesProxyHTTPS() {
cfg.HttpsEnabled = 2
} else {
cfg.HttpsEnabled = 1
}
}
err = config.WriteSSLConfig(&expected, cfg)
@@ -523,6 +533,12 @@ func GenSSL(update bool) error {
if err != nil {
return errors.Wrap(err)
}
if cfg.UsesProxyHTTPS() {
return newUserCertificateError("HTTPS 反代模式由反向代理管理证书,Smail 不自动申请或续期;邮件服务请导入外部维护的证书")
}
if !cfg.AutomaticSSLEnabled() {
return newUserCertificateError("当前证书由外部维护,不能自动申请或续期")
}
if err := validateWildcardConfiguration(cfg); err != nil {
return err
}
@@ -586,7 +602,7 @@ func CheckSSLCrtInfo() (int, time.Time, bool, error) {
func Update(needRestart bool) {
cfg := config.CurrentConfig()
if cfg != nil && cfg.IsSetupReady() && (cfg.SSLType == config.SSLTypeAutoHTTP || cfg.SSLType == config.SSLTypeAutoDNS) {
if cfg.AutomaticSSLEnabled() && cfg.IsSetupReady() {
days, _, nameMatchFail, err := CheckSSLCrtInfo()
if days < 30 || err != nil || nameMatchFail {