blob: ca7681d2fc31c6c605f415961738d28244009286 [file] [log] [blame]
[email protected]d2e6d592012-02-03 21:49:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]861c6c62009-04-20 16:50:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Lily Houghton582d4622018-01-22 22:43:405#include "net/proxy_resolution/proxy_config_service_linux.h"
[email protected]861c6c62009-04-20 16:50:566
[email protected]d7395e732009-08-28 23:13:437#include <errno.h>
[email protected]d7395e732009-08-28 23:13:438#include <limits.h>
[email protected]d7395e732009-08-28 23:13:439#include <sys/inotify.h>
10#include <unistd.h>
[email protected]861c6c62009-04-20 16:50:5611
[email protected]9bc8cff2010-04-03 01:05:3912#include <map>
thestig0c412e852016-06-30 08:04:4013#include <utility>
[email protected]9bc8cff2010-04-03 01:05:3914
[email protected]6af889c2011-10-06 23:11:4115#include "base/bind.h"
gabf4f904e2017-05-10 20:55:0216#include "base/files/file_descriptor_watcher_posix.h"
[email protected]57999812013-02-24 05:40:5217#include "base/files/file_path.h"
thestigd8df0332014-09-04 06:33:2918#include "base/files/file_util.h"
[email protected]b9b4a572014-03-17 23:11:1219#include "base/files/scoped_file.h"
[email protected]861c6c62009-04-20 16:50:5620#include "base/logging.h"
Avi Drissman13fc8932015-12-20 04:40:4621#include "base/macros.h"
Eric Romancd032fb62018-05-18 21:40:1322#include "base/memory/ptr_util.h"
[email protected]3a29593d2011-04-11 10:07:5223#include "base/nix/xdg_util.h"
eroman0070d412017-06-22 22:18:2424#include "base/sequenced_task_runner.h"
[email protected]76722472012-05-24 08:26:4625#include "base/single_thread_task_runner.h"
[email protected]fc9be5802013-06-11 10:56:5126#include "base/strings/string_number_conversions.h"
[email protected]f4ebe772013-02-02 00:21:3927#include "base/strings/string_tokenizer.h"
[email protected]66e96c42013-06-28 15:20:3128#include "base/strings/string_util.h"
Gabriel Charette44db1422018-08-06 11:19:3329#include "base/task/post_task.h"
30#include "base/task/task_traits.h"
[email protected]9a8c4022011-01-25 14:25:3331#include "base/threading/thread_restrictions.h"
[email protected]66e96c42013-06-28 15:20:3132#include "base/timer/timer.h"
Lily Houghton582d4622018-01-22 22:43:4033#include "net/base/proxy_server.h"
[email protected]861c6c62009-04-20 16:50:5634
[email protected]3fc24f52012-11-30 21:22:3435#if defined(USE_GIO)
Tim Brown1c307cc2017-12-08 02:40:3836#include <gio/gio.h>
[email protected]3fc24f52012-11-30 21:22:3437#endif // defined(USE_GIO)
38
[email protected]861c6c62009-04-20 16:50:5639namespace net {
40
41namespace {
42
Shimi Zhang13eace252020-01-31 01:49:1943// This turns all rules with a hostname into wildcard matches, which will
44// match not just the indicated hostname but also any hostname that ends with
45// it.
46void RewriteRulesForSuffixMatching(ProxyBypassRules* out) {
47 // Prepend a wildcard (*) to any hostname based rules, provided it isn't an IP
48 // address.
49 for (size_t i = 0; i < out->rules().size(); ++i) {
50 if (!out->rules()[i]->IsHostnamePatternRule())
51 continue;
52
53 const SchemeHostPortMatcherHostnamePatternRule* prev_rule =
54 static_cast<const SchemeHostPortMatcherHostnamePatternRule*>(
55 out->rules()[i].get());
56 out->ReplaceRule(i, prev_rule->GenerateSuffixMatchingRule());
57 }
58}
59
[email protected]861c6c62009-04-20 16:50:5660// Given a proxy hostname from a setting, returns that hostname with
61// an appropriate proxy server scheme prefix.
62// scheme indicates the desired proxy scheme: usually http, with
63// socks 4 or 5 as special cases.
[email protected]87a102b2009-07-14 05:23:3064// TODO(arindam): Remove URI string manipulation by using MapUrlSchemeToProxy.
[email protected]861c6c62009-04-20 16:50:5665std::string FixupProxyHostScheme(ProxyServer::Scheme scheme,
66 std::string host) {
[email protected]e8c50812010-09-28 00:16:1767 if (scheme == ProxyServer::SCHEME_SOCKS5 &&
brettw3a2c6902015-07-06 19:43:2968 base::StartsWith(host, "socks4://",
69 base::CompareCase::INSENSITIVE_ASCII)) {
[email protected]e8c50812010-09-28 00:16:1770 // We default to socks 5, but if the user specifically set it to
71 // socks4://, then use that.
72 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:5673 }
74 // Strip the scheme if any.
75 std::string::size_type colon = host.find("://");
76 if (colon != std::string::npos)
77 host = host.substr(colon + 3);
78 // If a username and perhaps password are specified, give a warning.
79 std::string::size_type at_sign = host.find("@");
80 // Should this be supported?
81 if (at_sign != std::string::npos) {
[email protected]62749f182009-07-15 13:16:5482 // ProxyConfig does not support authentication parameters, but Chrome
83 // will prompt for the password later. Disregard the
84 // authentication parameters and continue with this hostname.
85 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
[email protected]861c6c62009-04-20 16:50:5686 host = host.substr(at_sign + 1);
87 }
88 // If this is a socks proxy, prepend a scheme so as to tell
89 // ProxyServer. This also allows ProxyServer to choose the right
90 // default port.
91 if (scheme == ProxyServer::SCHEME_SOCKS4)
92 host = "socks4://" + host;
93 else if (scheme == ProxyServer::SCHEME_SOCKS5)
94 host = "socks5://" + host;
[email protected]d7395e732009-08-28 23:13:4395 // If there is a trailing slash, remove it so |host| will parse correctly
96 // even if it includes a port number (since the slash is not numeric).
pkasting9022cb42016-02-05 00:08:5697 if (!host.empty() && host.back() == '/')
[email protected]d7395e732009-08-28 23:13:4398 host.resize(host.length() - 1);
[email protected]861c6c62009-04-20 16:50:5699 return host;
100}
101
Ramin Halavatica8d5252018-03-12 05:33:49102ProxyConfigWithAnnotation GetConfigOrDirect(
103 const base::Optional<ProxyConfigWithAnnotation>& optional_config) {
Eric Roman750af4b12018-02-22 22:38:53104 if (optional_config)
105 return optional_config.value();
106
Ramin Halavatica8d5252018-03-12 05:33:49107 ProxyConfigWithAnnotation config = ProxyConfigWithAnnotation::CreateDirect();
Eric Roman750af4b12018-02-22 22:38:53108 return config;
109}
110
[email protected]861c6c62009-04-20 16:50:56111} // namespace
112
Chris Watkins3a13f632017-12-04 00:41:15113ProxyConfigServiceLinux::Delegate::~Delegate() = default;
[email protected]8e1845e12010-09-15 19:22:24114
[email protected]3e44697f2009-05-22 14:37:39115bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme(
thestig0c412e852016-06-30 08:04:40116 base::StringPiece variable,
117 ProxyServer::Scheme scheme,
[email protected]861c6c62009-04-20 16:50:56118 ProxyServer* result_server) {
119 std::string env_value;
thestig0c412e852016-06-30 08:04:40120 if (!env_var_getter_->GetVar(variable, &env_value))
121 return false;
122
123 if (env_value.empty())
124 return false;
125
126 env_value = FixupProxyHostScheme(scheme, env_value);
127 ProxyServer proxy_server =
128 ProxyServer::FromURI(env_value, ProxyServer::SCHEME_HTTP);
129 if (proxy_server.is_valid() && !proxy_server.is_direct()) {
130 *result_server = proxy_server;
131 return true;
[email protected]861c6c62009-04-20 16:50:56132 }
thestig0c412e852016-06-30 08:04:40133 LOG(ERROR) << "Failed to parse environment variable " << variable;
[email protected]861c6c62009-04-20 16:50:56134 return false;
135}
136
[email protected]3e44697f2009-05-22 14:37:39137bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar(
thestig0c412e852016-06-30 08:04:40138 base::StringPiece variable,
139 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:56140 return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP,
141 result_server);
142}
143
Ramin Halavatica8d5252018-03-12 05:33:49144base::Optional<ProxyConfigWithAnnotation>
Eric Roman750af4b12018-02-22 22:38:53145ProxyConfigServiceLinux::Delegate::GetConfigFromEnv() {
Ramin Halavatica8d5252018-03-12 05:33:49146 ProxyConfig config;
Eric Roman750af4b12018-02-22 22:38:53147
[email protected]861c6c62009-04-20 16:50:56148 // Check for automatic configuration first, in
149 // "auto_proxy". Possibly only the "environment_proxy" firefox
150 // extension has ever used this, but it still sounds like a good
151 // idea.
152 std::string auto_proxy;
[email protected]3ba7e082010-08-07 02:57:59153 if (env_var_getter_->GetVar("auto_proxy", &auto_proxy)) {
[email protected]861c6c62009-04-20 16:50:56154 if (auto_proxy.empty()) {
155 // Defined and empty => autodetect
Ramin Halavatica8d5252018-03-12 05:33:49156 config.set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:56157 } else {
158 // specified autoconfig URL
Ramin Halavatica8d5252018-03-12 05:33:49159 config.set_pac_url(GURL(auto_proxy));
[email protected]861c6c62009-04-20 16:50:56160 }
Ramin Halavatica8d5252018-03-12 05:33:49161 return ProxyConfigWithAnnotation(
162 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:56163 }
164 // "all_proxy" is a shortcut to avoid defining {http,https,ftp}_proxy.
165 ProxyServer proxy_server;
166 if (GetProxyFromEnvVar("all_proxy", &proxy_server)) {
Ramin Halavatica8d5252018-03-12 05:33:49167 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
168 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56169 } else {
170 bool have_http = GetProxyFromEnvVar("http_proxy", &proxy_server);
171 if (have_http)
Ramin Halavatica8d5252018-03-12 05:33:49172 config.proxy_rules().proxies_for_http.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56173 // It would be tempting to let http_proxy apply for all protocols
174 // if https_proxy and ftp_proxy are not defined. Googling turns up
175 // several documents that mention only http_proxy. But then the
176 // user really might not want to proxy https. And it doesn't seem
177 // like other apps do this. So we will refrain.
178 bool have_https = GetProxyFromEnvVar("https_proxy", &proxy_server);
179 if (have_https)
Ramin Halavatica8d5252018-03-12 05:33:49180 config.proxy_rules().proxies_for_https.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56181 bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server);
182 if (have_ftp)
Ramin Halavatica8d5252018-03-12 05:33:49183 config.proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56184 if (have_http || have_https || have_ftp) {
185 // mustn't change type unless some rules are actually set.
Ramin Halavatica8d5252018-03-12 05:33:49186 config.proxy_rules().type =
Lily Houghtone6b617e2018-01-19 20:13:07187 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
[email protected]861c6c62009-04-20 16:50:56188 }
189 }
Ramin Halavatica8d5252018-03-12 05:33:49190 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56191 // If the above were not defined, try for socks.
[email protected]e8c50812010-09-28 00:16:17192 // For environment variables, we default to version 5, per the gnome
193 // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html
194 ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5;
[email protected]861c6c62009-04-20 16:50:56195 std::string env_version;
[email protected]3ba7e082010-08-07 02:57:59196 if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version)
[email protected]e8c50812010-09-28 00:16:17197 && env_version == "4")
198 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:56199 if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) {
Ramin Halavatica8d5252018-03-12 05:33:49200 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
201 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56202 }
203 }
204 // Look for the proxy bypass list.
205 std::string no_proxy;
[email protected]3ba7e082010-08-07 02:57:59206 env_var_getter_->GetVar("no_proxy", &no_proxy);
Ramin Halavatica8d5252018-03-12 05:33:49207 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56208 // Having only "no_proxy" set, presumably to "*", makes it
209 // explicit that env vars do specify a configuration: having no
210 // rules specified only means the user explicitly asks for direct
211 // connections.
Ramin Halavatica8d5252018-03-12 05:33:49212 return !no_proxy.empty()
213 ? ProxyConfigWithAnnotation(
214 config, NetworkTrafficAnnotationTag(traffic_annotation_))
215 : base::Optional<ProxyConfigWithAnnotation>();
[email protected]861c6c62009-04-20 16:50:56216 }
[email protected]7541206c2010-02-19 20:24:06217 // Note that this uses "suffix" matching. So a bypass of "google.com"
218 // is understood to mean a bypass of "*google.com".
Shimi Zhang13eace252020-01-31 01:49:19219 config.proxy_rules().bypass_rules.ParseFromString(no_proxy);
220 RewriteRulesForSuffixMatching(&config.proxy_rules().bypass_rules);
221
Ramin Halavatica8d5252018-03-12 05:33:49222 return ProxyConfigWithAnnotation(
223 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:56224}
225
226namespace {
227
[email protected]d7395e732009-08-28 23:13:43228const int kDebounceTimeoutMilliseconds = 250;
[email protected]3e44697f2009-05-22 14:37:39229
[email protected]8c20e3d2011-05-19 21:03:57230#if defined(USE_GIO)
Tim Brown1c307cc2017-12-08 02:40:38231const char kProxyGSettingsSchema[] = "org.gnome.system.proxy";
[email protected]2297bb22014-06-19 06:30:14232
[email protected]8c20e3d2011-05-19 21:03:57233// This setting getter uses gsettings, as used in most GNOME 3 desktops.
234class SettingGetterImplGSettings
235 : public ProxyConfigServiceLinux::SettingGetter {
236 public:
danakj8c3eb802015-09-24 07:53:00237 SettingGetterImplGSettings()
thestig0c412e852016-06-30 08:04:40238 : client_(nullptr),
239 http_client_(nullptr),
240 https_client_(nullptr),
241 ftp_client_(nullptr),
242 socks_client_(nullptr),
243 notify_delegate_(nullptr),
danakj8c3eb802015-09-24 07:53:00244 debounce_timer_(new base::OneShotTimer()) {}
[email protected]8c20e3d2011-05-19 21:03:57245
dcheng67be2b1f2014-10-27 21:47:29246 ~SettingGetterImplGSettings() override {
[email protected]8c20e3d2011-05-19 21:03:57247 // client_ should have been released before now, from
248 // Delegate::OnDestroy(), while running on the UI thread. However
249 // on exiting the process, it may happen that
250 // Delegate::OnDestroy() task is left pending on the glib loop
251 // after the loop was quit, and pending tasks may then be deleted
252 // without being run.
253 if (client_) {
Tim Brown2a19f3b2017-12-12 01:08:40254 // gsettings client was not cleaned up.
eroman0070d412017-06-22 22:18:24255 if (task_runner_->RunsTasksInCurrentSequence()) {
Mostyn Bramley-Moore699c5312018-05-01 10:48:09256 // We are on the UI thread so we can clean it safely.
[email protected]8c20e3d2011-05-19 21:03:57257 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
258 ShutDown();
259 } else {
260 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
thestig0c412e852016-06-30 08:04:40261 client_ = nullptr;
[email protected]8c20e3d2011-05-19 21:03:57262 }
263 }
264 DCHECK(!client_);
[email protected]8c20e3d2011-05-19 21:03:57265 }
266
Tim Brown1c307cc2017-12-08 02:40:38267 // CheckVersion() must be called *before* Init()!
268 bool CheckVersion(base::Environment* env);
[email protected]8c20e3d2011-05-19 21:03:57269
eroman0070d412017-06-22 22:18:24270 bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner)
mostynbba063d6032014-10-09 11:01:13271 override {
eroman0070d412017-06-22 22:18:24272 DCHECK(glib_task_runner->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57273 DCHECK(!client_);
[email protected]90499482013-06-01 00:39:50274 DCHECK(!task_runner_.get());
[email protected]4cf80f0b2011-05-20 20:30:26275
Tim Brown1c307cc2017-12-08 02:40:38276 if (!g_settings_schema_source_lookup(g_settings_schema_source_get_default(),
277 kProxyGSettingsSchema, FALSE) ||
278 !(client_ = g_settings_new(kProxyGSettingsSchema))) {
[email protected]8c20e3d2011-05-19 21:03:57279 // It's not clear whether/when this can return NULL.
280 LOG(ERROR) << "Unable to create a gsettings client";
281 return false;
282 }
sergeyu3f923062014-09-05 01:39:40283 task_runner_ = glib_task_runner;
[email protected]8c20e3d2011-05-19 21:03:57284 // We assume these all work if the above call worked.
Tim Brown1c307cc2017-12-08 02:40:38285 http_client_ = g_settings_get_child(client_, "http");
286 https_client_ = g_settings_get_child(client_, "https");
287 ftp_client_ = g_settings_get_child(client_, "ftp");
288 socks_client_ = g_settings_get_child(client_, "socks");
[email protected]8c20e3d2011-05-19 21:03:57289 DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_);
290 return true;
291 }
292
dcheng67be2b1f2014-10-27 21:47:29293 void ShutDown() override {
[email protected]8c20e3d2011-05-19 21:03:57294 if (client_) {
eroman0070d412017-06-22 22:18:24295 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57296 // This also disables gsettings notifications.
297 g_object_unref(socks_client_);
298 g_object_unref(ftp_client_);
299 g_object_unref(https_client_);
300 g_object_unref(http_client_);
301 g_object_unref(client_);
302 // We only need to null client_ because it's the only one that we check.
thestig0c412e852016-06-30 08:04:40303 client_ = nullptr;
304 task_runner_ = nullptr;
[email protected]8c20e3d2011-05-19 21:03:57305 }
marshall8e5fe942015-03-06 19:22:40306 debounce_timer_.reset();
[email protected]8c20e3d2011-05-19 21:03:57307 }
308
dcheng67be2b1f2014-10-27 21:47:29309 bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13310 ProxyConfigServiceLinux::Delegate* delegate) override {
[email protected]8c20e3d2011-05-19 21:03:57311 DCHECK(client_);
eroman0070d412017-06-22 22:18:24312 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57313 notify_delegate_ = delegate;
314 // We could watch for the change-event signal instead of changed, but
315 // since we have to watch more than one object, we'd still have to
316 // debounce change notifications. This is conceptually simpler.
317 g_signal_connect(G_OBJECT(client_), "changed",
318 G_CALLBACK(OnGSettingsChangeNotification), this);
319 g_signal_connect(G_OBJECT(http_client_), "changed",
320 G_CALLBACK(OnGSettingsChangeNotification), this);
321 g_signal_connect(G_OBJECT(https_client_), "changed",
322 G_CALLBACK(OnGSettingsChangeNotification), this);
323 g_signal_connect(G_OBJECT(ftp_client_), "changed",
324 G_CALLBACK(OnGSettingsChangeNotification), this);
325 g_signal_connect(G_OBJECT(socks_client_), "changed",
326 G_CALLBACK(OnGSettingsChangeNotification), this);
327 // Simulate a change to avoid possibly losing updates before this point.
328 OnChangeNotification();
329 return true;
330 }
331
eroman0070d412017-06-22 22:18:24332 const scoped_refptr<base::SequencedTaskRunner>& GetNotificationTaskRunner()
dcheng67be2b1f2014-10-27 21:47:29333 override {
sergeyu3f923062014-09-05 01:39:40334 return task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57335 }
336
dcheng67be2b1f2014-10-27 21:47:29337 bool GetString(StringSetting key, std::string* result) override {
[email protected]8c20e3d2011-05-19 21:03:57338 DCHECK(client_);
339 switch (key) {
340 case PROXY_MODE:
341 return GetStringByPath(client_, "mode", result);
342 case PROXY_AUTOCONF_URL:
343 return GetStringByPath(client_, "autoconfig-url", result);
344 case PROXY_HTTP_HOST:
345 return GetStringByPath(http_client_, "host", result);
346 case PROXY_HTTPS_HOST:
347 return GetStringByPath(https_client_, "host", result);
348 case PROXY_FTP_HOST:
349 return GetStringByPath(ftp_client_, "host", result);
350 case PROXY_SOCKS_HOST:
351 return GetStringByPath(socks_client_, "host", result);
[email protected]8c20e3d2011-05-19 21:03:57352 }
[email protected]6b5fe742011-05-20 21:46:48353 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57354 }
dcheng67be2b1f2014-10-27 21:47:29355 bool GetBool(BoolSetting key, bool* result) override {
[email protected]8c20e3d2011-05-19 21:03:57356 DCHECK(client_);
357 switch (key) {
358 case PROXY_USE_HTTP_PROXY:
359 // Although there is an "enabled" boolean in http_client_, it is not set
360 // to true by the proxy config utility. We ignore it and return false.
361 return false;
362 case PROXY_USE_SAME_PROXY:
363 // Similarly, although there is a "use-same-proxy" boolean in client_,
364 // it is never set to false by the proxy config utility. We ignore it.
365 return false;
366 case PROXY_USE_AUTHENTICATION:
367 // There is also no way to set this in the proxy config utility, but it
368 // doesn't hurt us to get the actual setting (unlike the two above).
369 return GetBoolByPath(http_client_, "use-authentication", result);
[email protected]8c20e3d2011-05-19 21:03:57370 }
[email protected]6b5fe742011-05-20 21:46:48371 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57372 }
dcheng67be2b1f2014-10-27 21:47:29373 bool GetInt(IntSetting key, int* result) override {
[email protected]8c20e3d2011-05-19 21:03:57374 DCHECK(client_);
375 switch (key) {
376 case PROXY_HTTP_PORT:
377 return GetIntByPath(http_client_, "port", result);
378 case PROXY_HTTPS_PORT:
379 return GetIntByPath(https_client_, "port", result);
380 case PROXY_FTP_PORT:
381 return GetIntByPath(ftp_client_, "port", result);
382 case PROXY_SOCKS_PORT:
383 return GetIntByPath(socks_client_, "port", result);
[email protected]8c20e3d2011-05-19 21:03:57384 }
[email protected]6b5fe742011-05-20 21:46:48385 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57386 }
dcheng67be2b1f2014-10-27 21:47:29387 bool GetStringList(StringListSetting key,
388 std::vector<std::string>* result) override {
[email protected]8c20e3d2011-05-19 21:03:57389 DCHECK(client_);
390 switch (key) {
391 case PROXY_IGNORE_HOSTS:
392 return GetStringListByPath(client_, "ignore-hosts", result);
[email protected]8c20e3d2011-05-19 21:03:57393 }
[email protected]6b5fe742011-05-20 21:46:48394 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57395 }
396
dcheng67be2b1f2014-10-27 21:47:29397 bool BypassListIsReversed() override {
[email protected]8c20e3d2011-05-19 21:03:57398 // This is a KDE-specific setting.
399 return false;
400 }
401
Shimi Zhang13eace252020-01-31 01:49:19402 bool UseSuffixMatching() override { return false; }
[email protected]8c20e3d2011-05-19 21:03:57403
404 private:
thestig0c412e852016-06-30 08:04:40405 bool GetStringByPath(GSettings* client,
406 base::StringPiece key,
[email protected]8c20e3d2011-05-19 21:03:57407 std::string* result) {
eroman0070d412017-06-22 22:18:24408 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38409 gchar* value = g_settings_get_string(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57410 if (!value)
411 return false;
412 *result = value;
413 g_free(value);
414 return true;
415 }
thestig0c412e852016-06-30 08:04:40416 bool GetBoolByPath(GSettings* client, base::StringPiece key, bool* result) {
eroman0070d412017-06-22 22:18:24417 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38418 *result = static_cast<bool>(g_settings_get_boolean(client, key.data()));
[email protected]8c20e3d2011-05-19 21:03:57419 return true;
420 }
thestig0c412e852016-06-30 08:04:40421 bool GetIntByPath(GSettings* client, base::StringPiece key, int* result) {
eroman0070d412017-06-22 22:18:24422 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38423 *result = g_settings_get_int(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57424 return true;
425 }
thestig0c412e852016-06-30 08:04:40426 bool GetStringListByPath(GSettings* client,
427 base::StringPiece key,
[email protected]8c20e3d2011-05-19 21:03:57428 std::vector<std::string>* result) {
eroman0070d412017-06-22 22:18:24429 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38430 gchar** list = g_settings_get_strv(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57431 if (!list)
432 return false;
433 for (size_t i = 0; list[i]; ++i) {
434 result->push_back(static_cast<char*>(list[i]));
435 g_free(list[i]);
436 }
437 g_free(list);
438 return true;
439 }
440
441 // This is the callback from the debounce timer.
442 void OnDebouncedNotification() {
eroman0070d412017-06-22 22:18:24443 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57444 CHECK(notify_delegate_);
445 // Forward to a method on the proxy config service delegate object.
446 notify_delegate_->OnCheckProxyConfigSettings();
447 }
448
449 void OnChangeNotification() {
450 // We don't use Reset() because the timer may not yet be running.
451 // (In that case Stop() is a no-op.)
marshall8e5fe942015-03-06 19:22:40452 debounce_timer_->Stop();
453 debounce_timer_->Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57454 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
455 this, &SettingGetterImplGSettings::OnDebouncedNotification);
456 }
457
458 // gsettings notification callback, dispatched on the default glib main loop.
459 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
460 gpointer user_data) {
461 VLOG(1) << "gsettings change notification for key " << key;
462 // We don't track which key has changed, just that something did change.
463 SettingGetterImplGSettings* setting_getter =
464 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
465 setting_getter->OnChangeNotification();
466 }
467
468 GSettings* client_;
469 GSettings* http_client_;
470 GSettings* https_client_;
471 GSettings* ftp_client_;
472 GSettings* socks_client_;
473 ProxyConfigServiceLinux::Delegate* notify_delegate_;
danakj8a98ca22016-04-16 02:47:36474 std::unique_ptr<base::OneShotTimer> debounce_timer_;
[email protected]8c20e3d2011-05-19 21:03:57475
[email protected]76722472012-05-24 08:26:46476 // Task runner for the thread that we make gsettings calls on. It should
[email protected]8c20e3d2011-05-19 21:03:57477 // be the UI thread and all our methods should be called on this
478 // thread. Only for assertions.
eroman0070d412017-06-22 22:18:24479 scoped_refptr<base::SequencedTaskRunner> task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57480
481 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGSettings);
482};
483
Tim Brown1c307cc2017-12-08 02:40:38484bool SettingGetterImplGSettings::CheckVersion(
[email protected]8c20e3d2011-05-19 21:03:57485 base::Environment* env) {
Tim Brown1c307cc2017-12-08 02:40:38486 // CheckVersion() must be called *before* Init()!
[email protected]8c20e3d2011-05-19 21:03:57487 DCHECK(!client_);
488
thestig0c412e852016-06-30 08:04:40489 GSettings* client = nullptr;
Tim Brown1c307cc2017-12-08 02:40:38490 if (g_settings_schema_source_lookup(g_settings_schema_source_get_default(),
491 kProxyGSettingsSchema, FALSE)) {
492 client = g_settings_new(kProxyGSettingsSchema);
[email protected]4bbb72d2014-06-06 18:05:51493 }
494 if (!client) {
Tim Brown2a19f3b2017-12-12 01:08:40495 VLOG(1) << "Cannot create gsettings client.";
[email protected]8c20e3d2011-05-19 21:03:57496 return false;
497 }
498 g_object_unref(client);
499
[email protected]8c20e3d2011-05-19 21:03:57500 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
501 return true;
502}
503#endif // defined(USE_GIO)
504
eromane44498c2017-06-30 00:02:37505// Converts |value| from a decimal string to an int. If there was a failure
506// parsing, returns |default_value|.
507int StringToIntOrDefault(base::StringPiece value, int default_value) {
508 int result;
509 if (base::StringToInt(value, &result))
510 return result;
511 return default_value;
512}
513
Tim Brown2a19f3b2017-12-12 01:08:40514// This is the KDE version that reads kioslaverc and simulates gsettings.
[email protected]d7395e732009-08-28 23:13:43515// Doing this allows the main Delegate code, as well as the unit tests
516// for it, to stay the same - and the settings map fairly well besides.
gabf4f904e2017-05-10 20:55:02517class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter {
[email protected]d7395e732009-08-28 23:13:43518 public:
[email protected]573c0502011-05-17 22:19:50519 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
marshall8e5fe942015-03-06 19:22:40520 : inotify_fd_(-1),
thestig0c412e852016-06-30 08:04:40521 notify_delegate_(nullptr),
danakj8c3eb802015-09-24 07:53:00522 debounce_timer_(new base::OneShotTimer()),
marshall8e5fe942015-03-06 19:22:40523 indirect_manual_(false),
524 auto_no_pac_(false),
525 reversed_bypass_list_(false),
526 env_var_getter_(env_var_getter),
thestig0c412e852016-06-30 08:04:40527 file_task_runner_(nullptr) {
[email protected]9a8c4022011-01-25 14:25:33528 // This has to be called on the UI thread (http://crbug.com/69057).
529 base::ThreadRestrictions::ScopedAllowIO allow_io;
530
[email protected]f18fde22010-05-18 23:49:54531 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59532 std::string home;
[email protected]3ba7e082010-08-07 02:57:59533 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24534 // $KDEHOME is set. Use it unconditionally.
[email protected]6cdfd7f2013-02-08 20:40:15535 kde_config_dir_ = KDEHomeToConfigPath(base::FilePath(home));
[email protected]92d2dc82010-04-08 17:49:59536 } else {
[email protected]2e8cfe22010-06-12 00:26:24537 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59538 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59539 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43540 // User has no $HOME? Give up. Later we'll report the failure.
541 return;
[email protected]6b0349ef2010-10-16 04:56:06542 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
543 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59544 // KDE3 always uses .kde for its configuration.
[email protected]6cdfd7f2013-02-08 20:40:15545 base::FilePath kde_path = base::FilePath(home).Append(".kde");
[email protected]92d2dc82010-04-08 17:49:59546 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
edward.baker53bec302015-10-02 16:57:49547 } else if (base::nix::GetDesktopEnvironment(env_var_getter) ==
548 base::nix::DESKTOP_ENVIRONMENT_KDE4) {
[email protected]92d2dc82010-04-08 17:49:59549 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53550 // both can be installed side-by-side. Sadly they don't all do this, and
551 // they don't always do this: some distributions have started switching
552 // back as well. So if there is a .kde4 directory, check the timestamps
553 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59554 // Note that we should currently be running in the UI thread, because in
Tim Brown2a19f3b2017-12-12 01:08:40555 // the gsettings version, that is the only thread that can access the
556 // proxy settings (a gsettings restriction). As noted below, the initial
557 // read of the proxy settings will be done in this thread anyway, so we
558 // check for .kde4 here in this thread as well.
[email protected]6cdfd7f2013-02-08 20:40:15559 base::FilePath kde3_path = base::FilePath(home).Append(".kde");
560 base::FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
561 base::FilePath kde4_path = base::FilePath(home).Append(".kde4");
562 base::FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
[email protected]fad9c8a52010-06-10 22:30:53563 bool use_kde4 = false;
[email protected]dcd16612013-07-15 20:18:09564 if (base::DirectoryExists(kde4_path)) {
[email protected]54124ed02014-01-07 10:06:58565 base::File::Info kde3_info;
566 base::File::Info kde4_info;
[email protected]9eae4e62013-12-04 20:56:49567 if (base::GetFileInfo(kde4_config, &kde4_info)) {
568 if (base::GetFileInfo(kde3_config, &kde3_info)) {
[email protected]fad9c8a52010-06-10 22:30:53569 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
570 } else {
571 use_kde4 = true;
572 }
573 }
574 }
575 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59576 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
577 } else {
[email protected]fad9c8a52010-06-10 22:30:53578 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59579 }
edward.baker53bec302015-10-02 16:57:49580 } else {
581 // KDE 5 migrated to ~/.config for storing kioslaverc.
582 kde_config_dir_ = base::FilePath(home).Append(".config");
[email protected]92d2dc82010-04-08 17:49:59583 }
[email protected]d7395e732009-08-28 23:13:43584 }
[email protected]d7395e732009-08-28 23:13:43585 }
586
dcheng67be2b1f2014-10-27 21:47:29587 ~SettingGetterImplKDE() override {
[email protected]d7395e732009-08-28 23:13:43588 // inotify_fd_ should have been closed before now, from
589 // Delegate::OnDestroy(), while running on the file thread. However
590 // on exiting the process, it may happen that Delegate::OnDestroy()
591 // task is left pending on the file loop after the loop was quit,
592 // and pending tasks may then be deleted without being run.
593 // Here in the KDE version, we can safely close the file descriptor
594 // anyway. (Not that it really matters; the process is exiting.)
595 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20596 ShutDown();
thestig0c412e852016-06-30 08:04:40597 DCHECK_LT(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:43598 }
599
eroman0070d412017-06-22 22:18:24600 bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner)
mostynbba063d6032014-10-09 11:01:13601 override {
[email protected]9a8c4022011-01-25 14:25:33602 // This has to be called on the UI thread (http://crbug.com/69057).
603 base::ThreadRestrictions::ScopedAllowIO allow_io;
thestig0c412e852016-06-30 08:04:40604 DCHECK_LT(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:43605 inotify_fd_ = inotify_init();
606 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40607 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43608 return false;
609 }
tfarina89b4ae1c2015-12-16 18:59:18610 if (!base::SetNonBlocking(inotify_fd_)) {
611 PLOG(ERROR) << "base::SetNonBlocking failed";
[email protected]d7395e732009-08-28 23:13:43612 close(inotify_fd_);
613 inotify_fd_ = -1;
614 return false;
615 }
eroman0070d412017-06-22 22:18:24616
Sami Kyostila0b4314e2019-07-31 20:47:17617 constexpr base::TaskTraits kTraits = {
618 base::ThreadPool(), base::TaskPriority::USER_VISIBLE, base::MayBlock()};
619 file_task_runner_ = base::CreateSequencedTaskRunner(kTraits);
eroman0070d412017-06-22 22:18:24620
sergeyu3f923062014-09-05 01:39:40621 // The initial read is done on the current thread, not
622 // |file_task_runner_|, since we will need to have it for
623 // SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:43624 UpdateCachedSettings();
625 return true;
626 }
627
dcheng67be2b1f2014-10-27 21:47:29628 void ShutDown() override {
[email protected]d7395e732009-08-28 23:13:43629 if (inotify_fd_ >= 0) {
630 ResetCachedSettings();
gabf4f904e2017-05-10 20:55:02631 inotify_watcher_.reset();
[email protected]d7395e732009-08-28 23:13:43632 close(inotify_fd_);
633 inotify_fd_ = -1;
634 }
marshall8e5fe942015-03-06 19:22:40635 debounce_timer_.reset();
[email protected]d7395e732009-08-28 23:13:43636 }
637
dcheng67be2b1f2014-10-27 21:47:29638 bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13639 ProxyConfigServiceLinux::Delegate* delegate) override {
thestig0c412e852016-06-30 08:04:40640 DCHECK_GE(inotify_fd_, 0);
eroman0070d412017-06-22 22:18:24641 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]d7395e732009-08-28 23:13:43642 // We can't just watch the kioslaverc file directly, since KDE will write
643 // a new copy of it and then rename it whenever settings are changed and
644 // inotify watches inodes (so we'll be watching the old deleted file after
645 // the first change, and it will never change again). So, we watch the
646 // directory instead. We then act only on changes to the kioslaverc entry.
eroman6b0ca662017-06-22 00:16:36647 // TODO(eroman): What if the file is deleted? (handle with IN_DELETE).
[email protected]d7395e732009-08-28 23:13:43648 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
sergeyu3f923062014-09-05 01:39:40649 IN_MODIFY | IN_MOVED_TO) < 0) {
[email protected]d7395e732009-08-28 23:13:43650 return false;
sergeyu3f923062014-09-05 01:39:40651 }
[email protected]d7395e732009-08-28 23:13:43652 notify_delegate_ = delegate;
gabf4f904e2017-05-10 20:55:02653 inotify_watcher_ = base::FileDescriptorWatcher::WatchReadable(
654 inotify_fd_, base::Bind(&SettingGetterImplKDE::OnChangeNotification,
655 base::Unretained(this)));
[email protected]d3066142011-05-10 02:36:20656 // Simulate a change to avoid possibly losing updates before this point.
657 OnChangeNotification();
658 return true;
[email protected]d7395e732009-08-28 23:13:43659 }
660
eroman0070d412017-06-22 22:18:24661 const scoped_refptr<base::SequencedTaskRunner>& GetNotificationTaskRunner()
dcheng67be2b1f2014-10-27 21:47:29662 override {
sergeyu3f923062014-09-05 01:39:40663 return file_task_runner_;
[email protected]d7395e732009-08-28 23:13:43664 }
665
dcheng67be2b1f2014-10-27 21:47:29666 bool GetString(StringSetting key, std::string* result) override {
jdoerrie22a91d8b92018-10-05 08:43:26667 auto it = string_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43668 if (it == string_table_.end())
669 return false;
670 *result = it->second;
671 return true;
672 }
dcheng67be2b1f2014-10-27 21:47:29673 bool GetBool(BoolSetting key, bool* result) override {
[email protected]d7395e732009-08-28 23:13:43674 // We don't ever have any booleans.
675 return false;
676 }
dcheng67be2b1f2014-10-27 21:47:29677 bool GetInt(IntSetting key, int* result) override {
[email protected]d7395e732009-08-28 23:13:43678 // We don't ever have any integers. (See AddProxy() below about ports.)
679 return false;
680 }
dcheng67be2b1f2014-10-27 21:47:29681 bool GetStringList(StringListSetting key,
682 std::vector<std::string>* result) override {
jdoerrie22a91d8b92018-10-05 08:43:26683 auto it = strings_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43684 if (it == strings_table_.end())
685 return false;
686 *result = it->second;
687 return true;
688 }
689
dcheng67be2b1f2014-10-27 21:47:29690 bool BypassListIsReversed() override { return reversed_bypass_list_; }
[email protected]a48bf4a2010-06-14 18:24:53691
Shimi Zhang13eace252020-01-31 01:49:19692 bool UseSuffixMatching() override { return true; }
[email protected]1a597192010-07-09 16:58:38693
[email protected]d7395e732009-08-28 23:13:43694 private:
695 void ResetCachedSettings() {
696 string_table_.clear();
697 strings_table_.clear();
698 indirect_manual_ = false;
699 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:53700 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:43701 }
702
[email protected]6cdfd7f2013-02-08 20:40:15703 base::FilePath KDEHomeToConfigPath(const base::FilePath& kde_home) {
[email protected]92d2dc82010-04-08 17:49:59704 return kde_home.Append("share").Append("config");
705 }
706
[email protected]6b5fe742011-05-20 21:46:48707 void AddProxy(StringSetting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:43708 if (value.empty() || value.substr(0, 3) == "//:")
709 // No proxy.
710 return;
[email protected]4b90c202012-04-24 23:27:55711 size_t space = value.find(' ');
712 if (space != std::string::npos) {
713 // Newer versions of KDE use a space rather than a colon to separate the
714 // port number from the hostname. If we find this, we need to convert it.
715 std::string fixed = value;
716 fixed[space] = ':';
717 string_table_[host_key] = fixed;
718 } else {
719 // We don't need to parse the port number out; GetProxyFromSettings()
720 // would only append it right back again. So we just leave the port
721 // number right in the host string.
722 string_table_[host_key] = value;
723 }
[email protected]d7395e732009-08-28 23:13:43724 }
725
[email protected]6b5fe742011-05-20 21:46:48726 void AddHostList(StringListSetting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:54727 std::vector<std::string> tokens;
[email protected]f4ebe772013-02-02 00:21:39728 base::StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:54729 while (tk.GetNext()) {
730 std::string token = tk.token();
731 if (!token.empty())
732 tokens.push_back(token);
733 }
734 strings_table_[key] = tokens;
735 }
736
[email protected]9a3d8d42009-09-03 17:01:46737 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:43738 if (key == "ProxyType") {
739 const char* mode = "none";
740 indirect_manual_ = false;
741 auto_no_pac_ = false;
eromane44498c2017-06-30 00:02:37742 int int_value = StringToIntOrDefault(value, 0);
[email protected]e83326f2010-07-31 17:29:25743 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:43744 case 1: // Manual configuration.
745 mode = "manual";
746 break;
747 case 2: // PAC URL.
748 mode = "auto";
749 break;
750 case 3: // WPAD.
751 mode = "auto";
752 auto_no_pac_ = true;
753 break;
754 case 4: // Indirect manual via environment variables.
755 mode = "manual";
756 indirect_manual_ = true;
757 break;
eromane44498c2017-06-30 00:02:37758 default: // No proxy, or maybe kioslaverc syntax error.
759 break;
[email protected]d7395e732009-08-28 23:13:43760 }
[email protected]573c0502011-05-17 22:19:50761 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:43762 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:50763 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:43764 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:50765 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:43766 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:50767 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:43768 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:50769 AddProxy(PROXY_FTP_HOST, value);
[email protected]bfeb7232012-06-08 00:58:37770 } else if (key == "socksProxy") {
771 // Older versions of KDE configure SOCKS in a weird way involving
772 // LD_PRELOAD and a library that intercepts network calls to SOCKSify
773 // them. We don't support it. KDE 4.8 added a proper SOCKS setting.
774 AddProxy(PROXY_SOCKS_HOST, value);
[email protected]d7395e732009-08-28 23:13:43775 } else if (key == "ReversedException") {
776 // We count "true" or any nonzero number as true, otherwise false.
eromane44498c2017-06-30 00:02:37777 // A failure parsing the integer will also mean false.
778 reversed_bypass_list_ =
779 (value == "true" || StringToIntOrDefault(value, 0) != 0);
[email protected]d7395e732009-08-28 23:13:43780 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:50781 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:43782 } else if (key == "AuthMode") {
783 // Check for authentication, just so we can warn.
eromane44498c2017-06-30 00:02:37784 int mode = StringToIntOrDefault(value, 0);
[email protected]d7395e732009-08-28 23:13:43785 if (mode) {
786 // ProxyConfig does not support authentication parameters, but
787 // Chrome will prompt for the password later. So we ignore this.
788 LOG(WARNING) <<
789 "Proxy authentication parameters ignored, see bug 16709";
790 }
791 }
792 }
793
[email protected]6b5fe742011-05-20 21:46:48794 void ResolveIndirect(StringSetting key) {
jdoerrie22a91d8b92018-10-05 08:43:26795 auto it = string_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43796 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:54797 std::string value;
[email protected]3ba7e082010-08-07 02:57:59798 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:43799 it->second = value;
[email protected]8425adc02010-04-18 17:45:31800 else
801 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:43802 }
803 }
804
[email protected]6b5fe742011-05-20 21:46:48805 void ResolveIndirectList(StringListSetting key) {
jdoerrie22a91d8b92018-10-05 08:43:26806 auto it = strings_table_.find(key);
[email protected]f18fde22010-05-18 23:49:54807 if (it != strings_table_.end()) {
808 std::string value;
809 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:59810 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:54811 AddHostList(key, value);
812 else
813 strings_table_.erase(it);
814 }
815 }
816
[email protected]d7395e732009-08-28 23:13:43817 // The settings in kioslaverc could occur in any order, but some affect
818 // others. Rather than read the whole file in and then query them in an
819 // order that allows us to handle that, we read the settings in whatever
820 // order they occur and do any necessary tweaking after we finish.
821 void ResolveModeEffects() {
822 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:50823 ResolveIndirect(PROXY_HTTP_HOST);
824 ResolveIndirect(PROXY_HTTPS_HOST);
825 ResolveIndirect(PROXY_FTP_HOST);
826 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:43827 }
828 if (auto_no_pac_) {
829 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:50830 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:43831 }
[email protected]d7395e732009-08-28 23:13:43832 }
833
834 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
835 // each relevant name-value pair to the appropriate value table.
836 void UpdateCachedSettings() {
[email protected]6cdfd7f2013-02-08 20:40:15837 base::FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]b9b4a572014-03-17 23:11:12838 base::ScopedFILE input(base::OpenFile(kioslaverc, "r"));
[email protected]d7395e732009-08-28 23:13:43839 if (!input.get())
840 return;
841 ResetCachedSettings();
842 bool in_proxy_settings = false;
843 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:46844 char line[BUFFER_SIZE];
845 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:43846 while (fgets(line, sizeof(line), input.get())) {
847 // fgets() guarantees the line will be properly terminated.
848 size_t length = strlen(line);
849 if (!length)
850 continue;
851 // This should be true even with CRLF endings.
852 if (line[length - 1] != '\n') {
853 line_too_long = true;
854 continue;
855 }
856 if (line_too_long) {
857 // The previous line had no line ending, but this done does. This is
858 // the end of the line that was too long, so warn here and skip it.
859 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
860 line_too_long = false;
861 continue;
862 }
863 // Remove the LF at the end, and the CR if there is one.
864 line[--length] = '\0';
865 if (length && line[length - 1] == '\r')
866 line[--length] = '\0';
867 // Now parse the line.
868 if (line[0] == '[') {
869 // Switching sections. All we care about is whether this is
870 // the (a?) proxy settings section, for both KDE3 and KDE4.
871 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
872 } else if (in_proxy_settings) {
873 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:46874 char* split = strchr(line, '=');
875 // Skip this line if it does not contain an = sign.
876 if (!split)
[email protected]d7395e732009-08-28 23:13:43877 continue;
[email protected]9a3d8d42009-09-03 17:01:46878 // Split the line on the = and advance |split|.
879 *(split++) = 0;
880 std::string key = line;
881 std::string value = split;
[email protected]8af69c6c2014-03-03 19:05:31882 base::TrimWhitespaceASCII(key, base::TRIM_ALL, &key);
883 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value);
[email protected]9a3d8d42009-09-03 17:01:46884 // Skip this line if the key name is empty.
885 if (key.empty())
[email protected]d7395e732009-08-28 23:13:43886 continue;
887 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:46888 if (key[key.length() - 1] == ']') {
889 // Find the matching bracket.
890 length = key.rfind('[');
891 // Skip this line if the localization indicator is malformed.
892 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:43893 continue;
894 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:46895 key.resize(length);
896 // Remove any resulting trailing whitespace.
[email protected]8af69c6c2014-03-03 19:05:31897 base::TrimWhitespaceASCII(key, base::TRIM_TRAILING, &key);
[email protected]9a3d8d42009-09-03 17:01:46898 // Skip this line if the key name is now empty.
899 if (key.empty())
900 continue;
[email protected]d7395e732009-08-28 23:13:43901 }
[email protected]d7395e732009-08-28 23:13:43902 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:46903 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:43904 }
905 }
906 if (ferror(input.get()))
907 LOG(ERROR) << "error reading " << kioslaverc.value();
908 ResolveModeEffects();
909 }
910
911 // This is the callback from the debounce timer.
912 void OnDebouncedNotification() {
eroman0070d412017-06-22 22:18:24913 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]b30a3f52010-10-16 01:05:46914 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:43915 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:14916 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43917 // Forward to a method on the proxy config service delegate object.
918 notify_delegate_->OnCheckProxyConfigSettings();
919 }
920
921 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
922 // from the inotify file descriptor and starts up a debounce timer if
923 // an event for kioslaverc is seen.
924 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:04925 DCHECK_GE(inotify_fd_, 0);
eroman0070d412017-06-22 22:18:24926 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]d7395e732009-08-28 23:13:43927 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
928 bool kioslaverc_touched = false;
929 ssize_t r;
930 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
931 // inotify returns variable-length structures, which is why we have
932 // this strange-looking loop instead of iterating through an array.
933 char* event_ptr = event_buf;
934 while (event_ptr < event_buf + r) {
935 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
936 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:33937 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
938 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:43939 if (!strcmp(event->name, "kioslaverc"))
940 kioslaverc_touched = true;
941 // Advance the pointer just past the end of the filename.
942 event_ptr = event->name + event->len;
943 }
944 // We keep reading even if |kioslaverc_touched| is true to drain the
945 // inotify event queue.
946 }
947 if (!r)
948 // Instead of returning -1 and setting errno to EINVAL if there is not
949 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
950 // new behavior (EINVAL) so we can reuse the code below.
951 errno = EINVAL;
952 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:40953 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:43954 if (errno == EINVAL) {
955 // Our buffer is not large enough to read the next event. This should
956 // not happen (because its size is calculated to always be sufficiently
957 // large), but if it does we'd warn continuously since |inotify_fd_|
958 // would be forever ready to read. Close it and stop watching instead.
959 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
gabf4f904e2017-05-10 20:55:02960 inotify_watcher_.reset();
[email protected]d7395e732009-08-28 23:13:43961 close(inotify_fd_);
962 inotify_fd_ = -1;
963 }
964 }
965 if (kioslaverc_touched) {
eroman6b0ca662017-06-22 00:16:36966 LOG(ERROR) << "kioslaverc_touched";
[email protected]d7395e732009-08-28 23:13:43967 // We don't use Reset() because the timer may not yet be running.
968 // (In that case Stop() is a no-op.)
marshall8e5fe942015-03-06 19:22:40969 debounce_timer_->Stop();
970 debounce_timer_->Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:43971 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:50972 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43973 }
974 }
975
[email protected]6b5fe742011-05-20 21:46:48976 typedef std::map<StringSetting, std::string> string_map_type;
977 typedef std::map<StringListSetting,
978 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:43979
980 int inotify_fd_;
gabf4f904e2017-05-10 20:55:02981 std::unique_ptr<base::FileDescriptorWatcher::Controller> inotify_watcher_;
[email protected]d7395e732009-08-28 23:13:43982 ProxyConfigServiceLinux::Delegate* notify_delegate_;
danakj8a98ca22016-04-16 02:47:36983 std::unique_ptr<base::OneShotTimer> debounce_timer_;
[email protected]6cdfd7f2013-02-08 20:40:15984 base::FilePath kde_config_dir_;
[email protected]d7395e732009-08-28 23:13:43985 bool indirect_manual_;
986 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:53987 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:54988 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
989 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
990 // same lifetime.
[email protected]76b90d312010-08-03 03:00:50991 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:43992
993 // We cache these settings whenever we re-read the kioslaverc file.
994 string_map_type string_table_;
995 strings_map_type strings_table_;
996
eroman0070d412017-06-22 22:18:24997 // Task runner for doing blocking file IO on, as well as handling inotify
998 // events on.
999 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
[email protected]d7395e732009-08-28 23:13:431000
[email protected]573c0502011-05-17 22:19:501001 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE);
[email protected]861c6c62009-04-20 16:50:561002};
1003
1004} // namespace
1005
[email protected]573c0502011-05-17 22:19:501006bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481007 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501008 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561009 std::string host;
[email protected]573c0502011-05-17 22:19:501010 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561011 // Unset or empty.
1012 return false;
1013 }
1014 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431015 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481016 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501017 SettingGetter::HostSettingToPortSetting(host_key);
1018 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561019 if (port != 0) {
1020 // If a port is set and non-zero:
Raul Tambre8c1981d2019-02-08 02:22:261021 host += ":" + base::NumberToString(port);
[email protected]861c6c62009-04-20 16:50:561022 }
[email protected]76960f3d2011-04-30 02:15:231023
Tim Brown2a19f3b2017-12-12 01:08:401024 // gsettings settings do not appear to distinguish between SOCKS version. We
[email protected]573c0502011-05-17 22:19:501025 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231026 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501027 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1028 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1029 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301030 ProxyServer proxy_server = ProxyServer::FromURI(host,
1031 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561032 if (proxy_server.is_valid()) {
1033 *result_server = proxy_server;
1034 return true;
1035 }
1036 return false;
1037}
1038
Ramin Halavatica8d5252018-03-12 05:33:491039base::Optional<ProxyConfigWithAnnotation>
Eric Roman750af4b12018-02-22 22:38:531040ProxyConfigServiceLinux::Delegate::GetConfigFromSettings() {
Ramin Halavatica8d5252018-03-12 05:33:491041 ProxyConfig config;
Eric Roman750af4b12018-02-22 22:38:531042
[email protected]861c6c62009-04-20 16:50:561043 std::string mode;
[email protected]573c0502011-05-17 22:19:501044 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
Tim Brown2a19f3b2017-12-12 01:08:401045 // We expect this to always be set, so if we don't see it then we probably
1046 // have a gsettings problem, and so we don't have a valid proxy config.
Eric Romanc0553ba2018-05-21 19:28:571047 return base::nullopt;
[email protected]861c6c62009-04-20 16:50:561048 }
[email protected]3e44697f2009-05-22 14:37:391049 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561050 // Specifically specifies no proxy.
Ramin Halavatica8d5252018-03-12 05:33:491051 return ProxyConfigWithAnnotation(
1052 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]3e44697f2009-05-22 14:37:391053 }
[email protected]861c6c62009-04-20 16:50:561054
[email protected]3e44697f2009-05-22 14:37:391055 if (mode == "auto") {
[email protected]aa3ac2cc2012-06-19 00:28:041056 // Automatic proxy config.
[email protected]861c6c62009-04-20 16:50:561057 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501058 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1059 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561060 if (!pac_url_str.empty()) {
[email protected]aa3ac2cc2012-06-19 00:28:041061 // If the PAC URL is actually a file path, then put file:// in front.
1062 if (pac_url_str[0] == '/')
1063 pac_url_str = "file://" + pac_url_str;
[email protected]861c6c62009-04-20 16:50:561064 GURL pac_url(pac_url_str);
1065 if (!pac_url.is_valid())
Eric Romanc0553ba2018-05-21 19:28:571066 return base::nullopt;
Ramin Halavatica8d5252018-03-12 05:33:491067 config.set_pac_url(pac_url);
1068 return ProxyConfigWithAnnotation(
1069 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561070 }
1071 }
Ramin Halavatica8d5252018-03-12 05:33:491072 config.set_auto_detect(true);
1073 return ProxyConfigWithAnnotation(
1074 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561075 }
1076
[email protected]3e44697f2009-05-22 14:37:391077 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561078 // Mode is unrecognized.
Eric Romanc0553ba2018-05-21 19:28:571079 return base::nullopt;
[email protected]861c6c62009-04-20 16:50:561080 }
1081 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501082 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1083 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561084 && !use_http_proxy) {
1085 // Another master switch for some reason. If set to false, then no
1086 // proxy. But we don't panic if the key doesn't exist.
Ramin Halavatica8d5252018-03-12 05:33:491087 return ProxyConfigWithAnnotation(
1088 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561089 }
1090
1091 bool same_proxy = false;
1092 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501093 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561094 // case.
[email protected]573c0502011-05-17 22:19:501095 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1096 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561097
[email protected]76960f3d2011-04-30 02:15:231098 ProxyServer proxy_for_http;
1099 ProxyServer proxy_for_https;
1100 ProxyServer proxy_for_ftp;
1101 ProxyServer socks_proxy; // (socks)
1102
1103 // This counts how many of the above ProxyServers were defined and valid.
1104 size_t num_proxies_specified = 0;
1105
1106 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1107 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501108 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231109 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501110 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231111 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501112 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231113 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501114 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231115 num_proxies_specified++;
1116
1117 if (same_proxy) {
1118 if (proxy_for_http.is_valid()) {
1119 // Use the http proxy for all schemes.
Ramin Halavatica8d5252018-03-12 05:33:491120 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
1121 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_for_http);
[email protected]861c6c62009-04-20 16:50:561122 }
[email protected]76960f3d2011-04-30 02:15:231123 } else if (num_proxies_specified > 0) {
1124 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1125 // If the only proxy specified was for SOCKS, use it for all schemes.
Ramin Halavatica8d5252018-03-12 05:33:491126 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
1127 config.proxy_rules().single_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561128 } else {
[email protected]2189e092013-03-16 18:02:021129 // Otherwise use the indicated proxies per-scheme.
Ramin Halavatica8d5252018-03-12 05:33:491130 config.proxy_rules().type =
Lily Houghtone6b617e2018-01-19 20:13:071131 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
Ramin Halavatica8d5252018-03-12 05:33:491132 config.proxy_rules().proxies_for_http.SetSingleProxyServer(
1133 proxy_for_http);
1134 config.proxy_rules().proxies_for_https.SetSingleProxyServer(
1135 proxy_for_https);
1136 config.proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_for_ftp);
1137 config.proxy_rules().fallback_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561138 }
1139 }
1140
Ramin Halavatica8d5252018-03-12 05:33:491141 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561142 // Manual mode but we couldn't parse any rules.
Eric Romanc0553ba2018-05-21 19:28:571143 return base::nullopt;
[email protected]861c6c62009-04-20 16:50:561144 }
1145
1146 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431147 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501148 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1149 &use_auth);
[email protected]62749f182009-07-15 13:16:541150 if (use_auth) {
1151 // ProxyConfig does not support authentication parameters, but
1152 // Chrome will prompt for the password later. So we ignore
1153 // /system/http_proxy/*auth* settings.
1154 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1155 }
[email protected]861c6c62009-04-20 16:50:561156
1157 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061158 std::vector<std::string> ignore_hosts_list;
Ramin Halavatica8d5252018-03-12 05:33:491159 config.proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501160 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1161 &ignore_hosts_list)) {
Eric Romanda790f92018-11-07 19:17:151162 for (const auto& rule : ignore_hosts_list) {
Shimi Zhang13eace252020-01-31 01:49:191163 config.proxy_rules().bypass_rules.AddRuleFromString(rule);
[email protected]1a597192010-07-09 16:58:381164 }
[email protected]a8185d02010-06-11 00:19:501165 }
Shimi Zhang13eace252020-01-31 01:49:191166
1167 if (setting_getter_->UseSuffixMatching()) {
1168 RewriteRulesForSuffixMatching(&config.proxy_rules().bypass_rules);
1169 }
1170
[email protected]861c6c62009-04-20 16:50:561171 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381172 // bypass of local names in GNOME. In KDE, "<local>" is supported
1173 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561174
[email protected]a48bf4a2010-06-14 18:24:531175 // KDE allows one to reverse the bypass rules.
Ramin Halavatica8d5252018-03-12 05:33:491176 config.proxy_rules().reverse_bypass = setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531177
Ramin Halavatica8d5252018-03-12 05:33:491178 return ProxyConfigWithAnnotation(
1179 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561180}
1181
thestig0c412e852016-06-30 08:04:401182ProxyConfigServiceLinux::Delegate::Delegate(
Ramin Halavatica8d5252018-03-12 05:33:491183 std::unique_ptr<base::Environment> env_var_getter,
Eric Romancd032fb62018-05-18 21:40:131184 base::Optional<std::unique_ptr<SettingGetter>> setting_getter,
1185 base::Optional<NetworkTrafficAnnotationTag> traffic_annotation)
1186 : env_var_getter_(std::move(env_var_getter)) {
1187 if (traffic_annotation) {
1188 traffic_annotation_ =
1189 MutableNetworkTrafficAnnotationTag(traffic_annotation.value());
1190 }
1191
1192 if (setting_getter) {
1193 setting_getter_ = std::move(setting_getter.value());
1194 return;
1195 }
1196
[email protected]573c0502011-05-17 22:19:501197 // Figure out which SettingGetterImpl to use, if any.
thestig0c412e852016-06-30 08:04:401198 switch (base::nix::GetDesktopEnvironment(env_var_getter_.get())) {
Tim Brownd9bd4752017-12-14 20:26:341199 case base::nix::DESKTOP_ENVIRONMENT_CINNAMON:
[email protected]6b0349ef2010-10-16 04:56:061200 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
Tom Andersonac4d6f42017-10-13 20:14:201201 case base::nix::DESKTOP_ENVIRONMENT_PANTHEON:
[email protected]9e6c9bde2012-07-17 23:40:171202 case base::nix::DESKTOP_ENVIRONMENT_UNITY:
[email protected]8c20e3d2011-05-19 21:03:571203#if defined(USE_GIO)
1204 {
danakj8a98ca22016-04-16 02:47:361205 std::unique_ptr<SettingGetterImplGSettings> gs_getter(
1206 new SettingGetterImplGSettings());
1207 // We have to load symbols and check the GNOME version in use to decide
Tim Brown1c307cc2017-12-08 02:40:381208 // if we should use the gsettings getter. See CheckVersion().
1209 if (gs_getter->CheckVersion(env_var_getter_.get()))
inlinechan894515af2016-12-09 02:40:101210 setting_getter_ = std::move(gs_getter);
[email protected]8c20e3d2011-05-19 21:03:571211 }
1212#endif
[email protected]d7395e732009-08-28 23:13:431213 break;
[email protected]6b0349ef2010-10-16 04:56:061214 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1215 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
edward.baker53bec302015-10-02 16:57:491216 case base::nix::DESKTOP_ENVIRONMENT_KDE5:
thestig0c412e852016-06-30 08:04:401217 setting_getter_.reset(new SettingGetterImplKDE(env_var_getter_.get()));
[email protected]d7395e732009-08-28 23:13:431218 break;
[email protected]6b0349ef2010-10-16 04:56:061219 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1220 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431221 break;
1222 }
1223}
1224
[email protected]d3066142011-05-10 02:36:201225void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
sergeyu3f923062014-09-05 01:39:401226 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
Ramin Halavatica8d5252018-03-12 05:33:491227 const scoped_refptr<base::SequencedTaskRunner>& main_task_runner,
1228 const NetworkTrafficAnnotationTag& traffic_annotation) {
1229 traffic_annotation_ = MutableNetworkTrafficAnnotationTag(traffic_annotation);
1230
[email protected]3e44697f2009-05-22 14:37:391231 // We should be running on the default glib main loop thread right
Tim Brown2a19f3b2017-12-12 01:08:401232 // now. gsettings can only be accessed from this thread.
eroman0070d412017-06-22 22:18:241233 DCHECK(glib_task_runner->RunsTasksInCurrentSequence());
sergeyu3f923062014-09-05 01:39:401234 glib_task_runner_ = glib_task_runner;
Matt Menke75765062017-11-21 01:21:161235 main_task_runner_ = main_task_runner;
[email protected]3e44697f2009-05-22 14:37:391236
Matt Menke75765062017-11-21 01:21:161237 // If we are passed a NULL |main_task_runner|, then don't set up proxy
eroman0070d412017-06-22 22:18:241238 // setting change notifications. This should not be the usual case but is
1239 // intended to/ simplify test setups.
Matt Menke75765062017-11-21 01:21:161240 if (!main_task_runner_.get())
[email protected]b30a3f52010-10-16 01:05:461241 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391242
1243 // Fetch and cache the current proxy config. The config is left in
Matt Menke75765062017-11-21 01:21:161244 // cached_config_, where GetLatestProxyConfig() running on the main TaskRunner
[email protected]3e44697f2009-05-22 14:37:391245 // will expect to find it. This is safe to do because we return
1246 // before this ProxyConfigServiceLinux is passed on to
Lily Houghton8c2f97d2018-01-22 05:06:591247 // the ProxyResolutionService.
[email protected]d6cb85b2009-07-23 22:10:531248
1249 // Note: It would be nice to prioritize environment variables
Tim Brown2a19f3b2017-12-12 01:08:401250 // and only fall back to gsettings if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531251 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1252 // does so even if the proxy mode is set to auto, which would
1253 // mislead us.
1254
Eric Romanc0553ba2018-05-21 19:28:571255 cached_config_ = base::nullopt;
Eric Roman750af4b12018-02-22 22:38:531256 if (setting_getter_ && setting_getter_->Init(glib_task_runner)) {
1257 cached_config_ = GetConfigFromSettings();
1258 }
1259 if (cached_config_) {
Ramin Halavatica8d5252018-03-12 05:33:491260 VLOG(1) << "Obtained proxy settings from annotation hash code "
1261 << cached_config_->traffic_annotation().unique_id_hash_code;
[email protected]d3066142011-05-10 02:36:201262
Tim Brown2a19f3b2017-12-12 01:08:401263 // If gsettings proxy mode is "none", meaning direct, then we take
[email protected]d3066142011-05-10 02:36:201264 // that to be a valid config and will not check environment
1265 // variables. The alternative would have been to look for a proxy
Eric Roman750af4b12018-02-22 22:38:531266 // wherever we can find one.
[email protected]d3066142011-05-10 02:36:201267
1268 // Keep a copy of the config for use from this thread for
1269 // comparison with updated settings when we get notifications.
1270 reference_config_ = cached_config_;
[email protected]d3066142011-05-10 02:36:201271
Matt Menke75765062017-11-21 01:21:161272 // We only set up notifications if we have the main and file loops
1273 // available. We do this after getting the initial configuration so that we
1274 // don't have to worry about cancelling it if the initial fetch above fails.
1275 // Note that setting up notifications has the side effect of simulating a
1276 // change, so that we won't lose any updates that may have happened after
1277 // the initial fetch and before setting up notifications. We'll detect the
1278 // common case of no changes in OnCheckProxyConfigSettings() (or sooner) and
1279 // ignore it.
1280 if (main_task_runner.get()) {
eroman0070d412017-06-22 22:18:241281 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461282 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241283 if (!required_loop.get() || required_loop->RunsTasksInCurrentSequence()) {
[email protected]d3066142011-05-10 02:36:201284 // In this case we are already on an acceptable thread.
1285 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431286 } else {
[email protected]d3066142011-05-10 02:36:201287 // Post a task to set up notifications. We don't wait for success.
kylecharf4fe5172019-02-15 18:53:491288 required_loop->PostTask(
1289 FROM_HERE,
1290 base::BindOnce(
1291 &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this));
[email protected]d6cb85b2009-07-23 22:10:531292 }
[email protected]d7395e732009-08-28 23:13:431293 }
[email protected]861c6c62009-04-20 16:50:561294 }
[email protected]d6cb85b2009-07-23 22:10:531295
Eric Roman750af4b12018-02-22 22:38:531296 if (!cached_config_) {
[email protected]d6cb85b2009-07-23 22:10:531297 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391298 //
[email protected]d3066142011-05-10 02:36:201299 // Consulting environment variables doesn't need to be done from the
1300 // default glib main loop, but it's a tiny enough amount of work.
Eric Roman750af4b12018-02-22 22:38:531301 cached_config_ = GetConfigFromEnv();
1302 if (cached_config_) {
[email protected]b30a3f52010-10-16 01:05:461303 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391304 }
[email protected]861c6c62009-04-20 16:50:561305 }
[email protected]3e44697f2009-05-22 14:37:391306}
1307
[email protected]573c0502011-05-17 22:19:501308// Depending on the SettingGetter in use, this method will be called
Tim Brown2a19f3b2017-12-12 01:08:401309// on either the UI thread (GSettings) or the file thread (KDE).
[email protected]d3066142011-05-10 02:36:201310void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
eroman0070d412017-06-22 22:18:241311 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461312 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241313 DCHECK(!required_loop.get() || required_loop->RunsTasksInCurrentSequence());
[email protected]573c0502011-05-17 22:19:501314 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201315 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1316}
1317
[email protected]119655002010-07-23 06:02:401318void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1319 observers_.AddObserver(observer);
1320}
1321
1322void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1323 observers_.RemoveObserver(observer);
1324}
1325
[email protected]3a29593d2011-04-11 10:07:521326ProxyConfigService::ConfigAvailability
Ramin Halavatica8d5252018-03-12 05:33:491327ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1328 ProxyConfigWithAnnotation* config) {
Matt Menke75765062017-11-21 01:21:161329 // This is called from the main TaskRunner.
1330 DCHECK(!main_task_runner_.get() ||
1331 main_task_runner_->RunsTasksInCurrentSequence());
[email protected]3e44697f2009-05-22 14:37:391332
1333 // Simply return the last proxy configuration that glib_default_loop
1334 // notified us of.
Eric Roman750af4b12018-02-22 22:38:531335 *config = GetConfigOrDirect(cached_config_);
[email protected]119655002010-07-23 06:02:401336
[email protected]3a29593d2011-04-11 10:07:521337 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401338 // going to be available since we initialized eagerly on the UI thread.
1339 // TODO(eroman): do lazy initialization instead, so we no longer need
1340 // to construct ProxyConfigServiceLinux on the UI thread.
1341 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521342 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391343}
1344
[email protected]573c0502011-05-17 22:19:501345// Depending on the SettingGetter in use, this method will be called
Tim Brown2a19f3b2017-12-12 01:08:401346// on either the UI thread (GSettings) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391347void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
eroman0070d412017-06-22 22:18:241348 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461349 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241350 DCHECK(!required_loop.get() || required_loop->RunsTasksInCurrentSequence());
Ramin Halavatica8d5252018-03-12 05:33:491351 base::Optional<ProxyConfigWithAnnotation> new_config =
1352 GetConfigFromSettings();
[email protected]3e44697f2009-05-22 14:37:391353
[email protected]119655002010-07-23 06:02:401354 // See if it is different from what we had before.
Eric Roman750af4b12018-02-22 22:38:531355 if (new_config.has_value() != reference_config_.has_value() ||
Eric Roman3e185842018-06-01 18:10:521356 (new_config.has_value() &&
1357 !new_config->value().Equals(reference_config_->value()))) {
Matt Menke75765062017-11-21 01:21:161358 // Post a task to the main TaskRunner with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391359 // update |cached_config_|.
Matt Menke75765062017-11-21 01:21:161360 main_task_runner_->PostTask(
1361 FROM_HERE,
kylecharf4fe5172019-02-15 18:53:491362 base::BindOnce(&ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1363 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301364 // Update the thread-private copy in |reference_config_| as well.
1365 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201366 } else {
1367 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391368 }
1369}
1370
1371void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
Ramin Halavatica8d5252018-03-12 05:33:491372 const base::Optional<ProxyConfigWithAnnotation>& new_config) {
Matt Menke75765062017-11-21 01:21:161373 DCHECK(main_task_runner_->RunsTasksInCurrentSequence());
[email protected]b30a3f52010-10-16 01:05:461374 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391375 cached_config_ = new_config;
Eric Roman750af4b12018-02-22 22:38:531376 for (auto& observer : observers_) {
1377 observer.OnProxyConfigChanged(GetConfigOrDirect(new_config),
1378 ProxyConfigService::CONFIG_VALID);
1379 }
[email protected]3e44697f2009-05-22 14:37:391380}
1381
1382void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
thestig0c412e852016-06-30 08:04:401383 if (!setting_getter_)
[email protected]d7395e732009-08-28 23:13:431384 return;
thestig0c412e852016-06-30 08:04:401385
eroman0070d412017-06-22 22:18:241386 scoped_refptr<base::SequencedTaskRunner> shutdown_loop =
[email protected]76722472012-05-24 08:26:461387 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241388 if (!shutdown_loop.get() || shutdown_loop->RunsTasksInCurrentSequence()) {
[email protected]3e44697f2009-05-22 14:37:391389 // Already on the right thread, call directly.
1390 // This is the case for the unittests.
1391 OnDestroy();
1392 } else {
[email protected]d7395e732009-08-28 23:13:431393 // Post to shutdown thread. Note that on browser shutdown, we may quit
1394 // this MessageLoop and exit the program before ever running this.
kylecharf4fe5172019-02-15 18:53:491395 shutdown_loop->PostTask(
1396 FROM_HERE,
1397 base::BindOnce(&ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391398 }
1399}
1400void ProxyConfigServiceLinux::Delegate::OnDestroy() {
eroman0070d412017-06-22 22:18:241401 scoped_refptr<base::SequencedTaskRunner> shutdown_loop =
[email protected]76722472012-05-24 08:26:461402 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241403 DCHECK(!shutdown_loop.get() || shutdown_loop->RunsTasksInCurrentSequence());
[email protected]573c0502011-05-17 22:19:501404 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391405}
1406
1407ProxyConfigServiceLinux::ProxyConfigServiceLinux()
Eric Romancd032fb62018-05-18 21:40:131408 : delegate_(new Delegate(base::Environment::Create(),
1409 base::nullopt,
1410 base::nullopt)) {}
[email protected]3e44697f2009-05-22 14:37:391411
[email protected]8e1845e12010-09-15 19:22:241412ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1413 delegate_->PostDestroyTask();
1414}
1415
[email protected]3e44697f2009-05-22 14:37:391416ProxyConfigServiceLinux::ProxyConfigServiceLinux(
Ramin Halavatica8d5252018-03-12 05:33:491417 std::unique_ptr<base::Environment> env_var_getter,
1418 const NetworkTrafficAnnotationTag& traffic_annotation)
Eric Romancd032fb62018-05-18 21:40:131419 : delegate_(new Delegate(std::move(env_var_getter),
1420 base::nullopt,
1421 traffic_annotation)) {}
[email protected]9a3d8d42009-09-03 17:01:461422
1423ProxyConfigServiceLinux::ProxyConfigServiceLinux(
thestig0c412e852016-06-30 08:04:401424 std::unique_ptr<base::Environment> env_var_getter,
Ramin Halavatica8d5252018-03-12 05:33:491425 SettingGetter* setting_getter,
1426 const NetworkTrafficAnnotationTag& traffic_annotation)
1427 : delegate_(new Delegate(std::move(env_var_getter),
Eric Romancd032fb62018-05-18 21:40:131428 base::WrapUnique(setting_getter),
Ramin Halavatica8d5252018-03-12 05:33:491429 traffic_annotation)) {}
[email protected]861c6c62009-04-20 16:50:561430
[email protected]e4be2dd2010-12-14 00:44:391431void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1432 delegate_->AddObserver(observer);
1433}
1434
1435void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1436 delegate_->RemoveObserver(observer);
1437}
1438
[email protected]3a29593d2011-04-11 10:07:521439ProxyConfigService::ConfigAvailability
Ramin Halavatica8d5252018-03-12 05:33:491440ProxyConfigServiceLinux::GetLatestProxyConfig(
1441 ProxyConfigWithAnnotation* config) {
[email protected]e4be2dd2010-12-14 00:44:391442 return delegate_->GetLatestProxyConfig(config);
1443}
1444
[email protected]861c6c62009-04-20 16:50:561445} // namespace net