/
proxy_opt.go
69 lines (60 loc) · 1.32 KB
/
proxy_opt.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package gokhttp_ja3spoof
import (
"fmt"
oohttp "github.com/ooni/oohttp"
"net/http"
"net/url"
)
type ProxyOption struct {
ProxyURL string
}
func NewProxyOption(proxyURL string) *ProxyOption {
return &ProxyOption{ProxyURL: proxyURL}
}
// TODO: Allow for other types of proxies outside of stdlib too?
func (o *ProxyOption) Execute(client *http.Client) error {
puo, err := url.Parse(o.ProxyURL)
if err != nil {
return fmt.Errorf("ProxyOption: url.Parse: %w", err)
}
switch puo.Scheme {
case "socks":
fallthrough
case "socks4":
fallthrough
case "socks5":
fallthrough
default:
_, ok := client.Transport.(*http.Transport)
if ok {
client.Transport.(*http.Transport).Proxy = http.ProxyURL(puo)
}
_, ok = client.Transport.(*oohttp.StdlibTransport)
if ok {
client.Transport.(*oohttp.StdlibTransport).Transport.Proxy = oohttp.ProxyURL(puo)
}
break
}
return nil
}
func (o *ProxyOption) ExecuteV2(client *oohttp.Client) error {
puo, err := url.Parse(o.ProxyURL)
if err != nil {
return fmt.Errorf("ProxyOption: url.Parse: %w", err)
}
switch puo.Scheme {
case "socks":
fallthrough
case "socks4":
fallthrough
case "socks5":
fallthrough
default:
_, ok := client.Transport.(*oohttp.Transport)
if ok {
client.Transport.(*oohttp.Transport).Proxy = oohttp.ProxyURL(puo)
}
break
}
return nil
}