Template
feat: per-site SSL email and error display
This commit is contained in:
+32
-10
@@ -3,6 +3,7 @@ package sslsvc
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/panelhosting/panel/internal/config"
|
||||
@@ -12,29 +13,39 @@ import (
|
||||
|
||||
type Service struct {
|
||||
repo *repository.SSLRepository
|
||||
sites *repository.SiteRepository
|
||||
issuer *ssl.Issuer
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
func NewService(repo *repository.SSLRepository, issuer *ssl.Issuer, cfg *config.Config) *Service {
|
||||
return &Service{repo: repo, issuer: issuer, cfg: cfg}
|
||||
func NewService(repo *repository.SSLRepository, sites *repository.SiteRepository, issuer *ssl.Issuer, cfg *config.Config) *Service {
|
||||
return &Service{repo: repo, sites: sites, issuer: issuer, cfg: cfg}
|
||||
}
|
||||
|
||||
func (s *Service) IssueAsync(sslID, domainID int64, domain, webroot string) {
|
||||
func (s *Service) IssueAsync(sslID, domainID int64, domain, webroot, email string) {
|
||||
go func() {
|
||||
ctx := context.Background()
|
||||
if err := s.Issue(ctx, sslID, domainID, domain, webroot); err != nil {
|
||||
if err := s.Issue(ctx, sslID, domainID, domain, webroot, email); err != nil {
|
||||
log.Printf("ssl issue %s: %v", domain, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *Service) Issue(ctx context.Context, sslID, domainID int64, domain, webroot string) error {
|
||||
if s.cfg.ACMEEmail == "" {
|
||||
return s.repo.MarkError(ctx, sslID, "ACME_EMAIL not configured")
|
||||
func (s *Service) resolveEmail(email string) string {
|
||||
email = strings.TrimSpace(strings.ToLower(email))
|
||||
if email != "" {
|
||||
return email
|
||||
}
|
||||
return strings.TrimSpace(strings.ToLower(s.cfg.ACMEEmail))
|
||||
}
|
||||
|
||||
func (s *Service) Issue(ctx context.Context, sslID, domainID int64, domain, webroot, email string) error {
|
||||
email = s.resolveEmail(email)
|
||||
if email == "" {
|
||||
return s.repo.MarkError(ctx, sslID, "укажите email для Let's Encrypt")
|
||||
}
|
||||
|
||||
res, err := s.issuer.Obtain(ctx, domain, webroot)
|
||||
res, err := s.issuer.Obtain(ctx, email, domain, webroot)
|
||||
if err != nil {
|
||||
_ = s.repo.MarkError(ctx, sslID, err.Error())
|
||||
return err
|
||||
@@ -53,7 +64,13 @@ func (s *Service) Issue(ctx context.Context, sslID, domainID int64, domain, webr
|
||||
return s.repo.MarkActive(ctx, sslID, issuerName, string(res.Certificate), string(res.PrivateKey), string(res.IssuerCertificate), time.Now(), expiresAt)
|
||||
}
|
||||
|
||||
func (s *Service) IssueForSite(ctx context.Context, siteID int64) error {
|
||||
func (s *Service) IssueForSite(ctx context.Context, siteID int64, email string) error {
|
||||
if email != "" {
|
||||
if err := s.sites.UpdateSSLEmail(ctx, siteID, email); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
domainID, err := s.repo.GetDomainIDBySite(ctx, siteID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -64,6 +81,11 @@ func (s *Service) IssueForSite(ctx context.Context, siteID int64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
siteEmail, err := s.sites.GetSSLEmail(ctx, siteID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cert, err := s.repo.GetByDomainID(ctx, domainID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -80,7 +102,7 @@ func (s *Service) IssueForSite(ctx context.Context, siteID int64) error {
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := s.Issue(context.Background(), sslID, domainID, domain, webroot); err != nil {
|
||||
if err := s.Issue(context.Background(), sslID, domainID, domain, webroot, siteEmail); err != nil {
|
||||
log.Printf("ssl reissue %s: %v", domain, err)
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user