对象改大写
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/net/proxy"
|
"golang.org/x/net/proxy"
|
||||||
@@ -18,8 +19,6 @@ import (
|
|||||||
* Date: 2023年7月12日11:35:01
|
* Date: 2023年7月12日11:35:01
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// 默认的transport
|
// 默认的transport
|
||||||
transport http.Transport = http.Transport{
|
transport http.Transport = http.Transport{
|
||||||
@@ -36,7 +35,13 @@ var (
|
|||||||
// conn.SetDeadline(time.Now().Add(time.Second * time.Duration(timeOut))) // 设置发送接收数据超时
|
// conn.SetDeadline(time.Now().Add(time.Second * time.Duration(timeOut))) // 设置发送接收数据超时
|
||||||
// return conn, nil
|
// return conn, nil
|
||||||
// },
|
// },
|
||||||
// ResponseHeaderTimeout: time.Second * time.Duration(defaultTimeOut), // 响应超时
|
// DialContext: (&net.Dialer{
|
||||||
|
// Timeout: 3 * time.Second, // 建立TCP链接的超时时间
|
||||||
|
// KeepAlive: 30 * time.Second, // TCP keepalive超时时间
|
||||||
|
// }).DialContext,
|
||||||
|
// TLSHandshakeTimeout: time.Second * 10, // TLS握手超时
|
||||||
|
// ResponseHeaderTimeout: time.Second * 10, // 接收响应头的超时时间
|
||||||
|
// ExpectContinueTimeout: time.Second * 10, // 发送请求头超时时间 100-continue状态码超时时间
|
||||||
DisableKeepAlives: false, // 短连接(默认是使用长连接,连接过多时会造成服务器拒绝服务问题)
|
DisableKeepAlives: false, // 短连接(默认是使用长连接,连接过多时会造成服务器拒绝服务问题)
|
||||||
MaxIdleConns: 0, // 所有host的连接池最大连接数量,默认无穷大
|
MaxIdleConns: 0, // 所有host的连接池最大连接数量,默认无穷大
|
||||||
MaxIdleConnsPerHost: 5, // 每个host的连接池最大空闲连接收,默认2
|
MaxIdleConnsPerHost: 5, // 每个host的连接池最大空闲连接收,默认2
|
||||||
@@ -49,13 +54,13 @@ var (
|
|||||||
|
|
||||||
type DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
|
type DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||||
|
|
||||||
type curlx struct {
|
type Curlx struct {
|
||||||
transport *http.Transport
|
transport *http.Transport
|
||||||
timeOutSecond int
|
timeOutSecond int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCurlx() *curlx {
|
func NewCurlx() *Curlx {
|
||||||
return &curlx{
|
return &Curlx{
|
||||||
transport: &transport,
|
transport: &transport,
|
||||||
timeOutSecond: 180,
|
timeOutSecond: 180,
|
||||||
}
|
}
|
||||||
@@ -63,8 +68,9 @@ func NewCurlx() *curlx {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用Socks5代理
|
* 使用Socks5代理
|
||||||
|
* @param address "socks5://127.0.0.1:1080"
|
||||||
*/
|
*/
|
||||||
func (c *curlx) WithSocks5(address string) error {
|
func (c *Curlx) WithProxySocks5(address string) error {
|
||||||
baseDialer := &net.Dialer{
|
baseDialer := &net.Dialer{
|
||||||
Timeout: 180 * time.Second,
|
Timeout: 180 * time.Second,
|
||||||
KeepAlive: 180 * time.Second,
|
KeepAlive: 180 * time.Second,
|
||||||
@@ -82,24 +88,42 @@ func (c *curlx) WithSocks5(address string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用HTTP代理
|
||||||
|
* @param proxyAddr "https://proxyserver:port"
|
||||||
|
*/
|
||||||
|
func (c *Curlx) WithProxyHttp(proxyAddr string) error {
|
||||||
|
proxy, err := url.Parse(proxyAddr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.transport.Proxy = http.ProxyURL(proxy)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 不校验HTTPS证书
|
* 不校验HTTPS证书
|
||||||
*/
|
*/
|
||||||
func (c *curlx) WithInsecureSkipVerify() {
|
func (c *Curlx) WithInsecureSkipVerify() {
|
||||||
c.transport.TLSClientConfig.InsecureSkipVerify = true
|
c.transport.TLSClientConfig.InsecureSkipVerify = true
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置超时时间,单位秒
|
* 设置超时时间,单位秒
|
||||||
*/
|
*/
|
||||||
func (c *curlx) WithTimeout(timeout int) {
|
func (c *Curlx) WithTimeout(timeout int) {
|
||||||
c.timeOutSecond = timeout
|
c.timeOutSecond = timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 指定访问的IP
|
||||||
|
// func(c *curlx) WithIp(ip string) {
|
||||||
|
// c.transport.Dial
|
||||||
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 简单请求
|
* 简单请求
|
||||||
*/
|
*/
|
||||||
func (c *curlx) Send(ctx context.Context, p *CurlParams) (res string, httpcode int, err error) {
|
func (c *Curlx) Send(ctx context.Context, p *CurlParams) (res string, httpcode int, err error) {
|
||||||
response, err := c.sendExec(ctx, p)
|
response, err := c.sendExec(ctx, p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", -1, err
|
return "", -1, err
|
||||||
@@ -139,9 +163,9 @@ func (c *curlx) Send(ctx context.Context, p *CurlParams) (res string, httpcode i
|
|||||||
* 执行发送
|
* 执行发送
|
||||||
* 注意:外部使用需要加这一句 defer response.Body.Close()
|
* 注意:外部使用需要加这一句 defer response.Body.Close()
|
||||||
*/
|
*/
|
||||||
func (c *curlx) sendExec(ctx context.Context, p *CurlParams) (resp *http.Response, err error) {
|
func (c *Curlx) sendExec(ctx context.Context, p *CurlParams) (resp *http.Response, err error) {
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: time.Second * time.Duration(c.timeOutSecond), // 设置该条连接的超时
|
Timeout: time.Second * time.Duration(c.timeOutSecond), // 整个请求的超时时间 设置该条连接的超时
|
||||||
Transport: c.transport, //
|
Transport: c.transport, //
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,7 +220,7 @@ func (c *curlx) sendExec(ctx context.Context, p *CurlParams) (resp *http.Respons
|
|||||||
/**
|
/**
|
||||||
* 流式请求
|
* 流式请求
|
||||||
*/
|
*/
|
||||||
func (c *curlx) SendChan(ctx context.Context, p *CurlParams) (<-chan string, error) {
|
func (c *Curlx) SendChan(ctx context.Context, p *CurlParams) (<-chan string, error) {
|
||||||
|
|
||||||
data := make(chan string, 1000)
|
data := make(chan string, 1000)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user