1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00
This commit is contained in:
Timothy Ye 2018-06-15 22:16:21 +08:00
parent cc51388579
commit 91b55096b9
4 changed files with 26 additions and 62 deletions

View File

@ -52,8 +52,8 @@ func dnsLoop() {
log.Println("Creating DNS handler with provider:", configuration.Provider) log.Println("Creating DNS handler with provider:", configuration.Provider)
handler := handler.CreateHandler(configuration.Provider) handler := handler.CreateHandler(configuration.Provider)
handler.SetConfiguration(&configuration) handler.SetConfiguration(&configuration)
for _, domain := range configuration.Domains { for i, _ := range configuration.Domains {
go handler.DomainLoop(&domain, panicChan) go handler.DomainLoop(&configuration.Domains[i], panicChan)
} }
panicCount := 0 panicCount := 0

View File

@ -37,6 +37,7 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<-
}() }()
for { for {
log.Printf("Checking IP for domain %s \r\n", domain.DomainName)
domainID := handler.GetDomain(domain.DomainName) domainID := handler.GetDomain(domain.DomainName)
if domainID == -1 { if domainID == -1 {
@ -51,38 +52,28 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<-
} }
log.Println("currentIP is:", currentIP) log.Println("currentIP is:", currentIP)
// Compare currentIP with saved IP for _, subDomain := range domain.SubDomains {
savedIP := godns.LoadCurrentIP()
log.Println("savedIP is:", savedIP)
if savedIP != "" && strings.TrimRight(currentIP, "\n") == strings.TrimRight(savedIP, "\n") { subDomainID, ip := handler.GetSubDomain(domainID, subDomain)
log.Printf("Current IP is not changed, no need to update...")
} else {
godns.SaveCurrentIP(currentIP)
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 == "" { // Send mail notification if notify is enabled
log.Printf("domain: %s.%s subDomainID: %s ip: %s\n", subDomain, domain.DomainName, subDomainID, ip) if handler.Configuration.Notify.Enabled {
continue 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 } else {
if len(ip) > 0 && strings.TrimRight(currentIP, "\n") != strings.TrimRight(ip, "\n") { log.Printf("%s.%s Current IP is same as domain IP, no need to update...\n", subDomain, domain.DomainName)
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)
}
} }
} }

View File

@ -48,22 +48,14 @@ func (handler *HEHandler) DomainLoop(domain *godns.Domain, panicChan chan<- godn
} }
log.Println("currentIP is:", currentIP) log.Println("currentIP is:", currentIP)
//Compare currentIP with saved IP for _, subDomain := range domain.SubDomains {
savedIP := godns.LoadCurrentIP() 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") { // Send mail notification if notify is enabled
log.Printf("Current IP is not changed, no need to update...") if handler.Configuration.Notify.Enabled {
} else { log.Print("Sending notification to:", handler.Configuration.Notify.SendTo)
godns.SaveCurrentIP(currentIP) godns.SendNotify(handler.Configuration, fmt.Sprintf("%s.%s", subDomain, domain.DomainName), 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)
}
} }
} }

View File

@ -7,9 +7,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
"strings"
"golang.org/x/net/proxy" "golang.org/x/net/proxy"
"gopkg.in/gomail.v2" "gopkg.in/gomail.v2"
) )
@ -90,22 +87,6 @@ func CheckSettings(config *Settings) error {
return nil 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 // SendNotify sends mail notify if IP is changed
func SendNotify(configuration *Settings, domain, currentIP string) error { func SendNotify(configuration *Settings, domain, currentIP string) error {
m := gomail.NewMessage() m := gomail.NewMessage()