Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinnrry committed Nov 23, 2023
1 parent 215ff7e commit faa9dbc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion server/config/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"logLevel": "info",
"logLevel": "debug",
"domain": "domain.com",
"webDomain": "mail.domain.com",
"dkimPrivateKeyPath": "config/dkim/dkim.priv",
Expand Down
34 changes: 14 additions & 20 deletions server/cron_server/ssl_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func main() {
log.Infoln("***************************************************")

// 定时任务启动
cron_server.Start()
go cron_server.Start()

// 核心服务启动
res_init.Init()
Expand Down
3 changes: 3 additions & 0 deletions server/res_init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"pmail/hooks"
"pmail/http_server"
"pmail/pop3_server"
"pmail/services/setup/ssl"
"pmail/session"
"pmail/signal"
"pmail/smtp_server"
Expand All @@ -29,6 +30,8 @@ func Init() {

for {
config.Init()
// 启动前检查一遍证书
ssl.Update(false)
parsemail.Init()
err := db.Init()
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions server/services/setup/ssl/ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.")
}
}

}

0 comments on commit faa9dbc

Please sign in to comment.