-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
60 lines (54 loc) · 1.42 KB
/
Copy pathutils.go
File metadata and controls
60 lines (54 loc) · 1.42 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
package proxycheck
import (
"context"
"fmt"
"h12.io/socks"
"io"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
func proxyAddrToIpPort(proxyAddr string) (ip net.IP, port int64, err error) {
parts := strings.Split(proxyAddr, ":")
if len(parts) == 2 {
port, err = strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return ip, port, fmt.Errorf("invalid port: %v", err)
}
} else if len(parts) == 1 {
port = 80
}
ip = net.ParseIP(parts[0])
if len(ip) == 0 {
return ip, port, fmt.Errorf("invalid ip '%s'", parts[0])
}
return
}
func createProxyTransport(proxyURL *url.URL, timeout time.Duration) *http.Transport {
httpTransport := &http.Transport{
TLSHandshakeTimeout: timeout,
IdleConnTimeout: timeout,
ResponseHeaderTimeout: timeout,
DisableKeepAlives: true,
DisableCompression: false,
}
if strings.HasPrefix(proxyURL.Scheme, "socks") {
httpTransport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return socks.Dial(fmt.Sprintf("%s?timeout=%s", proxyURL.String(), timeout.String()))(network, addr)
}
} else {
httpTransport.Proxy = http.ProxyURL(proxyURL)
httpTransport.DialContext = (&net.Dialer{
Timeout: timeout,
KeepAlive: timeout,
}).DialContext
}
return httpTransport
}
func readResponse(httpResponse *http.Response) ([]byte, error) {
defer func() { _ = httpResponse.Body.Close() }()
return io.ReadAll(httpResponse.Body)
}