mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 13:41:23 +08:00
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
URL "net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Dreamacro/clash/component/tls"
|
|
"github.com/Dreamacro/clash/listener/inner"
|
|
)
|
|
|
|
const (
|
|
UA = "clash.meta"
|
|
)
|
|
|
|
func HttpRequest(ctx context.Context, url, method string, header map[string][]string, body io.Reader) (*http.Response, error) {
|
|
method = strings.ToUpper(method)
|
|
urlRes, err := URL.Parse(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err := http.NewRequest(method, urlRes.String(), body)
|
|
for k, v := range header {
|
|
for _, v := range v {
|
|
req.Header.Add(k, v)
|
|
}
|
|
}
|
|
|
|
if _, ok := header["User-Agent"]; !ok {
|
|
req.Header.Set("User-Agent", UA)
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if user := urlRes.User; user != nil {
|
|
password, _ := user.Password()
|
|
req.SetBasicAuth(user.Username(), password)
|
|
}
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
transport := &http.Transport{
|
|
// from http.DefaultTransport
|
|
MaxIdleConns: 100,
|
|
IdleConnTimeout: 30 * time.Second,
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
conn := inner.HandleTcp(address, "")
|
|
return conn, nil
|
|
},
|
|
TLSClientConfig: tls.GetDefaultTLSConfig(),
|
|
}
|
|
|
|
client := http.Client{Transport: transport}
|
|
return client.Do(req)
|
|
|
|
}
|