This commit is contained in:
jinnrry committed 2023-11-23 18:39:24 +08:00
1 parent 215ff7e7c4
commit faa9dbc673
5 files changed
+45 -22

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
{ {
"logLevel": "info", "logLevel": "debug",
"domain": "domain.com", "domain": "domain.com",
"webDomain": "mail.domain.com", "webDomain": "mail.domain.com",
"dkimPrivateKeyPath": "config/dkim/dkim.priv", "dkimPrivateKeyPath": "config/dkim/dkim.priv",
+14 -20
View File
@@ -11,8 +11,19 @@ import (
var expiredTime time.Time var expiredTime time.Time
func Start() { func Start() {
// 第一次启动,等待到初始化完成
if config.Instance == nil {
for {
time.Sleep(1 * time.Minute)
if config.Instance != nil && config.IsInit {
break
}
}
}
if config.Instance.SSLType == "0" { if config.Instance.SSLType == "0" {
go sslUpdate() go sslUpdateLoop()
} else { } else {
go sslCheck() go sslCheck()
} }
@@ -42,26 +53,9 @@ func sslCheck() {
} }
// 每天检查一遍SSL证书是否即将过期,即将过期就重新生成 // 每天检查一遍SSL证书是否即将过期,即将过期就重新生成
func sslUpdate() { func sslUpdateLoop() {
for { for {
if config.Instance != nil && config.Instance.IsInit && config.Instance.SSLType == "0" { ssl.Update(true)
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.")
}
}
// 每24小时检测一次证书有效期 // 每24小时检测一次证书有效期
time.Sleep(24 * time.Hour) time.Sleep(24 * time.Hour)
} }
+1 -1
View File
@@ -81,7 +81,7 @@ func main() {
log.Infoln("***************************************************") log.Infoln("***************************************************")
// 定时任务启动 // 定时任务启动
cron_server.Start() go cron_server.Start()
// 核心服务启动 // 核心服务启动
res_init.Init() res_init.Init()
+3
View File
@@ -10,6 +10,7 @@ import (
"pmail/hooks" "pmail/hooks"
"pmail/http_server" "pmail/http_server"
"pmail/pop3_server" "pmail/pop3_server"
"pmail/services/setup/ssl"
"pmail/session" "pmail/session"
"pmail/signal" "pmail/signal"
"pmail/smtp_server" "pmail/smtp_server"
@@ -29,6 +30,8 @@ func Init() {
for { for {
config.Init() config.Init()
// 启动前检查一遍证书
ssl.Update(false)
parsemail.Init() parsemail.Init()
err := db.Init() err := db.Init()
if err != nil { if err != nil {
+26
View File
@@ -8,10 +8,12 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"github.com/go-acme/lego/v4/certificate" "github.com/go-acme/lego/v4/certificate"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast" "github.com/spf13/cast"
"os" "os"
"pmail/config" "pmail/config"
"pmail/services/setup" "pmail/services/setup"
"pmail/signal"
"pmail/utils/errors" "pmail/utils/errors"
"time" "time"
@@ -170,3 +172,27 @@ func CheckSSLCrtInfo() (int, time.Time, error) {
return cast.ToInt(hours / 24), cert.NotAfter, nil 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.")
}
}
}