-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
43 lines (37 loc) · 879 Bytes
/
proxy.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
package main
import (
"errors"
"net"
"net/http"
"net/url"
"strings"
"time"
)
func SetupProxy(proxyOption string) (*http.Transport, error) {
if proxyOption == "DIRECT" {
return &http.Transport{}, nil
}
parts := strings.Split(proxyOption, " ")
if len(parts) != 2 {
return nil, errors.New("invalid proxy option format")
}
// 拼接代理 URL
proxyURL, err := url.Parse(parts[0] + "://" + parts[1])
if err != nil {
return nil, err
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
return transport, nil
}