diff --git a/dns_handler.go b/dns_handler.go index 29fb465..eaa2e3d 100644 --- a/dns_handler.go +++ b/dns_handler.go @@ -7,13 +7,73 @@ import ( "log" "net/http" "net/url" + "os" + "runtime/debug" "strconv" "strings" + "time" "github.com/bitly/go-simplejson" "golang.org/x/net/proxy" ) +type DNSPodHandler struct{} + +func (handler *DNSPodHandler) DomainLoop(domain *Domain) { + defer func() { + if err := recover(); err != nil { + panicCount++ + log.Printf("Recovered in %v: %v\n", err, debug.Stack()) + fmt.Println(identifyPanic()) + log.Print(identifyPanic()) + if panicCount < PANIC_MAX { + log.Println("Got panic in goroutine, will start a new one... :", panicCount) + go handler.DomainLoop(domain) + } else { + os.Exit(1) + } + } + }() + + for { + + domainID := getDomain(domain.DomainName) + + if domainID == -1 { + continue + } + + currentIP, err := getCurrentIP(configuration.IPUrl) + + if err != nil { + log.Println("get_currentIP:", err) + continue + } + log.Println("currentIp is:", currentIP) + + for _, subDomain := range domain.SubDomains { + + subDomainID, ip := 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.Contains(currentIP, ip) { + log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName) + updateIP(domainID, subDomainID, subDomain, 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 + time.Sleep(time.Minute * INTERVAL) + } +} + func getCurrentIP(url string) (string, error) { response, err := http.Get(url) diff --git a/godns.go b/godns.go index b687425..f92a9d0 100644 --- a/godns.go +++ b/godns.go @@ -5,9 +5,6 @@ import ( "fmt" "log" "os" - "runtime/debug" - "strings" - "time" ) const ( @@ -52,65 +49,10 @@ func main() { } func dnsLoop() { - + handler := createHandler(configuration.Type) for _, domain := range configuration.Domains { - go domainLoop(&domain) + go handler.DomainLoop(&domain) } select {} } - -func domainLoop(domain *Domain) { - defer func() { - if err := recover(); err != nil { - panicCount++ - log.Printf("Recovered in %v: %v\n", err, debug.Stack()) - fmt.Println(identifyPanic()) - log.Print(identifyPanic()) - if panicCount < PANIC_MAX { - log.Println("Got panic in goroutine, will start a new one... :", panicCount) - go domainLoop(domain) - } else { - os.Exit(1) - } - } - }() - - for { - - domainID := getDomain(domain.DomainName) - - if domainID == -1 { - continue - } - - currentIP, err := getCurrentIP(configuration.IPUrl) - - if err != nil { - log.Println("get_currentIP:", err) - continue - } - log.Println("currentIp is:", currentIP) - - for _, subDomain := range domain.SubDomains { - - subDomainID, ip := 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.Contains(currentIP, ip) { - log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName) - updateIP(domainID, subDomainID, subDomain, 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 - time.Sleep(time.Minute * INTERVAL) - } -} diff --git a/handler.go b/handler.go new file mode 100644 index 0000000..13a1aae --- /dev/null +++ b/handler.go @@ -0,0 +1,18 @@ +package main + +type IHandler interface { + DomainLoop(domain *Domain) +} + +func createHandler(provider string) IHandler { + var handler IHandler + + switch provider { + case "DNSPod": + handler = IHandler(&DNSPodHandler{}) + case "HE": + handler = IHandler(&HEHandler{}) + } + + return handler +} diff --git a/he_handler.go b/he_handler.go new file mode 100644 index 0000000..f27e357 --- /dev/null +++ b/he_handler.go @@ -0,0 +1,6 @@ +package main + +type HEHandler struct{} + +func (handler *HEHandler) DomainLoop(domain *Domain) { +} diff --git a/settings.go b/settings.go index e11f333..3c54ba5 100644 --- a/settings.go +++ b/settings.go @@ -15,6 +15,7 @@ type Domain struct { //Settings struct type Settings struct { + Type string `json:"type"` Email string `json:"email"` Password string `json:"password"` LoginToken string `json:"login_token"`