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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* 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/. */
#include "TRRServiceBase.h"
#include "TRRService.h"
#include "mozilla/Preferences.h"
#include "mozilla/ScopeExit.h"
#include "nsHostResolver.h"
#include "nsNetUtil.h"
#include "nsIOService.h"
#include "nsIDNSService.h"
#include "nsIProxyInfo.h"
#include "nsHttpConnectionInfo.h"
#include "nsHttpHandler.h"
#include "mozilla/StaticPrefs_network.h"
#include "AlternateServices.h"
#include "ProxyConfigLookup.h"
// Put DNSLogging.h at the end to avoid LOG being overwritten by other headers.
#include "DNSLogging.h"
#if defined(XP_WIN)
# include <shlobj.h> // for SHGetSpecialFolderPathA
#endif // XP_WIN
namespace mozilla {
namespace net {
NS_IMPL_ISUPPORTS(TRRServiceBase, nsIProxyConfigChangedCallback)
TRRServiceBase::TRRServiceBase()
: mDefaultTRRConnectionInfo("DataMutex::mDefaultTRRConnectionInfo") {}
TRRServiceBase::~TRRServiceBase() {
if (mTRRConnectionInfoInited) {
UnregisterProxyChangeListener();
}
}
void TRRServiceBase::ProcessURITemplate(nsACString& aURI) {
// URI Template, RFC 6570.
if (aURI.IsEmpty()) {
return;
}
nsAutoCString scheme;
nsCOMPtr<nsIIOService> ios(do_GetIOService());
if (ios) {
ios->ExtractScheme(aURI, scheme);
}
if (!scheme.Equals("https")) {
LOG(("TRRService TRR URI %s is not https. Not used.\n",
PromiseFlatCString(aURI).get()));
aURI.Truncate();
return;
}
// cut off everything from "{" to "}" sequences (potentially multiple),
// as a crude conversion from template into URI.
nsAutoCString uri(aURI);
do {
nsCCharSeparatedTokenizer openBrace(uri, '{');
if (openBrace.hasMoreTokens()) {
// the 'nextToken' is the left side of the open brace (or full uri)
nsAutoCString prefix(openBrace.nextToken());
// if there is an open brace, there's another token
const nsACString& endBrace = openBrace.nextToken();
nsCCharSeparatedTokenizer closeBrace(endBrace, '}');
if (closeBrace.hasMoreTokens()) {
// there is a close brace as well, make a URI out of the prefix
// and the suffix
closeBrace.nextToken();
nsAutoCString suffix(closeBrace.nextToken());
uri = prefix + suffix;
} else {
// no (more) close brace
break;
}
} else {
// no (more) open brace
break;
}
} while (true);
aURI = uri;
}
void TRRServiceBase::CheckURIPrefs() {
mURISetByDetection = false;
if (StaticPrefs::network_trr_use_ohttp() && !mOHTTPURIPref.IsEmpty()) {
MaybeSetPrivateURI(mOHTTPURIPref);
return;
}
// The user has set a custom URI so it takes precedence.
if (!mURIPref.IsEmpty()) {
MaybeSetPrivateURI(mURIPref);
return;
}
// Check if the rollout addon has set a pref.
if (!mRolloutURIPref.IsEmpty()) {
MaybeSetPrivateURI(mRolloutURIPref);
return;
}
// Otherwise just use the default value.
MaybeSetPrivateURI(mDefaultURIPref);
}
// static
nsIDNSService::ResolverMode ModeFromPrefs(
nsIDNSService::ResolverMode& aTRRModePrefValue) {
// 0 - off, 1 - reserved, 2 - TRR first, 3 - TRR only, 4 - reserved,
// 5 - explicit off
auto processPrefValue = [](uint32_t value) -> nsIDNSService::ResolverMode {
if (value == nsIDNSService::MODE_RESERVED1 ||
value == nsIDNSService::MODE_RESERVED4 ||
value > nsIDNSService::MODE_TRROFF) {
return nsIDNSService::MODE_TRROFF;
}
return static_cast<nsIDNSService::ResolverMode>(value);
};
uint32_t tmp;
if (NS_FAILED(Preferences::GetUint("network.trr.mode", &tmp))) {
tmp = 0;
}
nsIDNSService::ResolverMode modeFromPref = processPrefValue(tmp);
aTRRModePrefValue = modeFromPref;
if (modeFromPref != nsIDNSService::MODE_NATIVEONLY) {
return modeFromPref;
}
if (NS_FAILED(Preferences::GetUint(kRolloutModePref, &tmp))) {
tmp = 0;
}
modeFromPref = processPrefValue(tmp);
return modeFromPref;
}
void TRRServiceBase::OnTRRModeChange() {
uint32_t oldMode = mMode;
// This is the value of the pref "network.trr.mode"
nsIDNSService::ResolverMode trrModePrefValue;
mMode = ModeFromPrefs(trrModePrefValue);
if (mMode != oldMode) {
LOG(("TRR Mode changed from %d to %d", oldMode, int(mMode)));
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->NotifyObservers(nullptr, NS_NETWORK_TRR_MODE_CHANGED_TOPIC, nullptr);
}
TRRService::SetCurrentTRRMode(trrModePrefValue);
}
static bool readHosts = false;
if ((mMode == nsIDNSService::MODE_TRRFIRST ||
mMode == nsIDNSService::MODE_TRRONLY) &&
!readHosts) {
readHosts = true;
ReadEtcHostsFile();
}
}
void TRRServiceBase::OnTRRURIChange() {
Preferences::GetCString("network.trr.uri", mURIPref);
Preferences::GetCString(kRolloutURIPref, mRolloutURIPref);
Preferences::GetCString("network.trr.default_provider_uri", mDefaultURIPref);
Preferences::GetCString("network.trr.ohttp.uri", mOHTTPURIPref);
CheckURIPrefs();
}
static already_AddRefed<nsHttpConnectionInfo> CreateConnInfoHelper(
nsIURI* aURI, nsIProxyInfo* aProxyInfo) {
MOZ_ASSERT(NS_IsMainThread());
nsAutoCString host;
nsAutoCString scheme;
nsAutoCString username;
int32_t port = -1;
bool isHttps = aURI->SchemeIs("https");
nsresult rv = aURI->GetScheme(scheme);
if (NS_FAILED(rv)) {
return nullptr;
}
rv = aURI->GetAsciiHost(host);
if (NS_FAILED(rv)) {
return nullptr;
}
rv = aURI->GetPort(&port);
if (NS_FAILED(rv)) {
return nullptr;
}
// Just a warning here because some nsIURIs do not implement this method.
if (NS_WARN_IF(NS_FAILED(aURI->GetUsername(username)))) {
LOG(("Failed to get username for aURI(%s)",
aURI->GetSpecOrDefault().get()));
}
gHttpHandler->MaybeAddAltSvcForTesting(aURI, username, false, nullptr,
OriginAttributes());
nsCOMPtr<nsProxyInfo> proxyInfo = do_QueryInterface(aProxyInfo);
RefPtr<nsHttpConnectionInfo> connInfo = new nsHttpConnectionInfo(
host, port, ""_ns, username, proxyInfo, OriginAttributes(), isHttps);
bool http2Allowed = !gHttpHandler->IsHttp2Excluded(connInfo);
bool http3Allowed = proxyInfo ? proxyInfo->IsDirect() : true;
RefPtr<AltSvcMapping> mapping;
if ((http2Allowed || http3Allowed) &&
AltSvcMapping::AcceptableProxy(proxyInfo) &&
(scheme.EqualsLiteral("http") || scheme.EqualsLiteral("https")) &&
(mapping = gHttpHandler->GetAltServiceMapping(
scheme, host, port, false, OriginAttributes(), http2Allowed,
http3Allowed))) {
mapping->GetConnectionInfo(getter_AddRefs(connInfo), proxyInfo,
OriginAttributes());
}
return connInfo.forget();
}
void TRRServiceBase::InitTRRConnectionInfo() {
if (!XRE_IsParentProcess()) {
return;
}
if (mTRRConnectionInfoInited) {
return;
}
if (!NS_IsMainThread()) {
NS_DispatchToMainThread(NS_NewRunnableFunction(
"TRRServiceBase::InitTRRConnectionInfo",
[self = RefPtr{this}]() { self->InitTRRConnectionInfo(); }));
return;
}
LOG(("TRRServiceBase::InitTRRConnectionInfo"));
nsAutoCString uri;
GetURI(uri);
AsyncCreateTRRConnectionInfoInternal(uri);
}
void TRRServiceBase::AsyncCreateTRRConnectionInfo(const nsACString& aURI) {
LOG(
("TRRServiceBase::AsyncCreateTRRConnectionInfo "
"mTRRConnectionInfoInited=%d",
bool(mTRRConnectionInfoInited)));
if (!mTRRConnectionInfoInited) {
return;
}
AsyncCreateTRRConnectionInfoInternal(aURI);
}
void TRRServiceBase::AsyncCreateTRRConnectionInfoInternal(
const nsACString& aURI) {
if (!XRE_IsParentProcess()) {
return;
}
SetDefaultTRRConnectionInfo(nullptr);
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIURI> dnsURI;
nsresult rv = NS_NewURI(getter_AddRefs(dnsURI), aURI);
if (NS_FAILED(rv)) {
return;
}
rv = ProxyConfigLookup::Create(
[self = RefPtr{this}, uri(dnsURI)](nsIProxyInfo* aProxyInfo,
nsresult aStatus) mutable {
if (NS_FAILED(aStatus)) {
self->SetDefaultTRRConnectionInfo(nullptr);
return;
}
RefPtr<nsHttpConnectionInfo> connInfo =
CreateConnInfoHelper(uri, aProxyInfo);
self->SetDefaultTRRConnectionInfo(connInfo);
if (!self->mTRRConnectionInfoInited) {
self->mTRRConnectionInfoInited = true;
self->RegisterProxyChangeListener();
}
},
dnsURI, 0, nullptr);
// mDefaultTRRConnectionInfo is set to nullptr at the beginning of this
// method, so we don't really care aobut the |rv| here. If it's failed,
// mDefaultTRRConnectionInfo stays as nullptr and we'll create a new
// connection info in TRRServiceChannel again.
Unused << NS_WARN_IF(NS_FAILED(rv));
}
already_AddRefed<nsHttpConnectionInfo> TRRServiceBase::TRRConnectionInfo() {
RefPtr<nsHttpConnectionInfo> connInfo;
{
auto lock = mDefaultTRRConnectionInfo.Lock();
connInfo = *lock;
}
return connInfo.forget();
}
void TRRServiceBase::SetDefaultTRRConnectionInfo(
nsHttpConnectionInfo* aConnInfo) {
LOG(("TRRService::SetDefaultTRRConnectionInfo aConnInfo=%s",
aConnInfo ? aConnInfo->HashKey().get() : "none"));
{
auto lock = mDefaultTRRConnectionInfo.Lock();
lock.ref() = aConnInfo;
}
}
void TRRServiceBase::RegisterProxyChangeListener() {
if (!XRE_IsParentProcess()) {
return;
}
nsCOMPtr<nsIProtocolProxyService> pps =
do_GetService(NS_PROTOCOLPROXYSERVICE_CONTRACTID);
if (!pps) {
return;
}
pps->AddProxyConfigCallback(this);
}
void TRRServiceBase::UnregisterProxyChangeListener() {
if (!XRE_IsParentProcess()) {
return;
}
nsCOMPtr<nsIProtocolProxyService> pps =
do_GetService(NS_PROTOCOLPROXYSERVICE_CONTRACTID);
if (!pps) {
return;
}
pps->RemoveProxyConfigCallback(this);
}
void TRRServiceBase::DoReadEtcHostsFile(ParsingCallback aCallback) {
MOZ_ASSERT(XRE_IsParentProcess());
if (!StaticPrefs::network_trr_exclude_etc_hosts()) {
return;
}
auto readHostsTask = [aCallback]() {
MOZ_ASSERT(!NS_IsMainThread(), "Must not run on the main thread");
#if defined(XP_WIN)
// Inspired by libevent/evdns.c
// Windows is a little coy about where it puts its configuration
// files. Sure, they're _usually_ in C:\windows\system32, but
// there's no reason in principle they couldn't be in
// W:\hoboken chicken emergency
nsCString path;
path.SetLength(MAX_PATH + 1);
if (!SHGetSpecialFolderPathA(NULL, path.BeginWriting(), CSIDL_SYSTEM,
false)) {
LOG(("Calling SHGetSpecialFolderPathA failed"));
return;
}
path.SetLength(strlen(path.get()));
path.Append("\\drivers\\etc\\hosts");
#else
nsAutoCString path("/etc/hosts"_ns);
#endif
LOG(("Reading hosts file at %s", path.get()));
rust_parse_etc_hosts(&path, aCallback);
};
Unused << NS_DispatchBackgroundTask(
NS_NewRunnableFunction("Read /etc/hosts file", readHostsTask),
NS_DISPATCH_EVENT_MAY_BLOCK);
}
} // namespace net
} // namespace mozilla
|