use custom http client

This commit is contained in:
zu1k
2020-08-12 20:49:07 +08:00
parent 88469325bd
commit ab9c13d852
5 changed files with 35 additions and 14 deletions

View File

@@ -2,7 +2,6 @@ package getter
import (
"io/ioutil"
"net/http"
"strings"
"sync"
@@ -19,7 +18,7 @@ type Subscribe struct {
}
func (s *Subscribe) Get() []proxy.Proxy {
resp, err := http.Get(s.Url)
resp, err := tool.GetHttpClient().Get(s.Url)
if err != nil {
return nil
}

View File

@@ -3,7 +3,6 @@ package getter
import (
"encoding/json"
"io/ioutil"
"net/http"
"sync"
"github.com/zu1k/proxypool/proxy"
@@ -41,7 +40,7 @@ func (w *WebFreessrXyz) Get2Chan(pc chan proxy.Proxy, wg *sync.WaitGroup) {
}
func freessrxyzFetch(link string) []proxy.Proxy {
resp, err := http.Get(link)
resp, err := tool.GetHttpClient().Get(link)
if err != nil {
return nil
}

View File

@@ -2,7 +2,6 @@ package getter
import (
"io/ioutil"
"net/http"
"sync"
"github.com/zu1k/proxypool/proxy"
@@ -18,7 +17,7 @@ type WebFuzz struct {
}
func (w *WebFuzz) Get() []proxy.Proxy {
resp, err := http.Get(w.Url)
resp, err := tool.GetHttpClient().Get(w.Url)
if err != nil {
return nil
}

View File

@@ -4,7 +4,6 @@ import (
"encoding/base64"
"encoding/json"
"io/ioutil"
"net/http"
"sync"
"github.com/zu1k/proxypool/proxy"
@@ -25,7 +24,7 @@ func NewWebLucnorg(options tool.Options) Getter {
}
func (w *WebLucnOrg) Get() []proxy.Proxy {
resp, err := http.Post(lucnorgSsrLink, "", nil)
resp, err := tool.GetHttpClient().Post(lucnorgSsrLink, nil)
if err != nil {
return nil
}

View File

@@ -1,18 +1,43 @@
package tool
import (
"io"
"net/http"
"time"
)
var httpClient = http.DefaultClient
func init() {
httpClient.Timeout = time.Second * 10
http.DefaultClient.Timeout = time.Second * 10
type HttpClient struct {
*http.Client
}
func GetHttpClient() *http.Client {
var httpClient *HttpClient
func init() {
httpClient = &HttpClient{http.DefaultClient}
httpClient.Timeout = time.Second * 10
}
func GetHttpClient() *HttpClient {
c := *httpClient
return &c
}
func (c *HttpClient) Get(url string) (resp *http.Response, err error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36")
return c.Do(req)
}
func (c *HttpClient) Post(url string, body io.Reader) (resp *http.Response, err error) {
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36")
return c.Do(req)
}