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

optimized maillog_stat module. change maillog_stat tables "log_time_millis" to BIGINT type. fixed some bugs.

Jack8a9e92dc3447
diff --git a/core/internal/service/database_initialization/maillog_stat.go b/core/internal/service/database_initialization/maillog_stat.go
index 9e7a5612..24c34cba 100644
--- a/core/internal/service/database_initialization/maillog_stat.go
+++ b/core/internal/service/database_initialization/maillog_stat.go
@@ -11,7 +11,7 @@ func init() {
`-- Mail removal records from queue
CREATE TABLE IF NOT EXISTS mailstat_removed (
postfix_message_id TEXT PRIMARY KEY,
- log_time_millis INTEGER NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
+ log_time_millis BIGINT NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
log_time INTEGER GENERATED ALWAYS AS (log_time_millis / 1000) STORED
)`,
@@ -22,7 +22,7 @@ func init() {
`-- Mail MessageIDs
CREATE TABLE IF NOT EXISTS mailstat_message_ids (
postfix_message_id TEXT PRIMARY KEY,
- log_time_millis INTEGER NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
+ log_time_millis BIGINT NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
log_time INTEGER GENERATED ALWAYS AS (log_time_millis / 1000) STORED,
message_id TEXT NOT NULL DEFAULT ''
)`,
@@ -35,7 +35,7 @@ func init() {
`-- Mail senders
CREATE TABLE IF NOT EXISTS mailstat_senders (
postfix_message_id TEXT PRIMARY KEY,
- log_time_millis INTEGER NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
+ log_time_millis BIGINT NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
log_time INTEGER GENERATED ALWAYS AS (log_time_millis / 1000) STORED,
size INTEGER NOT NULL DEFAULT 0,
sender TEXT NOT NULL DEFAULT ''
@@ -48,7 +48,7 @@ func init() {
`-- Mail sending records (unique records, keep latest)
CREATE TABLE IF NOT EXISTS mailstat_send_mails (
postfix_message_id TEXT PRIMARY KEY,
- log_time_millis INTEGER NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
+ log_time_millis BIGINT NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
log_time INTEGER GENERATED ALWAYS AS (log_time_millis / 1000) STORED,
status TEXT NOT NULL DEFAULT '',
recipient TEXT NOT NULL DEFAULT '',
@@ -68,7 +68,7 @@ func init() {
`-- Mail receiving records (unique records, keep latest)
CREATE TABLE IF NOT EXISTS mailstat_receive_mails (
postfix_message_id TEXT PRIMARY KEY,
- log_time_millis INTEGER NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
+ log_time_millis BIGINT NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
log_time INTEGER GENERATED ALWAYS AS (log_time_millis / 1000) STORED,
status TEXT NOT NULL DEFAULT '',
recipient TEXT NOT NULL DEFAULT '',
@@ -87,7 +87,7 @@ func init() {
CREATE TABLE IF NOT EXISTS mailstat_deferred_mails (
id SERIAL PRIMARY KEY,
postfix_message_id TEXT NOT NULL DEFAULT '',
- log_time_millis INTEGER NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
+ log_time_millis BIGINT NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
log_time INTEGER GENERATED ALWAYS AS (log_time_millis / 1000) STORED,
delay REAL NOT NULL DEFAULT 0,
delays TEXT NOT NULL DEFAULT '',
@@ -104,7 +104,7 @@ func init() {
CREATE TABLE IF NOT EXISTS mailstat_opened (
id SERIAL PRIMARY KEY,
campaign_id INTEGER NOT NULL DEFAULT 0,
- log_time_millis INTEGER NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
+ log_time_millis BIGINT NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
log_time INTEGER GENERATED ALWAYS AS (log_time_millis / 1000) STORED,
recipient TEXT NOT NULL DEFAULT '',
postfix_message_id TEXT NOT NULL DEFAULT '',
@@ -120,7 +120,7 @@ func init() {
CREATE TABLE IF NOT EXISTS mailstat_clicked (
id SERIAL PRIMARY KEY,
campaign_id INTEGER NOT NULL DEFAULT 0,
- log_time_millis INTEGER NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
+ log_time_millis BIGINT NOT NULL DEFAULT (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)::INTEGER),
log_time INTEGER GENERATED ALWAYS AS (log_time_millis / 1000) STORED,
recipient TEXT NOT NULL DEFAULT '',
url TEXT NOT NULL DEFAULT '',
diff --git a/core/internal/service/database_initialization/options.go b/core/internal/service/database_initialization/options.go
index ac1c945e..5cd82b52 100644
--- a/core/internal/service/database_initialization/options.go
+++ b/core/internal/service/database_initialization/options.go
@@ -10,8 +10,7 @@ func init() {
sqlList := []string{
`-- BillionMail Options
CREATE TABLE IF NOT EXISTS bm_options (
- id serial PRIMARY KEY,
- name VARCHAR(255) NOT NULL UNIQUE,
+ name VARCHAR(255) NOT NULL PRIMARY KEY,
value TEXT NOT NULL
)`,
}
diff --git a/core/internal/service/maillog_stat/maillog_stat.go b/core/internal/service/maillog_stat/maillog_stat.go
index 163a89ac..f6d30df7 100644
--- a/core/internal/service/maillog_stat/maillog_stat.go
+++ b/core/internal/service/maillog_stat/maillog_stat.go
@@ -1,12 +1,15 @@
package maillog_stat
import (
+ "billionmail-core/internal/consts"
"billionmail-core/internal/service/public"
"context"
"fmt"
+ "github.com/gogf/gf/os/gfsnotify"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"os"
+ "path/filepath"
"regexp"
"strconv"
"strings"
@@ -15,18 +18,62 @@ import (
// Define constants
var (
- defaultMaillogPath = public.AbsPath("../logs/postfix/mail.log")
- defaultPostfixMainConf = public.AbsPath("../conf/postfix/main.cf")
- defaultLatestLogTimeFile = "data/last_maillog_time"
- defaultLatestAggregateMaillogTimeFile = "data/last_aggregate_maillog_time"
+ defaultMaillogPath = public.AbsPath(filepath.Join(consts.POSTFIX_MAILLOG_PATH, "mail.log"))
+ defaultPostfixMainConf = public.AbsPath(consts.POSTFIX_MAIN_CONF)
)
+// GetLastMaillogTimeMillis retrieves the last mail log time in milliseconds
+func GetLastMaillogTimeMillis() int64 {
+ val, err := g.DB().Model("bm_options").Where("name", "last_maillog_time_millis").Value("value")
+
+ if err != nil {
+ g.Log().Error(context.Background(), "GetLastMaillogTimeMillis error:", err.Error())
+ return 0
+ }
+
+ if val == nil {
+ return 0
+ }
+
+ return val.Int64()
+}
+
+// SetLastMaillogTimeMillis sets the last mail log time in milliseconds
+func SetLastMaillogTimeMillis(timeMillis int64) {
+ _, err := g.DB().Model("bm_options").OnConflict("name").OnDuplicate(g.Map{
+ "value": gdb.Raw("excluded.value"),
+ }).Save(g.Map{
+ "name": "last_maillog_time_millis",
+ "value": timeMillis,
+ })
+
+ if err != nil {
+ g.Log().Error(context.Background(), "SetLastMaillogTimeMillis error:", err.Error())
+ return
+ }
+}
+
+type MailRecorfContract interface {
+ GetPostfixMessageID() string
+ GetLogTimeMillis() int64
+}
+
// MailRecord basic mail record structure
type MailRecord struct {
PostfixMessageID string `json:"postfix_message_id"`
LogTimeMillis int64 `json:"log_time_millis"`
}
+// GetPostfixMessageID returns the Postfix message ID
+func (m *MailRecord) GetPostfixMessageID() string {
+ return m.PostfixMessageID
+}
+
+// GetLogTimeMillis returns the log time in milliseconds
+func (m *MailRecord) GetLogTimeMillis() int64 {
+ return m.LogTimeMillis
+}
+
// MailSendRecord mail sending record
type MailSendRecord struct {
MailRecord
@@ -111,6 +158,10 @@ type MaillogStat struct {
// NewMaillogStat creates a new mail log statistics analyzer
func NewMaillogStat(maillogPath string, startTime, endTime int64, doSummary bool) *MaillogStat {
+ if maillogPath == "" {
+ maillogPath = defaultMaillogPath
+ }
+
ms := &MaillogStat{
maillogPath: maillogPath,
startTime: startTime,
@@ -309,14 +360,14 @@ func isDigit(s string) bool {
}
// Analysis analyzes mail logs
-func (ms *MaillogStat) Analysis(ctx context.Context) (<-chan interface{}, error) {
+func (ms *MaillogStat) Analysis(ctx context.Context) (<-chan MailRecorfContract, error) {
ms.resetData()
if _, err := os.Stat(ms.maillogPath); err != nil {
return nil, err
}
- recordChan := make(chan interface{}, 1000)
+ recordChan := make(chan MailRecorfContract, 1000)
go func() {
defer close(recordChan)
@@ -328,7 +379,7 @@ func (ms *MaillogStat) Analysis(ctx context.Context) (<-chan interface{}, error)
record := ms.analyzeLine(line)
if record != nil {
if r, ok := record.(*MailSendRecord); ok && r.Status == "deferred" {
- recordChan <- MailDeferredRecord{
+ recordChan <- &MailDeferredRecord{
MailRecord: MailRecord{
PostfixMessageID: r.PostfixMessageID,
LogTimeMillis: r.LogTimeMillis,
@@ -356,7 +407,7 @@ func (ms *MaillogStat) Analysis(ctx context.Context) (<-chan interface{}, error)
}
// analyzeLine analyzes single log line
-func (ms *MaillogStat) analyzeLine(line string) interface{} {
+func (ms *MaillogStat) analyzeLine(line string) MailRecorfContract {
line = strings.TrimSpace(line)
logTime := ms.parseLogTimeMillis(line)
@@ -394,11 +445,14 @@ func (ms *MaillogStat) analyzeLine(line string) interface{} {
return ms.analyzeSendMail(line, logTime)
}
- return nil
+ return &MailRecord{
+ PostfixMessageID: "",
+ LogTimeMillis: logTime,
+ }
}
// analyzeReceiveMail analyzes mail receiving logs
-func (ms *MaillogStat) analyzeReceiveMail(line string, logTime int64) interface{} {
+func (ms *MaillogStat) analyzeReceiveMail(line string, logTime int64) MailRecorfContract {
matches := ms.messageIDPattern.FindStringSubmatch(line)
if matches == nil {
return nil
@@ -451,7 +505,7 @@ func (ms *MaillogStat) analyzeReceiveMail(line string, logTime int64) interface{
}
// analyzeSendMail analyzes mail sending logs
-func (ms *MaillogStat) analyzeSendMail(line string, logTime int64) interface{} {
+func (ms *MaillogStat) analyzeSendMail(line string, logTime int64) MailRecorfContract {
matches := ms.messageIDPattern.FindStringSubmatch(line)
if matches == nil {
return nil
@@ -528,7 +582,7 @@ func (ms *MaillogStat) analyzeSendMail(line string, logTime int64) interface{} {
}
// analyzeMessageID analyzes MessageID logs
-func (ms *MaillogStat) analyzeMessageID(line string, logTime int64) interface{} {
+func (ms *MaillogStat) analyzeMessageID(line string, logTime int64) MailRecorfContract {
matches := ms.messageIDPattern.FindStringSubmatch(line)
if matches == nil {
return nil
@@ -549,7 +603,7 @@ func (ms *MaillogStat) analyzeMessageID(line string, logTime int64) interface{}
}
// analyzeRemoved analyzes mail removed logs
-func (ms *MaillogStat) analyzeRemoved(line string, logTime int64) interface{} {
+func (ms *MaillogStat) analyzeRemoved(line string, logTime int64) MailRecorfContract {
matches := ms.mailRemovedPattern.FindStringSubmatch(line)
if matches == nil {
return nil
@@ -564,7 +618,7 @@ func (ms *MaillogStat) analyzeRemoved(line string, logTime int64) interface{} {
}
// analyzeSender analyzes sender logs
-func (ms *MaillogStat) analyzeSender(line string, logTime int64) interface{} {
+func (ms *MaillogStat) analyzeSender(line string, logTime int64) MailRecorfContract {
matches := ms.mailSenderPattern.FindStringSubmatch(line)
if matches == nil {
return nil
@@ -592,19 +646,43 @@ func (ms *MaillogStat) AnalysisAndSaveToDatabase(ctx context.Context) error {
return err
}
- sendRecords := make([]*MailSendRecord, 256)
- receiveRecords := make([]*MailReceiveRecord, 256)
- messageIDRecords := make([]*MailMessageID, 256)
- senderRecords := make([]*MailSender, 256)
- removedRecords := make([]*MailRemoved, 256)
- deferredRecords := make([]*MailDeferredRecord, 256)
+ lastMaillogTime := GetLastMaillogTimeMillis()
+ sendRecords := make([]*MailSendRecord, 0, 256)
+ receiveRecords := make([]*MailReceiveRecord, 0, 256)
+ messageIDRecords := make([]*MailMessageID, 0, 256)
+ senderRecords := make([]*MailSender, 0, 256)
+ removedRecords := make([]*MailRemoved, 0, 256)
+ deferredRecords := make([]*MailDeferredRecord, 0, 256)
+
+ existsPostfixMessageIdsForSends := make(map[string]struct{}, 256)
+ existsPostfixMessageIdsForReceives := make(map[string]struct{}, 256)
for record := range recordChan {
+ // g.Log().Debug(context.Background(), "AnalysisAndSaveToDatabase: record", record)
+
+ if record.GetLogTimeMillis() > lastMaillogTime {
+ lastMaillogTime = record.GetLogTimeMillis()
+ }
+
switch r := record.(type) {
case *MailSendRecord:
+ if _, ok := existsPostfixMessageIdsForSends[r.GetPostfixMessageID()]; ok {
+ continue
+ }
+ r.Description = public.SanitizeUTF8(r.Description)
+ r.Relay = public.SanitizeUTF8(r.Relay)
+ r.Dsn = public.SanitizeUTF8(r.Dsn)
sendRecords = append(sendRecords, r)
+ existsPostfixMessageIdsForSends[r.GetPostfixMessageID()] = struct{}{}
case *MailReceiveRecord:
+ if _, ok := existsPostfixMessageIdsForReceives[r.GetPostfixMessageID()]; ok {
+ continue
+ }
+ r.Description = public.SanitizeUTF8(r.Description)
+ r.Relay = public.SanitizeUTF8(r.Relay)
+ r.Dsn = public.SanitizeUTF8(r.Dsn)
receiveRecords = append(receiveRecords, r)
+ existsPostfixMessageIdsForReceives[r.GetPostfixMessageID()] = struct{}{}
case *MailSender:
senderRecords = append(senderRecords, r)
case *MailRemoved:
@@ -612,65 +690,79 @@ func (ms *MaillogStat) AnalysisAndSaveToDatabase(ctx context.Context) error {
case *MailMessageID:
messageIDRecords = append(messageIDRecords, r)
case *MailDeferredRecord:
+ r.Description = public.SanitizeUTF8(r.Description)
+ r.Relay = public.SanitizeUTF8(r.Relay)
+ r.Dsn = public.SanitizeUTF8(r.Dsn)
deferredRecords = append(deferredRecords, r)
default:
continue
}
}
+ if lastMaillogTime > 0 {
+ g.Log().Debug(context.Background(), "AnalysisAndSaveToDatabase: lastMaillogTime", lastMaillogTime)
+ SetLastMaillogTimeMillis(lastMaillogTime)
+ }
+
if len(sendRecords) > 0 {
- _, err = g.DB().Model("mailstat_send_mails").OnDuplicate(g.Map{
- "status": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.status else status end"),
- "delay": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.delay else delay end"),
- "delays": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.delays else delays end"),
- "dsn": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.dsn else dsn end"),
- "relay": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.relay else relay end"),
- "description": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.description else description end"),
- "log_time_millis": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.log_time_millis else log_time_millis end"),
- }).Insert(sendRecords)
+ g.Log().Debug(context.Background(), "AnalysisAndSaveToDatabase: sendRecords", len(sendRecords))
+ _, err = g.DB().Model("mailstat_send_mails").OnConflict("postfix_message_id").OnDuplicate(g.Map{
+ "status": gdb.Raw("case when excluded.log_time_millis > mailstat_send_mails.log_time_millis then excluded.status else mailstat_send_mails.status end"),
+ "delay": gdb.Raw("case when excluded.log_time_millis > mailstat_send_mails.log_time_millis then excluded.delay else mailstat_send_mails.delay end"),
+ "delays": gdb.Raw("case when excluded.log_time_millis > mailstat_send_mails.log_time_millis then excluded.delays else mailstat_send_mails.delays end"),
+ "dsn": gdb.Raw("case when excluded.log_time_millis > mailstat_send_mails.log_time_millis then excluded.dsn else mailstat_send_mails.dsn end"),
+ "relay": gdb.Raw("case when excluded.log_time_millis > mailstat_send_mails.log_time_millis then excluded.relay else mailstat_send_mails.relay end"),
+ "description": gdb.Raw("case when excluded.log_time_millis > mailstat_send_mails.log_time_millis then excluded.description else mailstat_send_mails.description end"),
+ "log_time_millis": gdb.Raw("case when excluded.log_time_millis > mailstat_send_mails.log_time_millis then excluded.log_time_millis else mailstat_send_mails.log_time_millis end"),
+ }).Batch(5000).Save(sendRecords)
if err != nil {
g.Log().Error(context.Background(), err.Error())
}
}
if len(receiveRecords) > 0 {
- _, err = g.DB().Model("mailstat_receive_mails").OnDuplicate(g.Map{
- "status": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.status else status end"),
- "delay": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.delay else delay end"),
- "delays": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.delays else delays end"),
- "dsn": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.dsn else dsn end"),
- "relay": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.relay else relay end"),
- "description": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.description else description end"),
- "log_time_millis": gdb.Raw("case when excluded.log_time_millis > log_time_millis then excluded.log_time_millis else log_time_millis end"),
- }).Insert(receiveRecords)
+ g.Log().Debug(context.Background(), "AnalysisAndSaveToDatabase: receiveRecords", len(receiveRecords))
+ _, err = g.DB().Model("mailstat_receive_mails").OnConflict("postfix_message_id").OnDuplicate(g.Map{
+ "status": gdb.Raw("case when excluded.log_time_millis > mailstat_receive_mails.log_time_millis then excluded.status else mailstat_receive_mails.status end"),
+ "delay": gdb.Raw("case when excluded.log_time_millis > mailstat_receive_mails.log_time_millis then excluded.delay else mailstat_receive_mails.delay end"),
+ "delays": gdb.Raw("case when excluded.log_time_millis > mailstat_receive_mails.log_time_millis then excluded.delays else mailstat_receive_mails.delays end"),
+ "dsn": gdb.Raw("case when excluded.log_time_millis > mailstat_receive_mails.log_time_millis then excluded.dsn else mailstat_receive_mails.dsn end"),
+ "relay": gdb.Raw("case when excluded.log_time_millis > mailstat_receive_mails.log_time_millis then excluded.relay else mailstat_receive_mails.relay end"),
+ "description": gdb.Raw("case when excluded.log_time_millis > mailstat_receive_mails.log_time_millis then excluded.description else mailstat_receive_mails.description end"),
+ "log_time_millis": gdb.Raw("case when excluded.log_time_millis > mailstat_receive_mails.log_time_millis then excluded.log_time_millis else mailstat_receive_mails.log_time_millis end"),
+ }).Batch(5000).Save(receiveRecords)
if err != nil {
g.Log().Error(context.Background(), err.Error())
}
}
if len(senderRecords) > 0 {
- _, err = g.DB().Model("mailstat_senders").Insert(senderRecords)
+ g.Log().Debug(context.Background(), "AnalysisAndSaveToDatabase: sendRecords", len(senderRecords))
+ _, err = g.DB().Model("mailstat_senders").Batch(5000).InsertIgnore(senderRecords)
if err != nil {
g.Log().Error(context.Background(), err.Error())
}
}
if len(removedRecords) > 0 {
- _, err = g.DB().Model("mailstat_removed").Insert(removedRecords)
+ g.Log().Debug(context.Background(), "AnalysisAndSaveToDatabase: removedRecords", len(removedRecords))
+ _, err = g.DB().Model("mailstat_removed").Batch(5000).InsertIgnore(removedRecords)
if err != nil {
g.Log().Error(context.Background(), err.Error())
}
}
if len(messageIDRecords) > 0 {
- _, err = g.DB().Model("mailstat_message_ids").Insert(messageIDRecords)
+ g.Log().Debug(context.Background(), "AnalysisAndSaveToDatabase: messageIDRecords", len(messageIDRecords))
+ _, err = g.DB().Model("mailstat_message_ids").Batch(5000).InsertIgnore(messageIDRecords)
if err != nil {
g.Log().Error(context.Background(), err.Error())
}
}
if len(deferredRecords) > 0 {
- _, err = g.DB().Model("mailstat_deferred").Insert(deferredRecords)
+ g.Log().Debug(context.Background(), "AnalysisAndSaveToDatabase: deferredRecords", len(deferredRecords))
+ _, err = g.DB().Model("mailstat_deferred_mails").Batch(5000).Insert(deferredRecords)
if err != nil {
g.Log().Error(context.Background(), err.Error())
}
@@ -678,3 +770,58 @@ func (ms *MaillogStat) AnalysisAndSaveToDatabase(ctx context.Context) error {
return nil
}
+
+// MallogEventHandler handles mail log events
+type MallogEventHandler struct {
+ maillogStat *MaillogStat
+ delay time.Duration
+ timer *time.Timer
+}
+
+// NewMallogEventHandler creates a new mail log event handler
+func NewMallogEventHandler(maillogPath string, delay time.Duration) *MallogEventHandler {
+ return &MallogEventHandler{
+ maillogStat: NewMaillogStat(maillogPath, GetLastMaillogTimeMillis(), 0, false),
+ delay: delay,
+ timer: time.NewTimer(delay),
+ }
+}
+
+// Start starts the mail log event handler
+func (handler *MallogEventHandler) Start() {
+ _, err := gfsnotify.Add(handler.maillogStat.maillogPath, func(event *gfsnotify.Event) {
+ g.Log().Debug(context.Background(), "MallogEventHandler: catch event", event)
+
+ if event.IsWrite() {
+ g.Log().Debug(context.Background(), "MallogEventHandler: write event", event)
+ if event.Path == handler.maillogStat.maillogPath {
+ if !handler.timer.Stop() {
+ <-handler.timer.C
+ }
+ handler.timer.Reset(handler.delay)
+ }
+ }
+
+ if event.IsRename() {
+ g.Log().Debug(context.Background(), "MallogEventHandler: rename event", event)
+ }
+ })
+
+ if err != nil {
+ g.Log().Error(context.Background(), "MallogEventHandler: error adding file watcher", err)
+ return
+ }
+
+ g.Log().Debug(context.Background(), "MallogEventHandler: start event")
+
+ for {
+ select {
+ case <-handler.timer.C:
+ g.Log().Debug(context.Background(), "MallogEventHandler: timer event")
+ handler.maillogStat.startTime = GetLastMaillogTimeMillis()
+ if err = handler.maillogStat.AnalysisAndSaveToDatabase(context.Background()); err != nil {
+ g.Log().Error(context.Background(), "MallogEventHandler: error analysing maillog", err)
+ }
+ }
+ }
+}
diff --git a/core/internal/service/public/common.go b/core/internal/service/public/common.go
index a9455ed4..ac15a243 100644
--- a/core/internal/service/public/common.go
+++ b/core/internal/service/public/common.go
@@ -28,6 +28,7 @@ import (
"strconv"
"strings"
"time"
+ "unicode/utf8"
"github.com/gogf/gf/v2/util/gconv"
@@ -2429,3 +2430,22 @@ func checkCgroup(path string) bool {
}
return false
}
+
+// SanitizeUTF8 clean up invalid UTF-8 characters in a string
+func SanitizeUTF8(s string) string {
+ if utf8.ValidString(s) {
+ return s
+ }
+
+ // substitute invalid UTF-8 characters with a replacement character
+ result := make([]rune, 0, len(s))
+ for i, r := range s {
+ if r == utf8.RuneError && i < len(s) {
+ // skip invalid UTF-8 characters
+ continue
+ }
+ result = append(result, r)
+ }
+
+ return string(result)
+}
diff --git a/core/internal/service/timers/timers.go b/core/internal/service/timers/timers.go
index 3b1c91a1..888c6008 100644
--- a/core/internal/service/timers/timers.go
+++ b/core/internal/service/timers/timers.go
@@ -2,6 +2,7 @@ package timers
import (
"billionmail-core/internal/service/domains"
+ "billionmail-core/internal/service/maillog_stat"
"context"
"github.com/gogf/gf/os/gtimer"
"github.com/gogf/gf/v2/frame/g"
@@ -21,6 +22,12 @@ func Start(ctx context.Context) (err error) {
domains.FreshRecords(ctx)
})
+ // Start maillog analysis
+ gtimer.AddOnce(5*time.Second, func() {
+ me := maillog_stat.NewMallogEventHandler("", 1*time.Second)
+ me.Start()
+ })
+
g.Log().Debug(ctx, "Start timers complete")
return
diff --git a/core/manifest/config/config.yaml b/core/manifest/config/config.yaml
index 3d36763b..b29e05a1 100644
--- a/core/manifest/config/config.yaml
+++ b/core/manifest/config/config.yaml
@@ -13,7 +13,7 @@ server:
indexFiles: ["index.html", "index.htm"]
indexFolder: false
serverRoot: "public/dist"
- searchPaths: []
+ searchPaths: ["public/html"]
fileServerEnabled: true
# Cookie Configuration
diff --git a/core/public/html/1.txt b/core/public/html/1.txt
new file mode 100644
index 00000000..e69de29b