From 025a5c0bbb2918bd36770faf39741c0fd81fc961 Mon Sep 17 00:00:00 2001 From: Timothy Date: Tue, 30 Jul 2019 14:35:13 +0800 Subject: [PATCH] refactor handlers --- handler/cloudflare/cloudflare_handler.go | 16 +++------------- handler/dnspod/dnspod_handler.go | 19 ++++--------------- handler/duck/duck_handler.go | 17 +---------------- handler/google/google_handler.go | 17 +---------------- handler/he/he_handler.go | 17 +---------------- utils.go | 20 ++++++++++++++++++++ 6 files changed, 30 insertions(+), 76 deletions(-) diff --git a/handler/cloudflare/cloudflare_handler.go b/handler/cloudflare/cloudflare_handler.go index 40f2e7f..ed94869 100644 --- a/handler/cloudflare/cloudflare_handler.go +++ b/handler/cloudflare/cloudflare_handler.go @@ -12,7 +12,6 @@ import ( "time" "github.com/TimothyYe/godns" - "golang.org/x/net/proxy" ) // Handler struct definition @@ -139,18 +138,9 @@ func recordTracked(domain *godns.Domain, record *DNSRecord) bool { // Create a new request with auth in place and optional proxy func (handler *Handler) newRequest(method, url string, body io.Reader) (*http.Request, *http.Client) { - client := &http.Client{} - - if handler.Configuration.Socks5Proxy != "" { - log.Println("use socks5 proxy:" + handler.Configuration.Socks5Proxy) - dialer, err := proxy.SOCKS5("tcp", handler.Configuration.Socks5Proxy, nil, proxy.Direct) - if err != nil { - log.Println("can't connect to the proxy:", err) - } else { - httpTransport := &http.Transport{} - client.Transport = httpTransport - httpTransport.Dial = dialer.Dial - } + client := godns.GetHttpClient(handler.Configuration) + if client == nil { + log.Println("cannot create HTTP client") } req, _ := http.NewRequest(method, handler.API+url, body) diff --git a/handler/dnspod/dnspod_handler.go b/handler/dnspod/dnspod_handler.go index 1aaa27e..4212025 100644 --- a/handler/dnspod/dnspod_handler.go +++ b/handler/dnspod/dnspod_handler.go @@ -2,6 +2,7 @@ package dnspod import ( "encoding/json" + "errors" "fmt" "io/ioutil" "log" @@ -14,7 +15,6 @@ import ( "github.com/TimothyYe/godns" simplejson "github.com/bitly/go-simplejson" - "golang.org/x/net/proxy" ) // Handler struct definition @@ -239,21 +239,10 @@ func (handler *Handler) UpdateIP(domainID int64, subDomainID string, subDomainNa // PostData post data and invoke DNSPod API func (handler *Handler) PostData(url string, content url.Values) (string, error) { - client := &http.Client{} + client := godns.GetHttpClient(handler.Configuration) - if handler.Configuration.Socks5Proxy != "" { - - log.Println("use socks5 proxy:" + handler.Configuration.Socks5Proxy) - - dialer, err := proxy.SOCKS5("tcp", handler.Configuration.Socks5Proxy, nil, proxy.Direct) - if err != nil { - fmt.Println("can't connect to the proxy:", err) - return "", err - } - - httpTransport := &http.Transport{} - client.Transport = httpTransport - httpTransport.Dial = dialer.Dial + if client == nil { + return "", errors.New("failed to create HTTP client") } values := handler.GenerateHeader(content) diff --git a/handler/duck/duck_handler.go b/handler/duck/duck_handler.go index 3ccf555..63398b7 100644 --- a/handler/duck/duck_handler.go +++ b/handler/duck/duck_handler.go @@ -4,12 +4,10 @@ import ( "fmt" "io/ioutil" "log" - "net/http" "runtime/debug" "time" "github.com/TimothyYe/godns" - "golang.org/x/net/proxy" ) var ( @@ -52,20 +50,7 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns. log.Printf("IP is the same as cached one. Skip update.\n") } else { lastIP = currentIP - client := &http.Client{} - - if handler.Configuration.Socks5Proxy != "" { - log.Println("use socks5 proxy:" + handler.Configuration.Socks5Proxy) - dialer, err := proxy.SOCKS5("tcp", handler.Configuration.Socks5Proxy, nil, proxy.Direct) - if err != nil { - log.Println("can't connect to the proxy:", err) - return - } - - httpTransport := &http.Transport{} - client.Transport = httpTransport - httpTransport.Dial = dialer.Dial - } + client := godns.GetHttpClient(handler.Configuration) for _, subDomain := range domain.SubDomains { // update IP with HTTP GET request diff --git a/handler/google/google_handler.go b/handler/google/google_handler.go index 8c4604e..22f3fcc 100644 --- a/handler/google/google_handler.go +++ b/handler/google/google_handler.go @@ -11,8 +11,6 @@ import ( "time" "github.com/TimothyYe/godns" - - "golang.org/x/net/proxy" ) var ( @@ -83,20 +81,7 @@ func (handler *Handler) UpdateIP(domain, subDomain, currentIP string) { values.Add("username:password", fmt.Sprintf("%s:%s", handler.Configuration.Email, handler.Configuration.Password)) values.Add("myip", currentIP) - client := &http.Client{} - - if handler.Configuration.Socks5Proxy != "" { - log.Println("use socks5 proxy:" + handler.Configuration.Socks5Proxy) - dialer, err := proxy.SOCKS5("tcp", handler.Configuration.Socks5Proxy, nil, proxy.Direct) - if err != nil { - log.Println("can't connect to the proxy:", err) - return - } - - httpTransport := &http.Transport{} - client.Transport = httpTransport - httpTransport.Dial = dialer.Dial - } + client := godns.GetHttpClient(handler.Configuration) req, _ := http.NewRequest("POST", GoogleUrl, strings.NewReader(values.Encode())) resp, err := client.Do(req) diff --git a/handler/he/he_handler.go b/handler/he/he_handler.go index 303aada..3ed9aa3 100644 --- a/handler/he/he_handler.go +++ b/handler/he/he_handler.go @@ -11,8 +11,6 @@ import ( "time" "github.com/TimothyYe/godns" - - "golang.org/x/net/proxy" ) var ( @@ -82,20 +80,7 @@ func (handler *Handler) UpdateIP(domain, subDomain, currentIP string) { values.Add("password", handler.Configuration.Password) values.Add("myip", currentIP) - client := &http.Client{} - - if handler.Configuration.Socks5Proxy != "" { - log.Println("use socks5 proxy:" + handler.Configuration.Socks5Proxy) - dialer, err := proxy.SOCKS5("tcp", handler.Configuration.Socks5Proxy, nil, proxy.Direct) - if err != nil { - log.Println("can't connect to the proxy:", err) - return - } - - httpTransport := &http.Transport{} - client.Transport = httpTransport - httpTransport.Dial = dialer.Dial - } + client := godns.GetHttpClient(handler.Configuration) req, _ := http.NewRequest("POST", HEUrl, strings.NewReader(values.Encode())) resp, err := client.Do(req) diff --git a/utils.go b/utils.go index 1a7278a..d927681 100644 --- a/utils.go +++ b/utils.go @@ -109,6 +109,26 @@ func isIPv4(ip string) bool { return strings.Count(ip, ":") < 2 } +// GetHttpClient creates the HTTP client and return it +func GetHttpClient(configuration *Settings) *http.Client { + client := &http.Client{} + + if configuration.Socks5Proxy != "" { + log.Println("use socks5 proxy:" + configuration.Socks5Proxy) + dialer, err := proxy.SOCKS5("tcp", configuration.Socks5Proxy, nil, proxy.Direct) + if err != nil { + log.Println("can't connect to the proxy:", err) + return nil + } + + httpTransport := &http.Transport{} + client.Transport = httpTransport + httpTransport.Dial = dialer.Dial + } + + return client +} + //GetCurrentIP gets an IP from either internet or specific interface, depending on configuration func GetCurrentIP(configuration *Settings) (string, error) { var err error