nameserver.go: use custom resolver to avoid update delays caused by dns caching
This commit is contained in:
parent
8e86f5992e
commit
204d5eacf6
2 changed files with 25 additions and 5 deletions
|
|
@ -1,17 +1,37 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func requiresRRUpdate(entry *RRConfig, addr net.IP) bool {
|
const (
|
||||||
// TODO: use custom resolver to query authoritive nameserver instead of
|
resolverTimeout = time.Millisecond * time.Duration(8000)
|
||||||
// local resolver
|
)
|
||||||
addrs, err := net.LookupIP(entry.Recordname)
|
|
||||||
|
func getResolver(nameserver string) *net.Resolver {
|
||||||
|
return &net.Resolver{
|
||||||
|
// use go's built-in dns resolver
|
||||||
|
PreferGo: true,
|
||||||
|
// specify alternate dialer for go's dns resolver
|
||||||
|
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
|
d := net.Dialer{
|
||||||
|
Timeout: resolverTimeout,
|
||||||
|
}
|
||||||
|
return d.DialContext(ctx, network, nameserver)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func requiresRRUpdate(record string, addr net.IP, resolver *net.Resolver) bool {
|
||||||
|
// TODO: the context could be replaced in order to allow cancelling the dns
|
||||||
|
// query if the user disconnects prematurely
|
||||||
|
addrs, err := resolver.LookupIP(context.Background(), "ip", record)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("dns lookup failed: %s", err)
|
log.Printf("dns lookup failed: %s", err)
|
||||||
// enforce update, it's better than not trying at all
|
// enforce update, it's better than not trying at all
|
||||||
|
|
|
||||||
2
web.go
2
web.go
|
|
@ -124,7 +124,7 @@ func RequestHandler(cfg *Config) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !requiresRRUpdate(entry, ipaddr) {
|
if !requiresRRUpdate(entry.Recordname, ipaddr, getResolver(entry.Nameserver)) {
|
||||||
fmt.Fprintf(w, "nochg %s\n", ipaddr.String())
|
fmt.Fprintf(w, "nochg %s\n", ipaddr.String())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue