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
|
/* -*- 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 "mozilla/Preferences.h"
#include "nsIOService.h"
namespace mozilla {
namespace net {
#undef LOG
extern mozilla::LazyLogModule gHostResolverLog;
#define LOG(args) MOZ_LOG(gHostResolverLog, mozilla::LogLevel::Debug, args)
TRRServiceBase::TRRServiceBase() : mMode(0), mURISetByDetection(false) {}
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;
// The user has set a custom URI so it takes precedence.
if (mURIPrefHasUserValue) {
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(mURIPref);
}
// static
uint32_t ModeFromPrefs() {
// 0 - off, 1 - reserved, 2 - TRR first, 3 - TRR only, 4 - reserved,
// 5 - explicit off
auto processPrefValue = [](uint32_t value) -> uint32_t {
if (value == MODE_RESERVED1 || value == MODE_RESERVED4 ||
value > MODE_TRROFF) {
return MODE_TRROFF;
}
return value;
};
uint32_t tmp = MODE_NATIVEONLY;
if (NS_SUCCEEDED(Preferences::GetUint("network.trr.mode", &tmp))) {
tmp = processPrefValue(tmp);
}
if (tmp != MODE_NATIVEONLY) {
return tmp;
}
if (NS_SUCCEEDED(Preferences::GetUint(kRolloutModePref, &tmp))) {
return processPrefValue(tmp);
}
return MODE_NATIVEONLY;
}
void TRRServiceBase::OnTRRModeChange() {
uint32_t oldMode = mMode;
mMode = ModeFromPrefs();
if (mMode != oldMode) {
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->NotifyObservers(nullptr, NS_NETWORK_TRR_MODE_CHANGED_TOPIC, nullptr);
}
}
static bool readHosts = false;
if ((mMode == MODE_TRRFIRST || mMode == MODE_TRRONLY) && !readHosts) {
readHosts = true;
ReadEtcHostsFile();
}
}
void TRRServiceBase::OnTRRURIChange() {
mURIPrefHasUserValue = Preferences::HasUserValue("network.trr.uri");
Preferences::GetCString("network.trr.uri", mURIPref);
Preferences::GetCString(kRolloutURIPref, mRolloutURIPref);
CheckURIPrefs();
}
} // namespace net
} // namespace mozilla
|