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 
16 namespace mozilla::net {
17 
18 static const char kPrefProxyType[] = "network.proxy.type";
19 static const char kPrefDisablePrefetch[] = "network.dns.disablePrefetch";
20 static const char kPrefNetworkProxySOCKS[] = "network.proxy.socks";
21 
NS_IMPL_ISUPPORTS(DNSServiceBase,nsIObserver)22 NS_IMPL_ISUPPORTS(DNSServiceBase, nsIObserver)
23 
24 void DNSServiceBase::AddPrefObserver(nsIPrefBranch* aPrefs) {
25   aPrefs->AddObserver(kPrefProxyType, this, false);
26   aPrefs->AddObserver(kPrefDisablePrefetch, this, false);
27   // Monitor these to see if there is a change in proxy configuration
28   aPrefs->AddObserver(kPrefNetworkProxySOCKS, this, false);
29 }
30 
ReadPrefs(const char * aName)31 void DNSServiceBase::ReadPrefs(const char* aName) {
32   if (!aName || !strcmp(aName, kPrefNetworkProxySOCKS)) {
33     nsAutoCString socks;
34     if (NS_SUCCEEDED(Preferences::GetCString(kPrefNetworkProxySOCKS, socks))) {
35       mHasSocksProxy = !socks.IsEmpty();
36     }
37   }
38   if (!aName || !strcmp(aName, kPrefDisablePrefetch) ||
39       !strcmp(aName, kPrefProxyType)) {
40     mDisablePrefetch = Preferences::GetBool(kPrefDisablePrefetch, false) ||
41                        (StaticPrefs::network_proxy_type() ==
42                         nsIProtocolProxyService::PROXYCONFIG_MANUAL);
43   }
44 }
45 
DNSForbiddenByActiveProxy(const nsACString & aHostname,uint32_t aFlags)46 bool DNSServiceBase::DNSForbiddenByActiveProxy(const nsACString& aHostname,
47                                                uint32_t aFlags) {
48   if (aFlags & nsIDNSService::RESOLVE_IGNORE_SOCKS_DNS) {
49     return false;
50   }
51 
52   // We should avoid doing DNS when a proxy is in use.
53   if (StaticPrefs::network_proxy_type() ==
54           nsIProtocolProxyService::PROXYCONFIG_MANUAL &&
55       mHasSocksProxy && StaticPrefs::network_proxy_socks_remote_dns()) {
56     // Allow IP lookups through, but nothing else.
57     if (!HostIsIPLiteral(aHostname)) {
58       return true;
59     }
60   }
61   return false;
62 }
63 
64 }  // namespace mozilla::net
65