DNSServiceBase.cpp (3155B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set sw=2 ts=8 et tw=80 : */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "DNSServiceBase.h" 8 9 #include "DNS.h" 10 #include "mozilla/Preferences.h" 11 #include "mozilla/StaticPrefs_network.h" 12 #include "nsIDNSService.h" 13 #include "nsIProtocolProxyService2.h" 14 #include "nsIPrefBranch.h" 15 #include "nsIProxyInfo.h" 16 17 namespace mozilla::net { 18 19 static const char kPrefProxyType[] = "network.proxy.type"; 20 static const char kPrefDisablePrefetch[] = "network.dns.disablePrefetch"; 21 static const char kPrefNetworkProxySOCKS[] = "network.proxy.socks"; 22 static const char kPrefNetworkProxySOCKSVersion[] = 23 "network.proxy.socks_version"; 24 25 NS_IMPL_ISUPPORTS(DNSServiceBase, nsIObserver) 26 27 void DNSServiceBase::AddPrefObserver(nsIPrefBranch* aPrefs) { 28 aPrefs->AddObserver(kPrefProxyType, this, false); 29 aPrefs->AddObserver(kPrefDisablePrefetch, this, false); 30 // Monitor these to see if there is a change in proxy configuration 31 aPrefs->AddObserver(kPrefNetworkProxySOCKS, this, false); 32 aPrefs->AddObserver(kPrefNetworkProxySOCKSVersion, this, false); 33 } 34 35 void DNSServiceBase::ReadPrefs(const char* aName) { 36 if (!aName || !strcmp(aName, kPrefNetworkProxySOCKS) || 37 !strcmp(aName, kPrefNetworkProxySOCKSVersion)) { 38 uint32_t socksVersion = Preferences::GetInt(kPrefNetworkProxySOCKSVersion); 39 nsAutoCString socks; 40 if (NS_SUCCEEDED(Preferences::GetCString(kPrefNetworkProxySOCKS, socks))) { 41 mSocksProxyVersion = 0; 42 if (!socks.IsEmpty()) { 43 if (socksVersion == nsIProxyInfo::SOCKS_V4) { 44 mSocksProxyVersion = nsIProxyInfo::SOCKS_V4; 45 } else if (socksVersion == nsIProxyInfo::SOCKS_V5) { 46 mSocksProxyVersion = nsIProxyInfo::SOCKS_V5; 47 } 48 } 49 } 50 } 51 if (!aName || !strcmp(aName, kPrefDisablePrefetch) || 52 !strcmp(aName, kPrefProxyType)) { 53 mDisablePrefetch = Preferences::GetBool(kPrefDisablePrefetch, false) || 54 (StaticPrefs::network_proxy_type() == 55 nsIProtocolProxyService::PROXYCONFIG_MANUAL); 56 } 57 } 58 59 bool DNSServiceBase::DNSForbiddenByActiveProxy(const nsACString& aHostname, 60 uint32_t aFlags) { 61 if (aFlags & nsIDNSService::RESOLVE_IGNORE_SOCKS_DNS) { 62 return false; 63 } 64 65 // TODO(Bug 1890542): use nsIProxyInfo object whether sending DNS request to 66 // local network is fine. 67 // We should avoid doing DNS when a proxy is in use. 68 if (StaticPrefs::network_proxy_type() == 69 nsIProtocolProxyService::PROXYCONFIG_MANUAL && 70 ((mSocksProxyVersion == nsIProxyInfo::SOCKS_V4 && 71 StaticPrefs::network_proxy_socks_remote_dns()) || 72 (mSocksProxyVersion == nsIProxyInfo::SOCKS_V5 && 73 StaticPrefs::network_proxy_socks5_remote_dns()))) { 74 // Allow IP lookups through, but nothing else. 75 if (!HostIsIPLiteral(aHostname)) { 76 return true; 77 } 78 } 79 return false; 80 } 81 82 } // namespace mozilla::net