From 4c82260c4d1292820445721aef1bd87f5a52ef00 Mon Sep 17 00:00:00 2001 From: TimothyYe Date: Fri, 7 Dec 2018 16:04:59 +0800 Subject: [PATCH] format code and fix the go lint warnings --- README.md | 2 +- handler/cloudflare_handler.go | 2 +- handler/dnspod_handler.go | 8 ++-- handler/he_handler.go | 4 +- utils.go | 71 +++++++++++++++++++---------------- 5 files changed, 47 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 86d2532..2685ba6 100644 --- a/README.md +++ b/README.md @@ -255,7 +255,7 @@ docker run -d --name godns --restart=always \ -v /path/to/config.json:/usr/local/godns/config.json timothyye/godns:latest ``` -## Run it as an Windows service +## Run it as a Windows service * Get [birkett/srvany-ng](https://github.com/birkett/srvany-ng/releases) from Github. * Uncompress and place the executable `srvany-ng.exe` to the same directory where `godns.exe` in. diff --git a/handler/cloudflare_handler.go b/handler/cloudflare_handler.go index 1ee4da3..e892be4 100644 --- a/handler/cloudflare_handler.go +++ b/handler/cloudflare_handler.go @@ -83,7 +83,7 @@ func (handler *CloudflareHandler) DomainLoop(domain *godns.Domain, panicChan cha } log.Println("Current IP is:", currentIP) //check against locally cached IP, if no change, skip update - if (currentIP == lastIP){ + if currentIP == lastIP { log.Printf("IP is the same as cached one. Skip update.\n") } else { lastIP = currentIP diff --git a/handler/dnspod_handler.go b/handler/dnspod_handler.go index ea116da..ee03c5e 100644 --- a/handler/dnspod_handler.go +++ b/handler/dnspod_handler.go @@ -54,11 +54,11 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<- log.Println("currentIP is:", currentIP) //check against locally cached IP, if no change, skip update - if (currentIP == lastIP){ + if currentIP == lastIP { log.Printf("IP is the same as cached one. Skip update.\n") - }else{ + } else { lastIP = currentIP - + for _, subDomain := range domain.SubDomains { subDomainID, ip := handler.GetSubDomain(domainID, subDomain) @@ -83,7 +83,7 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<- 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) diff --git a/handler/he_handler.go b/handler/he_handler.go index 56dc92a..b558510 100644 --- a/handler/he_handler.go +++ b/handler/he_handler.go @@ -50,11 +50,11 @@ func (handler *HEHandler) DomainLoop(domain *godns.Domain, panicChan chan<- godn log.Println("currentIP is:", currentIP) //check against locally cached IP, if no change, skip update - if (currentIP == lastIP){ + 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) diff --git a/utils.go b/utils.go index f2c1808..4a00369 100644 --- a/utils.go +++ b/utils.go @@ -6,9 +6,9 @@ import ( "html/template" "io/ioutil" "log" + "net" "net/http" "strings" - "net" "golang.org/x/net/proxy" "gopkg.in/gomail.v2" @@ -45,14 +45,14 @@ const ( ) //GetIPFromInterface gets IP address from the specific interface -func GetIPFromInterface(configuration *Settings) (string,error) { - ifaces, err := net.InterfaceByName(configuration.IPInterface) +func GetIPFromInterface(configuration *Settings) (string, error) { + ifaces, err := net.InterfaceByName(configuration.IPInterface) if err != nil { log.Println("can't get network device "+configuration.IPInterface+":", err) return "", err } - addrs, err :=ifaces.Addrs(); + addrs, err := ifaces.Addrs() if err != nil { log.Println("can't get address from "+configuration.IPInterface+":", err) return "", err @@ -66,36 +66,42 @@ func GetIPFromInterface(configuration *Settings) (string,error) { case *net.IPAddr: ip = v.IP } - if (ip == nil){ - continue; + if ip == nil { + continue } - if (!(ip.IsGlobalUnicast() &&!(ip.IsUnspecified()||ip.IsMulticast()||ip.IsLoopback()||ip.IsLinkLocalUnicast()||ip.IsLinkLocalMulticast()||ip.IsInterfaceLocalMulticast()))){ - continue; - } + if !(ip.IsGlobalUnicast() && + !(ip.IsUnspecified() || + ip.IsMulticast() || + ip.IsLoopback() || + ip.IsLinkLocalUnicast() || + ip.IsLinkLocalMulticast() || + ip.IsInterfaceLocalMulticast())) { + continue + } //the code is not ready for updating an AAAA record /* - if (isIPv4(ip.String())){ - if (configuration.IPType=="IPv6"){ - continue; - } - }else{ - if (configuration.IPType!="IPv6"){ - continue; - } - } */ - if (!isIPv4(ip.String())){ - continue; + if (isIPv4(ip.String())){ + if (configuration.IPType=="IPv6"){ + continue; + } + }else{ + if (configuration.IPType!="IPv6"){ + continue; + } + } */ + if !isIPv4(ip.String()) { + continue } - return ip.String(),nil - + return ip.String(), nil + } - return "", errors.New("can't get a vaild address from "+configuration.IPInterface) + return "", errors.New("can't get a vaild address from " + configuration.IPInterface) } -func isIPv4(ip string) bool{ +func isIPv4(ip string) bool { return strings.Count(ip, ":") < 2 } @@ -103,26 +109,27 @@ func isIPv4(ip string) bool{ func GetCurrentIP(configuration *Settings) (string, error) { var err error - if (configuration.IPUrl != ""){ + if configuration.IPUrl != "" { ip, err := GetIPOnline(configuration) - if (err != nil){ + if err != nil { log.Println("get ip online failed. Fallback to get ip from interface if possible.") - }else{ - return ip,nil + } else { + return ip, nil } } - if (configuration.IPInterface != ""){ + if configuration.IPInterface != "" { ip, err := GetIPFromInterface(configuration) - if (err != nil){ + if err != nil { log.Println("get ip from interface failed. There is no more ways to try.") - }else{ - return ip,nil + } else { + return ip, nil } } return "", err } + // GetIPOnline gets public IP from internet func GetIPOnline(configuration *Settings) (string, error) { client := &http.Client{}