diff --git a/cmd/godns/godns.go b/cmd/godns/godns.go index 836c53b..6cc795b 100644 --- a/cmd/godns/godns.go +++ b/cmd/godns/godns.go @@ -52,8 +52,8 @@ func dnsLoop() { log.Println("Creating DNS handler with provider:", configuration.Provider) handler := handler.CreateHandler(configuration.Provider) handler.SetConfiguration(&configuration) - for _, domain := range configuration.Domains { - go handler.DomainLoop(&domain, panicChan) + for i, _ := range configuration.Domains { + go handler.DomainLoop(&configuration.Domains[i], panicChan) } panicCount := 0 diff --git a/handler/dnspod_handler.go b/handler/dnspod_handler.go index 98fa337..06a4381 100644 --- a/handler/dnspod_handler.go +++ b/handler/dnspod_handler.go @@ -37,6 +37,7 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<- }() for { + log.Printf("Checking IP for domain %s \r\n", domain.DomainName) domainID := handler.GetDomain(domain.DomainName) if domainID == -1 { @@ -51,38 +52,28 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<- } log.Println("currentIP is:", currentIP) - // Compare currentIP with saved IP - savedIP := godns.LoadCurrentIP() - log.Println("savedIP is:", savedIP) + for _, subDomain := range domain.SubDomains { - if savedIP != "" && strings.TrimRight(currentIP, "\n") == strings.TrimRight(savedIP, "\n") { - log.Printf("Current IP is not changed, no need to update...") - } else { - godns.SaveCurrentIP(currentIP) + subDomainID, ip := handler.GetSubDomain(domainID, subDomain) - for _, subDomain := range domain.SubDomains { + if subDomainID == "" || ip == "" { + log.Printf("domain: %s.%s subDomainID: %s ip: %s\n", subDomain, domain.DomainName, subDomainID, ip) + continue + } - subDomainID, ip := handler.GetSubDomain(domainID, subDomain) + // Continue to check the IP of sub-domain + if len(ip) > 0 && strings.TrimRight(currentIP, "\n") != strings.TrimRight(ip, "\n") { + log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName) + handler.UpdateIP(domainID, subDomainID, subDomain, currentIP) - if subDomainID == "" || ip == "" { - log.Printf("domain: %s.%s subDomainID: %s ip: %s\n", subDomain, domain.DomainName, subDomainID, ip) - continue + // Send mail notification if notify is enabled + if handler.Configuration.Notify.Enabled { + log.Print("Sending notification to:", handler.Configuration.Notify.SendTo) + godns.SendNotify(handler.Configuration, fmt.Sprintf("%s.%s", subDomain, domain.DomainName), currentIP) } - // Continue to check the IP of sub-domain - if len(ip) > 0 && strings.TrimRight(currentIP, "\n") != strings.TrimRight(ip, "\n") { - log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName) - handler.UpdateIP(domainID, subDomainID, subDomain, currentIP) - - // Send mail notification if notify is enabled - if handler.Configuration.Notify.Enabled { - log.Print("Sending notification to:", handler.Configuration.Notify.SendTo) - godns.SendNotify(handler.Configuration, fmt.Sprintf("%s.%s", subDomain, domain.DomainName), currentIP) - } - - } else { - log.Printf("%s.%s Current IP is same as domain IP, no need to update...\n", subDomain, domain.DomainName) - } + } else { + log.Printf("%s.%s Current IP is same as domain IP, no need to update...\n", subDomain, domain.DomainName) } } diff --git a/handler/he_handler.go b/handler/he_handler.go index 2ac9b13..c34e02c 100644 --- a/handler/he_handler.go +++ b/handler/he_handler.go @@ -48,22 +48,14 @@ func (handler *HEHandler) DomainLoop(domain *godns.Domain, panicChan chan<- godn } log.Println("currentIP is:", currentIP) - //Compare currentIP with saved IP - savedIP := godns.LoadCurrentIP() + for _, subDomain := range domain.SubDomains { + log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName) + handler.UpdateIP(domain.DomainName, subDomain, currentIP) - if savedIP != "" && strings.TrimRight(currentIP, "\n") == strings.TrimRight(savedIP, "\n") { - log.Printf("Current IP is not changed, no need to update...") - } else { - godns.SaveCurrentIP(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) - - // Send mail notification if notify is enabled - if handler.Configuration.Notify.Enabled { - log.Print("Sending notification to:", handler.Configuration.Notify.SendTo) - godns.SendNotify(handler.Configuration, fmt.Sprintf("%s.%s", subDomain, domain.DomainName), currentIP) - } + // Send mail notification if notify is enabled + if handler.Configuration.Notify.Enabled { + log.Print("Sending notification to:", handler.Configuration.Notify.SendTo) + godns.SendNotify(handler.Configuration, fmt.Sprintf("%s.%s", subDomain, domain.DomainName), currentIP) } } diff --git a/utils.go b/utils.go index 779241c..b43f95b 100644 --- a/utils.go +++ b/utils.go @@ -7,9 +7,6 @@ import ( "io/ioutil" "log" "net/http" - "os" - "strings" - "golang.org/x/net/proxy" "gopkg.in/gomail.v2" ) @@ -90,22 +87,6 @@ func CheckSettings(config *Settings) error { return nil } -// SaveCurrentIP saves current IP into a template file -func SaveCurrentIP(currentIP string) { - ioutil.WriteFile("./.current_ip", []byte(currentIP), os.FileMode(0644)) -} - -// LoadCurrentIP loads saved IP from template file -func LoadCurrentIP() string { - content, err := ioutil.ReadFile("./.current_ip") - - if err != nil { - return "" - } - - return strings.Replace(string(content), "\n", "", -1) -} - // SendNotify sends mail notify if IP is changed func SendNotify(configuration *Settings, domain, currentIP string) error { m := gomail.NewMessage()