-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrelay_http_client.go
More file actions
51 lines (47 loc) · 1.34 KB
/
Copy pathrelay_http_client.go
File metadata and controls
51 lines (47 loc) · 1.34 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
package main
import (
"fmt"
"net/http"
"net/url"
"strings"
)
func relayProfileUsesHTTPProxy(profile relayProfile) bool {
if profile.RelayMode != "mixedApi" && profile.RelayMode != "pureApi" {
return false
}
return profile.ProxyEnabled && strings.TrimSpace(profile.ProxyURL) != ""
}
func relayProfileProxyURL(profile relayProfile) (*url.URL, error) {
if !profile.ProxyEnabled {
return nil, nil
}
rawURL := strings.TrimSpace(profile.ProxyURL)
if rawURL == "" {
return nil, nil
}
parsed, err := url.Parse(rawURL)
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
return nil, fmt.Errorf("HTTP 代理地址无效:%s", rawURL)
}
if parsed.Scheme != "http" && parsed.Scheme != "https" {
return nil, fmt.Errorf("HTTP 代理地址无效:仅支持 http:// 或 https:// 代理 URL")
}
return parsed, nil
}
func relayHTTPClient(profile relayProfile) (*http.Client, error) {
proxyURL, err := relayProfileProxyURL(profile)
if err != nil {
return nil, err
}
if proxyURL == nil {
return http.DefaultClient, nil
}
baseTransport, ok := http.DefaultTransport.(*http.Transport)
if !ok {
transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)}
return &http.Client{Transport: transport}, nil
}
transport := baseTransport.Clone()
transport.Proxy = http.ProxyURL(proxyURL)
return &http.Client{Transport: transport}, nil
}