1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

refactor handlers

This commit is contained in:
Timothy 2019-07-30 14:35:13 +08:00
parent 7720a92ad6
commit 025a5c0bbb
6 changed files with 30 additions and 76 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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