Move functions from dns package back into ACME.

This commit is contained in:
xenolf 2016-03-11 03:20:25 +01:00
parent b412c67aa6
commit 9008ec6949
7 changed files with 35 additions and 67 deletions

View file

@ -69,7 +69,7 @@ func (s *dnsChallenge) Solve(chlng challenge, domain string) error {
logf("[INFO][%s] Checking DNS record propagation...", domain) logf("[INFO][%s] Checking DNS record propagation...", domain)
err = waitFor(30, 2, func() (bool, error) { err = WaitFor(30, 2, func() (bool, error) {
return preCheckDNS(fqdn, value) return preCheckDNS(fqdn, value)
}) })
if err != nil { if err != nil {
@ -160,7 +160,7 @@ func dnsQuery(fqdn string, rtype uint16, nameserver string, recursive bool) (in
func lookupNameservers(fqdn string) ([]string, error) { func lookupNameservers(fqdn string) ([]string, error) {
var authoritativeNss []string var authoritativeNss []string
zone, err := findZoneByFqdn(fqdn, recursiveNameserver) zone, err := FindZoneByFqdn(fqdn, recursiveNameserver)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -182,8 +182,8 @@ func lookupNameservers(fqdn string) ([]string, error) {
return nil, fmt.Errorf("Could not determine authoritative nameservers") return nil, fmt.Errorf("Could not determine authoritative nameservers")
} }
// findZoneByFqdn determines the zone of the given fqdn // FindZoneByFqdn determines the zone of the given fqdn
func findZoneByFqdn(fqdn, nameserver string) (string, error) { func FindZoneByFqdn(fqdn, nameserver string) (string, error) {
// Do we have it cached? // Do we have it cached?
if zone, ok := fqdnToZone[fqdn]; ok { if zone, ok := fqdnToZone[fqdn]; ok {
return zone, nil return zone, nil
@ -208,8 +208,8 @@ func findZoneByFqdn(fqdn, nameserver string) (string, error) {
if soa, ok := ans.(*dns.SOA); ok { if soa, ok := ans.(*dns.SOA); ok {
zone := soa.Hdr.Name zone := soa.Hdr.Name
// If we ended up on one of the TLDs, it means the domain did not exist. // If we ended up on one of the TLDs, it means the domain did not exist.
publicsuffix, _ := publicsuffix.PublicSuffix(unFqdn(zone)) publicsuffix, _ := publicsuffix.PublicSuffix(UnFqdn(zone))
if publicsuffix == unFqdn(zone) { if publicsuffix == UnFqdn(zone) {
return "", fmt.Errorf("Could not determine zone authoritatively") return "", fmt.Errorf("Could not determine zone authoritatively")
} }
fqdnToZone[fqdn] = zone fqdnToZone[fqdn] = zone
@ -223,8 +223,8 @@ func findZoneByFqdn(fqdn, nameserver string) (string, error) {
if soa, ok := ns.(*dns.SOA); ok { if soa, ok := ns.(*dns.SOA); ok {
zone := soa.Hdr.Name zone := soa.Hdr.Name
// If we ended up on one of the TLDs, it means the domain did not exist. // If we ended up on one of the TLDs, it means the domain did not exist.
publicsuffix, _ := publicsuffix.PublicSuffix(unFqdn(zone)) publicsuffix, _ := publicsuffix.PublicSuffix(UnFqdn(zone))
if publicsuffix == unFqdn(zone) { if publicsuffix == UnFqdn(zone) {
return "", fmt.Errorf("Could not determine zone authoritatively") return "", fmt.Errorf("Could not determine zone authoritatively")
} }
fqdnToZone[fqdn] = zone fqdnToZone[fqdn] = zone
@ -239,8 +239,26 @@ func clearFqdnCache() {
fqdnToZone = map[string]string{} fqdnToZone = map[string]string{}
} }
// waitFor polls the given function 'f', once every 'interval' seconds, up to 'timeout' seconds. // ToFqdn converts the name into a fqdn appending a trailing dot.
func waitFor(timeout, interval int, f func() (bool, error)) error { func ToFqdn(name string) string {
n := len(name)
if n == 0 || name[n-1] == '.' {
return name
}
return name + "."
}
// UnFqdn converts the fqdn into a name removing the trailing dot.
func UnFqdn(name string) string {
n := len(name)
if n != 0 && name[n-1] == '.' {
return name[:n-1]
}
return name
}
// WaitFor polls the given function 'f', once every 'interval' seconds, up to 'timeout' seconds.
func WaitFor(timeout, interval int, f func() (bool, error)) error {
var lastErr string var lastErr string
timeup := time.After(time.Duration(timeout) * time.Second) timeup := time.After(time.Duration(timeout) * time.Second)
for { for {

View file

@ -167,7 +167,7 @@ func TestCheckAuthoritativeNssErr(t *testing.T) {
func TestWaitForTimeout(t *testing.T) { func TestWaitForTimeout(t *testing.T) {
c := make(chan error) c := make(chan error)
go func() { go func() {
err := waitFor(3, 1, func() (bool, error) { err := WaitFor(3, 1, func() (bool, error) {
return false, nil return false, nil
}) })
c <- err c <- err

View file

@ -11,7 +11,6 @@ import (
"time" "time"
"github.com/xenolf/lego/acme" "github.com/xenolf/lego/acme"
"github.com/xenolf/lego/providers/dns"
) )
// CloudFlareAPIURL represents the API endpoint to call. // CloudFlareAPIURL represents the API endpoint to call.
@ -50,7 +49,7 @@ func (c *DNSProviderCloudFlare) Present(domain, token, keyAuth string) error {
rec := cloudFlareRecord{ rec := cloudFlareRecord{
Type: "TXT", Type: "TXT",
Name: dns.UnFqdn(fqdn), Name: acme.UnFqdn(fqdn),
Content: value, Content: value,
TTL: 120, TTL: 120,
} }
@ -105,7 +104,7 @@ func (c *DNSProviderCloudFlare) getHostedZoneID(fqdn string) (string, error) {
var hostedZone HostedZone var hostedZone HostedZone
for _, zone := range zones { for _, zone := range zones {
name := dns.ToFqdn(zone.Name) name := acme.ToFqdn(zone.Name)
if strings.HasSuffix(fqdn, name) { if strings.HasSuffix(fqdn, name) {
if len(zone.Name) > len(hostedZone.Name) { if len(zone.Name) > len(hostedZone.Name) {
hostedZone = zone hostedZone = zone
@ -137,7 +136,7 @@ func (c *DNSProviderCloudFlare) findTxtRecord(fqdn string) (*cloudFlareRecord, e
} }
for _, rec := range records { for _, rec := range records {
if rec.Name == dns.UnFqdn(fqdn) && rec.Type == "TXT" { if rec.Name == acme.UnFqdn(fqdn) && rec.Type == "TXT" {
return &rec, nil return &rec, nil
} }
} }

View file

@ -7,7 +7,6 @@ import (
"github.com/weppos/dnsimple-go/dnsimple" "github.com/weppos/dnsimple-go/dnsimple"
"github.com/xenolf/lego/acme" "github.com/xenolf/lego/acme"
"github.com/xenolf/lego/providers/dns"
) )
// DNSProviderDNSimple is an implementation of the DNSProvider interface. // DNSProviderDNSimple is an implementation of the DNSProvider interface.
@ -124,7 +123,7 @@ func (c *DNSProviderDNSimple) newTxtRecord(zone, fqdn, value string, ttl int) *d
} }
func (c *DNSProviderDNSimple) extractRecordName(fqdn, domain string) string { func (c *DNSProviderDNSimple) extractRecordName(fqdn, domain string) string {
name := dns.UnFqdn(fqdn) name := acme.UnFqdn(fqdn)
if idx := strings.Index(name, "."+domain); idx != -1 { if idx := strings.Index(name, "."+domain); idx != -1 {
return name[:idx] return name[:idx]
} }

View file

@ -60,7 +60,7 @@ func (r *DNSProviderRFC2136) CleanUp(domain, token, keyAuth string) error {
func (r *DNSProviderRFC2136) changeRecord(action, fqdn, value string, ttl int) error { func (r *DNSProviderRFC2136) changeRecord(action, fqdn, value string, ttl int) error {
// Find the zone for the given fqdn // Find the zone for the given fqdn
zone, err := findZoneByFqdn(fqdn, r.nameserver) zone, err := acme.FindZoneByFqdn(fqdn, r.nameserver)
if err != nil { if err != nil {
return err return err
} }

View file

@ -8,7 +8,6 @@ import (
"github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/route53" "github.com/mitchellh/goamz/route53"
"github.com/xenolf/lego/acme" "github.com/xenolf/lego/acme"
"github.com/xenolf/lego/providers/dns"
) )
// DNSProviderRoute53 is an implementation of the DNSProvider interface // DNSProviderRoute53 is an implementation of the DNSProvider interface
@ -71,7 +70,7 @@ func (r *DNSProviderRoute53) changeRecord(action, fqdn, value string, ttl int) e
return err return err
} }
return dns.WaitFor(90, 5, func() (bool, error) { return acme.WaitFor(90, 5, func() (bool, error) {
status, err := r.client.GetChange(resp.ChangeInfo.ID) status, err := r.client.GetChange(resp.ChangeInfo.ID)
if err != nil { if err != nil {
return false, err return false, err

View file

@ -1,47 +0,0 @@
package dns
import (
"fmt"
"time"
)
// ToFqdn converts the name into a fqdn appending a trailing dot.
func ToFqdn(name string) string {
n := len(name)
if n == 0 || name[n-1] == '.' {
return name
}
return name + "."
}
// UnFqdn converts the fqdn into a name removing the trailing dot.
func UnFqdn(name string) string {
n := len(name)
if n != 0 && name[n-1] == '.' {
return name[:n-1]
}
return name
}
// WaitFor polls the given function 'f', once every 'interval' seconds, up to 'timeout' seconds.
func WaitFor(timeout, interval int, f func() (bool, error)) error {
var lastErr string
timeup := time.After(time.Duration(timeout) * time.Second)
for {
select {
case <-timeup:
return fmt.Errorf("Time limit exceeded. Last error: %s", lastErr)
default:
}
stop, err := f()
if stop {
return nil
}
if err != nil {
lastErr = err.Error()
}
time.Sleep(time.Duration(interval) * time.Second)
}
}