99 "net/url"
1010 "path"
1111 "strconv"
12- "strings"
1312
1413 "github.com/go-acme/lego/v4/challenge/dns01"
1514)
@@ -89,7 +88,10 @@ func (c *Client) GetZone(authFQDN string) (*Zone, error) {
8988
9089// FindTxtRecord returns the TXT record a zone ID and a FQDN.
9190func (c * Client ) FindTxtRecord (zoneName , fqdn string ) (* TXTRecord , error ) {
92- host := dns01 .UnFqdn (strings .TrimSuffix (dns01 .UnFqdn (fqdn ), zoneName ))
91+ subDomain , err := dns01 .ExtractSubDomain (fqdn , zoneName )
92+ if err != nil {
93+ return nil , err
94+ }
9395
9496 reqURL , err := c .BaseURL .Parse (path .Join (c .BaseURL .Path , "records.json" ))
9597 if err != nil {
@@ -98,7 +100,7 @@ func (c *Client) FindTxtRecord(zoneName, fqdn string) (*TXTRecord, error) {
98100
99101 q := reqURL .Query ()
100102 q .Set ("domain-name" , zoneName )
101- q .Set ("host" , host )
103+ q .Set ("host" , subDomain )
102104 q .Set ("type" , "TXT" )
103105 reqURL .RawQuery = q .Encode ()
104106
@@ -118,7 +120,7 @@ func (c *Client) FindTxtRecord(zoneName, fqdn string) (*TXTRecord, error) {
118120 }
119121
120122 for _ , record := range records {
121- if record .Host == host && record .Type == "TXT" {
123+ if record .Host == subDomain && record .Type == "TXT" {
122124 return & record , nil
123125 }
124126 }
@@ -128,7 +130,10 @@ func (c *Client) FindTxtRecord(zoneName, fqdn string) (*TXTRecord, error) {
128130
129131// ListTxtRecords returns the TXT records a zone ID and a FQDN.
130132func (c * Client ) ListTxtRecords (zoneName , fqdn string ) ([]TXTRecord , error ) {
131- host := dns01 .UnFqdn (strings .TrimSuffix (dns01 .UnFqdn (fqdn ), zoneName ))
133+ subDomain , err := dns01 .ExtractSubDomain (fqdn , zoneName )
134+ if err != nil {
135+ return nil , err
136+ }
132137
133138 reqURL , err := c .BaseURL .Parse (path .Join (c .BaseURL .Path , "records.json" ))
134139 if err != nil {
@@ -137,7 +142,7 @@ func (c *Client) ListTxtRecords(zoneName, fqdn string) ([]TXTRecord, error) {
137142
138143 q := reqURL .Query ()
139144 q .Set ("domain-name" , zoneName )
140- q .Set ("host" , host )
145+ q .Set ("host" , subDomain )
141146 q .Set ("type" , "TXT" )
142147 reqURL .RawQuery = q .Encode ()
143148
@@ -158,7 +163,7 @@ func (c *Client) ListTxtRecords(zoneName, fqdn string) ([]TXTRecord, error) {
158163
159164 var records []TXTRecord
160165 for _ , record := range raw {
161- if record .Host == host && record .Type == "TXT" {
166+ if record .Host == subDomain && record .Type == "TXT" {
162167 records = append (records , record )
163168 }
164169 }
@@ -168,7 +173,10 @@ func (c *Client) ListTxtRecords(zoneName, fqdn string) ([]TXTRecord, error) {
168173
169174// AddTxtRecord adds a TXT record.
170175func (c * Client ) AddTxtRecord (zoneName , fqdn , value string , ttl int ) error {
171- host := dns01 .UnFqdn (strings .TrimSuffix (dns01 .UnFqdn (fqdn ), zoneName ))
176+ subDomain , err := dns01 .ExtractSubDomain (fqdn , zoneName )
177+ if err != nil {
178+ return err
179+ }
172180
173181 reqURL , err := c .BaseURL .Parse (path .Join (c .BaseURL .Path , "add-record.json" ))
174182 if err != nil {
@@ -177,7 +185,7 @@ func (c *Client) AddTxtRecord(zoneName, fqdn, value string, ttl int) error {
177185
178186 q := reqURL .Query ()
179187 q .Set ("domain-name" , zoneName )
180- q .Set ("host" , host )
188+ q .Set ("host" , subDomain )
181189 q .Set ("record" , value )
182190 q .Set ("ttl" , strconv .Itoa (ttlRounder (ttl )))
183191 q .Set ("record-type" , "TXT" )
0 commit comments