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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* This module provides an interface to access DoH configuration - e.g. whether
* DoH is enabled, whether capabilities are enabled, etc. The configuration is
* sourced from either Remote Settings or pref values, with Remote Settings
* being preferred.
*/
import { RemoteSettings } from "resource://services-settings/remote-settings.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
Preferences: "resource://gre/modules/Preferences.sys.mjs",
Region: "resource://gre/modules/Region.sys.mjs",
});
const kGlobalPrefBranch = "doh-rollout";
var kRegionPrefBranch;
const kConfigPrefs = {
kEnabledPref: "enabled",
kProvidersPref: "provider-list",
kTRRSelectionEnabledPref: "trr-selection.enabled",
kTRRSelectionProvidersPref: "trr-selection.provider-list",
kTRRSelectionCommitResultPref: "trr-selection.commit-result",
kProviderSteeringEnabledPref: "provider-steering.enabled",
kProviderSteeringListPref: "provider-steering.provider-list",
};
const kPrefChangedTopic = "nsPref:changed";
const gProvidersCollection = RemoteSettings("doh-providers");
const gConfigCollection = RemoteSettings("doh-config");
function getPrefValueRegionFirst(prefName) {
let regionalPrefName = `${kRegionPrefBranch}.${prefName}`;
let regionalPrefValue = lazy.Preferences.get(regionalPrefName);
if (regionalPrefValue !== undefined) {
return regionalPrefValue;
}
return lazy.Preferences.get(`${kGlobalPrefBranch}.${prefName}`);
}
function getProviderListFromPref(prefName) {
let prefVal = getPrefValueRegionFirst(prefName);
if (prefVal) {
try {
return JSON.parse(prefVal);
} catch (e) {
console.error(`DoH provider list not a valid JSON array: ${prefName}`);
}
}
return undefined;
}
// Generate a base config object with getters that return pref values. When
// Remote Settings values become available, a new config object will be
// generated from this and specific fields will be replaced by the RS value.
// If we use a class to store base config and instantiate new config objects
// from it, we lose the ability to override getters because they are defined
// as non-configureable properties on class instances. So just use a function.
function makeBaseConfigObject() {
function makeConfigProperty({
obj,
propName,
defaultVal,
prefName,
isProviderList,
}) {
let prefFn = isProviderList
? getProviderListFromPref
: getPrefValueRegionFirst;
let overridePropName = "_" + propName;
Object.defineProperty(obj, propName, {
get() {
// If a pref value exists, it gets top priority. Otherwise, if it has an
// explicitly set value (from Remote Settings), we return that.
let prefVal = prefFn(prefName);
if (prefVal !== undefined) {
return prefVal;
}
if (this[overridePropName] !== undefined) {
return this[overridePropName];
}
return defaultVal;
},
set(val) {
this[overridePropName] = val;
},
});
}
let newConfig = {
get fallbackProviderURI() {
return this.providerList[0]?.uri;
},
trrSelection: {},
providerSteering: {},
};
makeConfigProperty({
obj: newConfig,
propName: "enabled",
defaultVal: false,
prefName: kConfigPrefs.kEnabledPref,
isProviderList: false,
});
makeConfigProperty({
obj: newConfig,
propName: "providerList",
defaultVal: [],
prefName: kConfigPrefs.kProvidersPref,
isProviderList: true,
});
makeConfigProperty({
obj: newConfig.trrSelection,
propName: "enabled",
defaultVal: false,
prefName: kConfigPrefs.kTRRSelectionEnabledPref,
isProviderList: false,
});
makeConfigProperty({
obj: newConfig.trrSelection,
propName: "commitResult",
defaultVal: false,
prefName: kConfigPrefs.kTRRSelectionCommitResultPref,
isProviderList: false,
});
makeConfigProperty({
obj: newConfig.trrSelection,
propName: "providerList",
defaultVal: [],
prefName: kConfigPrefs.kTRRSelectionProvidersPref,
isProviderList: true,
});
makeConfigProperty({
obj: newConfig.providerSteering,
propName: "enabled",
defaultVal: false,
prefName: kConfigPrefs.kProviderSteeringEnabledPref,
isProviderList: false,
});
makeConfigProperty({
obj: newConfig.providerSteering,
propName: "providerList",
defaultVal: [],
prefName: kConfigPrefs.kProviderSteeringListPref,
isProviderList: true,
});
return newConfig;
}
export const DoHConfigController = {
initComplete: null,
_resolveInitComplete: null,
// This field always contains the current config state, for
// consumer use.
currentConfig: makeBaseConfigObject(),
// Loads the client's region via Region.sys.mjs. This might mean waiting
// until the region is available.
async loadRegion() {
await new Promise(resolve => {
let homeRegion = lazy.Preferences.get(`${kGlobalPrefBranch}.home-region`);
if (homeRegion) {
kRegionPrefBranch = `${kGlobalPrefBranch}.${homeRegion.toLowerCase()}`;
resolve();
return;
}
let updateRegionAndResolve = () => {
kRegionPrefBranch = `${kGlobalPrefBranch}.${lazy.Region.home.toLowerCase()}`;
lazy.Preferences.set(
`${kGlobalPrefBranch}.home-region`,
lazy.Region.home
);
resolve();
};
if (lazy.Region.home) {
updateRegionAndResolve();
return;
}
Services.obs.addObserver(function obs(sub, top, data) {
Services.obs.removeObserver(obs, lazy.Region.REGION_TOPIC);
updateRegionAndResolve();
}, lazy.Region.REGION_TOPIC);
});
// Finally, reload config.
await this.updateFromRemoteSettings();
},
async init() {
await this.loadRegion();
Services.prefs.addObserver(`${kGlobalPrefBranch}.`, this, true);
gProvidersCollection.on("sync", this.updateFromRemoteSettings);
gConfigCollection.on("sync", this.updateFromRemoteSettings);
this._resolveInitComplete();
},
// Useful for tests to set prior state before init()
async _uninit() {
await this.initComplete;
Services.prefs.removeObserver(`${kGlobalPrefBranch}`, this);
gProvidersCollection.off("sync", this.updateFromRemoteSettings);
gConfigCollection.off("sync", this.updateFromRemoteSettings);
this.initComplete = new Promise(resolve => {
this._resolveInitComplete = resolve;
});
},
observe(subject, topic, data) {
switch (topic) {
case kPrefChangedTopic:
let allowedPrefs = Object.getOwnPropertyNames(kConfigPrefs).map(
k => kConfigPrefs[k]
);
if (
!allowedPrefs.some(pref =>
[
`${kRegionPrefBranch}.${pref}`,
`${kGlobalPrefBranch}.${pref}`,
].includes(data)
)
) {
break;
}
this.notifyNewConfig();
break;
}
},
QueryInterface: ChromeUtils.generateQI([
"nsIObserver",
"nsISupportsWeakReference",
]),
// Creates new config object from currently available
// Remote Settings values.
async updateFromRemoteSettings() {
let providers = await gProvidersCollection.get();
let config = await gConfigCollection.get();
let providersById = new Map();
providers.forEach(p => providersById.set(p.id, p));
let configByRegion = new Map();
config.forEach(c => {
c.id = c.id.toLowerCase();
configByRegion.set(c.id, c);
});
let homeRegion = lazy.Preferences.get(`${kGlobalPrefBranch}.home-region`);
let localConfig =
configByRegion.get(homeRegion?.toLowerCase()) ||
configByRegion.get("global");
// Make a new config object first, mutate it as needed, then synchronously
// replace the currentConfig object at the end to ensure atomicity.
let newConfig = makeBaseConfigObject();
if (!localConfig) {
DoHConfigController.currentConfig = newConfig;
DoHConfigController.notifyNewConfig();
return;
}
if (localConfig.rolloutEnabled) {
newConfig.enabled = true;
}
let parseProviderList = (list, checkFn) => {
let parsedList = [];
list?.split(",")?.forEach(p => {
p = p.trim();
if (!p.length) {
return;
}
p = providersById.get(p);
if (!p || (checkFn && !checkFn(p))) {
return;
}
parsedList.push(p);
});
return parsedList;
};
let regionalProviders = parseProviderList(localConfig.providers);
if (regionalProviders?.length) {
newConfig.providerList = regionalProviders;
}
if (localConfig.steeringEnabled) {
let steeringProviders = parseProviderList(
localConfig.steeringProviders,
p => p.canonicalName?.length
);
if (steeringProviders?.length) {
newConfig.providerSteering.providerList = steeringProviders;
newConfig.providerSteering.enabled = true;
}
}
if (localConfig.autoDefaultEnabled) {
let defaultProviders = parseProviderList(
localConfig.autoDefaultProviders
);
if (defaultProviders?.length) {
newConfig.trrSelection.providerList = defaultProviders;
newConfig.trrSelection.enabled = true;
}
}
// Finally, update the currentConfig object synchronously.
DoHConfigController.currentConfig = newConfig;
DoHConfigController.notifyNewConfig();
},
kConfigUpdateTopic: "doh-config-updated",
notifyNewConfig() {
Services.obs.notifyObservers(null, this.kConfigUpdateTopic);
},
};
DoHConfigController.initComplete = new Promise(resolve => {
DoHConfigController._resolveInitComplete = resolve;
});
DoHConfigController.init();
|