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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/proxy_resolution/proxy_config_service_mac.h"
#include <CFNetwork/CFProxySupport.h>
#include <CoreFoundation/CoreFoundation.h>
#include <SystemConfiguration/SystemConfiguration.h>
#include <memory>
#include "base/apple/foundation_util.h"
#include "base/apple/scoped_cftyperef.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/sys_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "net/base/net_errors.h"
#include "net/proxy_resolution/proxy_chain_util_apple.h"
#include "net/proxy_resolution/proxy_info.h"
namespace net {
namespace {
// Utility function to pull out a boolean value from a dictionary and return it,
// returning a default value if the key is not present.
bool GetBoolFromDictionary(CFDictionaryRef dict,
CFStringRef key,
bool default_value) {
CFNumberRef number =
base::apple::GetValueFromDictionary<CFNumberRef>(dict, key);
if (!number)
return default_value;
int int_value;
if (CFNumberGetValue(number, kCFNumberIntType, &int_value))
return int_value;
else
return default_value;
}
void GetCurrentProxyConfig(const NetworkTrafficAnnotationTag traffic_annotation,
ProxyConfigWithAnnotation* config) {
base::apple::ScopedCFTypeRef<CFDictionaryRef> config_dict(
SCDynamicStoreCopyProxies(nullptr));
DCHECK(config_dict);
ProxyConfig proxy_config;
proxy_config.set_from_system(true);
// auto-detect
// There appears to be no UI for this configuration option, and we're not sure
// if Apple's proxy code even takes it into account. But the constant is in
// the header file so we'll use it.
proxy_config.set_auto_detect(GetBoolFromDictionary(
config_dict.get(), kSCPropNetProxiesProxyAutoDiscoveryEnable, false));
// PAC file
if (GetBoolFromDictionary(config_dict.get(),
kSCPropNetProxiesProxyAutoConfigEnable,
false)) {
CFStringRef pac_url_ref = base::apple::GetValueFromDictionary<CFStringRef>(
config_dict.get(), kSCPropNetProxiesProxyAutoConfigURLString);
if (pac_url_ref)
proxy_config.set_pac_url(GURL(base::SysCFStringRefToUTF8(pac_url_ref)));
}
// proxies (for now ftp, http, https, and SOCKS)
if (GetBoolFromDictionary(config_dict.get(), kSCPropNetProxiesFTPEnable,
false)) {
ProxyChain proxy_chain = ProxyDictionaryToProxyChain(
kCFProxyTypeHTTP, config_dict.get(), kSCPropNetProxiesFTPProxy,
kSCPropNetProxiesFTPPort);
if (proxy_chain.IsValid()) {
proxy_config.proxy_rules().type =
ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
proxy_config.proxy_rules().proxies_for_ftp.SetSingleProxyChain(
proxy_chain);
}
}
if (GetBoolFromDictionary(config_dict.get(), kSCPropNetProxiesHTTPEnable,
false)) {
ProxyChain proxy_chain = ProxyDictionaryToProxyChain(
kCFProxyTypeHTTP, config_dict.get(), kSCPropNetProxiesHTTPProxy,
kSCPropNetProxiesHTTPPort);
if (proxy_chain.IsValid()) {
proxy_config.proxy_rules().type =
ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
proxy_config.proxy_rules().proxies_for_http.SetSingleProxyChain(
proxy_chain);
}
}
if (GetBoolFromDictionary(config_dict.get(), kSCPropNetProxiesHTTPSEnable,
false)) {
ProxyChain proxy_chain = ProxyDictionaryToProxyChain(
kCFProxyTypeHTTPS, config_dict.get(), kSCPropNetProxiesHTTPSProxy,
kSCPropNetProxiesHTTPSPort);
if (proxy_chain.IsValid()) {
proxy_config.proxy_rules().type =
ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
proxy_config.proxy_rules().proxies_for_https.SetSingleProxyChain(
proxy_chain);
}
}
if (GetBoolFromDictionary(config_dict.get(), kSCPropNetProxiesSOCKSEnable,
false)) {
ProxyChain proxy_chain = ProxyDictionaryToProxyChain(
kCFProxyTypeSOCKS, config_dict.get(), kSCPropNetProxiesSOCKSProxy,
kSCPropNetProxiesSOCKSPort);
if (proxy_chain.IsValid()) {
proxy_config.proxy_rules().type =
ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
proxy_config.proxy_rules().fallback_proxies.SetSingleProxyChain(
proxy_chain);
}
}
// proxy bypass list
CFArrayRef bypass_array_ref = base::apple::GetValueFromDictionary<CFArrayRef>(
config_dict.get(), kSCPropNetProxiesExceptionsList);
if (bypass_array_ref) {
CFIndex bypass_array_count = CFArrayGetCount(bypass_array_ref);
for (CFIndex i = 0; i < bypass_array_count; ++i) {
CFStringRef bypass_item_ref = base::apple::CFCast<CFStringRef>(
CFArrayGetValueAtIndex(bypass_array_ref, i));
if (!bypass_item_ref) {
LOG(WARNING) << "Expected value for item " << i
<< " in the kSCPropNetProxiesExceptionsList"
" to be a CFStringRef but it was not";
} else {
proxy_config.proxy_rules().bypass_rules.AddRuleFromString(
base::SysCFStringRefToUTF8(bypass_item_ref));
}
}
}
// proxy bypass boolean
if (GetBoolFromDictionary(config_dict.get(),
kSCPropNetProxiesExcludeSimpleHostnames,
false)) {
proxy_config.proxy_rules()
.bypass_rules.PrependRuleToBypassSimpleHostnames();
}
*config = ProxyConfigWithAnnotation(proxy_config, traffic_annotation);
}
} // namespace
// Reference-counted helper for posting a task to
// ProxyConfigServiceMac::OnProxyConfigChanged between the notifier and IO
// thread. This helper object may outlive the ProxyConfigServiceMac.
class ProxyConfigServiceMac::Helper
: public base::RefCountedThreadSafe<ProxyConfigServiceMac::Helper> {
public:
explicit Helper(ProxyConfigServiceMac* parent) : parent_(parent) {
DCHECK(parent);
}
// Called when the parent is destroyed.
void Orphan() { parent_ = nullptr; }
void OnProxyConfigChanged(const ProxyConfigWithAnnotation& new_config) {
if (parent_)
parent_->OnProxyConfigChanged(new_config);
}
private:
friend class base::RefCountedThreadSafe<Helper>;
~Helper() = default;
raw_ptr<ProxyConfigServiceMac> parent_;
};
void ProxyConfigServiceMac::Forwarder::SetDynamicStoreNotificationKeys(
base::apple::ScopedCFTypeRef<SCDynamicStoreRef> store) {
proxy_config_service_->SetDynamicStoreNotificationKeys(std::move(store));
}
void ProxyConfigServiceMac::Forwarder::OnNetworkConfigChange(
CFArrayRef changed_keys) {
proxy_config_service_->OnNetworkConfigChange(changed_keys);
}
ProxyConfigServiceMac::ProxyConfigServiceMac(
const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner,
const NetworkTrafficAnnotationTag& traffic_annotation)
: forwarder_(this),
helper_(base::MakeRefCounted<Helper>(this)),
sequenced_task_runner_(sequenced_task_runner),
traffic_annotation_(traffic_annotation) {
DCHECK(sequenced_task_runner_.get());
config_watcher_ = std::make_unique<NetworkConfigWatcherApple>(&forwarder_);
}
ProxyConfigServiceMac::~ProxyConfigServiceMac() {
DCHECK(sequenced_task_runner_->RunsTasksInCurrentSequence());
// Delete the config_watcher_ to ensure the notifier thread finishes before
// this object is destroyed.
config_watcher_.reset();
helper_->Orphan();
}
void ProxyConfigServiceMac::AddObserver(Observer* observer) {
DCHECK(sequenced_task_runner_->RunsTasksInCurrentSequence());
observers_.AddObserver(observer);
}
void ProxyConfigServiceMac::RemoveObserver(Observer* observer) {
DCHECK(sequenced_task_runner_->RunsTasksInCurrentSequence());
observers_.RemoveObserver(observer);
}
ProxyConfigService::ConfigAvailability
ProxyConfigServiceMac::GetLatestProxyConfig(ProxyConfigWithAnnotation* config) {
DCHECK(sequenced_task_runner_->RunsTasksInCurrentSequence());
// Lazy-initialize by fetching the proxy setting from this thread.
if (!has_fetched_config_) {
GetCurrentProxyConfig(traffic_annotation_, &last_config_fetched_);
has_fetched_config_ = true;
}
*config = last_config_fetched_;
return has_fetched_config_ ? CONFIG_VALID : CONFIG_PENDING;
}
void ProxyConfigServiceMac::SetDynamicStoreNotificationKeys(
base::apple::ScopedCFTypeRef<SCDynamicStoreRef> store) {
// Called on notifier thread.
base::apple::ScopedCFTypeRef<CFStringRef> proxies_key(
SCDynamicStoreKeyCreateProxies(nullptr));
base::apple::ScopedCFTypeRef<CFArrayRef> key_array(CFArrayCreate(
nullptr, (const void**)(&proxies_key), 1, &kCFTypeArrayCallBacks));
bool ret = SCDynamicStoreSetNotificationKeys(store.get(), key_array.get(),
/*patterns=*/nullptr);
// TODO(willchan): Figure out a proper way to handle this rather than crash.
CHECK(ret);
}
void ProxyConfigServiceMac::OnNetworkConfigChange(CFArrayRef changed_keys) {
// Called on notifier thread.
// Fetch the new system proxy configuration.
ProxyConfigWithAnnotation new_config;
GetCurrentProxyConfig(traffic_annotation_, &new_config);
// Call OnProxyConfigChanged() on the TakeRunner to notify our observers.
sequenced_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&Helper::OnProxyConfigChanged, helper_.get(), new_config));
}
void ProxyConfigServiceMac::OnProxyConfigChanged(
const ProxyConfigWithAnnotation& new_config) {
DCHECK(sequenced_task_runner_->RunsTasksInCurrentSequence());
// Keep track of the last value we have seen.
has_fetched_config_ = true;
last_config_fetched_ = new_config;
// Notify all the observers.
for (auto& observer : observers_)
observer.OnProxyConfigChanged(new_config, CONFIG_VALID);
}
} // namespace net
|