From fcf819bb70699663f68330a1fd2bf170e115021f Mon Sep 17 00:00:00 2001 From: Timothy Date: Fri, 1 May 2020 02:26:12 +0800 Subject: [PATCH 1/2] 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)) } From 4f72668e2bed802b40d520d993455d77a74696d2 Mon Sep 17 00:00:00 2001 From: Timothy Date: Sat, 21 Mar 2020 16:14:56 +0800 Subject: [PATCH 2/2] update Makefile --- .gitignore | 4 +++- Makefile | 13 +++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index b8bf4d9..3c3035e 100644 --- a/.gitignore +++ b/.gitignore @@ -28,7 +28,9 @@ config.json *.log *.swp *.gz -cmd/godns/godns +godns +godns.exe +config.json cmd/godns/config.json /.idea diff --git a/Makefile b/Makefile index 841d846..0bc2876 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ install: image: # Build docker image go clean - rm -rf *.gz docker buildx build --platform linux/amd64,linux/386,linux/arm64,linux/arm/v7 -t timothyye/godns:${VERSION} . --push docker buildx build --platform linux/amd64,linux/386,linux/arm64,linux/arm/v7 -t timothyye/godns:latest . --push release: @@ -17,27 +16,29 @@ release: go clean rm -rf *.gz # Build for mac - GO111MODULE=on go build cmd/godns/godns.go -o ${BINARY} -ldflags "-s -w -X main.Version=${VERSION}" + GO111MODULE=on go build -ldflags "-s -w -X main.Version=${VERSION}" cmd/godns/godns.go tar czvf ${BINARY}-mac64-${VERSION}.tar.gz ./${BINARY} # Build for linux go clean - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build cmd/godns/godns.go -o ${BINARY} -ldflags "-s -w -X main.Version=${VERSION}" + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -ldflags "-s -w -X main.Version=${VERSION}" cmd/godns/godns.go tar czvf ${BINARY}-linux64-${VERSION}.tar.gz ./${BINARY} # Build for arm go clean - CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go build cmd/godns/godns.go -o ${BINARY} -ldflags "-s -w -X main.Version=${VERSION}" + CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go build -ldflags "-s -w -X main.Version=${VERSION}" cmd/godns/godns.go tar czvf ${BINARY}-arm64-${VERSION}.tar.gz ./${BINARY} go clean - CGO_ENABLED=0 GOOS=linux GOARCH=arm GO111MODULE=on go build cmd/godns/godns.go -o ${BINARY} -ldflags "-s -w -X main.Version=${VERSION}" + CGO_ENABLED=0 GOOS=linux GOARCH=arm GO111MODULE=on go build -ldflags "-s -w -X main.Version=${VERSION}" cmd/godns/godns.go tar czvf ${BINARY}-arm-${VERSION}.tar.gz ./${BINARY} # Build for win go clean - CGO_ENABLED=0 GOOS=windows GOARCH=amd64 GO111MODULE=on go build cmd/godns/godns.go -o ${BINARY}.exe -ldflags "-s -w -X main.Version=${VERSION}" + CGO_ENABLED=0 GOOS=windows GOARCH=amd64 GO111MODULE=on go build -ldflags "-s -w -X main.Version=${VERSION}" cmd/godns/godns.go tar czvf ${BINARY}-win64-${VERSION}.tar.gz ./${BINARY}.exe make image # Cleans our projects: deletes binaries clean: go clean + rm -rf ./godns + rm -rf ./godns.exe rm -rf *.gz .PHONY: clean build