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 DNS record query functions. added manual refresh DNS records.
Jack56ff3568ce0f
diff --git a/core/api/domains/domains.go b/core/api/domains/domains.goindex f344b276..98418c14 100644--- a/core/api/domains/domains.go+++ b/core/api/domains/domains.go@@ -16,6 +16,7 @@ type IDomainsV1 interface {DeleteDomain(ctx context.Context, req *v1.DeleteDomainReq) (res *v1.DeleteDomainRes, err error)GetDomain(ctx context.Context, req *v1.GetDomainReq) (res *v1.GetDomainRes, err error)GetDomainAll(ctx context.Context, req *v1.GetDomainAllReq) (res *v1.GetDomainAllRes, err error)+ FreshDNSRecords(ctx context.Context, req *v1.FreshDNSRecordsReq) (res *v1.FreshDNSRecordsRes, err error)SetSSL(ctx context.Context, req *v1.SetSSLReq) (res *v1.SetSSLRes, err error)GetSSL(ctx context.Context, req *v1.GetSSLReq) (res *v1.GetSSLRes, err error)ApplyCert(ctx context.Context, req *v1.ApplyCertReq) (res *v1.ApplyCertRes, err error)diff --git a/core/api/domains/v1/domains.go b/core/api/domains/v1/domains.goindex a426cc6d..2fa7ce3d 100644--- a/core/api/domains/v1/domains.go+++ b/core/api/domains/v1/domains.go@@ -112,6 +112,17 @@ type GetDomainAllRes struct {Data []Domain `json:"data"`}+type FreshDNSRecordsReq struct {+ g.Meta `path:"/domains/fresh_dns_records" tags:"Domain" method:"post" sm:"Refresh DNS records" in:"body"`+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`+ Domain string `json:"domain" v:"required|domain" dc:"Domain"`+}++type FreshDNSRecordsRes struct {+ api_v1.StandardRes+ Data DNSRecords `json:"dns_records" dc:"DNS records"`+}+type SetSSLReq struct {g.Meta `path:"/domains/set_ssl" tags:"Domain" method:"post" sm:"Set SSL" in:"body"`Authorization string `json:"authorization" dc:"Authorization" in:"header"`diff --git a/core/internal/controller/domains/domains_v1_fresh_dns_records.go b/core/internal/controller/domains/domains_v1_fresh_dns_records.gonew file mode 100644index 00000000..b2ba95f9--- /dev/null+++ b/core/internal/controller/domains/domains_v1_fresh_dns_records.go@@ -0,0 +1,20 @@+package domains++import (+ "billionmail-core/internal/service/domains"+ "context"++ "billionmail-core/api/domains/v1"+)++func (c *ControllerV1) FreshDNSRecords(ctx context.Context, req *v1.FreshDNSRecordsReq) (res *v1.FreshDNSRecordsRes, err error) {+ res = &v1.FreshDNSRecordsRes{}++ domains.FreshRecords(ctx, req.Domain)++ res.Data = domains.GetRecordsInCache(req.Domain)++ res.SetSuccess("Success")++ return+}diff --git a/core/internal/service/domains/dns.go b/core/internal/service/domains/dns.goindex d03d636c..0fd2ff40 100644--- a/core/internal/service/domains/dns.go+++ b/core/internal/service/domains/dns.go@@ -2,6 +2,9 @@ package domainsimport (v1 "billionmail-core/api/domains/v1"+ "billionmail-core/internal/service/public"+ "context"+ "github.com/gogf/gf/v2/frame/g""net""strings")@@ -29,15 +32,29 @@ func ValidateARecord(record v1.DNSRecord) bool {}// ValidateTXTRecord checks if the given TXT record is valid-func ValidateTXTRecord(record v1.DNSRecord) bool {+func ValidateTXTRecord(record v1.DNSRecord, domain string) bool {if strings.ToUpper(record.Type) != "TXT" {return false}- // Query TXT records- txtRecords, err := net.LookupTXT(record.Host)- if err != nil {- return false+ cacheKey := "txt_records_" + domain+ txtRecords := make([]string, 0)+ cacheTxtRecords := public.GetCache(cacheKey)++ if cacheTxtRecords != nil {+ txtRecords = cacheTxtRecords.([]string)+ g.Log().Debug(context.Background(), "get txt records from cache", cacheTxtRecords)+ } else {+ // Query TXT records+ var err error+ txtRecords, err = net.LookupTXT(domain)+ if err != nil {+ // g.Log().Error(context.Background(), "query txt records failed", err)+ return false+ }+ g.Log().Debug(context.Background(), "query txt records success", txtRecords)+ // Store TXT records in cache+ public.SetCache(cacheKey, txtRecords, 60)}// Check if any TXT record matches the expected value@@ -59,12 +76,15 @@ func ValidateMXRecord(record v1.DNSRecord, domain string) bool {// Query MX recordsmxRecords, err := net.LookupMX(domain)if err != nil {+ // g.Log().Error(context.Background(), "query mx record failed", err)return false}+ g.Log().Debug(context.Background(), "query mx record success", mxRecords)+// Check if any MX record matches the expected valuefor _, mx := range mxRecords {- if mx.Host == record.Value {+ if strings.TrimSuffix(mx.Host, ".") == record.Value {return true}}diff --git a/core/internal/service/domains/domains.go b/core/internal/service/domains/domains.goindex 26a7c594..4f3ded3c 100644--- a/core/internal/service/domains/domains.go+++ b/core/internal/service/domains/domains.go@@ -120,88 +120,98 @@ func buildCacheKey(domain, recordType string) string {return fmt.Sprintf("DOMAIN_DNS_RECORDS_:%s:_%s", domain, recordType)}-func FreshRecords(ctx context.Context) {+func FreshRecords(ctx context.Context, domain ...string) {g.Log().Debug(ctx, "Fresh DNS records...")- domains, err := All(ctx)+ var domains []string- if err != nil {- return+ if len(domain) > 0 {+ domains = domain+ } else {+ ds, err := All(ctx)++ if err != nil {+ return+ }++ for _, d := range ds {+ domains = append(domains, d.Domain)+ }}cacheSeconds := 600wg := sync.WaitGroup{}- for _, domain := range domains {+ for _, d := range domains {// goroutine for each record typewg.Add(6) // total of 6 record types// retrieve A record- go func(domain v1.Domain) {+ go func(d string) {defer wg.Done()- dr, err := GetARecord(domain.Domain)+ dr, err := GetARecord(d)if err != nil {- g.Log().Error(ctx, "Failed to get A record for domain %s: %v", domain.Domain, err)+ g.Log().Error(ctx, "Failed to get A record for domain %s: %v", d, err)} else {- public.SetCache(buildCacheKey(domain.Domain, "A"), dr, cacheSeconds)+ public.SetCache(buildCacheKey(d, "A"), dr, cacheSeconds)}- }(domain)+ }(d)// retrieve MX record- go func(domain v1.Domain) {+ go func(d string) {defer wg.Done()- dr, err := GetMXRecord(domain.Domain)+ dr, err := GetMXRecord(d)if err != nil {- g.Log().Error(ctx, "Failed to get MX record for domain %s: %v", domain.Domain, err)+ g.Log().Error(ctx, "Failed to get MX record for domain %s: %v", d, err)} else {- public.SetCache(buildCacheKey(domain.Domain, "MX"), dr, cacheSeconds)+ public.SetCache(buildCacheKey(d, "MX"), dr, cacheSeconds)}- }(domain)+ }(d)// retrieve SPF record- go func(domain v1.Domain) {+ go func(d string) {defer wg.Done()- dr, err := GetSPFRecord(domain.Domain)+ dr, err := GetSPFRecord(d)if err != nil {- g.Log().Error(ctx, "Failed to get SPF record for domain %s: %v", domain.Domain, err)+ g.Log().Error(ctx, "Failed to get SPF record for domain %s: %v", d, err)} else {- public.SetCache(buildCacheKey(domain.Domain, "SPF"), dr, cacheSeconds)+ public.SetCache(buildCacheKey(d, "SPF"), dr, cacheSeconds)}- }(domain)+ }(d)// retrieve DKIM record- go func(domain v1.Domain) {+ go func(d string) {defer wg.Done()- dr, err := GetDKIMRecord(domain.Domain)+ dr, err := GetDKIMRecord(d)if err != nil {- g.Log().Error(ctx, "Failed to get DKIM record for domain %s: %v", domain.Domain, err)+ g.Log().Error(ctx, "Failed to get DKIM record for domain %s: %v", d, err)} else {- public.SetCache(buildCacheKey(domain.Domain, "DKIM"), dr, cacheSeconds)+ public.SetCache(buildCacheKey(d, "DKIM"), dr, cacheSeconds)}- }(domain)+ }(d)// retrieve DMARC record- go func(domain v1.Domain) {+ go func(d string) {defer wg.Done()- dr, err := GetDMARCRecord(domain.Domain)+ dr, err := GetDMARCRecord(d)if err != nil {- g.Log().Error(ctx, "Failed to get DMARC record for domain %s: %v", domain.Domain, err)+ g.Log().Error(ctx, "Failed to get DMARC record for domain %s: %v", d, err)} else {- public.SetCache(buildCacheKey(domain.Domain, "DMARC"), dr, cacheSeconds)+ public.SetCache(buildCacheKey(d, "DMARC"), dr, cacheSeconds)}- }(domain)+ }(d)//retrieve PTR record- go func(domain v1.Domain) {+ go func(d string) {defer wg.Done()- dr, err := GetPTRRecord(domain.Domain)+ dr, err := GetPTRRecord(d)if err != nil {- g.Log().Error(ctx, "Failed to get PTR record for domain %s: %v", domain.Domain, err)+ g.Log().Error(ctx, "Failed to get PTR record for domain %s: %v", d, err)} else {- public.SetCache(buildCacheKey(domain.Domain, "PTR"), dr, cacheSeconds)+ public.SetCache(buildCacheKey(d, "PTR"), dr, cacheSeconds)}- }(domain)+ }(d)}wg.Wait()@@ -341,7 +351,7 @@ func GetDKIMRecord(domain string) (record v1.DNSRecord, err error) {}// Validate the DKIM record- record.Valid = ValidateTXTRecord(record)+ record.Valid = ValidateTXTRecord(record, domain)return}@@ -355,7 +365,7 @@ func GetDMARCRecord(domain string) (record v1.DNSRecord, err error) {}// Validate the DMARC record- record.Valid = ValidateTXTRecord(record)+ record.Valid = ValidateTXTRecord(record, domain)return}@@ -383,7 +393,7 @@ func GetSPFRecord(domain string) (record v1.DNSRecord, err error) {}// Validate the SPF record- record.Valid = ValidateTXTRecord(record)+ record.Valid = ValidateTXTRecord(record, domain)return}