Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

fixed some bugs.

Jack529f6db62891
diff --git a/core/api/domains/v1/domains.go b/core/api/domains/v1/domains.go
index 43f6559e..81814ff6 100644
--- a/core/api/domains/v1/domains.go
+++ b/core/api/domains/v1/domains.go
@@ -13,6 +13,17 @@ type DNSRecord struct {
Valid bool `json:"valid"`
}
+type CertInfo struct {
+ Subject string `json:"subject" dc:"Primary domain"`
+ Issuer string `json:"issuer" dc:"Certificate brand information"`
+ NotBefore string `json:"not_before" dc:"Validity start time"`
+ NotAfter string `json:"not_after" dc:"Validity end time"`
+ DNSNames []string `json:"dns" dc:"Optional domain list"`
+ Endtime int `json:"endtime" dc:"Expiration time, timestamp"`
+ KeyPem string `json:"key_pem" dc:"Key pem file"`
+ CertPem string `json:"cert_pem" dc:"Certificate pem file"`
+}
+
// Domain defines the domain entity
type Domain struct {
Domain string `json:"domain" dc:"Domain name"`
@@ -30,6 +41,7 @@ type Domain struct {
MX DNSRecord `json:"mx" dc:"MX record"`
A DNSRecord `json:"a" dc:"A record"`
} `json:"dns_records" dc:"DNS records"`
+ CertInfo CertInfo `json:"cert_info" dc:"Certificate information"`
}
type AddDomainReq struct {
diff --git a/core/internal/cmd/cmd.go b/core/internal/cmd/cmd.go
index d814b964..9f1e79ad 100644
--- a/core/internal/cmd/cmd.go
+++ b/core/internal/cmd/cmd.go
@@ -143,6 +143,7 @@ var (
})
s.Run()
+
return nil
},
}
diff --git a/core/internal/controller/domains/domains_v1_get_domain_all.go b/core/internal/controller/domains/domains_v1_get_domain_all.go
new file mode 100644
index 00000000..429705cf
--- /dev/null
+++ b/core/internal/controller/domains/domains_v1_get_domain_all.go
@@ -0,0 +1,26 @@
+package domains
+
+import (
+ "billionmail-core/internal/service/domains"
+ "context"
+ "fmt"
+
+ "github.com/gogf/gf/v2/errors/gcode"
+ "github.com/gogf/gf/v2/errors/gerror"
+
+ "billionmail-core/api/domains/v1"
+)
+
+func (c *ControllerV1) GetDomainAll(ctx context.Context, req *v1.GetDomainAllReq) (res *v1.GetDomainAllRes, err error) {
+ res = &v1.GetDomainAllRes{}
+
+ res.Data, err = domains.All(ctx)
+
+ if err != nil {
+ res.SetError(fmt.Errorf("failed to get all domains: %w", gerror.NewCode(gcode.CodeInternalError, err.Error())))
+ return
+ }
+
+ res.SetSuccess("Success")
+ return
+}
diff --git a/core/internal/controller/domains/domains_v1_set_ssl.go b/core/internal/controller/domains/domains_v1_set_ssl.go
index 68b1f7a2..4cf377f8 100644
--- a/core/internal/controller/domains/domains_v1_set_ssl.go
+++ b/core/internal/controller/domains/domains_v1_set_ssl.go
@@ -1,14 +1,38 @@
package domains
import (
+ "billionmail-core/internal/service/domains"
+ "billionmail-core/internal/service/mail_service"
"context"
-
- "github.com/gogf/gf/v2/errors/gcode"
- "github.com/gogf/gf/v2/errors/gerror"
+ "fmt"
"billionmail-core/api/domains/v1"
)
func (c *ControllerV1) SetSSL(ctx context.Context, req *v1.SetSSLReq) (res *v1.SetSSLRes, err error) {
- return nil, gerror.NewCode(gcode.CodeNotImplemented)
+ res = &v1.SetSSLRes{}
+
+ if ex, err1 := domains.Exists(ctx, req.Domain); !ex {
+ if err1 != nil {
+ res.SetError(fmt.Errorf("fail to check domain: %w", err1))
+ return
+ }
+
+ res.SetError(fmt.Errorf("domain %s does not exist", req.Domain))
+ return
+ }
+
+ crt := mail_service.NewCertificate()
+
+ defer crt.Close()
+
+ err = crt.SetSNI(req.Domain, req.Certificate, req.Key)
+
+ if err != nil {
+ res.SetError(fmt.Errorf("fail to set ssl: %w", err))
+ return
+ }
+
+ res.SetSuccess("Success")
+ return
}
diff --git a/core/internal/service/acme/acme.go b/core/internal/service/acme/acme.go
index 57ddce76..feaade71 100644
--- a/core/internal/service/acme/acme.go
+++ b/core/internal/service/acme/acme.go
@@ -1,6 +1,7 @@
package acme
import (
+ v1 "billionmail-core/api/domains/v1"
"billionmail-core/internal/service/public"
"context"
"crypto"
@@ -450,14 +451,7 @@ func ApplySSLWithExistingServer(ctx context.Context, domains []string, email str
return string(certificates.Certificate), string(certificates.PrivateKey), nil
}
-type CertInfo struct {
- Subject string `json:"subject" dc:"Primary domain"`
- Issuer string `json:"issuer" dc:"Certificate brand information"`
- NotBefore string `json:"not_before" dc:"Validity start time"`
- NotAfter string `json:"not_after" dc:"Validity end time"`
- DNSNames []string `json:"dns" dc:"Optional domain list"`
- Endtime int `json:"endtime" dc:"Expiration time, timestamp"`
-}
+type CertInfo v1.CertInfo
/**
* @description: Get certificate information
diff --git a/core/internal/service/acme/renew.go b/core/internal/service/acme/renew.go
index c2d5f4b8..1be49685 100644
--- a/core/internal/service/acme/renew.go
+++ b/core/internal/service/acme/renew.go
@@ -238,18 +238,3 @@ func NewTimerEvery(CallBack func(), Sec int) *time.Ticker {
func WorkerStart() {
NewTimerEvery(Renew, 7200) // Renew every 2 hours
}
-
-/**
- * @brief Apply certificate to server
- * @param certificate Certificate content
- * @param privateKey Private key content
- * @param domain Domain name
- * @return bool Success status
- */
-func ApplyCertToServer(certificate string, privateKey string, domain string) bool {
- // This function was previously reloading Nginx
- // Now it returns true as the certificate files are already saved by the caller
- // The application using this package should implement its own logic to apply the certificate
- // to its server (if needed)
- return true
-}
diff --git a/core/internal/service/domains/domains.go b/core/internal/service/domains/domains.go
index 09c487e2..2e367cf1 100644
--- a/core/internal/service/domains/domains.go
+++ b/core/internal/service/domains/domains.go
@@ -4,6 +4,7 @@ import (
v1 "billionmail-core/api/domains/v1"
"billionmail-core/internal/consts"
docker "billionmail-core/internal/service/dockerapi"
+ "billionmail-core/internal/service/mail_service"
"billionmail-core/internal/service/public"
"context"
"fmt"
@@ -59,6 +60,10 @@ func Get(ctx context.Context, keyword string, page, pageSize int) ([]v1.Domain,
return nil, 0, err
}
+ crt := mail_service.NewCertificate()
+
+ defer crt.Close()
+
wg := sync.WaitGroup{}
for i, domain := range domains {
@@ -106,6 +111,13 @@ func Get(ctx context.Context, keyword string, page, pageSize int) ([]v1.Domain,
// return nil, 0, fmt.Errorf("Failed to get DMARC record for domain %s: %v", domain.Domain, err)
//}
}(i, domain)
+
+ // Retrieve Domains SSL certificate information
+ wg.Add(1)
+ go func(i int, domain v1.Domain) {
+ defer wg.Done()
+ domains[i].CertInfo, _ = crt.GetSSLInfo(domain.Domain)
+ }(i, domain)
}
wg.Wait()
@@ -162,6 +174,19 @@ func All(ctx context.Context) ([]v1.Domain, error) {
return domains, err
}
+func Exists(ctx context.Context, domainName string) (bool, error) {
+ count, err := g.DB().Model("domain").
+ Ctx(ctx).
+ Where("domain", domainName).
+ Count()
+
+ if err != nil {
+ return false, err
+ }
+
+ return count > 0, nil
+}
+
// GetDKIMRecord retrieves the DKIM record for a given domain.
func GetDKIMRecord(domain string) (record v1.DNSRecord, err error) {
// Create DKIM directory
diff --git a/core/internal/service/mail_service/certificate.go b/core/internal/service/mail_service/certificate.go
index e0e48dcd..44e4e7f1 100644
--- a/core/internal/service/mail_service/certificate.go
+++ b/core/internal/service/mail_service/certificate.go
@@ -1,6 +1,7 @@
package mail_service
import (
+ v1 "billionmail-core/api/domains/v1"
"billionmail-core/internal/consts"
"billionmail-core/internal/service/acme"
docker "billionmail-core/internal/service/dockerapi"
@@ -8,6 +9,7 @@ import (
"context"
"fmt"
"github.com/gogf/gf/v2/text/gregex"
+ "github.com/gogf/gf/v2/util/gconv"
"os"
"path/filepath"
"strings"
@@ -19,6 +21,7 @@ type Certificate struct {
PostfixMainConf string
PostfixMasterConf string
PostfixConfPath string
+ PostfixSNIPath string
DovecotSslConf string
DovecotConfPath string
}
@@ -38,6 +41,7 @@ func NewCertificate() *Certificate {
DovecotSslConf: public.AbsPath(filepath.Join(consts.DOVECOT_CONF_D_PATH, "10-ssl.conf")),
DovecotConfPath: public.AbsPath(consts.DOVECOT_CONF_D_PATH),
PostfixConfPath: public.AbsPath(consts.POSTFIX_CONF_PATH),
+ PostfixSNIPath: public.AbsPath(filepath.Join(consts.POSTFIX_CONF_PATH, "vmail_ssl.map")),
dk: dk,
}
}
@@ -51,6 +55,62 @@ func (c *Certificate) Close() error {
return nil
}
+// SetSSL configures SSL certificate for mail services
+func (c *Certificate) SetSSL(csrPem, keyPem string) error {
+ // Validate certificate data
+ if err := c.verifyCertificate(csrPem, keyPem); err != nil {
+ return err
+ }
+
+ // Update Postfix configuration
+ if err := c.updatePostfixConfig(csrPem, keyPem); err != nil {
+ return err
+ }
+
+ // Update Dovecot configuration
+ if err := c.updateDovecotConfig(csrPem, keyPem); err != nil {
+ return err
+ }
+
+ // Restart Postfix and Dovecot services
+ if err := c.restartPostfix(); err != nil {
+ return err
+ }
+ if err := c.restartDovecot(); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+// SetSNI configures SSL certificate for SNI
+func (c *Certificate) SetSNI(domain, csrPem, keyPem string) error {
+ // Validate certificate data
+ if err := c.verifyCertificate(csrPem, keyPem); err != nil {
+ return err
+ }
+
+ // Update Postfix configuration
+ if err := c.updatePostfixVMailConfig(domain, csrPem, keyPem); err != nil {
+ return err
+ }
+
+ // Update Dovecot SNI configuration
+ if err := c.updateDovecotSNIConfig(domain, csrPem, keyPem); err != nil {
+ return err
+ }
+
+ // Restart Postfix and Dovecot services
+ if err := c.restartPostfix(); err != nil {
+ return err
+ }
+ if err := c.restartDovecot(); err != nil {
+ return err
+ }
+
+ return nil
+}
+
// SetPostfixSSL configures SSL certificate for Postfix
func (c *Certificate) SetPostfixSSL(csrPem, keyPem string) error {
// Validate certificate data
@@ -156,11 +216,11 @@ func (c *Certificate) updatePostfixConfig(csrPem, keyPem string) error {
certPath := public.AbsPath(filepath.Join(consts.SSL_PATH, "postfix.crt"))
keyPath := public.AbsPath(filepath.Join(consts.SSL_PATH, "postfix.key"))
- if err := os.WriteFile(certPath, []byte(csrPem), 0644); err != nil {
+ if err := os.WriteFile(certPath, []byte(csrPem), 0755); err != nil {
return fmt.Errorf("failed to write certificate file: %v", err)
}
- if err := os.WriteFile(keyPath, []byte(keyPem), 0600); err != nil {
+ if err := os.WriteFile(keyPath, []byte(keyPem), 0755); err != nil {
return fmt.Errorf("failed to write key file: %v", err)
}
@@ -169,7 +229,7 @@ func (c *Certificate) updatePostfixConfig(csrPem, keyPem string) error {
config = c.updateConfigLine(config, "smtpd_tls_cert_file", certPath)
config = c.updateConfigLine(config, "smtpd_tls_key_file", keyPath)
- if err := os.WriteFile(mainCf, []byte(config), 0644); err != nil {
+ if err := os.WriteFile(mainCf, []byte(config), 0755); err != nil {
return fmt.Errorf("failed to write postfix config: %v", err)
}
@@ -213,11 +273,11 @@ func (c *Certificate) updatePostfixVMailConfig(domain, csrPem, keyPem string) er
vmailKey := filepath.Join(domainDir, "privkey.pem")
// Write certificate and key to files
- if err := os.WriteFile(vmailCert, []byte(csrPem), 0644); err != nil {
+ if err := os.WriteFile(vmailCert, []byte(csrPem), 0755); err != nil {
return fmt.Errorf("failed to write certificate file: %v", err)
}
- if err := os.WriteFile(vmailKey, []byte(keyPem), 0600); err != nil {
+ if err := os.WriteFile(vmailKey, []byte(keyPem), 0755); err != nil {
return fmt.Errorf("failed to write key file: %v", err)
}
@@ -232,7 +292,7 @@ func (c *Certificate) updatePostfixVMailConfig(domain, csrPem, keyPem string) er
// updatePostfixSNIMap updates Postfix SNI mapping table
func (c *Certificate) updatePostfixSNIMap(domain, certPath, keyPath string) error {
// Read Postfix configuration file
- content, err := os.ReadFile(filepath.Join(c.PostfixConfPath, "vmail_ssl.conf"))
+ content, err := os.ReadFile(c.PostfixSNIPath)
if err != nil {
return fmt.Errorf("failed to read postfix config: %v", err)
}
@@ -253,12 +313,12 @@ func (c *Certificate) updatePostfixSNIMap(domain, certPath, keyPath string) erro
lines = append(lines, fmt.Sprintf("%s %s %s", domain, certPath, keyPath))
}
- if err := os.WriteFile(c.PostfixConfPath, []byte(strings.Join(lines, "\n")), 0755); err != nil {
+ if err := os.WriteFile(c.PostfixSNIPath, []byte(strings.Join(lines, "\n")), 0755); err != nil {
return fmt.Errorf("failed to write postfix config: %v", err)
}
// Rehash configuration
- if _, err := c.dk.ExecCommand(context.Background(), "billionmail-postfix-billionmail-1", []string{"postmap", "/etc/postfix/conf/vmail_ssl.conf"}, "root"); err != nil {
+ if _, err := c.dk.ExecCommand(context.Background(), "billionmail-postfix-billionmail-1", []string{"postmap", "/etc/postfix/conf/vmail_ssl.map"}, "root"); err != nil {
return fmt.Errorf("failed to hash postfix config: %v", err)
}
@@ -277,11 +337,11 @@ func (c *Certificate) updateDovecotConfig(csrPem, keyPem string) error {
certPath := public.AbsPath(filepath.Join(consts.SSL_PATH, "dovecot.crt"))
keyPath := public.AbsPath(filepath.Join(consts.SSL_PATH, "dovecot.key"))
- if err := os.WriteFile(certPath, []byte(csrPem), 0644); err != nil {
+ if err := os.WriteFile(certPath, []byte(csrPem), 0755); err != nil {
return fmt.Errorf("failed to write certificate file: %v", err)
}
- if err := os.WriteFile(keyPath, []byte(keyPem), 0600); err != nil {
+ if err := os.WriteFile(keyPath, []byte(keyPem), 0755); err != nil {
return fmt.Errorf("failed to write key file: %v", err)
}
@@ -305,8 +365,8 @@ func (c *Certificate) updateDovecotSNIConfig(domain, certPem, keyPem string) err
return fmt.Errorf("failed to create domain directory: %v", err)
}
- sniCert := filepath.Join(domainDir, "dovecot.crt")
- sniKey := filepath.Join(domainDir, "dovecot.key")
+ sniCert := filepath.Join(domainDir, "fullchain.pem")
+ sniKey := filepath.Join(domainDir, "privkey.pem")
// Write certificate and key to files
if err := os.WriteFile(sniCert, []byte(certPem), 0755); err != nil {
@@ -387,6 +447,36 @@ func (c *Certificate) GetSSLStatus(domain string) (bool, error) {
return c.checkCertificateFiles(csrPath, ketPath), nil
}
+// GetSSLInfo retrieves SSL certificate information
+func (c *Certificate) GetSSLInfo(domain string) (certInfo v1.CertInfo, err error) {
+ // Check Postfix certificate
+ csrPath := filepath.Join(consts.SSL_PATH, domain, "/fullchain.pem")
+ keyPath := filepath.Join(consts.SSL_PATH, domain, "/privkey.pem")
+
+ if !c.checkCertificateFiles(csrPath, keyPath) {
+ err = fmt.Errorf("certificate files do not exist")
+ return
+ }
+
+ crtPem, err := public.ReadFile(csrPath)
+ if err != nil {
+ return
+ }
+
+ // Get certificate information
+ err = gconv.Struct(acme.GetCertInfo(crtPem), &certInfo)
+
+ if err == nil {
+ certInfo.CertPem = crtPem
+ certInfo.KeyPem, err = public.ReadFile(keyPath)
+ if err != nil {
+ return
+ }
+ }
+
+ return
+}
+
// checkCertificateFiles verifies certificate files exist
func (c *Certificate) checkCertificateFiles(certPath, keyPath string) bool {
if _, err := os.Stat(certPath); os.IsNotExist(err) {