77 "fmt"
88 "net/http"
99 "net/url"
10+ "strings"
1011
1112 "github.com/libdns/libdns"
1213)
@@ -59,13 +60,24 @@ func (p *Provider) updateRecord(ctx context.Context, oldRec, newRec cfDNSRecord)
5960}
6061
6162func (p * Provider ) getDNSRecords (ctx context.Context , zoneInfo cfZone , rec libdns.Record , matchContent bool ) ([]cfDNSRecord , error ) {
62- rr := rec .RR ()
63+ rr , err := cloudflareRecord (rec )
64+ if err != nil {
65+ return nil , err
66+ }
6367
6468 qs := make (url.Values )
6569 qs .Set ("type" , rr .Type )
6670 qs .Set ("name" , libdns .AbsoluteName (rr .Name , zoneInfo .Name ))
71+
72+ var unwrappedContent string
6773 if matchContent {
68- qs .Set ("content" , rr .Data )
74+ if rr .Type == "TXT" {
75+ unwrappedContent = unwrapContent (rr .Content )
76+ // Use the contains (wildcard) search with unquoted content to return both quoted and unquoted content
77+ qs .Set ("content.contains" , unwrappedContent )
78+ } else {
79+ qs .Set ("content.exact" , rr .Content )
80+ }
6981 }
7082
7183 reqURL := fmt .Sprintf ("%s/zones/%s/dns_records?%s" , baseURL , zoneInfo .ID , qs .Encode ())
@@ -76,6 +88,26 @@ func (p *Provider) getDNSRecords(ctx context.Context, zoneInfo cfZone, rec libdn
7688
7789 var results []cfDNSRecord
7890 _ , err = p .doAPIRequest (req , & results )
91+
92+ // Since the TXT search used contains (wildcard), check for exact matches
93+ if matchContent && rr .Type == "TXT" {
94+ for i := 0 ; i < len (results ); i ++ {
95+ // Prefer exact quoted content
96+ if results [i ].Content == rr .Content {
97+ return []cfDNSRecord {results [i ]}, nil
98+ }
99+ }
100+
101+ for i := 0 ; i < len (results ); i ++ {
102+ // Using exact unquoted content is acceptable
103+ if results [i ].Content == unwrappedContent {
104+ return []cfDNSRecord {results [i ]}, nil
105+ }
106+ }
107+
108+ return []cfDNSRecord {}, nil
109+ }
110+
79111 return results , err
80112}
81113
@@ -167,3 +199,17 @@ func (p *Provider) doAPIRequest(req *http.Request, result any) (cfResponse, erro
167199}
168200
169201const baseURL = "https://api.cloudflare.com/client/v4"
202+
203+ func unwrapContent (content string ) string {
204+ if strings .HasPrefix (content , `"` ) && strings .HasSuffix (content , `"` ) {
205+ content = strings .TrimPrefix (strings .TrimSuffix (content , `"` ), `"` )
206+ }
207+ return content
208+ }
209+
210+ func wrapContent (content string ) string {
211+ if ! strings .HasPrefix (content , `"` ) && ! strings .HasSuffix (content , `"` ) {
212+ content = fmt .Sprintf ("%q" , content )
213+ }
214+ return content
215+ }
0 commit comments