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/go.mod b/go.mod index 0daa42d..0eaa091 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,13 @@ module github.com/TimothyYe/godns require ( github.com/bitly/go-simplejson v0.5.0 github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect + github.com/bogdanovich/dns_resolver v0.0.0-20170211073258-a8e42bc6a5b6 github.com/fatih/color v1.7.0 github.com/kr/pretty v0.1.0 // indirect github.com/mattn/go-colorable v0.0.9 // indirect github.com/mattn/go-isatty v0.0.4 // indirect - golang.org/x/net v0.0.0-20190110200230-915654e7eabc - golang.org/x/sys v0.0.0-20190226215855-775f8194d0f9 // indirect + github.com/miekg/dns v1.1.29 // indirect + golang.org/x/net v0.0.0-20190923162816-aa69164e4478 gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df ) diff --git a/go.sum b/go.sum index 8f6bf9d..35d85dd 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkN github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA= github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= +github.com/bogdanovich/dns_resolver v0.0.0-20170211073258-a8e42bc6a5b6 h1:oV1V+uwP+sjmdSkvMxsl/l+HE+N8wbL49wCXZPel25M= +github.com/bogdanovich/dns_resolver v0.0.0-20170211073258-a8e42bc6a5b6/go.mod h1:txOV61Nn+21z77KUMkNsp8lTHoOFTtqotltQAFenS9I= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -13,10 +15,29 @@ github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRU github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/miekg/dns v1.1.29 h1:xHBEhR+t5RzcFJjBLJlax2daXOrTYtr9z4WdKEfWFzg= +github.com/miekg/dns v1.1.29/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/net v0.0.0-20190110200230-915654e7eabc h1:Yx9JGxI1SBhVLFjpAkWMaO1TF+xyqtHLjZpvQboJGiM= golang.org/x/net v0.0.0-20190110200230-915654e7eabc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190226215855-775f8194d0f9 h1:N26gncmS+iqc/W/SKhX3ElI5pkt72XYoRLgi5Z70LSc= golang.org/x/sys v0.0.0-20190226215855-775f8194d0f9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe h1:6fAMxZRR6sl1Uq8U61gxU+kPTs2tR8uOySCbBP7BN/M= +golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= 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() + +}