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 api sending whitelist ip

lotkaab689f1a406
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go
index 4aa79b8d..fded0123 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go
@@ -120,6 +120,8 @@ func (c *ControllerV1) ApiTemplatesList(ctx context.Context, req *v1.ApiTemplate
Where("api_id", item.Id).
Fields("ip").
Scan(&ipRows)
+ g.Log().Warningf(ctx, "[API List] IP whitelist for API ID %d: %+v", item.Id, ipRows)
+
if err != nil {
g.Log().Error(ctx, "Failed to get IP whitelist:", err)
continue
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_update.go b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_update.go
index 7f08f3c0..0d0d0486 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_update.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_update.go
@@ -74,6 +74,7 @@ func (c *ControllerV1) ApiTemplatesUpdate(ctx context.Context, req *v1.ApiTempla
Where("api_id", req.ID).
Delete()
if err != nil {
+ g.Log().Errorf(ctx, "[API Update] Failed to delete IP whitelist: %v", err)
return nil, err
}
@@ -81,10 +82,12 @@ func (c *ControllerV1) ApiTemplatesUpdate(ctx context.Context, req *v1.ApiTempla
for _, ip := range req.IpWhitelist {
ip = strings.TrimSpace(ip)
if ip == "" {
+ g.Log().Errorf(ctx, "[API Templates Update] Empty IP address")
continue
}
if net.ParseIP(ip) == nil {
+ g.Log().Errorf(ctx, "[API Templates Update] Invalid IP address: %s", ip)
continue
}
_, err = tx.Model("api_ip_whitelist").Insert(g.Map{
diff --git a/core/internal/controller/middleware/ip_whitelist.go b/core/internal/controller/middleware/ip_whitelist.go
index f3aaf8ad..e3c37b9d 100644
--- a/core/internal/controller/middleware/ip_whitelist.go
+++ b/core/internal/controller/middleware/ip_whitelist.go
@@ -51,6 +51,11 @@ func getAllowedIPs() []string {
ips = append(ips, "127.0.0.1")
ips = append(ips, "::1")
+ // Add server IP
+ ipServer, _ := public.GetServerIP()
+ if ipServer != "" {
+ ips = append(ips, ipServer)
+ }
return ips
}
diff --git a/core/internal/service/database_initialization/batch_mail.go b/core/internal/service/database_initialization/batch_mail.go
index 394ada7c..9c76ce7e 100644
--- a/core/internal/service/database_initialization/batch_mail.go
+++ b/core/internal/service/database_initialization/batch_mail.go
@@ -147,31 +147,6 @@ func init() {
`CREATE INDEX IF NOT EXISTS idx_bm_contacts_active ON bm_contacts (active)`,
`CREATE INDEX IF NOT EXISTS idx_abnormal_recipient_count ON abnormal_recipient (recipient,count)`,
`CREATE INDEX IF NOT EXISTS idx_group_email ON bm_contacts(group_id, email)`,
- // attribs
- `DO $$
- BEGIN
- IF NOT EXISTS (
- SELECT 1
- FROM information_schema.columns
- WHERE table_name = 'bm_contacts'
- AND column_name = 'attribs'
- ) THEN
- ALTER TABLE bm_contacts ADD COLUMN attribs JSONB DEFAULT '{}'::jsonb;
- END IF;
- END $$;`,
- // status
- `DO $$
- BEGIN
- IF NOT EXISTS (
- SELECT 1
- FROM information_schema.columns
- WHERE table_name = 'bm_contacts'
- AND column_name = 'status'
- ) THEN
- ALTER TABLE bm_contacts ADD COLUMN status INTEGER DEFAULT 0;
- END IF;
- END $$;`,
-
`CREATE INDEX IF NOT EXISTS idx_api_mail_logs_api_id ON api_mail_logs (api_id)`,
`CREATE INDEX IF NOT EXISTS idx_api_mail_logs_recipient ON api_mail_logs (recipient)`,
`CREATE INDEX IF NOT EXISTS idx_api_mail_logs_message_id ON api_mail_logs (message_id)`,
@@ -184,6 +159,16 @@ func init() {
return
}
}
+ // bm_contacts attribs
+ _ = AddColumnIfNotExists("bm_contacts", "attribs", "JSONB", "'{}'::jsonb", false)
+ // bm_contacts status
+ _ = AddColumnIfNotExists("bm_contacts", "status", "INTEGER", "0", false)
+ // api_mail_logs status
+ _ = AddColumnIfNotExists("api_mail_logs", "status", "SMALLINT", "0", true)
+ // api_mail_logs error_message
+ _ = AddColumnIfNotExists("api_mail_logs", "error_message", "TEXT", "''", false)
+ // api_mail_logs create_time
+ _ = AddColumnIfNotExists("api_mail_logs", "create_time", "INTEGER", "EXTRACT(EPOCH FROM NOW())", true)
g.Log().Info(context.Background(), "Batch mail tables initialized successfully")
})
diff --git a/core/internal/service/database_initialization/utils.go b/core/internal/service/database_initialization/utils.go
new file mode 100644
index 00000000..d9433abf
--- /dev/null
+++ b/core/internal/service/database_initialization/utils.go
@@ -0,0 +1,34 @@
+package database_initialization
+
+import (
+ "context"
+ "fmt"
+ "github.com/gogf/gf/v2/frame/g"
+)
+
+// Automatically adding table fields
+func AddColumnIfNotExists(table, column, columnType, defaultValue string, notNull bool) error {
+ notNullStr := ""
+ if notNull {
+ notNullStr = "NOT NULL"
+ }
+ defaultStr := ""
+ if defaultValue != "" {
+ defaultStr = "DEFAULT " + defaultValue
+ }
+ sql := fmt.Sprintf(`
+DO $$
+BEGIN
+ IF NOT EXISTS (
+ SELECT 1 FROM information_schema.columns
+ WHERE table_name = '%s' AND column_name = '%s'
+ ) THEN
+ ALTER TABLE %s ADD COLUMN %s %s %s %s;
+ END IF;
+END $$;`, table, column, table, column, columnType, notNullStr, defaultStr)
+ _, err := g.DB().Exec(context.Background(), sql)
+ if err != nil {
+ g.Log().Error(context.Background(), "Failed to add column:", err, sql)
+ }
+ return err
+}