2018-02-01 12:20:49 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "nsICaptivePortalService.h"
|
2019-08-24 00:17:15 +03:00
|
|
|
#include "nsIParentalControlsService.h"
|
2019-10-15 16:24:49 +03:00
|
|
|
#include "nsINetworkLinkService.h"
|
2018-02-01 12:20:49 +03:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsStandardURL.h"
|
|
|
|
#include "TRR.h"
|
|
|
|
#include "TRRService.h"
|
|
|
|
|
|
|
|
#include "mozilla/Preferences.h"
|
2019-08-26 22:52:20 +03:00
|
|
|
#include "mozilla/StaticPrefs_network.h"
|
2018-09-03 17:21:08 +03:00
|
|
|
#include "mozilla/Tokenizer.h"
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
static const char kOpenCaptivePortalLoginEvent[] = "captive-portal-login";
|
|
|
|
static const char kClearPrivateData[] = "clear-private-data";
|
|
|
|
static const char kPurge[] = "browser:purge-session-history";
|
2018-03-12 15:19:22 +03:00
|
|
|
static const char kDisableIpv6Pref[] = "network.dns.disableIPv6";
|
2020-01-07 23:20:38 +03:00
|
|
|
static const char kPrefSkipTRRParentalControl[] =
|
|
|
|
"network.dns.skipTRR-when-parental-control-enabled";
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
#define TRR_PREF_PREFIX "network.trr."
|
|
|
|
#define TRR_PREF(x) TRR_PREF_PREFIX x
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
#undef LOG
|
|
|
|
extern mozilla::LazyLogModule gHostResolverLog;
|
|
|
|
#define LOG(args) MOZ_LOG(gHostResolverLog, mozilla::LogLevel::Debug, args)
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
TRRService* gTRRService = nullptr;
|
2020-03-04 19:11:16 +03:00
|
|
|
StaticRefPtr<nsIThread> sTRRBackgroundThread;
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(TRRService, nsIObserver, nsISupportsWeakReference)
|
|
|
|
|
|
|
|
TRRService::TRRService()
|
|
|
|
: mInitialized(false),
|
|
|
|
mMode(0),
|
|
|
|
mTRRBlacklistExpireTime(72 * 3600),
|
|
|
|
mLock("trrservice"),
|
|
|
|
mConfirmationNS(NS_LITERAL_CSTRING("example.com")),
|
|
|
|
mWaitForCaptive(true),
|
|
|
|
mRfc1918(false),
|
|
|
|
mCaptiveIsPassed(false),
|
|
|
|
mUseGET(false),
|
2018-08-13 18:45:15 +03:00
|
|
|
mDisableECS(true),
|
2020-01-07 23:20:38 +03:00
|
|
|
mSkipTRRWhenParentalControlEnabled(true),
|
2018-10-03 14:53:46 +03:00
|
|
|
mDisableAfterFails(5),
|
2019-11-24 17:54:02 +03:00
|
|
|
mPlatformDisabledTRR(false),
|
2018-02-01 12:20:49 +03:00
|
|
|
mClearTRRBLStorage(false),
|
|
|
|
mConfirmationState(CONFIRM_INIT),
|
2018-03-09 11:05:48 +03:00
|
|
|
mRetryConfirmInterval(1000),
|
2019-08-24 00:17:15 +03:00
|
|
|
mTRRFailures(0),
|
2019-11-20 19:40:16 +03:00
|
|
|
mParentalControlEnabled(false) {
|
2018-02-01 12:20:49 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TRRService::Init() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
|
|
|
if (mInitialized) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
mInitialized = true;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->AddObserver(this, NS_CAPTIVE_PORTAL_CONNECTIVITY, true);
|
|
|
|
observerService->AddObserver(this, kOpenCaptivePortalLoginEvent, true);
|
|
|
|
observerService->AddObserver(this, kClearPrivateData, true);
|
|
|
|
observerService->AddObserver(this, kPurge, true);
|
2019-10-15 16:24:49 +03:00
|
|
|
observerService->AddObserver(this, NS_NETWORK_LINK_TOPIC, true);
|
2019-11-27 14:14:44 +03:00
|
|
|
observerService->AddObserver(this, NS_DNS_SUFFIX_LIST_UPDATED_TOPIC, true);
|
2020-03-04 19:11:16 +03:00
|
|
|
observerService->AddObserver(this, "xpcom-shutdown-threads", true);
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch;
|
|
|
|
GetPrefBranch(getter_AddRefs(prefBranch));
|
|
|
|
if (prefBranch) {
|
|
|
|
prefBranch->AddObserver(TRR_PREF_PREFIX, this, true);
|
2018-03-12 15:19:22 +03:00
|
|
|
prefBranch->AddObserver(kDisableIpv6Pref, this, true);
|
2020-01-07 23:20:38 +03:00
|
|
|
prefBranch->AddObserver(kPrefSkipTRRParentalControl, this, true);
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
2018-03-12 16:48:49 +03:00
|
|
|
nsCOMPtr<nsICaptivePortalService> captivePortalService =
|
|
|
|
do_GetService(NS_CAPTIVEPORTAL_CID);
|
|
|
|
if (captivePortalService) {
|
|
|
|
int32_t captiveState;
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(captivePortalService->GetState(&captiveState));
|
|
|
|
|
|
|
|
if ((captiveState == nsICaptivePortalService::UNLOCKED_PORTAL) ||
|
|
|
|
(captiveState == nsICaptivePortalService::NOT_CAPTIVE)) {
|
|
|
|
mCaptiveIsPassed = true;
|
|
|
|
}
|
|
|
|
LOG(("TRRService::Init mCaptiveState=%d mCaptiveIsPassed=%d\n",
|
|
|
|
captiveState, (int)mCaptiveIsPassed));
|
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2019-08-24 00:17:15 +03:00
|
|
|
GetParentalControlEnabledInternal();
|
|
|
|
|
2018-04-30 19:46:04 +03:00
|
|
|
ReadPrefs(nullptr);
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
gTRRService = this;
|
|
|
|
|
2019-11-27 14:14:44 +03:00
|
|
|
nsCOMPtr<nsINetworkLinkService> nls =
|
|
|
|
do_GetService(NS_NETWORK_LINK_SERVICE_CONTRACTID);
|
|
|
|
RebuildSuffixList(nls);
|
2020-03-04 19:11:16 +03:00
|
|
|
|
2020-03-08 16:52:07 +03:00
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
if (NS_FAILED(NS_NewNamedThread("TRR Background", getter_AddRefs(thread)))) {
|
|
|
|
NS_WARNING("NS_NewNamedThread failed!");
|
|
|
|
return NS_ERROR_FAILURE;
|
2020-03-04 19:11:16 +03:00
|
|
|
}
|
|
|
|
|
2020-03-08 16:52:07 +03:00
|
|
|
sTRRBackgroundThread = thread;
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
LOG(("Initialized TRRService\n"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-24 00:17:15 +03:00
|
|
|
void TRRService::GetParentalControlEnabledInternal() {
|
|
|
|
nsCOMPtr<nsIParentalControlsService> pc =
|
|
|
|
do_CreateInstance("@mozilla.org/parental-controls-service;1");
|
|
|
|
if (pc) {
|
|
|
|
pc->GetParentalControlsEnabled(&mParentalControlEnabled);
|
|
|
|
LOG(("TRRService::GetParentalControlEnabledInternal=%d\n",
|
|
|
|
mParentalControlEnabled));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 20:18:08 +03:00
|
|
|
void TRRService::SetDetectedTrrURI(const nsACString& aURI) {
|
2020-04-27 20:19:46 +03:00
|
|
|
nsAutoCString userPrefURI;
|
|
|
|
nsAutoCString defaultPrefURI;
|
|
|
|
|
|
|
|
nsresult rv = Preferences::GetCString(TRR_PREF("uri"), userPrefURI,
|
|
|
|
PrefValueKind::User);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rv = Preferences::GetCString(TRR_PREF("uri"), defaultPrefURI,
|
|
|
|
PrefValueKind::Default);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user has set a custom URI then we don't want to override that.
|
|
|
|
if (!userPrefURI.Equals(defaultPrefURI)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:43:51 +03:00
|
|
|
mURISetByDetection = MaybeSetPrivateURI(aURI);
|
2020-04-27 20:18:08 +03:00
|
|
|
}
|
|
|
|
|
2020-01-07 23:20:38 +03:00
|
|
|
bool TRRService::Enabled(nsIRequest::TRRMode aMode) {
|
|
|
|
if (mMode == MODE_TRROFF) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-12 16:48:49 +03:00
|
|
|
if (mConfirmationState == CONFIRM_INIT &&
|
2020-01-07 23:20:38 +03:00
|
|
|
(!mWaitForCaptive || mCaptiveIsPassed ||
|
|
|
|
(mMode == MODE_TRRONLY || aMode == nsIRequest::TRR_ONLY_MODE))) {
|
2018-03-09 11:05:48 +03:00
|
|
|
LOG(("TRRService::Enabled => CONFIRM_TRYING\n"));
|
2018-02-01 12:20:49 +03:00
|
|
|
mConfirmationState = CONFIRM_TRYING;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mConfirmationState == CONFIRM_TRYING) {
|
2018-03-09 11:05:48 +03:00
|
|
|
LOG(("TRRService::Enabled MaybeConfirm()\n"));
|
2018-02-01 12:20:49 +03:00
|
|
|
MaybeConfirm();
|
|
|
|
}
|
|
|
|
|
2018-03-09 11:05:48 +03:00
|
|
|
if (mConfirmationState != CONFIRM_OK) {
|
2018-03-12 15:19:22 +03:00
|
|
|
LOG(("TRRService::Enabled mConfirmationState=%d mCaptiveIsPassed=%d\n",
|
|
|
|
(int)mConfirmationState, (int)mCaptiveIsPassed));
|
2018-03-09 11:05:48 +03:00
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
return (mConfirmationState == CONFIRM_OK);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void TRRService::GetPrefBranch(nsIPrefBranch** result) {
|
2018-02-01 12:20:49 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
|
|
|
*result = nullptr;
|
|
|
|
CallGetService(NS_PREFSERVICE_CONTRACTID, result);
|
|
|
|
}
|
|
|
|
|
2020-04-27 20:20:18 +03:00
|
|
|
void TRRService::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;
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:43:51 +03:00
|
|
|
bool TRRService::MaybeSetPrivateURI(const nsACString& aURI) {
|
|
|
|
bool clearCache = false;
|
|
|
|
nsAutoCString newURI(aURI);
|
|
|
|
ProcessURITemplate(newURI);
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (mPrivateURI.Equals(newURI)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mPrivateURI.IsEmpty()) {
|
|
|
|
mClearTRRBLStorage = true;
|
|
|
|
LOG(("TRRService clearing blacklist because of change in uri service\n"));
|
|
|
|
clearCache = true;
|
|
|
|
}
|
|
|
|
mPrivateURI = newURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the cache because we changed the URI
|
|
|
|
if (clearCache) {
|
|
|
|
ClearEntireCache();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult TRRService::ReadPrefs(const char* name) {
|
2018-02-01 12:20:49 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
2019-10-14 17:57:38 +03:00
|
|
|
|
|
|
|
// Whenever a pref change occurs that would cause us to clear the cache
|
|
|
|
// we set this to true then do it at the end of the method.
|
|
|
|
bool clearEntireCache = false;
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
if (!name || !strcmp(name, TRR_PREF("mode"))) {
|
2019-06-01 12:44:20 +03:00
|
|
|
// 0 - off, 1 - reserved, 2 - TRR first, 3 - TRR only, 4 - reserved,
|
2018-08-27 16:13:12 +03:00
|
|
|
// 5 - explicit off
|
2018-02-01 12:20:49 +03:00
|
|
|
uint32_t tmp;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetUint(TRR_PREF("mode"), &tmp))) {
|
2018-08-27 16:13:12 +03:00
|
|
|
if (tmp > MODE_TRROFF) {
|
|
|
|
tmp = MODE_TRROFF;
|
|
|
|
}
|
2019-06-01 12:44:06 +03:00
|
|
|
if (tmp == MODE_RESERVED1) {
|
|
|
|
tmp = MODE_TRROFF;
|
|
|
|
}
|
2019-06-01 12:44:20 +03:00
|
|
|
if (tmp == MODE_RESERVED4) {
|
|
|
|
tmp = MODE_TRROFF;
|
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
mMode = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!name || !strcmp(name, TRR_PREF("uri"))) {
|
2020-04-27 20:20:18 +03:00
|
|
|
nsAutoCString newURI;
|
|
|
|
Preferences::GetCString(TRR_PREF("uri"), newURI);
|
2020-04-24 22:43:51 +03:00
|
|
|
MaybeSetPrivateURI(newURI);
|
|
|
|
mURISetByDetection = false;
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
if (!name || !strcmp(name, TRR_PREF("credentials"))) {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
Preferences::GetCString(TRR_PREF("credentials"), mPrivateCred);
|
|
|
|
}
|
|
|
|
if (!name || !strcmp(name, TRR_PREF("confirmationNS"))) {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
nsAutoCString old(mConfirmationNS);
|
|
|
|
Preferences::GetCString(TRR_PREF("confirmationNS"), mConfirmationNS);
|
|
|
|
if (name && !old.IsEmpty() && !mConfirmationNS.Equals(old) &&
|
|
|
|
(mConfirmationState > CONFIRM_TRYING)) {
|
|
|
|
LOG(("TRR::ReadPrefs: restart confirmationNS state\n"));
|
|
|
|
mConfirmationState = CONFIRM_TRYING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!name || !strcmp(name, TRR_PREF("bootstrapAddress"))) {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
Preferences::GetCString(TRR_PREF("bootstrapAddress"), mBootstrapAddr);
|
2019-10-14 17:57:38 +03:00
|
|
|
clearEntireCache = true;
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
if (!name || !strcmp(name, TRR_PREF("wait-for-portal"))) {
|
|
|
|
// Wait for captive portal?
|
|
|
|
bool tmp;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetBool(TRR_PREF("wait-for-portal"), &tmp))) {
|
|
|
|
mWaitForCaptive = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!name || !strcmp(name, TRR_PREF("allow-rfc1918"))) {
|
|
|
|
bool tmp;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetBool(TRR_PREF("allow-rfc1918"), &tmp))) {
|
|
|
|
mRfc1918 = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!name || !strcmp(name, TRR_PREF("useGET"))) {
|
|
|
|
bool tmp;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetBool(TRR_PREF("useGET"), &tmp))) {
|
|
|
|
mUseGET = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!name || !strcmp(name, TRR_PREF("blacklist-duration"))) {
|
|
|
|
// prefs is given in number of seconds
|
|
|
|
uint32_t secs;
|
|
|
|
if (NS_SUCCEEDED(
|
|
|
|
Preferences::GetUint(TRR_PREF("blacklist-duration"), &secs))) {
|
|
|
|
mTRRBlacklistExpireTime = secs;
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 18:07:29 +03:00
|
|
|
if (!name || !strcmp(name, TRR_PREF("early-AAAA"))) {
|
|
|
|
bool tmp;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetBool(TRR_PREF("early-AAAA"), &tmp))) {
|
|
|
|
mEarlyAAAA = tmp;
|
|
|
|
}
|
|
|
|
}
|
2019-06-04 00:17:08 +03:00
|
|
|
|
|
|
|
if (!name || !strcmp(name, TRR_PREF("skip-AAAA-when-not-supported"))) {
|
|
|
|
bool tmp;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetBool(
|
|
|
|
TRR_PREF("skip-AAAA-when-not-supported"), &tmp))) {
|
|
|
|
mCheckIPv6Connectivity = tmp;
|
|
|
|
}
|
|
|
|
}
|
2019-06-04 00:13:22 +03:00
|
|
|
if (!name || !strcmp(name, TRR_PREF("wait-for-A-and-AAAA"))) {
|
|
|
|
bool tmp;
|
|
|
|
if (NS_SUCCEEDED(
|
|
|
|
Preferences::GetBool(TRR_PREF("wait-for-A-and-AAAA"), &tmp))) {
|
|
|
|
mWaitForAllResponses = tmp;
|
|
|
|
}
|
|
|
|
}
|
2018-03-12 15:19:22 +03:00
|
|
|
if (!name || !strcmp(name, kDisableIpv6Pref)) {
|
|
|
|
bool tmp;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetBool(kDisableIpv6Pref, &tmp))) {
|
|
|
|
mDisableIPv6 = tmp;
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 18:45:15 +03:00
|
|
|
if (!name || !strcmp(name, TRR_PREF("disable-ECS"))) {
|
|
|
|
bool tmp;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetBool(TRR_PREF("disable-ECS"), &tmp))) {
|
|
|
|
mDisableECS = tmp;
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 14:53:46 +03:00
|
|
|
if (!name || !strcmp(name, TRR_PREF("max-fails"))) {
|
|
|
|
uint32_t fails;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetUint(TRR_PREF("max-fails"), &fails))) {
|
|
|
|
mDisableAfterFails = fails;
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 22:35:16 +03:00
|
|
|
if (!name || !strcmp(name, TRR_PREF("excluded-domains")) ||
|
2020-01-07 22:32:32 +03:00
|
|
|
!strcmp(name, TRR_PREF("builtin-excluded-domains"))) {
|
2019-11-21 13:43:02 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2019-08-23 23:23:12 +03:00
|
|
|
mExcludedDomains.Clear();
|
2019-09-12 15:44:16 +03:00
|
|
|
|
|
|
|
auto parseExcludedDomains = [this](const char* aPrefName) {
|
|
|
|
nsAutoCString excludedDomains;
|
|
|
|
Preferences::GetCString(aPrefName, excludedDomains);
|
|
|
|
if (excludedDomains.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCCharSeparatedTokenizer tokenizer(
|
|
|
|
excludedDomains, ',', nsCCharSeparatedTokenizer::SEPARATOR_OPTIONAL);
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
nsAutoCString token(tokenizer.nextToken());
|
|
|
|
LOG(("TRRService::ReadPrefs %s host:[%s]\n", aPrefName, token.get()));
|
|
|
|
mExcludedDomains.PutEntry(token);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
parseExcludedDomains(TRR_PREF("excluded-domains"));
|
|
|
|
parseExcludedDomains(TRR_PREF("builtin-excluded-domains"));
|
2019-10-14 17:57:38 +03:00
|
|
|
clearEntireCache = true;
|
2019-11-10 20:11:41 +03:00
|
|
|
}
|
|
|
|
|
2020-01-07 23:20:38 +03:00
|
|
|
if (!name || !strcmp(name, kPrefSkipTRRParentalControl)) {
|
|
|
|
bool tmp;
|
|
|
|
if (NS_SUCCEEDED(Preferences::GetBool(kPrefSkipTRRParentalControl, &tmp))) {
|
|
|
|
mSkipTRRWhenParentalControlEnabled = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:57:38 +03:00
|
|
|
// if name is null, then we're just now initializing. In that case we don't
|
|
|
|
// need to clear the cache.
|
|
|
|
if (name && clearEntireCache) {
|
2020-04-27 20:20:18 +03:00
|
|
|
ClearEntireCache();
|
2019-10-14 17:57:38 +03:00
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-27 20:20:18 +03:00
|
|
|
void TRRService::ClearEntireCache() {
|
|
|
|
bool tmp;
|
|
|
|
nsresult rv =
|
|
|
|
Preferences::GetBool(TRR_PREF("clear-cache-on-pref-change"), &tmp);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!tmp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID);
|
|
|
|
if (!dns) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dns->ClearCache(true);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult TRRService::GetURI(nsCString& result) {
|
2018-02-01 12:20:49 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
result = mPrivateURI;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult TRRService::GetCredentials(nsCString& result) {
|
2018-02-01 12:20:49 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
result = mPrivateCred;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-26 22:52:20 +03:00
|
|
|
uint32_t TRRService::GetRequestTimeout() {
|
|
|
|
if (mMode == MODE_TRRONLY) {
|
|
|
|
return StaticPrefs::network_trr_request_timeout_mode_trronly_ms();
|
|
|
|
}
|
|
|
|
|
|
|
|
return StaticPrefs::network_trr_request_timeout_ms();
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
nsresult TRRService::Start() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
|
|
|
if (!mInitialized) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRRService::~TRRService() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
|
|
|
LOG(("Exiting TRRService\n"));
|
|
|
|
gTRRService = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-03-04 19:11:16 +03:00
|
|
|
nsresult TRRService::DispatchTRRRequest(TRR* aTrrRequest) {
|
|
|
|
return DispatchTRRRequestInternal(aTrrRequest, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TRRService::DispatchTRRRequestInternal(TRR* aTrrRequest,
|
|
|
|
bool aWithLock) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aTrrRequest);
|
|
|
|
if (!StaticPrefs::network_trr_fetch_off_main_thread()) {
|
|
|
|
return NS_DispatchToMainThread(aTrrRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<TRR> trr = aTrrRequest;
|
|
|
|
nsCOMPtr<nsIThread> thread = aWithLock ? TRRThread() : TRRThread_locked();
|
|
|
|
if (!thread) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread->Dispatch(trr.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIThread> TRRService::TRRThread() {
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
return TRRThread_locked();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIThread> TRRService::TRRThread_locked() {
|
|
|
|
RefPtr<nsIThread> thread = sTRRBackgroundThread;
|
|
|
|
return thread.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TRRService::IsOnTRRThread() {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
thread = sTRRBackgroundThread;
|
|
|
|
}
|
|
|
|
if (!thread) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread->IsOnCurrentThread();
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
TRRService::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
2020-04-24 22:43:51 +03:00
|
|
|
nsresult rv;
|
2018-02-01 12:20:49 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
|
|
|
|
LOG(("TRR::Observe() topic=%s\n", aTopic));
|
|
|
|
if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
|
|
|
|
ReadPrefs(NS_ConvertUTF16toUTF8(aData).get());
|
|
|
|
|
2019-02-01 23:46:00 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2018-04-26 01:07:31 +03:00
|
|
|
if (((mConfirmationState == CONFIRM_INIT) && !mBootstrapAddr.IsEmpty() &&
|
|
|
|
(mMode == MODE_TRRONLY)) ||
|
|
|
|
(mConfirmationState == CONFIRM_FAILED)) {
|
2018-02-01 12:20:49 +03:00
|
|
|
mConfirmationState = CONFIRM_TRYING;
|
2019-02-01 23:46:00 +03:00
|
|
|
MaybeConfirm_locked();
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (!strcmp(aTopic, kOpenCaptivePortalLoginEvent)) {
|
|
|
|
// We are in a captive portal
|
|
|
|
LOG(("TRRservice in captive portal\n"));
|
|
|
|
mCaptiveIsPassed = false;
|
|
|
|
} else if (!strcmp(aTopic, NS_CAPTIVE_PORTAL_CONNECTIVITY)) {
|
|
|
|
nsAutoCString data = NS_ConvertUTF16toUTF8(aData);
|
|
|
|
LOG(("TRRservice captive portal was %s\n", data.get()));
|
2018-04-18 16:21:14 +03:00
|
|
|
if (!mTRRBLStorage) {
|
2019-02-01 23:46:00 +03:00
|
|
|
// We need a lock if we modify mTRRBLStorage variable because it is
|
|
|
|
// access off the main thread as well.
|
|
|
|
MutexAutoLock lock(mLock);
|
2018-04-18 16:21:14 +03:00
|
|
|
mTRRBLStorage = DataStorage::Get(DataStorageClass::TRRBlacklist);
|
|
|
|
if (mTRRBLStorage) {
|
2019-04-01 20:07:20 +03:00
|
|
|
if (NS_FAILED(mTRRBLStorage->Init(nullptr))) {
|
2018-04-18 16:21:14 +03:00
|
|
|
mTRRBLStorage = nullptr;
|
|
|
|
}
|
|
|
|
if (mClearTRRBLStorage) {
|
|
|
|
if (mTRRBLStorage) {
|
|
|
|
mTRRBLStorage->Clear();
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
2018-04-18 16:21:14 +03:00
|
|
|
mClearTRRBLStorage = false;
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 23:20:38 +03:00
|
|
|
// We should avoid doing calling MaybeConfirm in response to a pref change
|
|
|
|
// unless the service is in a TRR=enabled mode.
|
|
|
|
if (mMode == MODE_TRRFIRST || mMode == MODE_TRRONLY) {
|
|
|
|
if (!mCaptiveIsPassed) {
|
|
|
|
if (mConfirmationState != CONFIRM_OK) {
|
|
|
|
mConfirmationState = CONFIRM_TRYING;
|
|
|
|
MaybeConfirm();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(("TRRservice CP clear when already up!\n"));
|
2018-07-11 10:43:38 +03:00
|
|
|
}
|
2020-01-07 23:20:38 +03:00
|
|
|
mCaptiveIsPassed = true;
|
2018-04-18 16:21:14 +03:00
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
} else if (!strcmp(aTopic, kClearPrivateData) || !strcmp(aTopic, kPurge)) {
|
|
|
|
// flush the TRR blacklist, both in-memory and on-disk
|
|
|
|
if (mTRRBLStorage) {
|
|
|
|
mTRRBLStorage->Clear();
|
|
|
|
}
|
2019-12-10 12:37:05 +03:00
|
|
|
} else if (!strcmp(aTopic, NS_DNS_SUFFIX_LIST_UPDATED_TOPIC) ||
|
|
|
|
!strcmp(aTopic, NS_NETWORK_LINK_TOPIC)) {
|
2019-11-27 14:14:44 +03:00
|
|
|
nsCOMPtr<nsINetworkLinkService> link = do_QueryInterface(aSubject);
|
|
|
|
RebuildSuffixList(link);
|
2019-11-24 17:54:02 +03:00
|
|
|
CheckPlatformDNSStatus(link);
|
2020-04-24 22:43:51 +03:00
|
|
|
|
|
|
|
if (!strcmp(aTopic, NS_NETWORK_LINK_TOPIC) && mURISetByDetection) {
|
|
|
|
// If the URI was set via SetDetectedTrrURI we need to restore it to the
|
|
|
|
// default pref when a network link change occurs.
|
|
|
|
nsAutoCString userPrefURI;
|
|
|
|
rv = Preferences::GetCString(TRR_PREF("uri"), userPrefURI);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
MaybeSetPrivateURI(userPrefURI);
|
|
|
|
mURISetByDetection = false;
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 19:11:16 +03:00
|
|
|
} else if (!strcmp(aTopic, "xpcom-shutdown-threads")) {
|
|
|
|
if (sTRRBackgroundThread) {
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
thread = sTRRBackgroundThread.get();
|
|
|
|
sTRRBackgroundThread = nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(thread->Shutdown());
|
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:02:48 +03:00
|
|
|
void TRRService::RebuildSuffixList(nsINetworkLinkService* aLinkService) {
|
|
|
|
// The network link service notification normally passes itself as the
|
|
|
|
// subject, but some unit tests will sometimes pass a null subject.
|
|
|
|
if (!aLinkService) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsCString> suffixList;
|
|
|
|
aLinkService->GetDnsSuffixList(suffixList);
|
2019-11-24 17:54:02 +03:00
|
|
|
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
mDNSSuffixDomains.Clear();
|
2019-11-21 13:02:48 +03:00
|
|
|
for (const auto& item : suffixList) {
|
|
|
|
LOG(("TRRService adding %s to suffix list", item.get()));
|
|
|
|
mDNSSuffixDomains.PutEntry(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 17:54:02 +03:00
|
|
|
void TRRService::CheckPlatformDNSStatus(nsINetworkLinkService* aLinkService) {
|
2019-11-21 13:02:48 +03:00
|
|
|
if (!aLinkService) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-24 17:54:02 +03:00
|
|
|
uint32_t platformIndications = nsINetworkLinkService::NONE_DETECTED;
|
|
|
|
aLinkService->GetPlatformDNSIndications(&platformIndications);
|
|
|
|
LOG(("TRRService platformIndications=%u", platformIndications));
|
|
|
|
mPlatformDisabledTRR =
|
|
|
|
(!StaticPrefs::network_trr_enable_when_vpn_detected() &&
|
|
|
|
(platformIndications & nsINetworkLinkService::VPN_DETECTED)) ||
|
|
|
|
(!StaticPrefs::network_trr_enable_when_proxy_detected() &&
|
2019-11-24 17:33:26 +03:00
|
|
|
(platformIndications & nsINetworkLinkService::PROXY_DETECTED)) ||
|
|
|
|
(!StaticPrefs::network_trr_enable_when_nrpt_detected() &&
|
|
|
|
(platformIndications & nsINetworkLinkService::NRPT_DETECTED));
|
2019-11-21 13:02:48 +03:00
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
void TRRService::MaybeConfirm() {
|
2019-02-01 23:46:00 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
MaybeConfirm_locked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TRRService::MaybeConfirm_locked() {
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
2020-01-07 23:20:38 +03:00
|
|
|
if (mMode == MODE_TRROFF || mConfirmer ||
|
2018-02-01 12:20:49 +03:00
|
|
|
mConfirmationState != CONFIRM_TRYING) {
|
2018-03-09 11:05:48 +03:00
|
|
|
LOG(
|
|
|
|
("TRRService:MaybeConfirm mode=%d, mConfirmer=%p "
|
|
|
|
"mConfirmationState=%d\n",
|
2019-05-01 11:47:10 +03:00
|
|
|
(int)mMode, (void*)mConfirmer, (int)mConfirmationState));
|
2018-02-01 12:20:49 +03:00
|
|
|
return;
|
|
|
|
}
|
2019-02-01 23:46:00 +03:00
|
|
|
|
2020-03-12 14:24:27 +03:00
|
|
|
if (mConfirmationNS.Equals("skip") || mMode == MODE_TRRONLY) {
|
2018-02-01 12:20:49 +03:00
|
|
|
LOG(("TRRService starting confirmation test %s SKIPPED\n",
|
|
|
|
mPrivateURI.get()));
|
|
|
|
mConfirmationState = CONFIRM_OK;
|
|
|
|
} else {
|
|
|
|
LOG(("TRRService starting confirmation test %s %s\n", mPrivateURI.get(),
|
2019-02-01 23:46:00 +03:00
|
|
|
mConfirmationNS.get()));
|
|
|
|
mConfirmer =
|
|
|
|
new TRR(this, mConfirmationNS, TRRTYPE_NS, EmptyCString(), false);
|
2020-03-04 19:11:16 +03:00
|
|
|
DispatchTRRRequestInternal(mConfirmer, false);
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
bool TRRService::MaybeBootstrap(const nsACString& aPossible,
|
|
|
|
nsACString& aResult) {
|
2018-02-01 12:20:49 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2020-01-07 23:20:38 +03:00
|
|
|
if (mMode == MODE_TRROFF || mBootstrapAddr.IsEmpty()) {
|
2018-02-01 12:20:49 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> url;
|
|
|
|
nsresult rv =
|
|
|
|
NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
|
2018-02-21 03:00:54 +03:00
|
|
|
.Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init,
|
|
|
|
nsIStandardURL::URLTYPE_STANDARD, 443,
|
|
|
|
mPrivateURI, nullptr, nullptr, nullptr))
|
2018-02-01 12:20:49 +03:00
|
|
|
.Finalize(url);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG(("TRRService::MaybeBootstrap failed to create URI!\n"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString host;
|
|
|
|
url->GetHost(host);
|
|
|
|
if (!aPossible.Equals(host)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(("TRRService::MaybeBootstrap: use %s instead of %s\n",
|
|
|
|
mBootstrapAddr.get(), host.get()));
|
|
|
|
aResult = mBootstrapAddr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-12 00:11:18 +03:00
|
|
|
bool TRRService::IsDomainBlacklisted(const nsACString& aHost,
|
|
|
|
const nsACString& aOriginSuffix,
|
|
|
|
bool aPrivateBrowsing) {
|
2020-01-07 23:20:38 +03:00
|
|
|
if (!Enabled(nsIRequest::TRR_DEFAULT_MODE)) {
|
2019-10-15 16:24:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
|
2019-11-21 13:43:02 +03:00
|
|
|
// It's OK to call this method here because it only happens on the main
|
|
|
|
// thread, and we only change the excluded domains/dns suffix list
|
|
|
|
// on the main thread in response to observer notifications.
|
|
|
|
// Calling the locking version of this method would cause us to grab
|
|
|
|
// the mutex for every label of the hostname, which would be very
|
|
|
|
// inefficient.
|
2020-03-04 19:11:16 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (IsExcludedFromTRR_unlocked(aHost)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(IsOnTRRThread());
|
|
|
|
if (IsExcludedFromTRR(aHost)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-30 10:08:07 +03:00
|
|
|
}
|
|
|
|
|
2019-03-21 15:41:41 +03:00
|
|
|
if (!mTRRBLStorage) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mClearTRRBLStorage) {
|
|
|
|
mTRRBLStorage->Clear();
|
|
|
|
mClearTRRBLStorage = false;
|
|
|
|
return false; // just cleared!
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
// use a unified casing for the hashkey
|
2018-10-22 18:38:18 +03:00
|
|
|
nsAutoCString hashkey(aHost + aOriginSuffix);
|
|
|
|
nsCString val(mTRRBLStorage->Get(hashkey, aPrivateBrowsing
|
2018-02-01 12:20:49 +03:00
|
|
|
? DataStorage_Private
|
|
|
|
: DataStorage_Persistent));
|
|
|
|
|
|
|
|
if (!val.IsEmpty()) {
|
|
|
|
nsresult code;
|
|
|
|
int32_t until = val.ToInteger(&code) + mTRRBlacklistExpireTime;
|
|
|
|
int32_t expire = NowInSeconds();
|
|
|
|
if (NS_SUCCEEDED(code) && (until > expire)) {
|
|
|
|
LOG(("Host [%s] is TRR blacklisted\n", nsCString(aHost).get()));
|
|
|
|
return true;
|
2018-04-30 19:46:04 +03:00
|
|
|
}
|
2019-02-01 23:46:00 +03:00
|
|
|
|
2018-04-30 19:46:04 +03:00
|
|
|
// the blacklisted entry has expired
|
2019-02-01 23:46:00 +03:00
|
|
|
mTRRBLStorage->Remove(hashkey, aPrivateBrowsing ? DataStorage_Private
|
2018-04-30 19:46:04 +03:00
|
|
|
: DataStorage_Persistent);
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-12 00:11:18 +03:00
|
|
|
// When running in TRR-only mode, the blacklist is not used and it will also
|
|
|
|
// try resolving the localhost / .local names.
|
|
|
|
bool TRRService::IsTRRBlacklisted(const nsACString& aHost,
|
|
|
|
const nsACString& aOriginSuffix,
|
|
|
|
bool aPrivateBrowsing,
|
|
|
|
bool aParentsToo) // false if domain
|
|
|
|
{
|
|
|
|
if (mMode == MODE_TRRONLY) {
|
|
|
|
return false; // might as well try
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("Checking if host [%s] is blacklisted", aHost.BeginReading()));
|
|
|
|
|
|
|
|
int32_t dot = aHost.FindChar('.');
|
|
|
|
if ((dot == kNotFound) && aParentsToo) {
|
|
|
|
// Only if a full host name. Domains can be dotless to be able to
|
|
|
|
// blacklist entire TLDs
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsDomainBlacklisted(aHost, aOriginSuffix, aPrivateBrowsing)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentCSubstring domain = Substring(aHost, 0);
|
|
|
|
while (dot != kNotFound) {
|
|
|
|
dot++;
|
|
|
|
domain.Rebind(domain, dot, domain.Length() - dot);
|
|
|
|
|
|
|
|
if (IsDomainBlacklisted(domain, aOriginSuffix, aPrivateBrowsing)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
dot = domain.FindChar('.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
bool TRRService::IsExcludedFromTRR(const nsACString& aHost) {
|
2019-11-21 13:43:02 +03:00
|
|
|
// This method may be called off the main thread. We need to lock so
|
|
|
|
// mExcludedDomains and mDNSSuffixDomains don't change while this code
|
|
|
|
// is running.
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
return IsExcludedFromTRR_unlocked(aHost);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TRRService::IsExcludedFromTRR_unlocked(const nsACString& aHost) {
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
mLock.AssertCurrentThreadOwns();
|
|
|
|
}
|
|
|
|
|
2019-11-24 17:54:02 +03:00
|
|
|
if (mPlatformDisabledTRR) {
|
|
|
|
LOG(("%s is excluded from TRR because of platform indications",
|
|
|
|
aHost.BeginReading()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-18 21:02:58 +03:00
|
|
|
int32_t dot = 0;
|
|
|
|
// iteratively check the sub-domain of |aHost|
|
|
|
|
while (dot < static_cast<int32_t>(aHost.Length())) {
|
|
|
|
nsDependentCSubstring subdomain =
|
|
|
|
Substring(aHost, dot, aHost.Length() - dot);
|
|
|
|
|
|
|
|
if (mExcludedDomains.GetEntry(subdomain)) {
|
|
|
|
LOG(("Subdomain [%s] of host [%s] Is Excluded From TRR via pref\n",
|
|
|
|
subdomain.BeginReading(), aHost.BeginReading()));
|
2019-03-28 16:15:05 +03:00
|
|
|
return true;
|
|
|
|
}
|
2019-10-15 16:24:49 +03:00
|
|
|
if (mDNSSuffixDomains.GetEntry(subdomain)) {
|
|
|
|
LOG(("Subdomain [%s] of host [%s] Is Excluded From TRR via pref\n",
|
|
|
|
subdomain.BeginReading(), aHost.BeginReading()));
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-18 21:02:58 +03:00
|
|
|
|
|
|
|
dot = aHost.FindChar('.', dot + 1);
|
|
|
|
if (dot == kNotFound) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dot++;
|
2019-03-28 16:15:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
class ProxyBlacklist : public Runnable {
|
|
|
|
public:
|
2019-05-01 11:47:10 +03:00
|
|
|
ProxyBlacklist(TRRService* service, const nsACString& aHost,
|
|
|
|
const nsACString& aOriginSuffix, bool pb, bool aParentsToo)
|
2018-02-01 12:20:49 +03:00
|
|
|
: mozilla::Runnable("proxyBlackList"),
|
2018-10-22 18:38:18 +03:00
|
|
|
mService(service),
|
|
|
|
mHost(aHost),
|
|
|
|
mOriginSuffix(aOriginSuffix),
|
|
|
|
mPB(pb),
|
|
|
|
mParentsToo(aParentsToo) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2018-10-22 18:38:18 +03:00
|
|
|
mService->TRRBlacklist(mHost, mOriginSuffix, mPB, mParentsToo);
|
2018-02-01 12:20:49 +03:00
|
|
|
mService = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<TRRService> mService;
|
|
|
|
nsCString mHost;
|
2018-10-22 18:38:18 +03:00
|
|
|
nsCString mOriginSuffix;
|
2018-02-01 12:20:49 +03:00
|
|
|
bool mPB;
|
|
|
|
bool mParentsToo;
|
|
|
|
};
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void TRRService::TRRBlacklist(const nsACString& aHost,
|
|
|
|
const nsACString& aOriginSuffix,
|
2018-10-22 18:38:18 +03:00
|
|
|
bool privateBrowsing, bool aParentsToo) {
|
2019-02-01 23:46:00 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (!mTRRBLStorage) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
2018-10-22 18:38:18 +03:00
|
|
|
NS_DispatchToMainThread(new ProxyBlacklist(this, aHost, aOriginSuffix,
|
2018-02-01 12:20:49 +03:00
|
|
|
privateBrowsing, aParentsToo));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-01 23:46:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
LOG(("TRR blacklist %s\n", nsCString(aHost).get()));
|
2018-10-22 18:38:18 +03:00
|
|
|
nsAutoCString hashkey(aHost + aOriginSuffix);
|
2018-02-01 12:20:49 +03:00
|
|
|
nsAutoCString val;
|
|
|
|
val.AppendInt(NowInSeconds()); // creation time
|
|
|
|
|
|
|
|
// this overwrites any existing entry
|
2019-02-01 23:46:00 +03:00
|
|
|
mTRRBLStorage->Put(
|
|
|
|
hashkey, val,
|
|
|
|
privateBrowsing ? DataStorage_Private : DataStorage_Persistent);
|
2018-02-01 12:20:49 +03:00
|
|
|
|
|
|
|
if (aParentsToo) {
|
|
|
|
// when given a full host name, verify its domain as well
|
|
|
|
int32_t dot = aHost.FindChar('.');
|
|
|
|
if (dot != kNotFound) {
|
|
|
|
// this has a domain to be checked
|
|
|
|
dot++;
|
|
|
|
nsDependentCSubstring domain =
|
|
|
|
Substring(aHost, dot, aHost.Length() - dot);
|
|
|
|
nsAutoCString check(domain);
|
2018-10-22 18:38:18 +03:00
|
|
|
if (IsTRRBlacklisted(check, aOriginSuffix, privateBrowsing, false)) {
|
2018-02-01 12:20:49 +03:00
|
|
|
// the domain part is already blacklisted, no need to add this entry
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// verify 'check' over TRR
|
|
|
|
LOG(("TRR: verify if '%s' resolves as NS\n", check.get()));
|
|
|
|
|
|
|
|
// check if there's an NS entry for this name
|
2018-10-30 16:06:24 +03:00
|
|
|
RefPtr<TRR> trr =
|
|
|
|
new TRR(this, check, TRRTYPE_NS, aOriginSuffix, privateBrowsing);
|
2020-03-04 19:11:16 +03:00
|
|
|
DispatchTRRRequest(trr);
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 11:05:48 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
TRRService::Notify(nsITimer* aTimer) {
|
2018-03-09 11:05:48 +03:00
|
|
|
if (aTimer == mRetryConfirmTimer) {
|
|
|
|
mRetryConfirmTimer = nullptr;
|
|
|
|
if (mConfirmationState == CONFIRM_FAILED) {
|
|
|
|
LOG(("TRRService retry NS of %s\n", mConfirmationNS.get()));
|
|
|
|
mConfirmationState = CONFIRM_TRYING;
|
|
|
|
MaybeConfirm();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Unknown timer");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-10-17 10:59:43 +03:00
|
|
|
void TRRService::TRRIsOkay(enum TrrOkay aReason) {
|
2020-03-04 19:11:16 +03:00
|
|
|
MOZ_ASSERT_IF(StaticPrefs::network_trr_fetch_off_main_thread(),
|
|
|
|
IsOnTRRThread());
|
|
|
|
MOZ_ASSERT_IF(!StaticPrefs::network_trr_fetch_off_main_thread(),
|
|
|
|
NS_IsMainThread());
|
|
|
|
|
2018-10-17 10:59:43 +03:00
|
|
|
Telemetry::AccumulateCategorical(
|
|
|
|
aReason == OKAY_NORMAL ? Telemetry::LABELS_DNS_TRR_SUCCESS::Fine
|
|
|
|
: (aReason == OKAY_TIMEOUT
|
|
|
|
? Telemetry::LABELS_DNS_TRR_SUCCESS::Timeout
|
|
|
|
: Telemetry::LABELS_DNS_TRR_SUCCESS::Bad));
|
|
|
|
if (aReason == OKAY_NORMAL) {
|
2018-10-03 14:53:46 +03:00
|
|
|
mTRRFailures = 0;
|
|
|
|
} else if ((mMode == MODE_TRRFIRST) && (mConfirmationState == CONFIRM_OK)) {
|
|
|
|
// only count failures while in OK state
|
|
|
|
uint32_t fails = ++mTRRFailures;
|
|
|
|
if (fails >= mDisableAfterFails) {
|
|
|
|
LOG(("TRRService goes FAILED after %u failures in a row\n", fails));
|
|
|
|
mConfirmationState = CONFIRM_FAILED;
|
|
|
|
// Fire off a timer and start re-trying the NS domain again
|
|
|
|
NS_NewTimerWithCallback(getter_AddRefs(mRetryConfirmTimer), this,
|
|
|
|
mRetryConfirmInterval, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
mTRRFailures = 0; // clear it again
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
AHostResolver::LookupStatus TRRService::CompleteLookup(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsHostRecord* rec, nsresult status, AddrInfo* aNewRRSet, bool pb,
|
|
|
|
const nsACString& aOriginSuffix) {
|
2018-02-01 12:20:49 +03:00
|
|
|
// this is an NS check for the TRR blacklist or confirmationNS check
|
|
|
|
|
2020-03-04 19:11:16 +03:00
|
|
|
MOZ_ASSERT_IF(StaticPrefs::network_trr_fetch_off_main_thread(),
|
|
|
|
IsOnTRRThread());
|
|
|
|
MOZ_ASSERT_IF(!StaticPrefs::network_trr_fetch_off_main_thread(),
|
|
|
|
NS_IsMainThread());
|
2018-02-01 12:20:49 +03:00
|
|
|
MOZ_ASSERT(!rec);
|
|
|
|
|
2019-03-19 15:22:12 +03:00
|
|
|
RefPtr<AddrInfo> newRRSet(aNewRRSet);
|
2018-02-01 12:20:49 +03:00
|
|
|
MOZ_ASSERT(newRRSet && newRRSet->IsTRR() == TRRTYPE_NS);
|
|
|
|
|
2019-02-01 23:46:00 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
MOZ_ASSERT(!mConfirmer || (mConfirmationState == CONFIRM_TRYING));
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-01 12:20:49 +03:00
|
|
|
if (mConfirmationState == CONFIRM_TRYING) {
|
2019-02-01 23:46:00 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
MOZ_ASSERT(mConfirmer);
|
|
|
|
mConfirmationState = NS_SUCCEEDED(status) ? CONFIRM_OK : CONFIRM_FAILED;
|
|
|
|
LOG(("TRRService finishing confirmation test %s %d %X\n",
|
|
|
|
mPrivateURI.get(), (int)mConfirmationState, (unsigned int)status));
|
|
|
|
mConfirmer = nullptr;
|
|
|
|
}
|
2018-10-03 14:53:46 +03:00
|
|
|
if (mConfirmationState == CONFIRM_FAILED) {
|
|
|
|
// retry failed NS confirmation
|
2018-03-09 11:05:48 +03:00
|
|
|
NS_NewTimerWithCallback(getter_AddRefs(mRetryConfirmTimer), this,
|
|
|
|
mRetryConfirmInterval, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
if (mRetryConfirmInterval < 64000) {
|
|
|
|
// double the interval up to this point
|
|
|
|
mRetryConfirmInterval *= 2;
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-13 01:47:45 +03:00
|
|
|
if (mMode != MODE_TRRONLY) {
|
|
|
|
// don't accumulate trronly data here since trronly failures are
|
|
|
|
// handled above by trying again, so counting the successes here would
|
|
|
|
// skew the numbers
|
|
|
|
Telemetry::Accumulate(Telemetry::DNS_TRR_NS_VERFIFIED,
|
|
|
|
(mConfirmationState == CONFIRM_OK));
|
|
|
|
}
|
2018-03-09 11:05:48 +03:00
|
|
|
mRetryConfirmInterval = 1000;
|
|
|
|
}
|
2018-02-01 12:20:49 +03:00
|
|
|
return LOOKUP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// when called without a host record, this is a domain name check response.
|
|
|
|
if (NS_SUCCEEDED(status)) {
|
2017-11-30 10:30:27 +03:00
|
|
|
LOG(("TRR verified %s to be fine!\n", newRRSet->mHostName.get()));
|
2018-02-01 12:20:49 +03:00
|
|
|
} else {
|
2017-11-30 10:30:27 +03:00
|
|
|
LOG(("TRR says %s doesn't resolve as NS!\n", newRRSet->mHostName.get()));
|
2018-10-30 16:06:24 +03:00
|
|
|
TRRBlacklist(newRRSet->mHostName, aOriginSuffix, pb, false);
|
2018-02-01 12:20:49 +03:00
|
|
|
}
|
|
|
|
return LOOKUP_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-22 23:54:11 +03:00
|
|
|
AHostResolver::LookupStatus TRRService::CompleteLookupByType(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsHostRecord*, nsresult, const nsTArray<nsCString>* aResult, uint32_t aTtl,
|
2018-09-22 23:54:11 +03:00
|
|
|
bool aPb) {
|
|
|
|
return LOOKUP_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:20:49 +03:00
|
|
|
#undef LOG
|
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|