-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
119 lines (111 loc) · 2.91 KB
/
Copy pathhttp.go
File metadata and controls
119 lines (111 loc) · 2.91 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package ipcheck
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"time"
)
type transportBody struct {
io.ReadCloser
transport *http.Transport
}
func (b *transportBody) Close() error {
err := b.ReadCloser.Close()
if b.transport != nil {
b.transport.CloseIdleConnections()
}
return err
}
func pinnedTransport(targetIP string, port int, serverName string, timeout time.Duration) *http.Transport {
dialer := &net.Dialer{Timeout: timeout}
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) {
return dialer.DialContext(ctx, network, net.JoinHostPort(targetIP, fmt.Sprint(port)))
},
TLSClientConfig: &tls.Config{
ServerName: serverName,
InsecureSkipVerify: true,
},
DisableKeepAlives: true,
TLSHandshakeTimeout: timeout,
DisableCompression: true,
ResponseHeaderTimeout: timeout,
ForceAttemptHTTP2: false,
}
}
func doPinnedGET(ctx context.Context, targetIP string, port int, rawURL string, timeout time.Duration, userAgent string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
if err != nil {
return nil, err
}
if userAgent != "" {
req.Header.Set("User-Agent", userAgent)
}
transport := pinnedTransport(targetIP, port, req.URL.Hostname(), timeout)
client := &http.Client{
Transport: transport,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) >= 5 {
return errors.New("too many redirects")
}
if req.URL.Scheme == "" {
base := via[len(via)-1].URL
req.URL = base.ResolveReference(req.URL)
}
req.Host = req.URL.Hostname()
return nil
},
}
resp, err := client.Do(req)
if err != nil {
transport.CloseIdleConnections()
return nil, err
}
if resp.Body != nil {
resp.Body = &transportBody{ReadCloser: resp.Body, transport: transport}
} else {
transport.CloseIdleConnections()
}
return resp, nil
}
func newTimeoutHTTPClient(proxy string, timeout time.Duration) (*http.Client, error) {
dialer := &net.Dialer{Timeout: timeout}
transport := &http.Transport{
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
return dialer.DialContext(ctx, network, address)
},
TLSHandshakeTimeout: timeout,
ResponseHeaderTimeout: timeout,
DisableCompression: true,
DisableKeepAlives: true,
ForceAttemptHTTP2: false,
}
if proxy != "" {
pu, err := url.Parse(proxy)
if err != nil {
return nil, err
}
transport.Proxy = http.ProxyURL(pu)
} else {
transport.Proxy = http.ProxyFromEnvironment
}
return &http.Client{
Transport: transport,
}, nil
}
func readLimitedBody(r io.Reader, limit int64) ([]byte, error) {
return io.ReadAll(io.LimitReader(r, limit))
}
func normalizeURL(raw string) (string, error) {
u, err := url.Parse(raw)
if err != nil {
return "", err
}
return u.String(), nil
}