1Panel/backend/app/service/website_ssl.go

214 lines
5.7 KiB
Go
Raw Normal View History

2022-11-11 17:41:39 +08:00
package service
import (
2022-11-16 10:31:35 +08:00
"context"
"crypto/x509"
"encoding/pem"
2022-11-11 17:41:39 +08:00
"github.com/1Panel-dev/1Panel/backend/app/dto"
2022-11-16 10:31:35 +08:00
"github.com/1Panel-dev/1Panel/backend/app/model"
2022-11-24 14:58:29 +08:00
"github.com/1Panel-dev/1Panel/backend/constant"
2022-11-16 10:31:35 +08:00
"github.com/1Panel-dev/1Panel/backend/utils/ssl"
2022-11-24 14:58:29 +08:00
"path"
2022-11-16 10:31:35 +08:00
"strings"
2022-11-11 17:41:39 +08:00
)
type WebSiteSSLService struct {
}
2022-11-20 18:32:56 +08:00
func (w WebSiteSSLService) Page(search dto.WebsiteSSLSearch) (int64, []dto.WebsiteSSLDTO, error) {
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
}
2022-11-11 17:41:39 +08:00
var sslDTOs []dto.WebsiteSSLDTO
for _, ssl := range sslList {
sslDTOs = append(sslDTOs, dto.WebsiteSSLDTO{
WebSiteSSL: ssl,
})
}
return total, sslDTOs, err
}
2022-11-20 18:32:56 +08:00
func (w WebSiteSSLService) Search() ([]dto.WebsiteSSLDTO, error) {
sslList, err := websiteSSLRepo.List()
if err != nil {
return nil, err
}
var sslDTOs []dto.WebsiteSSLDTO
for _, ssl := range sslList {
sslDTOs = append(sslDTOs, dto.WebsiteSSLDTO{
WebSiteSSL: ssl,
})
}
return sslDTOs, err
}
2022-11-16 10:31:35 +08:00
func (w WebSiteSSLService) Create(create dto.WebsiteSSLCreate) (dto.WebsiteSSLCreate, error) {
var res dto.WebsiteSSLCreate
acmeAccount, err := websiteAcmeRepo.GetFirst(commonRepo.WithByID(create.AcmeAccountID))
if err != nil {
return res, err
}
2022-11-16 10:31:35 +08:00
client, err := ssl.NewPrivateKeyClient(acmeAccount.Email, acmeAccount.PrivateKey)
if err != nil {
return res, err
}
switch create.Provider {
case dto.DNSAccount:
dnsAccount, err := websiteDnsRepo.GetFirst(commonRepo.WithByID(create.DnsAccountID))
if err != nil {
return res, err
}
2022-11-16 10:31:35 +08:00
if err := client.UseDns(ssl.DnsType(dnsAccount.Type), dnsAccount.Authorization); err != nil {
return res, err
}
case dto.Http:
2022-11-24 14:58:29 +08:00
appInstall, err := getAppInstallByKey("nginx")
if err != nil {
return dto.WebsiteSSLCreate{}, err
}
if err := client.UseHTTP(path.Join(constant.AppInstallDir, "nginx", appInstall.Name, "root")); err != nil {
return res, err
}
case dto.DnsManual:
2022-11-16 10:31:35 +08:00
}
domains := []string{create.PrimaryDomain}
otherDomainArray := strings.Split(create.OtherDomains, "\n")
if create.OtherDomains != "" {
domains = append(otherDomainArray, domains...)
}
resource, err := client.ObtainSSL(domains)
2022-11-16 10:31:35 +08:00
if err != nil {
return res, err
}
var websiteSSL model.WebSiteSSL
websiteSSL.DnsAccountID = create.DnsAccountID
websiteSSL.AcmeAccountID = acmeAccount.ID
websiteSSL.Provider = string(create.Provider)
websiteSSL.Domains = strings.Join(otherDomainArray, ",")
websiteSSL.PrimaryDomain = create.PrimaryDomain
2022-11-16 10:31:35 +08:00
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)
2022-11-16 10:31:35 +08:00
if err != nil {
return dto.WebsiteSSLCreate{}, err
}
websiteSSL.ExpireDate = cert.NotAfter
websiteSSL.StartDate = cert.NotBefore
websiteSSL.Type = cert.Issuer.CommonName
websiteSSL.Organization = cert.Issuer.Organization[0]
2022-11-16 10:31:35 +08:00
if err := websiteSSLRepo.Create(context.TODO(), &websiteSSL); err != nil {
return res, err
}
return create, nil
}
func (w WebSiteSSLService) Renew(sslId uint) error {
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
}
client, err := ssl.NewPrivateKeyClient(acmeAccount.Email, acmeAccount.PrivateKey)
if err != nil {
return err
}
switch websiteSSL.Provider {
case dto.DNSAccount:
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
}
case dto.Http:
2022-11-24 17:50:47 +08:00
appInstall, err := getAppInstallByKey("nginx")
if err != nil {
return err
}
if err := client.UseHTTP(path.Join(constant.AppInstallDir, "nginx", appInstall.Name, "root")); err != nil {
return err
}
case dto.DnsManual:
}
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]
return websiteSSLRepo.Save(websiteSSL)
}
2022-11-25 00:08:44 +08:00
func (w WebSiteSSLService) GetDNSResolve(req dto.WebsiteDNSReq) ([]dto.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
}
client, err := ssl.NewPrivateKeyClient(acmeAccount.Email, acmeAccount.PrivateKey)
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
}
var res []dto.WebsiteDNSRes
for k, v := range resolves {
res = append(res, dto.WebsiteDNSRes{
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
func (w WebSiteSSLService) GetWebsiteSSL(websiteId uint) (dto.WebsiteSSLDTO, error) {
var res dto.WebsiteSSLDTO
website, err := websiteRepo.GetFirst(commonRepo.WithByID(websiteId))
if err != nil {
return res, err
}
websiteSSL, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(website.WebSiteSSLID))
if err != nil {
return res, err
}
res.WebSiteSSL = websiteSSL
return res, nil
}
2022-11-11 17:41:39 +08:00
func (w WebSiteSSLService) Delete(id uint) error {
return websiteSSLRepo.DeleteBy(commonRepo.WithByID(id))
}