2023-01-04 03:44:43 +00:00
|
|
|
package job
|
|
|
|
|
|
|
|
import (
|
2023-11-27 10:36:10 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
2023-01-04 03:44:43 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/service"
|
2023-11-27 10:36:10 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-01-04 03:44:43 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-05-08 05:01:39 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2023-11-29 14:04:08 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-01-04 03:44:43 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ssl struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSSLJob() *ssl {
|
|
|
|
return &ssl{}
|
|
|
|
}
|
|
|
|
|
2023-11-29 14:04:08 +00:00
|
|
|
func getSystemSSL() (bool, uint) {
|
|
|
|
settingRepo := repo.NewISettingRepo()
|
|
|
|
sslSetting, err := settingRepo.Get(settingRepo.WithByKey("SSL"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load service ssl from setting failed, err: %v", err)
|
|
|
|
return false, 0
|
|
|
|
}
|
|
|
|
if sslSetting.Value == "enable" {
|
|
|
|
sslID, _ := settingRepo.Get(settingRepo.WithByKey("SSLID"))
|
|
|
|
idValue, _ := strconv.Atoi(sslID.Value)
|
|
|
|
if idValue > 0 {
|
|
|
|
return true, uint(idValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, 0
|
|
|
|
}
|
|
|
|
|
2023-01-04 03:44:43 +00:00
|
|
|
func (ssl *ssl) Run() {
|
2023-11-29 14:04:08 +00:00
|
|
|
systemSSLEnable, sslID := getSystemSSL()
|
2023-01-04 03:44:43 +00:00
|
|
|
sslRepo := repo.NewISSLRepo()
|
|
|
|
sslService := service.NewIWebsiteSSLService()
|
|
|
|
sslList, _ := sslRepo.List()
|
2023-05-08 05:01:39 +00:00
|
|
|
nyc, _ := time.LoadLocation(common.LoadTimeZone())
|
2023-04-13 00:24:32 +00:00
|
|
|
global.LOG.Info("The scheduled certificate update task is currently in progress ...")
|
2023-03-21 05:10:27 +00:00
|
|
|
now := time.Now().Add(10 * time.Second)
|
2023-01-04 03:44:43 +00:00
|
|
|
for _, s := range sslList {
|
2023-11-20 08:16:20 +00:00
|
|
|
if !s.AutoRenew || s.Provider == "manual" || s.Provider == "dnsManual" || s.Status == "applying" {
|
2023-01-04 03:44:43 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-05-08 05:01:39 +00:00
|
|
|
expireDate := s.ExpireDate.In(nyc)
|
2023-04-13 00:24:32 +00:00
|
|
|
sub := expireDate.Sub(now)
|
|
|
|
if sub.Hours() < 720 {
|
|
|
|
global.LOG.Errorf("Update the SSL certificate for the [%s] domain", s.PrimaryDomain)
|
2023-11-27 10:36:10 +00:00
|
|
|
if s.Provider == constant.SelfSigned {
|
|
|
|
caService := service.NewIWebsiteCAService()
|
2023-11-29 14:04:08 +00:00
|
|
|
if _, err := caService.ObtainSSL(request.WebsiteCAObtain{
|
2023-11-27 10:36:10 +00:00
|
|
|
ID: s.CaID,
|
|
|
|
SSLID: s.ID,
|
|
|
|
Renew: true,
|
|
|
|
Unit: "year",
|
|
|
|
Time: 1,
|
|
|
|
}); err != nil {
|
|
|
|
global.LOG.Errorf("Failed to update the SSL certificate for the [%s] domain , err:%s", s.PrimaryDomain, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := sslService.ObtainSSL(request.WebsiteSSLApply{
|
|
|
|
ID: s.ID,
|
|
|
|
}); err != nil {
|
|
|
|
global.LOG.Errorf("Failed to update the SSL certificate for the [%s] domain , err:%s", s.PrimaryDomain, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2023-01-04 03:44:43 +00:00
|
|
|
}
|
2023-11-29 14:04:08 +00:00
|
|
|
if systemSSLEnable && sslID == s.ID {
|
|
|
|
websiteSSL, _ := sslRepo.GetFirst(repo.NewCommonRepo().WithByID(s.ID))
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
secretDir := path.Join(global.CONF.System.BaseDir, "1panel/secret")
|
|
|
|
if err := fileOp.WriteFile(path.Join(secretDir, "server.crt"), strings.NewReader(websiteSSL.Pem), 0600); err != nil {
|
|
|
|
global.LOG.Errorf("Failed to update the SSL certificate File for 1Panel System domain [%s] , err:%s", s.PrimaryDomain, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := fileOp.WriteFile(path.Join(secretDir, "server.key"), strings.NewReader(websiteSSL.PrivateKey), 0600); err != nil {
|
|
|
|
global.LOG.Errorf("Failed to update the SSL certificate for 1Panel System domain [%s] , err:%s", s.PrimaryDomain, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 00:24:32 +00:00
|
|
|
global.LOG.Errorf("The SSL certificate for the [%s] domain has been successfully updated", s.PrimaryDomain)
|
2023-01-04 03:44:43 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-13 00:24:32 +00:00
|
|
|
global.LOG.Info("The scheduled certificate update task has completed")
|
2023-01-04 03:44:43 +00:00
|
|
|
}
|