-
Notifications
You must be signed in to change notification settings - Fork 12
/
httpclient.go
44 lines (39 loc) · 919 Bytes
/
httpclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package client
import (
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
"strings"
"time"
)
type Client struct {
client fasthttp.Client
timeout int64
maxRetries int64
proxy string
}
func DefaultClient() *Client {
return NewClient(30, "")
}
func NewClient(timeout int64, proxy string) *Client {
cli := &Client{
client: fasthttp.Client{
MaxConnsPerHost: 65000,
//Dial: func(addr string) (net.Conn, error) {
// return nil, nil
//},
},
timeout: timeout,
proxy: proxy,
}
if proxy != "" {
if strings.HasPrefix(proxy, "socks5://") {
cli.client.Dial = fasthttpproxy.FasthttpSocksDialer(proxy)
} else {
cli.client.Dial = fasthttpproxy.FasthttpHTTPDialer(proxy)
}
}
return cli
}
func (cli *Client) Do(req *fasthttp.Request, resp *fasthttp.Response) error {
return cli.client.DoTimeout(req, resp, time.Second*time.Duration(cli.timeout))
}