2018-12-06 21:50:17 +00:00
package duckdns
import (
"fmt"
2021-08-25 09:44:11 +00:00
"io"
2018-12-06 21:50:17 +00:00
"net/url"
"strconv"
"strings"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/challenge/dns01"
2018-12-06 21:50:17 +00:00
"github.com/miekg/dns"
)
// updateTxtRecord Update the domains TXT record
// To update the TXT record we just need to make one simple get request.
// In DuckDNS you only have one TXT record shared with the domain and all sub domains.
func ( d * DNSProvider ) updateTxtRecord ( domain , token , txt string , clear bool ) error {
u , _ := url . Parse ( "https://www.duckdns.org/update" )
mainDomain := getMainDomain ( domain )
2021-03-04 19:16:59 +00:00
if mainDomain == "" {
2018-12-06 21:50:17 +00:00
return fmt . Errorf ( "unable to find the main domain for: %s" , domain )
}
query := u . Query ( )
query . Set ( "domains" , mainDomain )
query . Set ( "token" , token )
query . Set ( "clear" , strconv . FormatBool ( clear ) )
query . Set ( "txt" , txt )
u . RawQuery = query . Encode ( )
response , err := d . config . HTTPClient . Get ( u . String ( ) )
if err != nil {
return err
}
defer response . Body . Close ( )
2021-08-25 09:44:11 +00:00
bodyBytes , err := io . ReadAll ( response . Body )
2018-12-06 21:50:17 +00:00
if err != nil {
return err
}
body := string ( bodyBytes )
if body != "OK" {
return fmt . Errorf ( "request to change TXT record for DuckDNS returned the following result (%s) this does not match expectation (OK) used url [%s]" , body , u )
}
return nil
}
2020-05-08 17:35:25 +00:00
// DuckDNS only lets you write to your subdomain.
// It must be in format subdomain.duckdns.org,
// not in format subsubdomain.subdomain.duckdns.org.
// So strip off everything that is not top 3 levels.
2018-12-06 21:50:17 +00:00
func getMainDomain ( domain string ) string {
domain = dns01 . UnFqdn ( domain )
split := dns . Split ( domain )
if strings . HasSuffix ( strings . ToLower ( domain ) , "duckdns.org" ) {
if len ( split ) < 3 {
return ""
}
firstSubDomainIndex := split [ len ( split ) - 3 ]
return domain [ firstSubDomainIndex : ]
}
return domain [ split [ len ( split ) - 1 ] : ]
}