diff --git a/server/config/config.json b/server/config/config.json index 9f8cccd..77fab9a 100644 --- a/server/config/config.json +++ b/server/config/config.json @@ -1,5 +1,5 @@ { - "logLevel": "info", + "logLevel": "debug", "domain": "domain.com", "webDomain": "mail.domain.com", "dkimPrivateKeyPath": "config/dkim/dkim.priv", diff --git a/server/cron_server/ssl_update.go b/server/cron_server/ssl_update.go index aefd186..05334cd 100644 --- a/server/cron_server/ssl_update.go +++ b/server/cron_server/ssl_update.go @@ -11,8 +11,19 @@ import ( var expiredTime time.Time func Start() { + + // 第一次启动,等待到初始化完成 + if config.Instance == nil { + for { + time.Sleep(1 * time.Minute) + if config.Instance != nil && config.IsInit { + break + } + } + } + if config.Instance.SSLType == "0" { - go sslUpdate() + go sslUpdateLoop() } else { go sslCheck() } @@ -42,26 +53,9 @@ func sslCheck() { } // 每天检查一遍SSL证书是否即将过期,即将过期就重新生成 -func sslUpdate() { +func sslUpdateLoop() { for { - if config.Instance != nil && config.Instance.IsInit && config.Instance.SSLType == "0" { - days, _, err := ssl.CheckSSLCrtInfo() - if days < 30 || err != nil { - if err != nil { - log.Errorf("SSL Check Error, Update SSL Certificate. Error Info :%+v", err) - } else { - log.Infof("SSL certificate remaining time is only %d days, renew SSL certificate.", days) - } - err = ssl.GenSSL(true) - if err != nil { - log.Errorf("SSL Update Error! %+v", err) - } - // 更新完证书,重启服务 - signal.RestartChan <- true - } else { - log.Debugf("SSL Check.") - } - } + ssl.Update(true) // 每24小时检测一次证书有效期 time.Sleep(24 * time.Hour) } diff --git a/server/main.go b/server/main.go index 458f65b..12f6323 100644 --- a/server/main.go +++ b/server/main.go @@ -81,7 +81,7 @@ func main() { log.Infoln("***************************************************") // 定时任务启动 - cron_server.Start() + go cron_server.Start() // 核心服务启动 res_init.Init() diff --git a/server/res_init/init.go b/server/res_init/init.go index 71144f7..d7e90eb 100644 --- a/server/res_init/init.go +++ b/server/res_init/init.go @@ -10,6 +10,7 @@ import ( "pmail/hooks" "pmail/http_server" "pmail/pop3_server" + "pmail/services/setup/ssl" "pmail/session" "pmail/signal" "pmail/smtp_server" @@ -29,6 +30,8 @@ func Init() { for { config.Init() + // 启动前检查一遍证书 + ssl.Update(false) parsemail.Init() err := db.Init() if err != nil { diff --git a/server/services/setup/ssl/ssl.go b/server/services/setup/ssl/ssl.go index 9a79bd4..8bd3dea 100644 --- a/server/services/setup/ssl/ssl.go +++ b/server/services/setup/ssl/ssl.go @@ -8,10 +8,12 @@ import ( "crypto/tls" "crypto/x509" "github.com/go-acme/lego/v4/certificate" + log "github.com/sirupsen/logrus" "github.com/spf13/cast" "os" "pmail/config" "pmail/services/setup" + "pmail/signal" "pmail/utils/errors" "time" @@ -170,3 +172,27 @@ func CheckSSLCrtInfo() (int, time.Time, error) { return cast.ToInt(hours / 24), cert.NotAfter, nil } + +func Update(needRestart bool) { + if config.Instance != nil && config.Instance.IsInit && config.Instance.SSLType == "0" { + days, _, err := CheckSSLCrtInfo() + if days < 30 || err != nil { + if err != nil { + log.Errorf("SSL Check Error, Update SSL Certificate. Error Info :%+v", err) + } else { + log.Infof("SSL certificate remaining time is only %d days, renew SSL certificate.", days) + } + err = GenSSL(true) + if err != nil { + log.Errorf("SSL Update Error! %+v", err) + } + if needRestart { + // 更新完证书,重启服务 + signal.RestartChan <- true + } + } else { + log.Debugf("SSL Check.") + } + } + +}