From 508aa7321ad923d4773ee48ee41082917f30a894 Mon Sep 17 00:00:00 2001 From: Timothy Date: Mon, 30 Oct 2017 11:04:25 +0800 Subject: [PATCH] fix #14 Avoid too much api call times by creating a file to store last IP address --- .gitignore | 1 + handler/dnspod_handler.go | 14 ++++++++++++-- handler/he_handler.go | 12 +++++++++++- utils.go | 16 ++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 23e40f7..f45916b 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ vendor/* /godns.iml /godns.ipr /godns.iws +.current_ip diff --git a/handler/dnspod_handler.go b/handler/dnspod_handler.go index f236062..4adce35 100644 --- a/handler/dnspod_handler.go +++ b/handler/dnspod_handler.go @@ -37,7 +37,6 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<- }() for { - domainID := handler.GetDomain(domain.DomainName) if domainID == -1 { @@ -50,7 +49,18 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<- log.Println("get_currentIP:", err) continue } - log.Println("currentIp is:", currentIP) + log.Println("currentIP is:", currentIP) + + //Compare currentIP with saved IP + savedIP := godns.LoadCurrentIP() + log.Println("savedIP is:", savedIP) + + if savedIP != "" && strings.TrimRight(currentIP, "\n") == strings.TrimRight(savedIP, "\n") { + log.Printf("Current IP is not changed, no need to update...") + continue + } else { + godns.SaveCurrentIP(currentIP) + } for _, subDomain := range domain.SubDomains { diff --git a/handler/he_handler.go b/handler/he_handler.go index 9a34e77..ac0b286 100644 --- a/handler/he_handler.go +++ b/handler/he_handler.go @@ -46,7 +46,17 @@ func (handler *HEHandler) DomainLoop(domain *godns.Domain, panicChan chan<- godn log.Println("get_currentIP:", err) continue } - log.Println("currentIp is:", currentIP) + log.Println("currentIP is:", currentIP) + + //Compare currentIP with saved IP + savedIP := godns.LoadCurrentIP() + + if savedIP != "" && currentIP == savedIP { + log.Printf("Current IP is not changed, no need to update...") + continue + } else { + godns.SaveCurrentIP(currentIP) + } for _, subDomain := range domain.SubDomains { log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName) diff --git a/utils.go b/utils.go index 1932d78..f85c64c 100644 --- a/utils.go +++ b/utils.go @@ -5,6 +5,8 @@ import ( "io/ioutil" "log" "net/http" + "os" + "strings" "golang.org/x/net/proxy" ) @@ -67,3 +69,17 @@ func CheckSettings(config *Settings) error { return nil } + +func SaveCurrentIP(currentIP string) { + ioutil.WriteFile("./.current_ip", []byte(currentIP), os.FileMode(0644)) +} + +func LoadCurrentIP() string { + content, err := ioutil.ReadFile("./.current_ip") + + if err != nil { + return "" + } + + return strings.Replace(string(content), "\n", "", -1) +}