-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclient.go
More file actions
259 lines (212 loc) · 7.74 KB
/
client.go
File metadata and controls
259 lines (212 loc) · 7.74 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package warc
import (
"net/http"
"os"
"sync"
"sync/atomic"
"time"
"github.com/maypok86/otter"
"github.com/miekg/dns"
)
type Error struct {
Err error
Func string
}
type HTTPClientSettings struct {
RotatorSettings *RotatorSettings
Proxy string
TempDir string
DiscardHook DiscardHook
DNSServers []string
DNSFallback *dns.ClientConfig
DedupeOptions DedupeOptions
DialTimeout time.Duration
ResponseHeaderTimeout time.Duration
DNSResolutionTimeout time.Duration
DNSRecordsTTL time.Duration
DNSCacheSize int
DNSConcurrency int
TLSHandshakeTimeout time.Duration
ConnReadDeadline time.Duration
MaxReadBeforeTruncate int
DecompressBody bool
FollowRedirects bool
FullOnDisk bool
MaxRAMUsageFraction float64
VerifyCerts bool
RandomLocalIP bool
DisableIPv4 bool
DisableIPv6 bool
IPv6AnyIP bool
DigestAlgorithm DigestAlgorithm
}
type CustomHTTPClient struct {
interfacesWatcherStop chan bool
WaitGroup *WaitGroupWithCount
dedupeHashTable *otter.Cache[string, revisitRecord]
ErrChan chan *Error
WARCWriter chan *RecordBatch
interfacesWatcherStarted chan bool
http.Client
TempDir string
WARCWriterDoneChannels []chan bool
DiscardHook DiscardHook
dedupeOptions DedupeOptions
TLSHandshakeTimeout time.Duration
ConnReadDeadline time.Duration
MaxReadBeforeTruncate int
verifyCerts bool
FullOnDisk bool
DigestAlgorithm DigestAlgorithm
closeDNSCache func()
closeDedupeCache func()
// MaxRAMUsageFraction is the fraction of system RAM above which we'll force spooling to disk. For example, 0.5 = 50%.
// If set to <= 0, the default value is DefaultMaxRAMUsageFraction.
MaxRAMUsageFraction float64
randomLocalIP bool
DataTotal *atomic.Int64
CDXDedupeTotalBytes *atomic.Int64
DoppelgangerDedupeTotalBytes *atomic.Int64
LocalDedupeTotalBytes *atomic.Int64
CDXDedupeTotal *atomic.Int64
DoppelgangerDedupeTotal *atomic.Int64
LocalDedupeTotal *atomic.Int64
}
func (c *CustomHTTPClient) Close() error {
var wg sync.WaitGroup
c.WaitGroup.Wait()
c.CloseIdleConnections()
close(c.WARCWriter)
wg.Add(len(c.WARCWriterDoneChannels))
for _, doneChan := range c.WARCWriterDoneChannels {
go func(done chan bool) {
defer wg.Done()
<-done
}(doneChan)
}
wg.Wait()
close(c.ErrChan)
if c.randomLocalIP {
c.interfacesWatcherStop <- true
close(c.interfacesWatcherStop)
}
c.closeDNSCache()
c.closeDedupeCache()
return nil
}
func NewWARCWritingHTTPClient(HTTPClientSettings HTTPClientSettings) (httpClient *CustomHTTPClient, err error) {
httpClient = new(CustomHTTPClient)
// Initialize counters
httpClient.DataTotal = &DataTotal
httpClient.CDXDedupeTotalBytes = &CDXDedupeTotalBytes
httpClient.DoppelgangerDedupeTotalBytes = &DoppelgangerDedupeTotalBytes
httpClient.LocalDedupeTotalBytes = &LocalDedupeTotalBytes
httpClient.CDXDedupeTotal = &CDXDedupeTotal
httpClient.DoppelgangerDedupeTotal = &DoppelgangerDedupeTotal
httpClient.LocalDedupeTotal = &LocalDedupeTotal
// Configure random local IP
httpClient.randomLocalIP = HTTPClientSettings.RandomLocalIP
if httpClient.randomLocalIP {
httpClient.interfacesWatcherStop = make(chan bool)
httpClient.interfacesWatcherStarted = make(chan bool)
go httpClient.getAvailableIPs(HTTPClientSettings.IPv6AnyIP)
<-httpClient.interfacesWatcherStarted
}
// Set block and payload digest algorithm (it's important to set that before we configure the WARC writer)
httpClient.DigestAlgorithm = HTTPClientSettings.DigestAlgorithm
HTTPClientSettings.RotatorSettings.digestAlgorithm = HTTPClientSettings.DigestAlgorithm
// Toggle deduplication options and create map for deduplication records.
httpClient.dedupeOptions = HTTPClientSettings.DedupeOptions
// Set default dedupe cache size to 1M entries if not specified
dedupeCacheSize := HTTPClientSettings.DedupeOptions.DedupeCacheSize
if dedupeCacheSize == 0 {
dedupeCacheSize = 1_000_000
}
dedupeCache, err := otter.MustBuilder[string, revisitRecord](dedupeCacheSize).Build()
if err != nil {
return nil, err
}
httpClient.dedupeHashTable = &dedupeCache
httpClient.closeDedupeCache = func() {
httpClient.dedupeHashTable.Close()
time.Sleep(1 * time.Second)
}
// Set default deduplication threshold to 2048 bytes
if httpClient.dedupeOptions.SizeThreshold == 0 {
httpClient.dedupeOptions.SizeThreshold = 2048
}
// Set a hook to determine if we should discard a response
httpClient.DiscardHook = HTTPClientSettings.DiscardHook
// Create an error channel for sending WARC errors through
httpClient.ErrChan = make(chan *Error)
// Toggle verification of certificates
// InsecureSkipVerify expects the opposite of the verifyCerts flag, as such we flip it.
httpClient.verifyCerts = !HTTPClientSettings.VerifyCerts
// Configure WARC temporary file directory
if HTTPClientSettings.TempDir != "" {
httpClient.TempDir = HTTPClientSettings.TempDir
err = os.MkdirAll(httpClient.TempDir, os.ModePerm)
if err != nil {
return nil, err
}
}
// Configure if we are only storing responses only on disk or in memory and on disk.
httpClient.FullOnDisk = HTTPClientSettings.FullOnDisk
// Configure the maximum RAM usage fraction
httpClient.MaxRAMUsageFraction = HTTPClientSettings.MaxRAMUsageFraction
// Configure our max read before we start truncating records
if HTTPClientSettings.MaxReadBeforeTruncate == 0 {
httpClient.MaxReadBeforeTruncate = 1000000000
} else {
httpClient.MaxReadBeforeTruncate = HTTPClientSettings.MaxReadBeforeTruncate
}
// Configure the waitgroup
httpClient.WaitGroup = new(WaitGroupWithCount)
// Configure WARC writer
httpClient.WARCWriter, httpClient.WARCWriterDoneChannels, err = HTTPClientSettings.RotatorSettings.NewWARCRotator()
if err != nil {
return nil, err
}
// Configure HTTP client
if !HTTPClientSettings.FollowRedirects {
httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
// Verify timeouts and set default values
if HTTPClientSettings.DialTimeout == 0 {
HTTPClientSettings.DialTimeout = 10 * time.Second
}
if HTTPClientSettings.ResponseHeaderTimeout == 0 {
HTTPClientSettings.ResponseHeaderTimeout = 10 * time.Second
}
if HTTPClientSettings.TLSHandshakeTimeout == 0 {
HTTPClientSettings.TLSHandshakeTimeout = 10 * time.Second
}
if HTTPClientSettings.DNSResolutionTimeout == 0 {
HTTPClientSettings.DNSResolutionTimeout = 5 * time.Second
}
if HTTPClientSettings.DNSRecordsTTL == 0 {
HTTPClientSettings.DNSRecordsTTL = 5 * time.Minute
}
if HTTPClientSettings.DNSCacheSize == 0 {
HTTPClientSettings.DNSCacheSize = 10_000
}
httpClient.TLSHandshakeTimeout = HTTPClientSettings.TLSHandshakeTimeout
httpClient.ConnReadDeadline = HTTPClientSettings.ConnReadDeadline
// Configure custom dialer / transport
customDialer, err := newCustomDialer(httpClient, HTTPClientSettings.Proxy, HTTPClientSettings.DialTimeout, HTTPClientSettings.DNSRecordsTTL, HTTPClientSettings.DNSResolutionTimeout, HTTPClientSettings.DNSCacheSize, HTTPClientSettings.DNSServers, HTTPClientSettings.DNSFallback, HTTPClientSettings.DNSConcurrency, HTTPClientSettings.DisableIPv4, HTTPClientSettings.DisableIPv6)
if err != nil {
return nil, err
}
httpClient.closeDNSCache = func() {
customDialer.DNSRecords.Close()
time.Sleep(1 * time.Second)
}
customTransport, err := newCustomTransport(customDialer, HTTPClientSettings.DecompressBody, HTTPClientSettings.TLSHandshakeTimeout)
if err != nil {
return nil, err
}
httpClient.Transport = customTransport
return httpClient, nil
}