Skip to content

Commit 17493ec

Browse files
committed
add DNS resolve checking
1 parent 6f410f2 commit 17493ec

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

internal/handler/handler.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package handler
22

33
import (
4+
"errors"
45
"fmt"
56
"runtime/debug"
67
"time"
@@ -15,6 +16,11 @@ import (
1516
"github.com/TimothyYe/godns/pkg/notification"
1617
)
1718

19+
var (
20+
errEmptyResult = errors.New("empty result")
21+
errEmptyDomain = errors.New("NXDOMAIN")
22+
)
23+
1824
type Handler struct {
1925
Configuration *settings.Settings
2026
dnsProvider provider.IDNSProvider
@@ -52,10 +58,12 @@ func (handler *Handler) UpdateIP(domain *settings.Domain) {
5258
log.Error(err)
5359
return
5460
}
61+
5562
if ip == handler.cachedIP {
5663
log.Debugf("IP (%s) matches cached IP (%s), skipping", ip, handler.cachedIP)
5764
return
5865
}
66+
5967
err = handler.updateDNS(domain, ip)
6068
if err != nil {
6169
log.Error(err)
@@ -75,18 +83,30 @@ func (handler *Handler) updateDNS(domain *settings.Domain, ip string) error {
7583
hostname = domain.DomainName
7684
}
7785

78-
if err := handler.dnsProvider.UpdateIP(domain.DomainName, subdomainName, ip); err != nil {
79-
return err
86+
lastIP, err := utils.ResolveDNS(hostname, handler.Configuration.Resolver, handler.Configuration.IPType)
87+
if err != nil && (errors.Is(err, errEmptyResult) || errors.Is(err, errEmptyDomain)) {
88+
log.Errorf("Failed to resolve DNS for domain: %s, error: %s", hostname, err)
89+
continue
8090
}
8191

82-
successMessage := fmt.Sprintf("%s.%s", subdomainName, domain.DomainName)
83-
handler.notificationManager.Send(successMessage, ip)
92+
//check against the current known IP, if no change, skip update
93+
if ip == lastIP {
94+
log.Infof("IP is the same as cached one (%s). Skip update.", ip)
95+
} else {
8496

85-
// execute webhook when it is enabled
86-
if handler.Configuration.Webhook.Enabled {
87-
if err := lib.GetWebhook(handler.Configuration).Execute(hostname, ip); err != nil {
97+
if err := handler.dnsProvider.UpdateIP(domain.DomainName, subdomainName, ip); err != nil {
8898
return err
8999
}
100+
101+
successMessage := fmt.Sprintf("%s.%s", subdomainName, domain.DomainName)
102+
handler.notificationManager.Send(successMessage, ip)
103+
104+
// execute webhook when it is enabled
105+
if handler.Configuration.Webhook.Enabled {
106+
if err := lib.GetWebhook(handler.Configuration).Execute(hostname, ip); err != nil {
107+
return err
108+
}
109+
}
90110
}
91111
}
92112

internal/provider/duck/duck_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (provider *DNSProvider) updateIP(domainName, subdomainName, currentIP strin
4545
resp, err := client.Get(fmt.Sprintf(URL, subdomainName, provider.configuration.LoginToken, ip))
4646
if err != nil {
4747
// handle error
48-
log.Errorf("Failed to update sub domain: %s.%s", domainName, subdomainName)
48+
log.Errorf("Failed to update sub domain: %s.%s, error: %s", domainName, subdomainName, err)
4949
return err
5050
}
5151

@@ -58,7 +58,7 @@ func (provider *DNSProvider) updateIP(domainName, subdomainName, currentIP strin
5858

5959
body, err := ioutil.ReadAll(resp.Body)
6060
if err != nil || string(body) != "OK" {
61-
log.Error("Failed to update the IP:", err)
61+
log.Errorf("Failed to update the IP, error: %s, body: %s", err, string(body))
6262
return err
6363
}
6464

0 commit comments

Comments
 (0)