From 1ddd1dfb8ba653d49b2ad7ac09b6d718a418f306 Mon Sep 17 00:00:00 2001 From: Timothy Date: Mon, 10 Aug 2020 14:39:56 +0800 Subject: [PATCH] refactor: fix code lint warnings --- cmd/godns/godns.go | 10 +++++----- handler/alidns/alidns.go | 2 +- handler/dnspod/dnspod_handler.go | 2 +- handler/google/google_handler.go | 1 + resolver/resolver.go | 13 +++++++++---- utils.go | 14 +++++++------- 6 files changed, 24 insertions(+), 18 deletions(-) diff --git a/cmd/godns/godns.go b/cmd/godns/godns.go index fd020cd..5700343 100644 --- a/cmd/godns/godns.go +++ b/cmd/godns/godns.go @@ -49,18 +49,18 @@ func main() { func dnsLoop() { panicChan := make(chan godns.Domain) - log.Println("Creating DNS handler with provider:", configuration.Provider) - handler := handler.CreateHandler(configuration.Provider) - handler.SetConfiguration(&configuration) + log.Println("Creating DNS h with provider:", configuration.Provider) + h := handler.CreateHandler(configuration.Provider) + h.SetConfiguration(&configuration) for i := range configuration.Domains { - go handler.DomainLoop(&configuration.Domains[i], panicChan) + go h.DomainLoop(&configuration.Domains[i], panicChan) } panicCount := 0 for { failDomain := <-panicChan log.Println("Got panic in goroutine, will start a new one... :", panicCount) - go handler.DomainLoop(&failDomain, panicChan) + go h.DomainLoop(&failDomain, panicChan) panicCount++ if panicCount >= godns.PanicMax { diff --git a/handler/alidns/alidns.go b/handler/alidns/alidns.go index df2c5f4..e972135 100644 --- a/handler/alidns/alidns.go +++ b/handler/alidns/alidns.go @@ -133,7 +133,7 @@ func (d *AliDNS) UpdateDomainRecord(r DomainRecord) error { } func (d *AliDNS) genRequestURL(parms map[string]string) string { - pArr := []string{} + var pArr []string ps := map[string]string{} for k, v := range publicParm { ps[k] = v diff --git a/handler/dnspod/dnspod_handler.go b/handler/dnspod/dnspod_handler.go index f00cbb6..84ad0b1 100644 --- a/handler/dnspod/dnspod_handler.go +++ b/handler/dnspod/dnspod_handler.go @@ -14,7 +14,7 @@ import ( "time" "github.com/TimothyYe/godns" - simplejson "github.com/bitly/go-simplejson" + "github.com/bitly/go-simplejson" ) // Handler struct definition diff --git a/handler/google/google_handler.go b/handler/google/google_handler.go index 45ed435..5225723 100644 --- a/handler/google/google_handler.go +++ b/handler/google/google_handler.go @@ -85,6 +85,7 @@ func (handler *Handler) UpdateIP(domain, subDomain, currentIP string) { if err != nil { // handle error log.Print("Failed to update sub domain:", subDomain) + return } defer resp.Body.Close() diff --git a/resolver/resolver.go b/resolver/resolver.go index d592fbd..1badd89 100644 --- a/resolver/resolver.go +++ b/resolver/resolver.go @@ -34,8 +34,13 @@ func NewFromResolvConf(path string) (*DNSResolver, error) { if _, err := os.Stat(path); os.IsNotExist(err) { return &DNSResolver{}, errors.New("no such file or directory: " + path) } + config, err := dns.ClientConfigFromFile(path) - servers := []string{} + if err != nil { + return &DNSResolver{}, err + } + + var servers []string for _, ipAddress := range config.Servers { servers = append(servers, net.JoinHostPort(ipAddress, "53")) } @@ -56,14 +61,14 @@ func (r *DNSResolver) lookupHost(host string, dnsType uint16, triesLeft int) ([] switch dnsType { case dns.TypeA: - m1.Question[0] = dns.Question{dns.Fqdn(host), dns.TypeA, dns.ClassINET} + m1.Question[0] = dns.Question{Name: dns.Fqdn(host), Qtype: dns.TypeA, Qclass: dns.ClassINET} case dns.TypeAAAA: - m1.Question[0] = dns.Question{dns.Fqdn(host), dns.TypeAAAA, dns.ClassINET} + m1.Question[0] = dns.Question{Name: dns.Fqdn(host), Qtype: dns.TypeAAAA, Qclass: dns.ClassINET} } in, err := dns.Exchange(m1, r.Servers[r.r.Intn(len(r.Servers))]) - result := []net.IP{} + var result []net.IP if err != nil { if strings.HasSuffix(err.Error(), "i/o timeout") && triesLeft > 0 { diff --git a/utils.go b/utils.go index 643bb0f..0db6df5 100644 --- a/utils.go +++ b/utils.go @@ -17,7 +17,7 @@ import ( "github.com/miekg/dns" "golang.org/x/net/proxy" - gomail "gopkg.in/gomail.v2" + "gopkg.in/gomail.v2" ) var ( @@ -118,10 +118,10 @@ func isIPv4(ip string) bool { } // GetHttpClient creates the HTTP client and return it -func GetHttpClient(configuration *Settings, use_proxy bool) *http.Client { +func GetHttpClient(configuration *Settings, useProxy bool) *http.Client { client := &http.Client{} - if use_proxy && configuration.Socks5Proxy != "" { + if useProxy && configuration.Socks5Proxy != "" { log.Println("use socks5 proxy:" + configuration.Socks5Proxy) dialer, err := proxy.SOCKS5("tcp", configuration.Socks5Proxy, nil, proxy.Direct) if err != nil { @@ -258,14 +258,14 @@ func SendTelegramNotify(configuration *Settings, domain, currentIP string) error } msg := buildTemplate(currentIP, domain, tpl) - url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&parse_mode=Markdown&text=%s", + reqURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&parse_mode=Markdown&text=%s", configuration.Notify.Telegram.BotApiKey, configuration.Notify.Telegram.ChatId, msg) var response *http.Response var err error - response, err = client.Get(url) + response, err = client.Get(reqURL) if err != nil { return err @@ -286,7 +286,7 @@ func SendTelegramNotify(configuration *Settings, domain, currentIP string) error Parameters *ResponseParameters `json:"parameters"` } var resp APIResponse - err = json.Unmarshal([]byte(body), &resp) + err = json.Unmarshal(body, &resp) if err != nil { fmt.Println("error:", err) return errors.New("failed to parse response") @@ -373,7 +373,7 @@ func SendSlackNotify(configuration *Settings, domain, currentIP string) error { Parameters *ResponseParameters `json:"parameters"` } var resp APIResponse - err = json.Unmarshal([]byte(body), &resp) + err = json.Unmarshal(body, &resp) if err != nil { fmt.Println("error:", err) return errors.New("failed to parse response")