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
Added configuration repairs in background. Fixed DKIM Signature header duplicate. #50 Fixed source IP lake when mail sending. #52 Fixed configuration warning in dovecot logs. Fixed password incorrect after mailbox updates.
Jackc0bcbf6815e1
diff --git a/core/internal/service/collect/collect.go b/core/internal/service/collect/collect.goindex 1fd61f38..6f35d91e 100644--- a/core/internal/service/collect/collect.go+++ b/core/internal/service/collect/collect.go@@ -20,7 +20,9 @@ func Collect(ctx context.Context) {curTime := time.Now().Unix()if lastPostTime == 0 || curTime-lastPostTime > 24*60*60 {- lastPostTime = curTime+ // get 0 clock time of today+ // If last post time is not set or more than 24 hours ago, reset to current day start+ lastPostTime = time.Now().Truncate(24 * time.Hour).Unix()}defer func() {@@ -31,7 +33,7 @@ func Collect(ctx context.Context) {var cnt int// Get sent total count- cnt, _ = g.DB().Model("mailstate_send_mails").Where("log_time > ?", lastPostTime).Count()+ cnt, _ = g.DB().Model("mailstat_send_mails").Where("log_time > ?", lastPostTime).Count()resp, err := g.Client().ContentJson().Post(ctx, apiBase, g.Map{"feature": "bm.sent",diff --git a/core/internal/service/domains/domains.go b/core/internal/service/domains/domains.goindex f139abb0..fbb7f108 100644--- a/core/internal/service/domains/domains.go+++ b/core/internal/service/domains/domains.go@@ -341,7 +341,7 @@ func GetDKIMRecord(domain string, validateImmediate bool) (record v1.DNSRecord,selectors [{path: "/var/lib/rspamd/dkim/%s/default.private";- selector: "default"+ selector: "default";}]}@@ -350,7 +350,9 @@ func GetDKIMRecord(domain string, validateImmediate bool) (record v1.DNSRecord,// Write DKIM sign config to filesignConfPath := public.AbsPath(filepath.Join(consts.RSPAMD_LOCAL_D_PATH, "dkim_signing.conf"))- signContent := `domain {+ signContent := `sign_headers = "from:sender:reply-to:subject:date:message-id:to:cc:mime-version:content-type:content-transfer-encoding:content-language:resent-to:resent-cc:resent-from:resent-sender:resent-message-id:in-reply-to:references:list-id:list-help:list-owner:list-unsubscribe:list-subscribe:list-post:list-unsubscribe-post:disposition-notification-to:disposition-notification-options:original-recipient:openpgp:autocrypt";++domain {#BT_DOMAIN_DKIM_BEGIN#BT_DOMAIN_DKIM_END}`diff --git a/core/internal/service/mail_boxes/mail_boxes.go b/core/internal/service/mail_boxes/mail_boxes.goindex f3c534dc..59725fdb 100644--- a/core/internal/service/mail_boxes/mail_boxes.go+++ b/core/internal/service/mail_boxes/mail_boxes.go@@ -46,6 +46,12 @@ func Update(ctx context.Context, mailbox *v1.Mailbox) (err error) {mailbox.UpdateTime = time.Now().Unix()if mailbox.Password != "" {mailbox.PasswordEncode = PasswdEncode(ctx, mailbox.Password)+ mailbox.Password, err = PasswdMD5Crypt(ctx, mailbox.Password)++ if err != nil {+ err = fmt.Errorf("Generate password md5-crypt failed: %w", err)+ return+ }}mailbox.Username = strings.ToLower(mailbox.Username)diff --git a/core/internal/service/mail_service/certificate.go b/core/internal/service/mail_service/certificate.goindex e80aa9f6..06d70a63 100644--- a/core/internal/service/mail_service/certificate.go+++ b/core/internal/service/mail_service/certificate.go@@ -412,8 +412,8 @@ func (c *Certificate) updateDovecotSNIConfig(domain, certPem, keyPem string) err// updatecontent, err = gregex.ReplaceString(fmt.Sprintf(`#DOMAIN_SSL_BEGIN_%s[\s\S]+#DOMAIN_SSL_END_%s`, domain, domain), replaceStr, content)} else {- // add- content, err = gregex.ReplaceString(`ssl\s*=\s*yes`, "ssl = yes\n"+replaceStr, content)+ // append+ content += replaceStr}// Update SNI configurationdiff --git a/core/internal/service/mail_service/fix.go b/core/internal/service/mail_service/fix.gonew file mode 100644index 00000000..53939a4f--- /dev/null+++ b/core/internal/service/mail_service/fix.go@@ -0,0 +1,267 @@+package mail_service++import (+ "billionmail-core/internal/consts"+ docker "billionmail-core/internal/service/dockerapi"+ "billionmail-core/internal/service/public"+ "context"+ "github.com/gogf/gf/v2/frame/g"+ "path/filepath"+ "strings"+)++func FixPostfixMainConfig(ctx context.Context) {+ g.Log().Debug(ctx, "Fix postfix main config")+ defer func() {+ g.Log().Debug(ctx, "Fix postfix main config done")+ }()++ var err error++ // initialize a header_checks file if it does not exist+ headerChecksFile := public.AbsPath(filepath.Join(consts.POSTFIX_CONF_PATH, "header_checks"))+ headerChecksContent := ""+ regularHeaderChecks := `if /^\s*Received:.*Authenticated sender.*\(PostBillionMail\)/+/^Received: from .*? \([\w\-.]* \[.*?\]\)(.*|\n.*)\(Authenticated sender: (.+)\)\s+by.+\(PostBillionMail\) with (.*)/+ REPLACE Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with $3+endif++/^\s*X-Enigmail/ IGNORE+/^\s*X-Originating-IP/ IGNORE+/^\s*X-Forward/ IGNORE+`++ if !public.FileExists(headerChecksFile) {+ headerChecksContent = regularHeaderChecks+ _, err = public.WriteFile(headerChecksFile, headerChecksContent)++ if err != nil {+ g.Log().Warning(ctx, "Failed to create header_checks file: %v", err)+ return+ }+ } else {+ // Read the existing header_checks file content+ headerChecksContent, err = public.ReadFile(headerChecksFile)++ if err != nil {+ g.Log().Warning(ctx, "Failed to read header_checks file: %v", err)+ return+ }+ }++ // Ensure Received header replacement is in place+ if !strings.Contains(headerChecksContent, "REPLACE Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with $3") {+ headerChecksContent += "\n" + regularHeaderChecks+ _, err = public.WriteFile(headerChecksFile, headerChecksContent)++ if err != nil {+ g.Log().Warning(ctx, "Failed to update header_checks file with Received header replacement: %v", err)+ return+ }+ }++ lines := make([]string, 0)+ skipNextEmptyLine := false++ // Read the main configuration file each rows+ err = public.ReadEach(consts.POSTFIX_MAIN_CONF, func(row string, cnt int) bool {+ if strings.HasPrefix(strings.TrimSpace(row), "smtp_header_checks") || strings.HasPrefix(strings.TrimSpace(row), "lmtp_header_checks") || strings.HasPrefix(strings.TrimSpace(row), "mail_name") {+ // Skip existing smtp_header_checks, lmtp_header_checks lines, and mail_name line+ skipNextEmptyLine = true+ return true+ }++ if skipNextEmptyLine {+ skipNextEmptyLine = false+ if strings.TrimSpace(row) == "" {+ return true+ }+ }++ if strings.HasPrefix(strings.TrimSpace(row), "compatibility_level") {+ lines = append(lines, "compatibility_level = 3.7\n")+ lines = append(lines, "mail_name = PostBillionMail\n")+ lines = append(lines, "\n")+ lines = append(lines, "smtp_header_checks = pcre:/etc/postfix/conf/header_checks\n")+ lines = append(lines, "lmtp_header_checks = pcre:/etc/postfix/conf/header_checks\n\n")+ return true+ }++ lines = append(lines, row)++ return true+ })++ if err != nil {+ g.Log().Warning(ctx, "Failed to read Postfix main configuration file: %v", err)+ return+ }++ // Write the updated lines back to the main configuration file+ _, err = public.WriteFile(consts.POSTFIX_MAIN_CONF, strings.Join(lines, ""))++ if err != nil {+ g.Log().Warning(ctx, "Failed to write updated Postfix main configuration file: %v", err)+ return+ }++ dk, err := docker.NewDockerAPI()++ if err != nil {+ g.Log().Warning(ctx, "Failed to create Docker API instance: %v", err)+ return+ }++ defer dk.Close()++ // Restart the Postfix container to apply the changes+ err = dk.RestartContainer(ctx, "billionmail-postfix-billionmail-1")++ if err != nil {+ g.Log().Warning(ctx, "Failed to restart Postfix container: %v", err)+ return+ }+}++func FixRspamdDKIMSigningConfig(ctx context.Context) {+ g.Log().Debug(ctx, "Fix rspamd dkim signing config")+ defer func() {+ g.Log().Debug(ctx, "Fix rspamd dkim signing config done")+ }()++ lines := make([]string, 0)++ // Read the DKIM signing configuration file each rows+ err := public.ReadEach(public.AbsPath(filepath.Join(consts.RSPAMD_LOCAL_D_PATH, "dkim_signing.conf")), func(row string, cnt int) bool {+ if strings.HasPrefix(strings.TrimSpace(row), "sign_headers") {+ return true+ }++ lines = append(lines, row)++ return true+ })++ if err != nil {+ g.Log().Warning(ctx, "Failed to read DKIM signing configuration file: %v", err)+ return+ }++ lineLen := len(lines)+ lineBegin := -1++ for i := 0; i < lineLen; i++ {+ lineBegin = i+ if strings.TrimSpace(lines[i]) != "" {+ break+ }+ }++ if lineBegin > 0 {+ lines = lines[lineBegin : lineLen-1]+ }++ lines = append([]string{"sign_headers = \"from:sender:reply-to:subject:date:message-id:to:cc:mime-version:content-type:content-transfer-encoding:content-language:resent-to:resent-cc:resent-from:resent-sender:resent-message-id:in-reply-to:references:list-id:list-help:list-owner:list-unsubscribe:list-subscribe:list-post:list-unsubscribe-post:disposition-notification-to:disposition-notification-options:original-recipient:openpgp:autocrypt\";\n\n"}, lines...)++ // Write the updated lines back to the DKIM signing configuration file+ _, err = public.WriteFile(public.AbsPath(filepath.Join(consts.RSPAMD_LOCAL_D_PATH, "dkim_signing.conf")), strings.Join(lines, ""))++ if err != nil {+ g.Log().Warning(ctx, "Failed to write updated DKIM signing configuration file: %v", err)+ return+ }++ dk, err := docker.NewDockerAPI()++ if err != nil {+ g.Log().Warning(ctx, "Failed to create Docker API instance: %v", err)+ return+ }++ defer dk.Close()++ // Restart the Rspamd container to apply the changes+ err = dk.RestartContainer(ctx, "billionmail-rspamd-billionmail-1")++ if err != nil {+ g.Log().Warning(ctx, "Failed to restart Rspamd container: %v", err)+ return+ }+}++func FixDovecotSSLConfig(ctx context.Context) {+ g.Log().Debug(ctx, "Fix Dovecot SSL config")+ defer func() {+ g.Log().Debug(ctx, "Fix Dovecot SSL config done")+ }()++ lines := make([]string, 0)+ appendLines := make([]string, 0, 1)+ appendLineFlag := false++ appendLines = append(appendLines, "\n")++ // Read the Dovecot SSL configuration file each rows+ err := public.ReadEach(public.AbsPath(filepath.Join(consts.DOVECOT_CONF_D_PATH, "10-ssl.conf")), func(row string, cnt int) bool {+ if strings.HasPrefix(strings.TrimSpace(row), "#DOMAIN_SSL_END_") {+ appendLineFlag = false+ appendLines = append(appendLines, row)+ return true+ }++ if appendLineFlag || strings.HasPrefix(strings.TrimSpace(row), "#DOMAIN_SSL_BEGIN_") {+ appendLineFlag = true+ appendLines = append(appendLines, row)+ return true+ }++ lines = append(lines, row)+ return true+ })++ if err != nil {+ g.Log().Warning(ctx, "Failed to read Dovecot SSL configuration file: %v", err)+ return+ }++ lineLen := len(lines)+ lineEnd := -1++ for i := lineLen - 1; i > -1; i-- {+ if strings.TrimSpace(lines[i]) != "" {+ break+ }+ lineEnd = i+ }++ if lineEnd > 0 {+ lines = lines[:lineEnd]+ }++ lines = append(lines, appendLines...)++ // Write the updated lines back to the Dovecot SSL configuration file+ _, err = public.WriteFile(public.AbsPath(filepath.Join(consts.DOVECOT_CONF_D_PATH, "10-ssl.conf")), strings.Join(lines, ""))++ if err != nil {+ g.Log().Warning(ctx, "Failed to write updated Dovecot SSL configuration file: %v", err)+ return+ }++ dk, err := docker.NewDockerAPI()++ if err != nil {+ g.Log().Warning(ctx, "Failed to create Docker API instance: %v", err)+ return+ }++ defer dk.Close()++ // Restart the Dovecot container to apply the changes+ err = dk.RestartContainer(ctx, "billionmail-dovecot-billionmail-1")++ if err != nil {+ g.Log().Warning(ctx, "Failed to restart Dovecot container: %v", err)+ return+ }+}diff --git a/core/internal/service/timers/timers.go b/core/internal/service/timers/timers.goindex 88c59503..ef2cf116 100644--- a/core/internal/service/timers/timers.go+++ b/core/internal/service/timers/timers.go@@ -5,6 +5,7 @@ import ("billionmail-core/internal/service/collect""billionmail-core/internal/service/domains""billionmail-core/internal/service/mail_boxes"+ "billionmail-core/internal/service/mail_service""billionmail-core/internal/service/maillog_stat""billionmail-core/internal/service/relay""context"@@ -83,6 +84,13 @@ func Start(ctx context.Context) (err error) {collect.Collect(ctx)})+ // Fix Postfix main configuration and Rspamd DKIM signing config+ gtimer.AddOnce(5*time.Second, func() {+ mail_service.FixPostfixMainConfig(ctx)+ mail_service.FixRspamdDKIMSigningConfig(ctx)+ mail_service.FixDovecotSSLConfig(ctx)+ })+// ========== Mail task processing: one executor per task ==========gtimer.Add(5*time.Second, func() {batch_mail.ProcessEmailTasks(ctx)