-
Notifications
You must be signed in to change notification settings - Fork 15
/
userpolicy.go
125 lines (106 loc) · 3.54 KB
/
userpolicy.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
package enginenetx
//
// user policy - the possibility of loading a user policy from a JSON
// document named `bridges.conf` in $OONI_HOME/engine that contains
// a specific policy for TLS dialing for specific endpoints.
//
// This policy helps a lot with exploration and experimentation.
//
import (
"context"
"errors"
"fmt"
"net"
"github.com/ooni/probe-engine/pkg/hujsonx"
"github.com/ooni/probe-engine/pkg/model"
)
// userPolicy is an [httpsDialerPolicy] incorporating verbatim
// a user policy loaded from the engine's key-value store.
//
// This policy is very useful for exploration and experimentation.
type userPolicy struct {
// Fallback is the fallback policy in case the user one does not
// contain a rule for a specific domain.
Fallback httpsDialerPolicy
// Root is the root of the user policy loaded from disk.
Root *userPolicyRoot
}
// userPolicyKey is the kvstore key used to retrieve the user policy.
const userPolicyKey = "bridges.conf"
// errUserPolicyWrongVersion means that the user policy document has the wrong version number.
var errUserPolicyWrongVersion = errors.New("wrong user policy version")
// newUserPolicy attempts to constructs a user policy using a given fallback
// policy and either returns a good policy or an error. The typical error case is the one
// in which there's no httpsDialerUserPolicyKey in the key-value store.
func newUserPolicy(
kvStore model.KeyValueStore, fallback httpsDialerPolicy) (*userPolicy, error) {
// attempt to read the user policy bytes from the kvstore
data, err := kvStore.Get(userPolicyKey)
if err != nil {
return nil, err
}
// attempt to parse the user policy using human-readable JSON
var root userPolicyRoot
if err := hujsonx.Unmarshal(data, &root); err != nil {
return nil, err
}
// make sure the version is OK
if root.Version != userPolicyVersion {
err := fmt.Errorf(
"%s: %w: expected=%d got=%d",
userPolicyKey,
errUserPolicyWrongVersion,
userPolicyVersion,
root.Version,
)
return nil, err
}
out := &userPolicy{
Fallback: fallback,
Root: &root,
}
return out, nil
}
// userPolicyVersion is the current version of the user policy file.
const userPolicyVersion = 3
// userPolicyRoot is the root of the user policy.
type userPolicyRoot struct {
// DomainEndpoints maps each domain endpoint to its policies.
DomainEndpoints map[string][]*httpsDialerTactic
// Version is the data structure version.
Version int
}
var _ httpsDialerPolicy = &userPolicy{}
// LookupTactics implements httpsDialerPolicy.
func (ldp *userPolicy) LookupTactics(
ctx context.Context, domain string, port string) <-chan *httpsDialerTactic {
// check whether an entry exists in the user-provided map, which MAY be nil
// if/when the user has chosen their policy to be as such
tactics, found := ldp.Root.DomainEndpoints[net.JoinHostPort(domain, port)]
if !found {
return ldp.Fallback.LookupTactics(ctx, domain, port)
}
// note that we also need to fallback when the tactics contains an empty list
// or a list that only contains nil entries
tactics = userPolicyRemoveNilEntries(tactics)
if len(tactics) <= 0 {
return ldp.Fallback.LookupTactics(ctx, domain, port)
}
// emit the resuults, which may possibly be empty
out := make(chan *httpsDialerTactic)
go func() {
defer close(out) // let the caller know we're done
for _, tactic := range tactics {
out <- tactic
}
}()
return out
}
func userPolicyRemoveNilEntries(input []*httpsDialerTactic) (output []*httpsDialerTactic) {
for _, entry := range input {
if entry != nil {
output = append(output, entry)
}
}
return
}