forked from SagerNet/LibSagerNetCore
-
Notifications
You must be signed in to change notification settings - Fork 9
/
request.go
51 lines (49 loc) · 1.39 KB
/
request.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
45
46
47
48
49
50
51
package libcore
import (
"context"
"fmt"
"github.com/pkg/errors"
v2rayNet "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/core"
"io/ioutil"
"net"
"net/http"
"time"
)
func (instance *V2RayInstance) DialHTTP(inbound string, timeout int32, link string) (string, error) {
transport := &http.Transport{
TLSHandshakeTimeout: time.Duration(timeout) * time.Millisecond,
DisableKeepAlives: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
dest, err := v2rayNet.ParseDestination(fmt.Sprintf("%s:%s", network, addr))
if err != nil {
return nil, err
}
if inbound != "" {
ctx = session.ContextWithInbound(ctx, &session.Inbound{Tag: inbound})
}
return core.Dial(ctx, instance.core, dest)
},
}
req, err := http.NewRequestWithContext(context.Background(), "GET", link, nil)
req.Header.Set("User-Agent", "curl/7.74.0")
if err != nil {
return "", errors.WithMessage(err, "create get request")
}
resp, err := (&http.Client{
Transport: transport,
Timeout: time.Duration(timeout) * time.Millisecond,
}).Do(req)
if err == nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", errors.Errorf("HTTP %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.WithMessage(err, "read body")
}
return string(body), nil
}