From fcf819bb70699663f68330a1fd2bf170e115021f Mon Sep 17 00:00:00 2001 From: Timothy Date: Fri, 1 May 2020 02:26:12 +0800 Subject: [PATCH] fix Google Domain handler issue #46 --- handler/google/google_handler.go | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/handler/google/google_handler.go b/handler/google/google_handler.go index 083613f..963408f 100644 --- a/handler/google/google_handler.go +++ b/handler/google/google_handler.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "log" "net/http" - "net/url" "runtime/debug" "strings" "time" @@ -14,8 +13,8 @@ import ( ) var ( - // GoogleUrl the API address for Google Domains - GoogleUrl = "https://domains.google.com/nic/update" + // GoogleURL the API address for Google Domains + GoogleURL = "https://%s:%s@domains.google.com/nic/update?hostname=%s.%s&myip=%s" ) // Handler struct @@ -55,7 +54,7 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns. lastIP = currentIP for _, subDomain := range domain.SubDomains { - log.Printf("%s.%s Start to update record IP...\n", subDomain, domain.DomainName) + log.Printf("[%s.%s] Start to update record IP...\n", subDomain, domain.DomainName) handler.UpdateIP(domain.DomainName, subDomain, currentIP) // Send notification @@ -73,19 +72,20 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns. // UpdateIP update subdomain with current IP func (handler *Handler) UpdateIP(domain, subDomain, currentIP string) { - values := url.Values{} - values.Add("hostname", fmt.Sprintf("%s.%s", subDomain, domain)) - values.Add("myip", currentIP) - client := godns.GetHttpClient(handler.Configuration) - req, _ := http.NewRequest("POST", GoogleUrl, strings.NewReader(values.Encode())) - req.SetBasicAuth(handler.Configuration.Email, handler.Configuration.Password) + resp, err := client.Get(fmt.Sprintf(GoogleURL, + handler.Configuration.Email, + handler.Configuration.Password, + subDomain, + domain, + currentIP)) - if handler.Configuration.UserAgent != "" { - req.Header.Add("User-Agent", handler.Configuration.UserAgent) + if err != nil { + // handle error + log.Print("Failed to update sub domain:", subDomain) } - resp, err := client.Do(req) + defer resp.Body.Close() if err != nil { log.Println("Request error...") @@ -93,7 +93,11 @@ func (handler *Handler) UpdateIP(domain, subDomain, currentIP string) { } else { body, _ := ioutil.ReadAll(resp.Body) if resp.StatusCode == http.StatusOK { - log.Println("Update IP success:", string(body)) + if strings.Contains(string(body), "good") { + log.Println("Update IP success:", string(body)) + } else if strings.Contains(string(body), "nochg") { + log.Println("IP not changed:", string(body)) + } } else { log.Println("Update IP failed:", string(body)) }