From f0a62914069b310f2a99102f870e5ff05e7f2501 Mon Sep 17 00:00:00 2001 From: Eri Bastos Date: Sat, 18 Apr 2020 12:51:50 -0400 Subject: [PATCH] Replaced cached IP with remote resolver --- config_sample.json | 1 + handler/google/google_handler.go | 17 +++++++---------- settings.go | 13 +++++++------ utils.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/config_sample.json b/config_sample.json index c5fccac..e33bd03 100644 --- a/config_sample.json +++ b/config_sample.json @@ -22,6 +22,7 @@ "ipv6_url": "https://api-ipv6.ip.sb/ip", "ip_type": "IPv4", "interval": 300, + "resolver": "8.8.8.8", "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36", "ip_interface": "eth0", "socks5_proxy": "", diff --git a/handler/google/google_handler.go b/handler/google/google_handler.go index 083613f..e93b53c 100644 --- a/handler/google/google_handler.go +++ b/handler/google/google_handler.go @@ -37,24 +37,21 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns. } }() - var lastIP string - for { currentIP, err := godns.GetCurrentIP(handler.Configuration) - if err != nil { log.Println("get_currentIP:", err) continue } log.Println("currentIP is:", currentIP) + for _, subDomain := range domain.SubDomains { + hostname := subDomain + "." + domain.DomainName + lastIP := godns.ResolveDNS(hostname, handler.Configuration.Resolver) + //check against currently known IP, if no change, skip update + if currentIP == lastIP { + log.Printf("IP is the same as cached one. Skip update.\n") + } else { - //check against locally cached IP, if no change, skip update - if currentIP == lastIP { - log.Printf("IP is the same as cached one. Skip update.\n") - } else { - lastIP = currentIP - - for _, subDomain := range domain.SubDomains { log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName) handler.UpdateIP(domain.DomainName, subDomain, currentIP) diff --git a/settings.go b/settings.go index fbf945f..899d729 100644 --- a/settings.go +++ b/settings.go @@ -14,10 +14,10 @@ type Domain struct { // Notify struct for telegram notification type TelegramNotify struct { - Enabled bool `json:"enabled"` - BotApiKey string `json:"bot_api_key"` - ChatId string `json:"chat_id"` - MsgTemplate string `json:"message_template"` + Enabled bool `json:"enabled"` + BotApiKey string `json:"bot_api_key"` + ChatId string `json:"chat_id"` + MsgTemplate string `json:"message_template"` } // Notify struct for SMTP notification @@ -32,8 +32,8 @@ type MailNotify struct { // Notify struct type Notify struct { - Telegram TelegramNotify `json:"telegram"` - Mail MailNotify `json:"mail"` + Telegram TelegramNotify `json:"telegram"` + Mail MailNotify `json:"mail"` } // Settings struct @@ -52,6 +52,7 @@ type Settings struct { Notify Notify `json:"notify"` IPInterface string `json:"ip_interface"` IPType string `json:"ip_type"` + Resolver string `json:"resolver"` } // LoadSettings -- Load settings from config file diff --git a/utils.go b/utils.go index 7330ec2..c094694 100644 --- a/utils.go +++ b/utils.go @@ -12,6 +12,7 @@ import ( "net/http" "strings" + "github.com/bogdanovich/dns_resolver" "golang.org/x/net/proxy" gomail "gopkg.in/gomail.v2" ) @@ -346,3 +347,32 @@ func buildTemplate(currentIP, domain string, tplsrc string) string { return tpl.String() } + +// ResolveDNS will query DNS for a given hostname. +func ResolveDNS(hostname, resolver string) string { + + // If no DNS server is set in config file, falls back to default resolver. + if resolver == "" { + dnsAdress, err := net.LookupHost(hostname) + if err != nil { + if strings.HasSuffix(err.Error(), "no such host") { + return "" + } + log.Fatalf(err.Error()) + } + return dnsAdress[0] + } + res := dns_resolver.New([]string{resolver}) + // In case of i/o timeout + res.RetryTimes = 5 + + ip, err := res.LookupHost(hostname) + if err != nil { + if strings.HasSuffix(err.Error(), "NXDOMAIN") { + return "" + } + log.Fatalf(err.Error()) + } + return ip[0].String() + +}