mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2024-11-16 04:24:57 +08:00
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"errors"
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func GetHttpRes(url string) (*http.Response, error) {
|
|
client := &http.Client{
|
|
Timeout: time.Second * 300,
|
|
Transport: &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,
|
|
},
|
|
}
|
|
|
|
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 {
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
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
|
|
}
|