-
Notifications
You must be signed in to change notification settings - Fork 10
/
round_tripper_test.go
104 lines (83 loc) · 3.23 KB
/
round_tripper_test.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
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
package cloudflarebp_test
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"os"
"testing"
cloudflarebp "github.com/DaRealFreak/cloudflare-bp-go"
"github.com/stretchr/testify/assert"
"golang.org/x/net/proxy"
)
func TestApplyCloudFlareByPassDefaultClient(t *testing.T) {
client := http.DefaultClient
res, err := client.Get("https://www.patreon.com/login")
assert.New(t).NoError(err)
assert.New(t).Equal(403, res.StatusCode)
// apply our bypass for request headers and client TLS configurations
http.DefaultClient.Transport = cloudflarebp.AddCloudFlareByPass(http.DefaultClient.Transport)
res, err = client.Get("https://www.patreon.com/login")
assert.New(t).NoError(err)
assert.New(t).Equal(200, res.StatusCode)
}
func TestApplyCloudFlareByPassDefinedTransport(t *testing.T) {
client := &http.Client{
Transport: &http.Transport{},
}
// if the client requests something before applying the fix some configurations are applied already
// and our ByPass won't work anymore, so we have to apply our ByPass as the first thing
client.Transport = cloudflarebp.AddCloudFlareByPass(client.Transport)
res, err := client.Get("https://www.patreon.com/login")
assert.New(t).NoError(err)
assert.New(t).Equal(200, res.StatusCode)
}
// TestAddCloudFlareByPassSocksProxy tests the CloudFlare bypass while we're using a SOCK5 proxy transport layer.
func TestAddCloudFlareByPassSocksProxy(t *testing.T) {
auth := proxy.Auth{
User: os.Getenv("PROXY_USER"),
Password: os.Getenv("PROXY_PASS"),
}
dialer, err := proxy.SOCKS5(
"tcp",
fmt.Sprintf("%s:1080", os.Getenv("PROXY_HOST_SOCKS5")),
&auth,
proxy.Direct,
)
assert.New(t).NoError(err)
dc := dialer.(interface {
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
})
client := &http.Client{
Transport: &http.Transport{DialContext: dc.DialContext},
}
// if the client requests something before applying the fix some configurations are applied already
// and our ByPass won't work anymore, so we have to apply our ByPass as the first thing
client.Transport = cloudflarebp.AddCloudFlareByPass(client.Transport)
res, err := client.Get("https://www.patreon.com/login")
assert.New(t).NoError(err)
assert.New(t).Equal(200, res.StatusCode)
}
// TestAddCloudFlareByPassHTTPProxy tests the CloudFlare bypass while we're using a HTTP proxy transport layer.
func TestAddCloudFlareByPassHTTPProxy(t *testing.T) {
proxyURL, _ := url.Parse(
fmt.Sprintf(
"https://%s:%s@%s:%s",
url.QueryEscape(os.Getenv("PROXY_USER")), url.QueryEscape(os.Getenv("PROXY_PASS")),
url.QueryEscape(os.Getenv("PROXY_HOST_HTTPS")), url.QueryEscape(os.Getenv("PROXY_PORT_HTTPS")),
),
)
client := &http.Client{
Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)},
}
res, err := client.Get("https://www.patreon.com/login")
assert.New(t).NoError(err)
assert.New(t).Equal(403, res.StatusCode)
// if the client requests something before applying the fix some configurations are applied already
// and our ByPass won't work anymore, so we have to apply our ByPass as the first thing
client.Transport = cloudflarebp.AddCloudFlareByPass(client.Transport)
res, err = client.Get("https://www.patreon.com/login")
assert.New(t).NoError(err)
assert.New(t).Equal(200, res.StatusCode)
}