mirror of
https://github.com/taigrr/godns
synced 2025-01-18 04:03:25 -08:00
support for getting an IP from an interface, and skip DNS record update if IP address is the same with last one
This commit is contained in:
@@ -74,6 +74,7 @@ func (handler *CloudflareHandler) DomainLoop(domain *godns.Domain, panicChan cha
|
||||
}
|
||||
}()
|
||||
|
||||
var lastIP string
|
||||
for {
|
||||
currentIP, err := godns.GetCurrentIP(handler.Configuration)
|
||||
if err != nil {
|
||||
@@ -81,30 +82,34 @@ func (handler *CloudflareHandler) DomainLoop(domain *godns.Domain, panicChan cha
|
||||
continue
|
||||
}
|
||||
log.Println("Current IP is:", currentIP)
|
||||
// TODO: check against locally cached IP, if no change, skip update
|
||||
|
||||
log.Println("Checking IP for domain", domain.DomainName)
|
||||
zoneID := handler.getZone(domain.DomainName)
|
||||
if zoneID != "" {
|
||||
records := handler.getDNSRecords(zoneID)
|
||||
|
||||
// update records
|
||||
for _, rec := range records {
|
||||
if recordTracked(domain, &rec) != true {
|
||||
log.Println("Skiping record:", rec.Name)
|
||||
continue
|
||||
}
|
||||
if rec.IP != currentIP {
|
||||
log.Printf("IP mismatch: Current(%+v) vs Cloudflare(%+v)\r\n", currentIP, rec.IP)
|
||||
handler.updateRecord(rec, currentIP)
|
||||
} else {
|
||||
log.Printf("Record OK: %+v - %+v\r\n", rec.Name, rec.IP)
|
||||
}
|
||||
}
|
||||
//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 {
|
||||
log.Println("Failed to find zone for domain:", domain.DomainName)
|
||||
}
|
||||
lastIP = currentIP
|
||||
|
||||
log.Println("Checking IP for domain", domain.DomainName)
|
||||
zoneID := handler.getZone(domain.DomainName)
|
||||
if zoneID != "" {
|
||||
records := handler.getDNSRecords(zoneID)
|
||||
|
||||
// update records
|
||||
for _, rec := range records {
|
||||
if recordTracked(domain, &rec) != true {
|
||||
log.Println("Skiping record:", rec.Name)
|
||||
continue
|
||||
}
|
||||
if rec.IP != currentIP {
|
||||
log.Printf("IP mismatch: Current(%+v) vs Cloudflare(%+v)\r\n", currentIP, rec.IP)
|
||||
handler.updateRecord(rec, currentIP)
|
||||
} else {
|
||||
log.Printf("Record OK: %+v - %+v\r\n", rec.Name, rec.IP)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Println("Failed to find zone for domain:", domain.DomainName)
|
||||
}
|
||||
}
|
||||
// Interval is 5 minutes
|
||||
log.Printf("Going to sleep, will start next checking in %d minutes...\r\n", godns.INTERVAL)
|
||||
time.Sleep(time.Minute * godns.INTERVAL)
|
||||
|
||||
@@ -36,6 +36,7 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<-
|
||||
}
|
||||
}()
|
||||
|
||||
var lastIP string
|
||||
for {
|
||||
log.Printf("Checking IP for domain %s \r\n", domain.DomainName)
|
||||
domainID := handler.GetDomain(domain.DomainName)
|
||||
@@ -52,31 +53,37 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<-
|
||||
}
|
||||
log.Println("currentIP is:", currentIP)
|
||||
|
||||
for _, subDomain := range domain.SubDomains {
|
||||
//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 {
|
||||
|
||||
subDomainID, ip := handler.GetSubDomain(domainID, subDomain)
|
||||
subDomainID, ip := handler.GetSubDomain(domainID, subDomain)
|
||||
|
||||
if subDomainID == "" || ip == "" {
|
||||
log.Printf("domain: %s.%s subDomainID: %s ip: %s\n", subDomain, domain.DomainName, subDomainID, ip)
|
||||
continue
|
||||
}
|
||||
|
||||
// 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)
|
||||
if subDomainID == "" || ip == "" {
|
||||
log.Printf("domain: %s.%s subDomainID: %s ip: %s\n", subDomain, domain.DomainName, subDomainID, ip)
|
||||
continue
|
||||
}
|
||||
|
||||
} else {
|
||||
log.Printf("%s.%s Current IP is same as domain IP, no need to update...\n", subDomain, domain.DomainName)
|
||||
}
|
||||
}
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Interval is 5 minutes
|
||||
log.Printf("Going to sleep, will start next checking in %d minutes...\r\n", godns.INTERVAL)
|
||||
time.Sleep(time.Minute * godns.INTERVAL)
|
||||
|
||||
@@ -39,6 +39,7 @@ func (handler *HEHandler) DomainLoop(domain *godns.Domain, panicChan chan<- godn
|
||||
}
|
||||
}()
|
||||
|
||||
var lastIP string
|
||||
for {
|
||||
currentIP, err := godns.GetCurrentIP(handler.Configuration)
|
||||
|
||||
@@ -48,21 +49,28 @@ func (handler *HEHandler) DomainLoop(domain *godns.Domain, panicChan chan<- godn
|
||||
}
|
||||
log.Println("currentIP is:", 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)
|
||||
//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)
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Interval is 5 minutes
|
||||
log.Printf("Going to sleep, will start next checking in %d minutes...\r\n", godns.INTERVAL)
|
||||
time.Sleep(time.Minute * godns.INTERVAL)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// UpdateIP update subdomain with current IP
|
||||
|
||||
Reference in New Issue
Block a user