Skip to content

Commit

Permalink
Fix DNS resolver issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrsagen committed Aug 28, 2023
1 parent 6007f0d commit febd584
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
25 changes: 24 additions & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,37 @@ package main
import (
"context"
"errors"
"fmt"
"net"
"sync"
"time"
)

var errInconsistent = errors.New("[error] Inconsistent record count from CF_NS1 and CF_NS2")

func lookupCompareTXT(rs1 net.Resolver, rs2 net.Resolver, name string) ([]string, error) {
func resolver(address string) net.Resolver {
return net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, _ string) (conn net.Conn, err error) {
d := net.Dialer{}
var addr net.Addr
if network == "tcp" {
addr, err = net.ResolveTCPAddr(network, address)
} else if network == "udp" {
addr, err = net.ResolveUDPAddr(network, address)
} else {
err = fmt.Errorf("unknown DNS resolver network type %q", network)
}
if err != nil {
return
}
conn, err = d.DialContext(ctx, network, addr.String())
return
},
}
}

func lookupCompareTXT(rs1, rs2 net.Resolver, name string) ([]string, error) {
wg := &sync.WaitGroup{}

var res1, res2 []string
Expand Down
39 changes: 7 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -227,33 +226,8 @@ func main() {
}
}
} else if !*onlySaveRenewCreds { // Auth/normal mode
// Resolve IP of first nameserver
addr1, err := net.ResolveUDPAddr("udp", zonesRes.Result[0].Nameservers[0]+":53")
if err != nil {
fmt.Printf("[error] Could not resolve nameserver in CF_NS1\n%v\n", err)
return
}
rs1 := net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, "udp", addr1.String())
},
}

// Resolve IP of second nameserver
addr2, err := net.ResolveUDPAddr("udp", zonesRes.Result[0].Nameservers[1]+":53")
if err != nil {
fmt.Printf("[error] Could not resolve nameserver in CF_NS2\n%v\n", err)
return
}
rs2 := net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, "udp", addr2.String())
},
}
rs1 := resolver(net.JoinHostPort(zonesRes.Result[0].Nameservers[0], "53"))
rs2 := resolver(net.JoinHostPort(zonesRes.Result[0].Nameservers[1], "53"))

// Perform initial lookup of _acme-challenge TXT records using the Cloudflare DNS servers
if *verbose {
Expand Down Expand Up @@ -313,17 +287,18 @@ func main() {
if *verbose {
fmt.Println(err.Error())
}
time.Sleep(1 * time.Second)
time.Sleep(time.Second)
continue
} else if err != nil && !strings.Contains(err.Error(), "no such host") {
fmt.Printf("[error] Failed lookup TXT %s\n%v\n", subdomain, err)
return
fmt.Printf("[warning] Failed lookup TXT %s: %v\n", subdomain, err)
time.Sleep(time.Second)
continue
}
if dnsRes == nil || len(dnsRes) == 0 || !strSliceLookup(dnsRes, vt) {
if *verbose {
fmt.Printf("[info] Challenge record \"%s\" missing from domain, retrying...\n", vt)
}
time.Sleep(1 * time.Second)
time.Sleep(time.Second)
continue
}
break
Expand Down

0 comments on commit febd584

Please sign in to comment.