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

Add catchall function to domain

lotkd0a4a92e8ff5
diff --git a/core/api/domains/v1/domains.go b/core/api/domains/v1/domains.go
index ff27420c..4ead157c 100644
--- a/core/api/domains/v1/domains.go
+++ b/core/api/domains/v1/domains.go
@@ -45,6 +45,7 @@ type Domain struct {
Active int `json:"active" dc:"Status: 1-enabled, 0-disabled"`
DNSRecords DNSRecords `json:"dns_records" dc:"DNS records"`
CertInfo CertInfo `json:"cert_info" dc:"Certificate information"`
+ Catchall string `json:"email" dc:"Cache all DNS records, used for domain verification"`
}
type AddDomainReq struct {
@@ -55,6 +56,7 @@ type AddDomainReq struct {
MailboxQuota int `json:"mailboxQuota" v:"min:1" dc:"MailboxQuota" d:"5242880"`
Quota int `json:"quota" v:"required" dc:"Quota" d:"10485760"`
RateLimit int `json:"rateLimit" v:"min:1" dc:"RateLimit" d:"12"`
+ Catchall string `json:"email" v:"email" dc:"Catch all email address, used for domain verification"`
}
type AddDomainRes struct {
@@ -70,6 +72,7 @@ type UpdateDomainReq struct {
Quota int `json:"quota" v:"required" dc:"Quota" d:"10485760"`
RateLimit int `json:"rateLimit" v:"min:1" dc:"RateLimit" d:"12"`
Active int `json:"active" v:"required" dc:"Active" d:"1"`
+ Catchall string `json:"email" v:"email" dc:"Catch all email address, used for domain verification"`
}
type UpdateDomainRes struct {
diff --git a/core/internal/controller/domains/domains_v1_add_domain.go b/core/internal/controller/domains/domains_v1_add_domain.go
index 8e09cf9b..ac8bf708 100644
--- a/core/internal/controller/domains/domains_v1_add_domain.go
+++ b/core/internal/controller/domains/domains_v1_add_domain.go
@@ -21,6 +21,7 @@ func (c *ControllerV1) AddDomain(ctx context.Context, req *v1.AddDomainReq) (res
MailboxQuota: int64(req.MailboxQuota),
Quota: int64(req.Quota),
RateLimit: req.RateLimit,
+ Catchall: req.Catchall,
}
if err = domains.Add(ctx, domain); err != nil {
diff --git a/core/internal/controller/domains/domains_v1_update_domain.go b/core/internal/controller/domains/domains_v1_update_domain.go
index f49ea660..29a51e38 100644
--- a/core/internal/controller/domains/domains_v1_update_domain.go
+++ b/core/internal/controller/domains/domains_v1_update_domain.go
@@ -24,6 +24,7 @@ func (c *ControllerV1) UpdateDomain(ctx context.Context, req *v1.UpdateDomainReq
Quota: int64(req.Quota),
RateLimit: req.RateLimit,
Active: req.Active,
+ Catchall: req.Catchall,
}
if err = domains.Update(ctx, domain); err != nil {
diff --git a/core/internal/service/domains/domains.go b/core/internal/service/domains/domains.go
index ff5117bd..8c038f31 100644
--- a/core/internal/service/domains/domains.go
+++ b/core/internal/service/domains/domains.go
@@ -25,6 +25,51 @@ var (
mutex sync.Mutex
)
+func setCatchall(ctx context.Context, domainName, catchall string) error {
+ address := fmt.Sprintf("@%s", domainName)
+ //g.Log().Warningf(ctx, "Setting catchall 11111 domainName:%s , address: %s, goto: %s,", domainName, address, catchall)
+ if catchall != "" {
+ var count int
+ count, err := g.DB().Model("alias").
+ Where("address", address).
+ Where("domain", domainName).
+ Count()
+ if err != nil {
+ return fmt.Errorf("failed to check alias existence: %v", err)
+ }
+ if count > 0 {
+ _, err = g.DB().Model("alias").
+ Where("address", address).
+ Where("domain", domainName).
+ Data(g.Map{"goto": catchall, "active": 1, "update_time": time.Now().Unix()}).
+ Update()
+ if err != nil {
+ return fmt.Errorf("failed to update alias: %v", err)
+ }
+ } else {
+ _, err = g.DB().Model("alias").Data(g.Map{
+ "address": address,
+ "goto": catchall,
+ "domain": domainName,
+ "active": 1,
+ "create_time": time.Now().Unix(),
+ "update_time": time.Now().Unix(),
+ }).Insert()
+ if err != nil {
+ return fmt.Errorf("failed to insert alias: %v", err)
+ }
+ }
+ } else {
+ // catchall is empty, disabled
+ _, _ = g.DB().Model("alias").
+ Where("address", address).
+ Where("domain", domainName).
+ Data(g.Map{"active": 0, "update_time": time.Now().Unix()}).
+ Update()
+ }
+ return nil
+}
+
func Add(ctx context.Context, domain *v1.Domain) error {
domain.CreateTime = time.Now().Unix()
domain.Active = 1
@@ -72,6 +117,12 @@ func Add(ctx context.Context, domain *v1.Domain) error {
return fmt.Errorf("failed to restart postfix container: %v", err)
}
}
+
+ // catchall
+ if domain.Catchall != "" {
+ err = setCatchall(ctx, domain.Domain, domain.Catchall)
+ }
+ return err
}
return err
@@ -82,6 +133,10 @@ func Update(ctx context.Context, domain *v1.Domain) error {
Ctx(ctx).
Where("domain", domain.Domain).
Update(domain)
+
+ // catchall
+ err = setCatchall(ctx, domain.Domain, domain.Catchall)
+
return err
}
@@ -97,6 +152,13 @@ func Delete(ctx context.Context, domainName string) error {
Ctx(ctx).
Where("domain", domainName).
Delete()
+
+ // remove associated alias
+ _, err = g.DB().Model("alias").
+ Ctx(ctx).
+ Where("domain", domainName).
+ Delete()
+
}
return err
@@ -146,6 +208,38 @@ func Get(ctx context.Context, keyword string, page, pageSize int) ([]v1.Domain,
}
}(i, domain)
+
+ // catchall
+ wg.Add(1)
+ go func(i int, domain v1.Domain) {
+ defer wg.Done()
+ type Alias struct {
+ Address string `json:"address" dc:"Alias address"`
+ Goto string `json:"goto" dc:"Forwarding target"`
+ Domain string `json:"domain" dc:"Domain name"`
+ CreateTime int64 `json:"create_time" dc:"Creation time"`
+ UpdateTime int64 `json:"update_time" dc:"Update time"`
+ Active int `json:"active" dc:"Status: 1-enabled, 0-disabled"`
+ }
+
+ address := fmt.Sprintf("@%s", domain.Domain)
+ var alias Alias
+ err := g.DB().Model("alias").
+ Where("address = ? AND domain = ?", address, domain.Domain).
+ Scan(&alias)
+
+ if err != nil {
+ domains[i].Catchall = ""
+
+ } else {
+ if alias.Active == 1 {
+ domains[i].Catchall = alias.Goto
+ } else {
+ domains[i].Catchall = ""
+ }
+ }
+
+ }(i, domain)
}
wg.Wait()