2023-06-01 17:05:14 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-05 16:30:10 +08:00
|
|
|
"crypto/tls"
|
2023-11-17 13:50:08 +08:00
|
|
|
"errors"
|
|
|
|
"net"
|
2023-06-01 17:05:14 +08:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2024-04-25 16:45:12 +08:00
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2024-05-29 11:05:09 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/xpack"
|
2023-06-01 17:05:14 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetHttpRes(url string) (*http.Response, error) {
|
|
|
|
client := &http.Client{
|
2023-11-17 13:50:08 +08:00
|
|
|
Timeout: time.Second * 300,
|
2023-06-01 17:05:14 +08:00
|
|
|
}
|
2024-05-29 11:05:09 +08:00
|
|
|
transportItem := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 60 * time.Second,
|
|
|
|
KeepAlive: 60 * time.Second,
|
|
|
|
}).DialContext,
|
|
|
|
TLSHandshakeTimeout: 5 * time.Second,
|
|
|
|
ResponseHeaderTimeout: 10 * time.Second,
|
|
|
|
IdleConnTimeout: 15 * time.Second,
|
|
|
|
}
|
|
|
|
ok, transport := xpack.LoadRequestTransport()
|
|
|
|
if ok {
|
|
|
|
transportItem.DialContext = transport.DialContext
|
|
|
|
transportItem.Proxy = transport.Proxy
|
|
|
|
}
|
|
|
|
client.Transport = transportItem
|
2023-06-01 17:05:14 +08:00
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, buserr.WithMap("ErrCreateHttpClient", map[string]interface{}{"err": err.Error()}, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2023-11-17 13:50:08 +08:00
|
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
2023-06-01 17:05:14 +08:00
|
|
|
return nil, buserr.WithMap("ErrHttpReqTimeOut", map[string]interface{}{"err": err.Error()}, err)
|
|
|
|
} else {
|
|
|
|
if strings.Contains(err.Error(), "no such host") {
|
|
|
|
return nil, buserr.New("ErrNoSuchHost")
|
|
|
|
}
|
|
|
|
return nil, buserr.WithMap("ErrHttpReqFailed", map[string]interface{}{"err": err.Error()}, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if resp.StatusCode == 404 {
|
|
|
|
return nil, buserr.New("ErrHttpReqNotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|