2022-11-11 17:41:39 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-11-16 10:31:35 +08:00
|
|
|
"context"
|
|
|
|
"crypto/x509"
|
2022-11-17 17:39:33 +08:00
|
|
|
"encoding/pem"
|
2023-11-20 16:16:20 +08:00
|
|
|
"fmt"
|
2022-12-14 15:26:51 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
|
2022-11-16 10:31:35 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-07-04 16:14:11 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
2022-12-05 15:50:53 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2022-11-24 14:58:29 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-01-04 11:44:43 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-11-20 16:16:20 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/i18n"
|
2023-11-20 10:44:10 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2023-10-31 16:01:50 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2022-11-16 10:31:35 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/ssl"
|
2023-11-20 10:44:10 +08:00
|
|
|
"github.com/go-acme/lego/v4/certcrypto"
|
2023-11-20 16:16:20 +08:00
|
|
|
legoLogger "github.com/go-acme/lego/v4/log"
|
2023-11-20 10:44:10 +08:00
|
|
|
"github.com/jinzhu/gorm"
|
2023-11-20 16:16:20 +08:00
|
|
|
"log"
|
|
|
|
"os"
|
2022-11-24 14:58:29 +08:00
|
|
|
"path"
|
2023-07-13 14:17:18 +08:00
|
|
|
"strconv"
|
2022-11-16 10:31:35 +08:00
|
|
|
"strings"
|
2023-11-20 10:44:10 +08:00
|
|
|
"time"
|
2022-11-11 17:41:39 +08:00
|
|
|
)
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
type WebsiteSSLService struct {
|
2022-11-11 17:41:39 +08:00
|
|
|
}
|
|
|
|
|
2023-01-04 11:44:43 +08:00
|
|
|
type IWebsiteSSLService interface {
|
|
|
|
Page(search request.WebsiteSSLSearch) (int64, []response.WebsiteSSLDTO, error)
|
|
|
|
GetSSL(id uint) (*response.WebsiteSSLDTO, error)
|
2023-07-04 16:14:11 +08:00
|
|
|
Search(req request.WebsiteSSLSearch) ([]response.WebsiteSSLDTO, error)
|
2023-01-04 11:44:43 +08:00
|
|
|
Create(create request.WebsiteSSLCreate) (request.WebsiteSSLCreate, error)
|
|
|
|
Renew(sslId uint) error
|
|
|
|
GetDNSResolve(req request.WebsiteDNSReq) ([]response.WebsiteDNSRes, error)
|
|
|
|
GetWebsiteSSL(websiteId uint) (response.WebsiteSSLDTO, error)
|
|
|
|
Delete(id uint) error
|
2023-03-21 14:42:50 +08:00
|
|
|
Update(update request.WebsiteSSLUpdate) error
|
2023-10-31 16:01:50 +08:00
|
|
|
Upload(req request.WebsiteSSLUpload) error
|
2023-11-20 10:44:10 +08:00
|
|
|
ObtainSSL(apply request.WebsiteSSLApply) error
|
2023-11-20 16:16:20 +08:00
|
|
|
SyncForRestart() error
|
2023-01-04 11:44:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIWebsiteSSLService() IWebsiteSSLService {
|
|
|
|
return &WebsiteSSLService{}
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:26:51 +08:00
|
|
|
func (w WebsiteSSLService) Page(search request.WebsiteSSLSearch) (int64, []response.WebsiteSSLDTO, error) {
|
2023-07-04 16:14:11 +08:00
|
|
|
var (
|
|
|
|
result []response.WebsiteSSLDTO
|
|
|
|
)
|
2022-11-11 17:41:39 +08:00
|
|
|
total, sslList, err := websiteSSLRepo.Page(search.Page, search.PageSize, commonRepo.WithOrderBy("created_at desc"))
|
2022-11-20 18:32:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2023-11-20 16:16:20 +08:00
|
|
|
for _, model := range sslList {
|
2023-07-04 16:14:11 +08:00
|
|
|
result = append(result, response.WebsiteSSLDTO{
|
2023-11-20 16:16:20 +08:00
|
|
|
WebsiteSSL: model,
|
|
|
|
LogPath: path.Join(constant.SSLLogDir, fmt.Sprintf("%s-ssl-%d.log", model.PrimaryDomain, model.ID)),
|
2022-11-11 17:41:39 +08:00
|
|
|
})
|
|
|
|
}
|
2023-07-04 16:14:11 +08:00
|
|
|
return total, result, err
|
2022-11-11 17:41:39 +08:00
|
|
|
}
|
|
|
|
|
2022-12-29 14:55:20 +08:00
|
|
|
func (w WebsiteSSLService) GetSSL(id uint) (*response.WebsiteSSLDTO, error) {
|
|
|
|
var res response.WebsiteSSLDTO
|
|
|
|
websiteSSL, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.WebsiteSSL = websiteSSL
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
2023-07-04 16:14:11 +08:00
|
|
|
func (w WebsiteSSLService) Search(search request.WebsiteSSLSearch) ([]response.WebsiteSSLDTO, error) {
|
|
|
|
var (
|
|
|
|
opts []repo.DBOption
|
|
|
|
result []response.WebsiteSSLDTO
|
|
|
|
)
|
2023-07-13 14:17:18 +08:00
|
|
|
opts = append(opts, commonRepo.WithOrderBy("created_at desc"))
|
|
|
|
if search.AcmeAccountID != "" {
|
|
|
|
acmeAccountID, err := strconv.ParseUint(search.AcmeAccountID, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opts = append(opts, websiteSSLRepo.WithByAcmeAccountId(uint(acmeAccountID)))
|
|
|
|
}
|
2023-07-04 16:14:11 +08:00
|
|
|
sslList, err := websiteSSLRepo.List(opts...)
|
2022-11-20 18:32:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-04 16:14:11 +08:00
|
|
|
for _, sslModel := range sslList {
|
|
|
|
result = append(result, response.WebsiteSSLDTO{
|
|
|
|
WebsiteSSL: sslModel,
|
2022-11-20 18:32:56 +08:00
|
|
|
})
|
|
|
|
}
|
2023-07-04 16:14:11 +08:00
|
|
|
return result, err
|
2022-11-20 18:32:56 +08:00
|
|
|
}
|
|
|
|
|
2022-12-14 15:26:51 +08:00
|
|
|
func (w WebsiteSSLService) Create(create request.WebsiteSSLCreate) (request.WebsiteSSLCreate, error) {
|
|
|
|
var res request.WebsiteSSLCreate
|
2022-11-16 10:31:35 +08:00
|
|
|
acmeAccount, err := websiteAcmeRepo.GetFirst(commonRepo.WithByID(create.AcmeAccountID))
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2023-11-20 10:44:10 +08:00
|
|
|
websiteSSL := model.WebsiteSSL{
|
|
|
|
Status: constant.SSLInit,
|
|
|
|
Provider: create.Provider,
|
|
|
|
AcmeAccountID: acmeAccount.ID,
|
|
|
|
PrimaryDomain: create.PrimaryDomain,
|
|
|
|
ExpireDate: time.Now(),
|
|
|
|
KeyType: create.KeyType,
|
2023-11-27 14:00:09 +08:00
|
|
|
PushDir: create.PushDir,
|
|
|
|
}
|
|
|
|
if create.PushDir {
|
|
|
|
if !files.NewFileOp().Stat(create.Dir) {
|
|
|
|
return res, buserr.New(constant.ErrLinkPathNotFound)
|
|
|
|
}
|
|
|
|
websiteSSL.Dir = create.Dir
|
2022-11-16 10:31:35 +08:00
|
|
|
}
|
|
|
|
|
2023-11-20 10:44:10 +08:00
|
|
|
var domains []string
|
|
|
|
if create.OtherDomains != "" {
|
|
|
|
otherDomainArray := strings.Split(create.OtherDomains, "\n")
|
|
|
|
for _, domain := range otherDomainArray {
|
|
|
|
if !common.IsValidDomain(domain) {
|
|
|
|
err = buserr.WithName("ErrDomainFormat", domain)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
domains = append(domains, domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
websiteSSL.Domains = strings.Join(domains, ",")
|
2023-04-12 23:00:30 +08:00
|
|
|
|
2023-11-20 10:44:10 +08:00
|
|
|
if create.Provider == constant.DNSAccount || create.Provider == constant.Http {
|
|
|
|
websiteSSL.AutoRenew = create.AutoRenew
|
|
|
|
}
|
|
|
|
if create.Provider == constant.DNSAccount {
|
2022-11-17 17:39:33 +08:00
|
|
|
dnsAccount, err := websiteDnsRepo.GetFirst(commonRepo.WithByID(create.DnsAccountID))
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2023-11-20 10:44:10 +08:00
|
|
|
websiteSSL.DnsAccountID = dnsAccount.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := websiteSSLRepo.Create(context.TODO(), &websiteSSL); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return create, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error {
|
2023-11-20 16:16:20 +08:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
websiteSSL model.WebsiteSSL
|
|
|
|
acmeAccount *model.WebsiteAcmeAccount
|
|
|
|
dnsAccount *model.WebsiteDnsAccount
|
|
|
|
)
|
|
|
|
|
|
|
|
websiteSSL, err = websiteSSLRepo.GetFirst(commonRepo.WithByID(apply.ID))
|
2023-11-20 10:44:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-20 16:16:20 +08:00
|
|
|
acmeAccount, err = websiteAcmeRepo.GetFirst(commonRepo.WithByID(websiteSSL.AcmeAccountID))
|
2023-11-20 10:44:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
client, err := ssl.NewAcmeClient(acmeAccount)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch websiteSSL.Provider {
|
|
|
|
case constant.DNSAccount:
|
2023-11-20 16:16:20 +08:00
|
|
|
dnsAccount, err = websiteDnsRepo.GetFirst(commonRepo.WithByID(websiteSSL.DnsAccountID))
|
2023-11-20 10:44:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-20 16:16:20 +08:00
|
|
|
if err = client.UseDns(ssl.DnsType(dnsAccount.Type), dnsAccount.Authorization); err != nil {
|
2023-11-20 10:44:10 +08:00
|
|
|
return err
|
2022-11-16 10:31:35 +08:00
|
|
|
}
|
2022-12-14 15:26:51 +08:00
|
|
|
case constant.Http:
|
2023-02-22 17:13:08 +08:00
|
|
|
appInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
2022-11-24 14:58:29 +08:00
|
|
|
if err != nil {
|
2023-11-20 10:44:10 +08:00
|
|
|
if gorm.IsRecordNotFoundError(err) {
|
|
|
|
return buserr.New("ErrOpenrestyNotFound")
|
|
|
|
}
|
|
|
|
return err
|
2022-11-24 14:58:29 +08:00
|
|
|
}
|
2023-08-24 18:20:15 +08:00
|
|
|
if err := client.UseHTTP(path.Join(appInstall.GetPath(), "root")); err != nil {
|
2023-11-20 10:44:10 +08:00
|
|
|
return err
|
2022-11-24 14:58:29 +08:00
|
|
|
}
|
2023-03-16 18:44:32 +08:00
|
|
|
case constant.DnsManual:
|
|
|
|
if err := client.UseManualDns(); err != nil {
|
2023-11-20 10:44:10 +08:00
|
|
|
return err
|
2023-03-16 18:44:32 +08:00
|
|
|
}
|
2022-11-16 10:31:35 +08:00
|
|
|
}
|
|
|
|
|
2023-11-20 10:44:10 +08:00
|
|
|
domains := []string{websiteSSL.PrimaryDomain}
|
2023-11-20 16:16:20 +08:00
|
|
|
if websiteSSL.Domains != "" {
|
|
|
|
domains = append(domains, strings.Split(websiteSSL.Domains, ",")...)
|
|
|
|
}
|
2023-11-16 14:38:07 +08:00
|
|
|
|
2023-11-20 10:44:10 +08:00
|
|
|
privateKey, err := certcrypto.GeneratePrivateKey(ssl.KeyType(websiteSSL.KeyType))
|
2022-11-16 10:31:35 +08:00
|
|
|
if err != nil {
|
2023-11-20 10:44:10 +08:00
|
|
|
return err
|
2022-11-16 10:31:35 +08:00
|
|
|
}
|
2023-04-12 23:00:30 +08:00
|
|
|
|
2023-11-20 10:44:10 +08:00
|
|
|
websiteSSL.Status = constant.SSLApply
|
|
|
|
err = websiteSSLRepo.Save(websiteSSL)
|
2022-11-16 10:31:35 +08:00
|
|
|
if err != nil {
|
2023-11-20 10:44:10 +08:00
|
|
|
return err
|
2022-11-16 10:31:35 +08:00
|
|
|
}
|
|
|
|
|
2023-11-20 10:44:10 +08:00
|
|
|
go func() {
|
2023-11-27 12:04:09 +08:00
|
|
|
logFile, _ := os.OpenFile(path.Join(constant.SSLLogDir, fmt.Sprintf("%s-ssl-%d.log", websiteSSL.PrimaryDomain, websiteSSL.ID)), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
2023-11-20 16:16:20 +08:00
|
|
|
defer logFile.Close()
|
|
|
|
logger := log.New(logFile, "", log.LstdFlags)
|
|
|
|
legoLogger.Logger = logger
|
|
|
|
startMsg := i18n.GetMsgWithMap("ApplySSLStart", map[string]interface{}{"domain": strings.Join(domains, ","), "type": i18n.GetMsgByKey(websiteSSL.Provider)})
|
|
|
|
if websiteSSL.Provider == constant.DNSAccount {
|
|
|
|
startMsg = startMsg + i18n.GetMsgWithMap("DNSAccountName", map[string]interface{}{"name": dnsAccount.Name, "type": dnsAccount.Type})
|
|
|
|
}
|
|
|
|
legoLogger.Logger.Println(startMsg)
|
2023-11-20 10:44:10 +08:00
|
|
|
resource, err := client.ObtainSSL(domains, privateKey)
|
|
|
|
if err != nil {
|
|
|
|
handleError(websiteSSL, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
websiteSSL.PrivateKey = string(resource.PrivateKey)
|
|
|
|
websiteSSL.Pem = string(resource.Certificate)
|
|
|
|
websiteSSL.CertURL = resource.CertURL
|
|
|
|
certBlock, _ := pem.Decode(resource.Certificate)
|
|
|
|
cert, err := x509.ParseCertificate(certBlock.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
handleError(websiteSSL, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
websiteSSL.ExpireDate = cert.NotAfter
|
|
|
|
websiteSSL.StartDate = cert.NotBefore
|
|
|
|
websiteSSL.Type = cert.Issuer.CommonName
|
|
|
|
websiteSSL.Organization = cert.Issuer.Organization[0]
|
|
|
|
websiteSSL.Status = constant.SSLReady
|
2023-11-20 16:16:20 +08:00
|
|
|
legoLogger.Logger.Println(i18n.GetMsgWithMap("ApplySSLSuccess", map[string]interface{}{"domain": strings.Join(domains, ",")}))
|
2023-11-27 14:00:09 +08:00
|
|
|
saveCertificateFile(websiteSSL, logger)
|
2023-11-20 10:44:10 +08:00
|
|
|
err = websiteSSLRepo.Save(websiteSSL)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2022-11-16 10:31:35 +08:00
|
|
|
|
2023-11-20 10:44:10 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleError(websiteSSL model.WebsiteSSL, err error) {
|
|
|
|
if websiteSSL.Status == constant.SSLInit || websiteSSL.Status == constant.SSLError {
|
|
|
|
websiteSSL.Status = constant.Error
|
|
|
|
} else {
|
|
|
|
websiteSSL.Status = constant.SSLApplyError
|
|
|
|
}
|
|
|
|
websiteSSL.Message = err.Error()
|
2023-11-20 16:16:20 +08:00
|
|
|
legoLogger.Logger.Println(i18n.GetErrMsg("ApplySSLFailed", map[string]interface{}{"domain": websiteSSL.PrimaryDomain, "err": err.Error()}))
|
2023-11-20 10:44:10 +08:00
|
|
|
_ = websiteSSLRepo.Save(websiteSSL)
|
2022-11-16 10:31:35 +08:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w WebsiteSSLService) Renew(sslId uint) error {
|
2022-11-17 17:39:33 +08:00
|
|
|
websiteSSL, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(sslId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
acmeAccount, err := websiteAcmeRepo.GetFirst(commonRepo.WithByID(websiteSSL.AcmeAccountID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-14 18:44:09 +08:00
|
|
|
client, err := ssl.NewAcmeClient(acmeAccount)
|
2022-11-17 17:39:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch websiteSSL.Provider {
|
2022-12-14 15:26:51 +08:00
|
|
|
case constant.DNSAccount:
|
2022-11-17 17:39:33 +08:00
|
|
|
dnsAccount, err := websiteDnsRepo.GetFirst(commonRepo.WithByID(websiteSSL.DnsAccountID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := client.UseDns(ssl.DnsType(dnsAccount.Type), dnsAccount.Authorization); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-14 15:26:51 +08:00
|
|
|
case constant.Http:
|
2023-02-22 17:13:08 +08:00
|
|
|
appInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
2022-11-24 17:50:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-22 17:13:08 +08:00
|
|
|
if err := client.UseHTTP(path.Join(constant.AppInstallDir, constant.AppOpenresty, appInstall.Name, "root")); err != nil {
|
2022-11-24 17:50:47 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-11-17 17:39:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource, err := client.RenewSSL(websiteSSL.CertURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
websiteSSL.PrivateKey = string(resource.PrivateKey)
|
|
|
|
websiteSSL.Pem = string(resource.Certificate)
|
|
|
|
websiteSSL.CertURL = resource.CertURL
|
|
|
|
certBlock, _ := pem.Decode(resource.Certificate)
|
|
|
|
cert, err := x509.ParseCertificate(certBlock.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
websiteSSL.ExpireDate = cert.NotAfter
|
|
|
|
websiteSSL.StartDate = cert.NotBefore
|
|
|
|
websiteSSL.Type = cert.Issuer.CommonName
|
|
|
|
websiteSSL.Organization = cert.Issuer.Organization[0]
|
|
|
|
|
2023-01-04 11:44:43 +08:00
|
|
|
if err := websiteSSLRepo.Save(websiteSSL); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
websites, _ := websiteRepo.GetBy(websiteRepo.WithWebsiteSSLID(sslId))
|
|
|
|
for _, website := range websites {
|
|
|
|
if err := createPemFile(website, websiteSSL); err != nil {
|
|
|
|
global.LOG.Errorf("create website [%s] ssl file failed! err:%s", website.PrimaryDomain, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(websites) > 0 {
|
2023-02-22 17:13:08 +08:00
|
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
2023-01-04 11:44:43 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := opNginx(nginxInstall.ContainerName, constant.NginxReload); err != nil {
|
|
|
|
return buserr.New(constant.ErrSSLApply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-11-17 17:39:33 +08:00
|
|
|
}
|
|
|
|
|
2022-12-14 15:26:51 +08:00
|
|
|
func (w WebsiteSSLService) GetDNSResolve(req request.WebsiteDNSReq) ([]response.WebsiteDNSRes, error) {
|
2022-11-16 10:31:35 +08:00
|
|
|
acmeAccount, err := websiteAcmeRepo.GetFirst(commonRepo.WithByID(req.AcmeAccountID))
|
|
|
|
if err != nil {
|
2022-11-25 00:08:44 +08:00
|
|
|
return nil, err
|
2022-11-16 10:31:35 +08:00
|
|
|
}
|
|
|
|
|
2023-11-14 18:44:09 +08:00
|
|
|
client, err := ssl.NewAcmeClient(acmeAccount)
|
2022-11-16 10:31:35 +08:00
|
|
|
if err != nil {
|
2022-11-25 00:08:44 +08:00
|
|
|
return nil, err
|
2022-11-16 10:31:35 +08:00
|
|
|
}
|
2022-11-25 00:08:44 +08:00
|
|
|
resolves, err := client.GetDNSResolve(req.Domains)
|
2022-11-16 10:31:35 +08:00
|
|
|
if err != nil {
|
2022-11-25 00:08:44 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-14 15:26:51 +08:00
|
|
|
var res []response.WebsiteDNSRes
|
2022-11-25 00:08:44 +08:00
|
|
|
for k, v := range resolves {
|
2022-12-14 15:26:51 +08:00
|
|
|
res = append(res, response.WebsiteDNSRes{
|
2022-11-25 00:08:44 +08:00
|
|
|
Domain: k,
|
|
|
|
Key: v.Key,
|
|
|
|
Value: v.Value,
|
|
|
|
Err: v.Err,
|
|
|
|
})
|
2022-11-16 10:31:35 +08:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2022-11-11 17:41:39 +08:00
|
|
|
|
2022-12-14 15:26:51 +08:00
|
|
|
func (w WebsiteSSLService) GetWebsiteSSL(websiteId uint) (response.WebsiteSSLDTO, error) {
|
|
|
|
var res response.WebsiteSSLDTO
|
2022-11-17 17:39:33 +08:00
|
|
|
website, err := websiteRepo.GetFirst(commonRepo.WithByID(websiteId))
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2022-12-13 17:20:13 +08:00
|
|
|
websiteSSL, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(website.WebsiteSSLID))
|
2022-11-17 17:39:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2022-12-13 17:20:13 +08:00
|
|
|
res.WebsiteSSL = websiteSSL
|
2022-11-17 17:39:33 +08:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w WebsiteSSLService) Delete(id uint) error {
|
2022-12-05 15:50:53 +08:00
|
|
|
if websites, _ := websiteRepo.GetBy(websiteRepo.WithWebsiteSSLID(id)); len(websites) > 0 {
|
|
|
|
return buserr.New(constant.ErrSSLCannotDelete)
|
|
|
|
}
|
2022-11-11 17:41:39 +08:00
|
|
|
return websiteSSLRepo.DeleteBy(commonRepo.WithByID(id))
|
|
|
|
}
|
2023-03-21 14:42:50 +08:00
|
|
|
|
|
|
|
func (w WebsiteSSLService) Update(update request.WebsiteSSLUpdate) error {
|
|
|
|
websiteSSL, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(update.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
websiteSSL.AutoRenew = update.AutoRenew
|
|
|
|
return websiteSSLRepo.Save(websiteSSL)
|
|
|
|
}
|
2023-10-31 16:01:50 +08:00
|
|
|
|
|
|
|
func (w WebsiteSSLService) Upload(req request.WebsiteSSLUpload) error {
|
|
|
|
newSSL := &model.WebsiteSSL{
|
|
|
|
Provider: constant.Manual,
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Type == "local" {
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(req.PrivateKeyPath) {
|
|
|
|
return buserr.New("ErrSSLKeyNotFound")
|
|
|
|
}
|
|
|
|
if !fileOp.Stat(req.CertificatePath) {
|
|
|
|
return buserr.New("ErrSSLCertificateNotFound")
|
|
|
|
}
|
|
|
|
if content, err := fileOp.GetContent(req.PrivateKeyPath); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
newSSL.PrivateKey = string(content)
|
|
|
|
}
|
|
|
|
if content, err := fileOp.GetContent(req.CertificatePath); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
newSSL.Pem = string(content)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newSSL.PrivateKey = req.PrivateKey
|
|
|
|
newSSL.Pem = req.Certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKeyCertBlock, _ := pem.Decode([]byte(newSSL.PrivateKey))
|
|
|
|
if privateKeyCertBlock == nil {
|
|
|
|
return buserr.New("ErrSSLKeyFormat")
|
|
|
|
}
|
|
|
|
|
|
|
|
certBlock, _ := pem.Decode([]byte(newSSL.Pem))
|
|
|
|
if certBlock == nil {
|
|
|
|
return buserr.New("ErrSSLCertificateFormat")
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(certBlock.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newSSL.ExpireDate = cert.NotAfter
|
|
|
|
newSSL.StartDate = cert.NotBefore
|
|
|
|
newSSL.Type = cert.Issuer.CommonName
|
|
|
|
if len(cert.Issuer.Organization) > 0 {
|
|
|
|
newSSL.Organization = cert.Issuer.Organization[0]
|
|
|
|
} else {
|
|
|
|
newSSL.Organization = cert.Issuer.CommonName
|
|
|
|
}
|
2023-11-15 14:20:09 +08:00
|
|
|
|
|
|
|
var domains []string
|
2023-10-31 16:01:50 +08:00
|
|
|
if len(cert.DNSNames) > 0 {
|
|
|
|
newSSL.PrimaryDomain = cert.DNSNames[0]
|
2023-11-15 14:20:09 +08:00
|
|
|
domains = cert.DNSNames[1:]
|
2023-11-27 12:04:09 +08:00
|
|
|
}
|
|
|
|
if len(cert.IPAddresses) > 0 {
|
|
|
|
if newSSL.PrimaryDomain == "" {
|
|
|
|
newSSL.PrimaryDomain = cert.IPAddresses[0].String()
|
|
|
|
for _, ip := range cert.IPAddresses[1:] {
|
|
|
|
domains = append(domains, ip.String())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, ip := range cert.IPAddresses {
|
|
|
|
domains = append(domains, ip.String())
|
|
|
|
}
|
2023-11-15 14:20:09 +08:00
|
|
|
}
|
2023-10-31 16:01:50 +08:00
|
|
|
}
|
2023-11-15 14:20:09 +08:00
|
|
|
newSSL.Domains = strings.Join(domains, ",")
|
2023-10-31 16:01:50 +08:00
|
|
|
|
|
|
|
return websiteSSLRepo.Create(context.Background(), newSSL)
|
|
|
|
}
|
2023-11-20 16:16:20 +08:00
|
|
|
|
|
|
|
func (w WebsiteSSLService) SyncForRestart() error {
|
|
|
|
sslList, err := websiteSSLRepo.List()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, ssl := range sslList {
|
|
|
|
if ssl.Status == constant.SSLApply {
|
|
|
|
ssl.Status = constant.SystemRestart
|
|
|
|
ssl.Message = "System restart causing interrupt"
|
|
|
|
_ = websiteSSLRepo.Save(ssl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|