/
stealthTransport.go
180 lines (153 loc) · 6.7 KB
/
stealthTransport.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package stealth
import (
"fmt"
"log/slog"
"math/rand"
"net/http"
"sync"
"time"
"github.com/FrauElster/proxy/internal"
goProxy "golang.org/x/net/proxy"
)
type StealthTransport struct {
mu sync.Mutex
// Transport is the underlying transport used by the stealth transport
Transport http.RoundTripper
// userAgents is a list of user agents used by the stealth transport
// if the list is empty, the stealth transport will not set a user agent
userAgents []string
// minDelay and maxDelay are the minimum and maximum delay between requests
// the actual delay will be a random value between min and max
// if minDelay or maxDelay are 0, the stealth transport will not delay requests
minDelay time.Duration
maxDelay time.Duration
lastRequest time.Time
// socks5Proxy is the SOCKS5 proxy used by the stealth transport
// if socks5Proxy is empty, the stealth transport will not use a SOCKS5 proxy
socks5Proxy string
socksAuth *goProxy.Auth
socks5Initialized bool
// compression is true if the stealth transport will compress requests and decompress responses
// if the request is already compressed, the stealth transport will not compress it again, and will not decompress the response
compression bool
}
type StealthOption func(*StealthTransport)
// WithCompression enables compression for the stealth transport
var WithCompression = func(s *StealthTransport) {
s.compression = true
}
// WithSocks5 sets the SOCKS5 proxy used by the stealth transport
func WithSocks5(proxyAddr string, auth *goProxy.Auth) StealthOption {
return func(s *StealthTransport) {
s.socks5Proxy = proxyAddr
s.socksAuth = auth
}
}
// WithUserAgents the stealth transport will randomly choose one of the given user agents
// the most common user agents can be found in CommonUserAgents
func WithUserAgents(agents ...string) StealthOption {
return func(s *StealthTransport) {
s.userAgents = agents
}
}
// WithDelay sets the minimum and maximum delay between requests
// the actual delay will be a random value between min and max
func WithDelay(min, max time.Duration) StealthOption {
return func(s *StealthTransport) {
s.minDelay = min
s.maxDelay = max
}
}
func NewStealthTransport(opts ...StealthOption) *StealthTransport {
t := &StealthTransport{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
for _, opt := range opts {
opt(t)
}
return t
}
// RoundTrip implements the http.RoundTripper interface
func (t *StealthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
t.mu.Lock()
defer t.mu.Unlock()
// set a random user agent if one is not already set
if len(t.userAgents) > 0 {
randomUserAgent := t.userAgents[rand.Intn(len(t.userAgents))]
addHeaderIfNotExists(req, "User-Agent", randomUserAgent)
}
// add basic headers if they are not already set
addHeaderIfNotExists(req, "Accept-Language", "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7")
addHeaderIfNotExists(req, "Accept", "*/*")
addHeaderIfNotExists(req, "Connection", "keep-alive")
addHeaderIfNotExists(req, "Cache-Control", "no-cache")
addHeaderIfNotExists(req, "Pragma", "no-cache")
addHeaderIfNotExists(req, "DNT", "1")
// add compression header
hadCompression := req.Header.Get("Accept-Encoding") != ""
if t.compression && !hadCompression {
slog.Info("accepting compression")
req.Header.Set("Accept-Encoding", "gzip, deflate, br")
}
// initialize the SOCKS5 proxy if one is set
if t.socks5Proxy != "" && !t.socks5Initialized {
dialer, err := goProxy.SOCKS5("tcp", t.socks5Proxy, t.socksAuth, goProxy.Direct)
if err != nil {
return nil, fmt.Errorf("failed to initialize SOCKS5 proxy: %w", err)
}
currentTransport := t.Transport.(*http.Transport)
currentTransport.Dial = dialer.Dial
t.socks5Initialized = true
}
// delay the request if necessary
if t.minDelay > 0 && t.maxDelay > 0 {
actualDelay := time.Since(t.lastRequest)
wantDelay := rand.Int63n(int64(t.maxDelay-t.minDelay)) + int64(t.minDelay)
if actualDelay < time.Duration(wantDelay) {
time.Sleep(time.Duration(wantDelay) - actualDelay)
}
}
t.lastRequest = time.Now()
res, resErr := t.Transport.RoundTrip(req)
if resErr != nil {
return nil, resErr
}
// decompress
if t.compression && !hadCompression && res.Header.Get("Content-Encoding") != "" {
slog.Info("decompressing")
err := internal.DecompressResponse(res)
if err != nil {
return nil, err
}
}
return res, nil
}
func addHeaderIfNotExists(req *http.Request, key, value string) {
if req.Header.Get(key) == "" {
req.Header.Set(key, value)
}
}
var CommonUserAgents = []string{
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537",
"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36 Edge/16.16299",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8",
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; AS; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
"Mozilla/5.0 (Windows NT 6.1; Trident/7.0; AS; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 Edge/16.16299",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36",
}