/
analysisclassic.go
436 lines (369 loc) · 14.7 KB
/
analysisclassic.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package webconnectivitylte
//
// The "classic" analysis engine.
//
// We try to emulate results produced by v0.4 of Web Connectivity and
// also attempt to provide a more fine-grained view of the results.
//
import (
"net"
"github.com/ooni/probe-cli/v3/internal/geoipx"
"github.com/ooni/probe-cli/v3/internal/minipipeline"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/optional"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
// analysisEngineClassic is an alternative analysis engine that aims to produce
// results that are backward compatible with Web Connectivity v0.4 while also
// procuding more fine-grained blocking flags.
func analysisEngineClassic(tk *TestKeys, logger model.Logger) {
tk.analysisClassic(model.GeoIPASNLookupperFunc(geoipx.LookupASN), logger)
}
func (tk *TestKeys) analysisClassic(lookupper model.GeoIPASNLookupper, logger model.Logger) {
// Since we run after all tasks have completed (or so we assume) we're
// not going to use any form of locking here.
// 1. produce web observations
container := minipipeline.NewWebObservationsContainer()
container.IngestDNSLookupEvents(lookupper, tk.Queries...)
container.IngestTCPConnectEvents(lookupper, tk.TCPConnect...)
container.IngestTLSHandshakeEvents(tk.TLSHandshakes...)
container.IngestHTTPRoundTripEvents(tk.Requests...)
// be defensive in case the control request or response are not defined
if tk.ControlRequest != nil && tk.Control != nil {
// Implementation note: the only error that can happen here is when the input
// doesn't parse as a URL, which should have caused measurer.go to fail
runtimex.Try0(container.IngestControlMessages(tk.ControlRequest, tk.Control))
}
// 2. compute extended analysis flags
analysisExtMain(lookupper, tk, container)
// 3. filter observations to only include results collected by the
// system resolver, which approximates v0.4's results
classic := minipipeline.ClassicFilter(container)
// 3. produce a web observations analysis based on the web observations
woa := minipipeline.AnalyzeWebObservationsWithLinearAnalysis(lookupper, classic)
// 5. determine the DNS consistency
tk.DNSConsistency = analysisClassicDNSConsistency(woa)
// 6. set DNSExperimentFailure
if !woa.DNSExperimentFailure.IsNone() && woa.DNSExperimentFailure.Unwrap() != "" {
value := woa.DNSExperimentFailure.Unwrap()
tk.DNSExperimentFailure = &value
}
// 7. compute the HTTPDiff values
tk.setHTTPDiffValues(woa)
// 8. compute blocking & accessible
analysisClassicComputeBlockingAccessible(woa, tk)
}
func analysisClassicDNSConsistency(woa *minipipeline.WebAnalysis) optional.Value[string] {
switch {
case woa.DNSLookupUnexpectedFailure.Len() <= 0 && // no unexpected failures; and
woa.DNSLookupSuccessWithInvalidAddressesClassic.Len() <= 0 && // no invalid addresses; and
(woa.DNSLookupSuccessWithValidAddressClassic.Len() > 0 || // good addrs; or
woa.DNSLookupExpectedFailure.Len() > 0): // expected failures
return optional.Some("consistent")
case woa.DNSLookupSuccessWithInvalidAddressesClassic.Len() > 0 || // unexpected addrs; or
woa.DNSLookupUnexpectedFailure.Len() > 0 || // unexpected failures; or
(woa.DNSLookupSuccess.Len() > 0 && // successful lookups; and
!woa.ControlExpectations.IsNone() && // we have control info; and
woa.ControlExpectations.Unwrap().DNSAddresses.Len() <= 0): // control resolved nothing
return optional.Some("inconsistent")
default:
return optional.None[string]() // none of the above
}
}
func (tk *TestKeys) setHTTPDiffValues(woa *minipipeline.WebAnalysis) {
hds := newAnalysisHTTPDiffStatus(woa)
tk.BodyProportion = hds.BodyProportion.UnwrapOr(0)
tk.BodyLengthMatch = hds.BodyLengthMatch
tk.HeadersMatch = hds.HeadersMatch
tk.StatusCodeMatch = hds.StatusCodeMatch
tk.TitleMatch = hds.TitleMatch
}
type analysisClassicTestKeysProxy interface {
// httpDiff returns true if there's an http-diff.
httpDiff() bool
// setBlockingString sets blocking to a string.
setBlockingString(value string)
// setBlockingNil sets blocking to nil.
setBlockingNil()
// setBlockingFalse sets Blocking to false.
setBlockingFalse()
// setHTTPExperimentFailure sets the HTTPExperimentFailure field.
setHTTPExperimentFailure(value optional.Value[string])
// setWebsiteDown sets the test keys for a down website.
setWebsiteDown()
}
var _ analysisClassicTestKeysProxy = &TestKeys{}
// httpDiff implements analysisClassicTestKeysProxy.
func (tk *TestKeys) httpDiff() bool {
return analysisHTTPDiffAlgorithm(tk)
}
// bodyLengthMatch implements analysisHTTPDiffValuesProvider.
func (tk *TestKeys) bodyLengthMatch() optional.Value[bool] {
return tk.BodyLengthMatch
}
// headersMatch implements analysisHTTPDiffValuesProvider.
func (tk *TestKeys) headersMatch() optional.Value[bool] {
return tk.HeadersMatch
}
// statusCodeMatch implements analysisHTTPDiffValuesProvider.
func (tk *TestKeys) statusCodeMatch() optional.Value[bool] {
return tk.StatusCodeMatch
}
// titleMatch implements analysisHTTPDiffValuesProvider.
func (tk *TestKeys) titleMatch() optional.Value[bool] {
return tk.TitleMatch
}
// setBlockingFalse implements analysisClassicTestKeysProxy.
func (tk *TestKeys) setBlockingFalse() {
tk.Blocking = false
tk.Accessible = optional.Some(true)
}
// setBlockingNil implements analysisClassicTestKeysProxy.
func (tk *TestKeys) setBlockingNil() {
if !tk.DNSConsistency.IsNone() && tk.DNSConsistency.Unwrap() == "inconsistent" {
tk.Blocking = "dns"
tk.Accessible = optional.Some(false)
} else {
tk.Blocking = nil
tk.Accessible = optional.None[bool]()
}
}
// setBlockingString implements analysisClassicTestKeysProxy.
func (tk *TestKeys) setBlockingString(value string) {
if !tk.DNSConsistency.IsNone() && tk.DNSConsistency.Unwrap() == "inconsistent" {
tk.Blocking = "dns"
} else {
tk.Blocking = value
}
tk.Accessible = optional.Some(false)
}
// setHTTPExperimentFailure implements analysisClassicTestKeysProxy.
func (tk *TestKeys) setHTTPExperimentFailure(value optional.Value[string]) {
tk.HTTPExperimentFailure = value
}
// setWebsiteDown implements analysisClassicTestKeysProxy.
func (tk *TestKeys) setWebsiteDown() {
if !tk.DNSConsistency.IsNone() && tk.DNSConsistency.Unwrap() == "inconsistent" {
tk.Blocking = "dns"
tk.Accessible = optional.Some(false)
} else {
tk.Blocking = false
tk.Accessible = optional.Some(false)
}
}
func analysisClassicComputeBlockingAccessible(woa *minipipeline.WebAnalysis, tk analysisClassicTestKeysProxy) {
// minipipeline.NewLinearWebAnalysis produces a woa.Linear sorted
//
// 1. by descending TagDepth;
//
// 2. with TagDepth being equal, by descending [WebObservationType];
//
// 3. with [WebObservationType] being equal, by ascending failure string;
//
// This means that you divide the list in groups like this:
//
// +------------+------------+------------+------------+
// | TagDepth=3 | TagDepth=2 | TagDepth=1 | TagDepth=0 |
// +------------+------------+------------+------------+
//
// Where TagDepth=3 is the last redirect and TagDepth=0 is the initial request.
//
// Each group is further divided as follows:
//
// +------+-----+-----+-----+
// | HTTP | TLS | TCP | DNS |
// +------+-----+-----+-----+
//
// Where each group may be empty. The first non-empty group is about the
// operation that failed for the current TagDepth.
//
// Within each group, successes sort before failures because the empty
// string has priority over non-empty strings.
//
// So, when walking the list from index 0 to index N, you encounter the
// latest redirects first, you observe the more complex operations first,
// and you see errors before failures.
for _, entry := range woa.Linear {
// 1. As a special case, handle a "final" response first. We define "final" a
// successful response whose status code is like 2xx, 4xx, or 5xx.
if !entry.HTTPResponseIsFinal.IsNone() && entry.HTTPResponseIsFinal.Unwrap() {
// 1.1. Handle the case of succesful response over TLS.
if !entry.TLSHandshakeFailure.IsNone() && entry.TLSHandshakeFailure.Unwrap() == "" {
tk.setBlockingFalse()
return
}
// 1.2. Handle the case of missing HTTP control.
if entry.ControlHTTPFailure.IsNone() {
tk.setBlockingNil()
return
}
// 1.3. Figure out whether the measurement and the control are close enough.
if !tk.httpDiff() {
tk.setBlockingFalse()
return
}
// 1.4. There's something different in the two responses.
tk.setBlockingString("http-diff")
return
}
// 2. Let's now focus on failed HTTP round trips.
if entry.Type == minipipeline.WebObservationTypeHTTPRoundTrip &&
!entry.Failure.IsNone() && entry.Failure.Unwrap() != "" {
// 2.1. Handle the case of a missing HTTP control. Maybe
// the control server is unreachable or blocked.
if entry.ControlHTTPFailure.IsNone() {
tk.setBlockingNil()
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 2.2. Handle the case where both the probe and the control failed.
if entry.ControlHTTPFailure.Unwrap() != "" {
tk.setWebsiteDown()
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 2.3. Handle the case where just the probe failed.
tk.setBlockingString("http-failure")
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 3. Handle the case of TLS failure.
if entry.Type == minipipeline.WebObservationTypeTLSHandshake &&
!entry.Failure.IsNone() && entry.Failure.Unwrap() != "" {
// 3.1. Handle the case of missing TLS control information. The control
// only provides information for the first request. Once we start following
// redirects we do not have TLS/TCP/DNS control.
if entry.ControlTLSHandshakeFailure.IsNone() {
// 3.1.1 Handle the case of missing an expectation about what
// accessing the website should lead to, which is set forth by
// the control accessing the website and telling us.
if entry.ControlHTTPFailure.IsNone() {
tk.setBlockingNil()
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 3.1.2. Otherwise, if the control worked, that's blocking.
tk.setBlockingString("http-failure")
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 3.2. Handle the case where both probe and control failed.
if entry.ControlTLSHandshakeFailure.Unwrap() != "" {
tk.setWebsiteDown()
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 3.3. Handle the case where just the probe failed.
tk.setBlockingString("http-failure")
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 4. Handle the case of TCP failure.
if entry.Type == minipipeline.WebObservationTypeTCPConnect &&
!entry.Failure.IsNone() && entry.Failure.Unwrap() != "" {
// 4.1. Handle the case of missing TCP control info.
if entry.ControlTCPConnectFailure.IsNone() {
// 4.1.1 Handle the case of missing an expectation about what
// accessing the website should lead to.
if entry.ControlHTTPFailure.IsNone() {
tk.setBlockingNil()
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 4.1.2. Otherwise, if the control worked, that's blocking.
tk.setBlockingString("http-failure")
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 4.2. Handle the case where both probe and control failed.
if entry.ControlTCPConnectFailure.Unwrap() != "" {
tk.setWebsiteDown()
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 4.3. Handle the case where just the probe failed.
tk.setBlockingString("tcp_ip")
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 5. Handle the case of DNS failure
if entry.Type == minipipeline.WebObservationTypeDNSLookup &&
!entry.Failure.IsNone() && entry.Failure.Unwrap() != "" {
// 5.1. Handle the case of missing DNS control info.
if entry.ControlDNSLookupFailure.IsNone() {
// 5.1.1 Handle the case of missing an expectation about what
// accessing the website should lead to.
if entry.ControlHTTPFailure.IsNone() {
tk.setBlockingFalse()
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 5.1.2. Otherwise, if the control worked, that's blocking.
tk.setBlockingString("dns")
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 5.2. Handle the case where both probe and control failed.
if entry.ControlDNSLookupFailure.Unwrap() != "" {
tk.setWebsiteDown()
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 5.3. When the probe says dns_no_answer the control would otherwise say that
// we have resolved zero IP addresses for historical reasons. In such a case,
// let's pretend that also the control returned dns_no_answer.
if entry.Failure.Unwrap() == netxlite.FailureDNSNoAnswer &&
!entry.ControlDNSResolvedAddrs.IsNone() &&
entry.ControlDNSResolvedAddrs.Unwrap().Len() <= 0 {
tk.setWebsiteDown()
return
}
// 5.4. Handle the case where just the probe failed.
tk.setBlockingString("dns")
tk.setHTTPExperimentFailure(entry.Failure)
return
}
// 6. handle the case of DNS success with the probe only seeing loopback
// addrs while the TH sees real addresses, which is a case where in the
// classic analysis (which is what we're doing) the probe does not attempt
// to connect to loopback addresses because it doesn't make sense.
if entry.Type == minipipeline.WebObservationTypeDNSLookup &&
!entry.Failure.IsNone() && entry.Failure.Unwrap() == "" &&
!entry.ControlDNSLookupFailure.IsNone() &&
entry.ControlDNSLookupFailure.Unwrap() == "" &&
!entry.DNSResolvedAddrs.IsNone() && !entry.ControlDNSResolvedAddrs.IsNone() &&
analysisContainsOnlyLoopbackAddrs(entry.DNSResolvedAddrs.Unwrap()) &&
!analysisContainsOnlyLoopbackAddrs(entry.ControlDNSResolvedAddrs.Unwrap()) {
tk.setBlockingString("dns")
return
}
// 7. handle the case of DNS success with loopback addrs, which is the case
// where neither the probe nor the TH attempt to measure endpoints.
if entry.Type == minipipeline.WebObservationTypeDNSLookup &&
!entry.Failure.IsNone() && entry.Failure.Unwrap() == "" &&
!entry.ControlDNSLookupFailure.IsNone() &&
entry.ControlDNSLookupFailure.Unwrap() == "" &&
!entry.DNSResolvedAddrs.IsNone() && !entry.ControlDNSResolvedAddrs.IsNone() &&
analysisContainsOnlyLoopbackAddrs(entry.DNSResolvedAddrs.Unwrap()) &&
analysisContainsOnlyLoopbackAddrs(entry.ControlDNSResolvedAddrs.Unwrap()) {
tk.setWebsiteDown()
return
}
}
}
// analysisContainsOnlyLoopbackAddrs returns true iff the given set contains one or
// more IP addresses and all these adresses are loopback addresses.
func analysisContainsOnlyLoopbackAddrs(addrs minipipeline.Set[string]) bool {
var count int
for _, addr := range addrs.Keys() {
if net.ParseIP(addr) == nil {
continue
}
if !netxlite.IsLoopback(addr) {
return false
}
count++
}
return count > 0
}