blob: baf86b8196051f9f9ebb9d536342f6f1829887ff [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>
Peter Boström08e7ed82021-04-19 17:49:5913#include <memory>
thestig0c412e852016-06-30 08:04:4014#include <utility>
[email protected]9bc8cff2010-04-03 01:05:3915
[email protected]6af889c2011-10-06 23:11:4116#include "base/bind.h"
gabf4f904e2017-05-10 20:55:0217#include "base/files/file_descriptor_watcher_posix.h"
[email protected]57999812013-02-24 05:40:5218#include "base/files/file_path.h"
thestigd8df0332014-09-04 06:33:2919#include "base/files/file_util.h"
[email protected]b9b4a572014-03-17 23:11:1220#include "base/files/scoped_file.h"
[email protected]861c6c62009-04-20 16:50:5621#include "base/logging.h"
Avi Drissman13fc8932015-12-20 04:40:4622#include "base/macros.h"
Eric Romancd032fb62018-05-18 21:40:1323#include "base/memory/ptr_util.h"
[email protected]3a29593d2011-04-11 10:07:5224#include "base/nix/xdg_util.h"
eroman0070d412017-06-22 22:18:2425#include "base/sequenced_task_runner.h"
[email protected]76722472012-05-24 08:26:4626#include "base/single_thread_task_runner.h"
[email protected]fc9be5802013-06-11 10:56:5127#include "base/strings/string_number_conversions.h"
[email protected]f4ebe772013-02-02 00:21:3928#include "base/strings/string_tokenizer.h"
[email protected]66e96c42013-06-28 15:20:3129#include "base/strings/string_util.h"
Gabriel Charette44db1422018-08-06 11:19:3330#include "base/task/post_task.h"
31#include "base/task/task_traits.h"
Gabriel Charette99f5df32021-03-19 19:55:5532#include "base/task/thread_pool.h"
[email protected]9a8c4022011-01-25 14:25:3333#include "base/threading/thread_restrictions.h"
[email protected]66e96c42013-06-28 15:20:3134#include "base/timer/timer.h"
Lily Houghton582d4622018-01-22 22:43:4035#include "net/base/proxy_server.h"
[email protected]861c6c62009-04-20 16:50:5636
[email protected]3fc24f52012-11-30 21:22:3437#if defined(USE_GIO)
Tim Brown1c307cc2017-12-08 02:40:3838#include <gio/gio.h>
[email protected]3fc24f52012-11-30 21:22:3439#endif // defined(USE_GIO)
40
[email protected]861c6c62009-04-20 16:50:5641namespace net {
42
43namespace {
44
Shimi Zhang13eace252020-01-31 01:49:1945// This turns all rules with a hostname into wildcard matches, which will
46// match not just the indicated hostname but also any hostname that ends with
47// it.
48void RewriteRulesForSuffixMatching(ProxyBypassRules* out) {
49 // Prepend a wildcard (*) to any hostname based rules, provided it isn't an IP
50 // address.
51 for (size_t i = 0; i < out->rules().size(); ++i) {
52 if (!out->rules()[i]->IsHostnamePatternRule())
53 continue;
54
55 const SchemeHostPortMatcherHostnamePatternRule* prev_rule =
56 static_cast<const SchemeHostPortMatcherHostnamePatternRule*>(
57 out->rules()[i].get());
58 out->ReplaceRule(i, prev_rule->GenerateSuffixMatchingRule());
59 }
60}
61
[email protected]861c6c62009-04-20 16:50:5662// Given a proxy hostname from a setting, returns that hostname with
63// an appropriate proxy server scheme prefix.
64// scheme indicates the desired proxy scheme: usually http, with
65// socks 4 or 5 as special cases.
[email protected]87a102b2009-07-14 05:23:3066// TODO(arindam): Remove URI string manipulation by using MapUrlSchemeToProxy.
[email protected]861c6c62009-04-20 16:50:5667std::string FixupProxyHostScheme(ProxyServer::Scheme scheme,
68 std::string host) {
[email protected]e8c50812010-09-28 00:16:1769 if (scheme == ProxyServer::SCHEME_SOCKS5 &&
brettw3a2c6902015-07-06 19:43:2970 base::StartsWith(host, "socks4://",
71 base::CompareCase::INSENSITIVE_ASCII)) {
[email protected]e8c50812010-09-28 00:16:1772 // We default to socks 5, but if the user specifically set it to
73 // socks4://, then use that.
74 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:5675 }
76 // Strip the scheme if any.
77 std::string::size_type colon = host.find("://");
78 if (colon != std::string::npos)
79 host = host.substr(colon + 3);
80 // If a username and perhaps password are specified, give a warning.
81 std::string::size_type at_sign = host.find("@");
82 // Should this be supported?
83 if (at_sign != std::string::npos) {
[email protected]62749f182009-07-15 13:16:5484 // ProxyConfig does not support authentication parameters, but Chrome
85 // will prompt for the password later. Disregard the
86 // authentication parameters and continue with this hostname.
87 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
[email protected]861c6c62009-04-20 16:50:5688 host = host.substr(at_sign + 1);
89 }
90 // If this is a socks proxy, prepend a scheme so as to tell
91 // ProxyServer. This also allows ProxyServer to choose the right
92 // default port.
93 if (scheme == ProxyServer::SCHEME_SOCKS4)
94 host = "socks4://" + host;
95 else if (scheme == ProxyServer::SCHEME_SOCKS5)
96 host = "socks5://" + host;
[email protected]d7395e732009-08-28 23:13:4397 // If there is a trailing slash, remove it so |host| will parse correctly
98 // even if it includes a port number (since the slash is not numeric).
pkasting9022cb42016-02-05 00:08:5699 if (!host.empty() && host.back() == '/')
[email protected]d7395e732009-08-28 23:13:43100 host.resize(host.length() - 1);
[email protected]861c6c62009-04-20 16:50:56101 return host;
102}
103
Ramin Halavatica8d5252018-03-12 05:33:49104ProxyConfigWithAnnotation GetConfigOrDirect(
Anton Bikineev068d2912021-05-15 20:43:52105 const absl::optional<ProxyConfigWithAnnotation>& optional_config) {
Eric Roman750af4b12018-02-22 22:38:53106 if (optional_config)
107 return optional_config.value();
108
Ramin Halavatica8d5252018-03-12 05:33:49109 ProxyConfigWithAnnotation config = ProxyConfigWithAnnotation::CreateDirect();
Eric Roman750af4b12018-02-22 22:38:53110 return config;
111}
112
[email protected]861c6c62009-04-20 16:50:56113} // namespace
114
Chris Watkins3a13f632017-12-04 00:41:15115ProxyConfigServiceLinux::Delegate::~Delegate() = default;
[email protected]8e1845e12010-09-15 19:22:24116
[email protected]3e44697f2009-05-22 14:37:39117bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVarForScheme(
thestig0c412e852016-06-30 08:04:40118 base::StringPiece variable,
119 ProxyServer::Scheme scheme,
[email protected]861c6c62009-04-20 16:50:56120 ProxyServer* result_server) {
121 std::string env_value;
thestig0c412e852016-06-30 08:04:40122 if (!env_var_getter_->GetVar(variable, &env_value))
123 return false;
124
125 if (env_value.empty())
126 return false;
127
128 env_value = FixupProxyHostScheme(scheme, env_value);
129 ProxyServer proxy_server =
130 ProxyServer::FromURI(env_value, ProxyServer::SCHEME_HTTP);
131 if (proxy_server.is_valid() && !proxy_server.is_direct()) {
132 *result_server = proxy_server;
133 return true;
[email protected]861c6c62009-04-20 16:50:56134 }
thestig0c412e852016-06-30 08:04:40135 LOG(ERROR) << "Failed to parse environment variable " << variable;
[email protected]861c6c62009-04-20 16:50:56136 return false;
137}
138
[email protected]3e44697f2009-05-22 14:37:39139bool ProxyConfigServiceLinux::Delegate::GetProxyFromEnvVar(
thestig0c412e852016-06-30 08:04:40140 base::StringPiece variable,
141 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:56142 return GetProxyFromEnvVarForScheme(variable, ProxyServer::SCHEME_HTTP,
143 result_server);
144}
145
Anton Bikineev068d2912021-05-15 20:43:52146absl::optional<ProxyConfigWithAnnotation>
Eric Roman750af4b12018-02-22 22:38:53147ProxyConfigServiceLinux::Delegate::GetConfigFromEnv() {
Ramin Halavatica8d5252018-03-12 05:33:49148 ProxyConfig config;
Eric Roman750af4b12018-02-22 22:38:53149
[email protected]861c6c62009-04-20 16:50:56150 // Check for automatic configuration first, in
151 // "auto_proxy". Possibly only the "environment_proxy" firefox
152 // extension has ever used this, but it still sounds like a good
153 // idea.
154 std::string auto_proxy;
[email protected]3ba7e082010-08-07 02:57:59155 if (env_var_getter_->GetVar("auto_proxy", &auto_proxy)) {
[email protected]861c6c62009-04-20 16:50:56156 if (auto_proxy.empty()) {
157 // Defined and empty => autodetect
Ramin Halavatica8d5252018-03-12 05:33:49158 config.set_auto_detect(true);
[email protected]861c6c62009-04-20 16:50:56159 } else {
160 // specified autoconfig URL
Ramin Halavatica8d5252018-03-12 05:33:49161 config.set_pac_url(GURL(auto_proxy));
[email protected]861c6c62009-04-20 16:50:56162 }
Ramin Halavatica8d5252018-03-12 05:33:49163 return ProxyConfigWithAnnotation(
164 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:56165 }
166 // "all_proxy" is a shortcut to avoid defining {http,https,ftp}_proxy.
167 ProxyServer proxy_server;
168 if (GetProxyFromEnvVar("all_proxy", &proxy_server)) {
Ramin Halavatica8d5252018-03-12 05:33:49169 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
170 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56171 } else {
172 bool have_http = GetProxyFromEnvVar("http_proxy", &proxy_server);
173 if (have_http)
Ramin Halavatica8d5252018-03-12 05:33:49174 config.proxy_rules().proxies_for_http.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56175 // It would be tempting to let http_proxy apply for all protocols
176 // if https_proxy and ftp_proxy are not defined. Googling turns up
177 // several documents that mention only http_proxy. But then the
178 // user really might not want to proxy https. And it doesn't seem
179 // like other apps do this. So we will refrain.
180 bool have_https = GetProxyFromEnvVar("https_proxy", &proxy_server);
181 if (have_https)
Ramin Halavatica8d5252018-03-12 05:33:49182 config.proxy_rules().proxies_for_https.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56183 bool have_ftp = GetProxyFromEnvVar("ftp_proxy", &proxy_server);
184 if (have_ftp)
Ramin Halavatica8d5252018-03-12 05:33:49185 config.proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56186 if (have_http || have_https || have_ftp) {
187 // mustn't change type unless some rules are actually set.
Ramin Halavatica8d5252018-03-12 05:33:49188 config.proxy_rules().type =
Lily Houghtone6b617e2018-01-19 20:13:07189 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
[email protected]861c6c62009-04-20 16:50:56190 }
191 }
Ramin Halavatica8d5252018-03-12 05:33:49192 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56193 // If the above were not defined, try for socks.
[email protected]e8c50812010-09-28 00:16:17194 // For environment variables, we default to version 5, per the gnome
195 // documentation: http://library.gnome.org/devel/gnet/stable/gnet-socks.html
196 ProxyServer::Scheme scheme = ProxyServer::SCHEME_SOCKS5;
[email protected]861c6c62009-04-20 16:50:56197 std::string env_version;
[email protected]3ba7e082010-08-07 02:57:59198 if (env_var_getter_->GetVar("SOCKS_VERSION", &env_version)
[email protected]e8c50812010-09-28 00:16:17199 && env_version == "4")
200 scheme = ProxyServer::SCHEME_SOCKS4;
[email protected]861c6c62009-04-20 16:50:56201 if (GetProxyFromEnvVarForScheme("SOCKS_SERVER", scheme, &proxy_server)) {
Ramin Halavatica8d5252018-03-12 05:33:49202 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
203 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_server);
[email protected]861c6c62009-04-20 16:50:56204 }
205 }
206 // Look for the proxy bypass list.
207 std::string no_proxy;
[email protected]3ba7e082010-08-07 02:57:59208 env_var_getter_->GetVar("no_proxy", &no_proxy);
Ramin Halavatica8d5252018-03-12 05:33:49209 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:56210 // Having only "no_proxy" set, presumably to "*", makes it
211 // explicit that env vars do specify a configuration: having no
212 // rules specified only means the user explicitly asks for direct
213 // connections.
Ramin Halavatica8d5252018-03-12 05:33:49214 return !no_proxy.empty()
215 ? ProxyConfigWithAnnotation(
216 config, NetworkTrafficAnnotationTag(traffic_annotation_))
Anton Bikineev068d2912021-05-15 20:43:52217 : absl::optional<ProxyConfigWithAnnotation>();
[email protected]861c6c62009-04-20 16:50:56218 }
[email protected]7541206c2010-02-19 20:24:06219 // Note that this uses "suffix" matching. So a bypass of "google.com"
220 // is understood to mean a bypass of "*google.com".
Shimi Zhang13eace252020-01-31 01:49:19221 config.proxy_rules().bypass_rules.ParseFromString(no_proxy);
222 RewriteRulesForSuffixMatching(&config.proxy_rules().bypass_rules);
223
Ramin Halavatica8d5252018-03-12 05:33:49224 return ProxyConfigWithAnnotation(
225 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:56226}
227
228namespace {
229
[email protected]d7395e732009-08-28 23:13:43230const int kDebounceTimeoutMilliseconds = 250;
[email protected]3e44697f2009-05-22 14:37:39231
[email protected]8c20e3d2011-05-19 21:03:57232#if defined(USE_GIO)
Tim Brown1c307cc2017-12-08 02:40:38233const char kProxyGSettingsSchema[] = "org.gnome.system.proxy";
[email protected]2297bb22014-06-19 06:30:14234
[email protected]8c20e3d2011-05-19 21:03:57235// This setting getter uses gsettings, as used in most GNOME 3 desktops.
236class SettingGetterImplGSettings
237 : public ProxyConfigServiceLinux::SettingGetter {
238 public:
danakj8c3eb802015-09-24 07:53:00239 SettingGetterImplGSettings()
thestig0c412e852016-06-30 08:04:40240 : client_(nullptr),
241 http_client_(nullptr),
242 https_client_(nullptr),
243 ftp_client_(nullptr),
244 socks_client_(nullptr),
245 notify_delegate_(nullptr),
danakj8c3eb802015-09-24 07:53:00246 debounce_timer_(new base::OneShotTimer()) {}
[email protected]8c20e3d2011-05-19 21:03:57247
dcheng67be2b1f2014-10-27 21:47:29248 ~SettingGetterImplGSettings() override {
[email protected]8c20e3d2011-05-19 21:03:57249 // client_ should have been released before now, from
250 // Delegate::OnDestroy(), while running on the UI thread. However
251 // on exiting the process, it may happen that
252 // Delegate::OnDestroy() task is left pending on the glib loop
253 // after the loop was quit, and pending tasks may then be deleted
254 // without being run.
255 if (client_) {
Tim Brown2a19f3b2017-12-12 01:08:40256 // gsettings client was not cleaned up.
eroman0070d412017-06-22 22:18:24257 if (task_runner_->RunsTasksInCurrentSequence()) {
Mostyn Bramley-Moore699c5312018-05-01 10:48:09258 // We are on the UI thread so we can clean it safely.
[email protected]8c20e3d2011-05-19 21:03:57259 VLOG(1) << "~SettingGetterImplGSettings: releasing gsettings client";
260 ShutDown();
261 } else {
262 LOG(WARNING) << "~SettingGetterImplGSettings: leaking gsettings client";
thestig0c412e852016-06-30 08:04:40263 client_ = nullptr;
[email protected]8c20e3d2011-05-19 21:03:57264 }
265 }
266 DCHECK(!client_);
[email protected]8c20e3d2011-05-19 21:03:57267 }
268
Tim Brown1c307cc2017-12-08 02:40:38269 // CheckVersion() must be called *before* Init()!
270 bool CheckVersion(base::Environment* env);
[email protected]8c20e3d2011-05-19 21:03:57271
eroman0070d412017-06-22 22:18:24272 bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner)
mostynbba063d6032014-10-09 11:01:13273 override {
eroman0070d412017-06-22 22:18:24274 DCHECK(glib_task_runner->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57275 DCHECK(!client_);
[email protected]90499482013-06-01 00:39:50276 DCHECK(!task_runner_.get());
[email protected]4cf80f0b2011-05-20 20:30:26277
Tim Brown1c307cc2017-12-08 02:40:38278 if (!g_settings_schema_source_lookup(g_settings_schema_source_get_default(),
279 kProxyGSettingsSchema, FALSE) ||
280 !(client_ = g_settings_new(kProxyGSettingsSchema))) {
[email protected]8c20e3d2011-05-19 21:03:57281 // It's not clear whether/when this can return NULL.
282 LOG(ERROR) << "Unable to create a gsettings client";
283 return false;
284 }
sergeyu3f923062014-09-05 01:39:40285 task_runner_ = glib_task_runner;
[email protected]8c20e3d2011-05-19 21:03:57286 // We assume these all work if the above call worked.
Tim Brown1c307cc2017-12-08 02:40:38287 http_client_ = g_settings_get_child(client_, "http");
288 https_client_ = g_settings_get_child(client_, "https");
289 ftp_client_ = g_settings_get_child(client_, "ftp");
290 socks_client_ = g_settings_get_child(client_, "socks");
[email protected]8c20e3d2011-05-19 21:03:57291 DCHECK(http_client_ && https_client_ && ftp_client_ && socks_client_);
292 return true;
293 }
294
dcheng67be2b1f2014-10-27 21:47:29295 void ShutDown() override {
[email protected]8c20e3d2011-05-19 21:03:57296 if (client_) {
eroman0070d412017-06-22 22:18:24297 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57298 // This also disables gsettings notifications.
299 g_object_unref(socks_client_);
300 g_object_unref(ftp_client_);
301 g_object_unref(https_client_);
302 g_object_unref(http_client_);
303 g_object_unref(client_);
304 // We only need to null client_ because it's the only one that we check.
thestig0c412e852016-06-30 08:04:40305 client_ = nullptr;
306 task_runner_ = nullptr;
[email protected]8c20e3d2011-05-19 21:03:57307 }
marshall8e5fe942015-03-06 19:22:40308 debounce_timer_.reset();
[email protected]8c20e3d2011-05-19 21:03:57309 }
310
dcheng67be2b1f2014-10-27 21:47:29311 bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13312 ProxyConfigServiceLinux::Delegate* delegate) override {
[email protected]8c20e3d2011-05-19 21:03:57313 DCHECK(client_);
eroman0070d412017-06-22 22:18:24314 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57315 notify_delegate_ = delegate;
316 // We could watch for the change-event signal instead of changed, but
317 // since we have to watch more than one object, we'd still have to
318 // debounce change notifications. This is conceptually simpler.
319 g_signal_connect(G_OBJECT(client_), "changed",
320 G_CALLBACK(OnGSettingsChangeNotification), this);
321 g_signal_connect(G_OBJECT(http_client_), "changed",
322 G_CALLBACK(OnGSettingsChangeNotification), this);
323 g_signal_connect(G_OBJECT(https_client_), "changed",
324 G_CALLBACK(OnGSettingsChangeNotification), this);
325 g_signal_connect(G_OBJECT(ftp_client_), "changed",
326 G_CALLBACK(OnGSettingsChangeNotification), this);
327 g_signal_connect(G_OBJECT(socks_client_), "changed",
328 G_CALLBACK(OnGSettingsChangeNotification), this);
329 // Simulate a change to avoid possibly losing updates before this point.
330 OnChangeNotification();
331 return true;
332 }
333
eroman0070d412017-06-22 22:18:24334 const scoped_refptr<base::SequencedTaskRunner>& GetNotificationTaskRunner()
dcheng67be2b1f2014-10-27 21:47:29335 override {
sergeyu3f923062014-09-05 01:39:40336 return task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57337 }
338
dcheng67be2b1f2014-10-27 21:47:29339 bool GetString(StringSetting key, std::string* result) override {
[email protected]8c20e3d2011-05-19 21:03:57340 DCHECK(client_);
341 switch (key) {
342 case PROXY_MODE:
343 return GetStringByPath(client_, "mode", result);
344 case PROXY_AUTOCONF_URL:
345 return GetStringByPath(client_, "autoconfig-url", result);
346 case PROXY_HTTP_HOST:
347 return GetStringByPath(http_client_, "host", result);
348 case PROXY_HTTPS_HOST:
349 return GetStringByPath(https_client_, "host", result);
350 case PROXY_FTP_HOST:
351 return GetStringByPath(ftp_client_, "host", result);
352 case PROXY_SOCKS_HOST:
353 return GetStringByPath(socks_client_, "host", result);
[email protected]8c20e3d2011-05-19 21:03:57354 }
[email protected]6b5fe742011-05-20 21:46:48355 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57356 }
dcheng67be2b1f2014-10-27 21:47:29357 bool GetBool(BoolSetting key, bool* result) override {
[email protected]8c20e3d2011-05-19 21:03:57358 DCHECK(client_);
359 switch (key) {
360 case PROXY_USE_HTTP_PROXY:
361 // Although there is an "enabled" boolean in http_client_, it is not set
362 // to true by the proxy config utility. We ignore it and return false.
363 return false;
364 case PROXY_USE_SAME_PROXY:
365 // Similarly, although there is a "use-same-proxy" boolean in client_,
366 // it is never set to false by the proxy config utility. We ignore it.
367 return false;
368 case PROXY_USE_AUTHENTICATION:
369 // There is also no way to set this in the proxy config utility, but it
370 // doesn't hurt us to get the actual setting (unlike the two above).
371 return GetBoolByPath(http_client_, "use-authentication", result);
[email protected]8c20e3d2011-05-19 21:03:57372 }
[email protected]6b5fe742011-05-20 21:46:48373 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57374 }
dcheng67be2b1f2014-10-27 21:47:29375 bool GetInt(IntSetting key, int* result) override {
[email protected]8c20e3d2011-05-19 21:03:57376 DCHECK(client_);
377 switch (key) {
378 case PROXY_HTTP_PORT:
379 return GetIntByPath(http_client_, "port", result);
380 case PROXY_HTTPS_PORT:
381 return GetIntByPath(https_client_, "port", result);
382 case PROXY_FTP_PORT:
383 return GetIntByPath(ftp_client_, "port", result);
384 case PROXY_SOCKS_PORT:
385 return GetIntByPath(socks_client_, "port", result);
[email protected]8c20e3d2011-05-19 21:03:57386 }
[email protected]6b5fe742011-05-20 21:46:48387 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57388 }
dcheng67be2b1f2014-10-27 21:47:29389 bool GetStringList(StringListSetting key,
390 std::vector<std::string>* result) override {
[email protected]8c20e3d2011-05-19 21:03:57391 DCHECK(client_);
392 switch (key) {
393 case PROXY_IGNORE_HOSTS:
394 return GetStringListByPath(client_, "ignore-hosts", result);
[email protected]8c20e3d2011-05-19 21:03:57395 }
[email protected]6b5fe742011-05-20 21:46:48396 return false; // Placate compiler.
[email protected]8c20e3d2011-05-19 21:03:57397 }
398
dcheng67be2b1f2014-10-27 21:47:29399 bool BypassListIsReversed() override {
[email protected]8c20e3d2011-05-19 21:03:57400 // This is a KDE-specific setting.
401 return false;
402 }
403
Shimi Zhang13eace252020-01-31 01:49:19404 bool UseSuffixMatching() override { return false; }
[email protected]8c20e3d2011-05-19 21:03:57405
406 private:
thestig0c412e852016-06-30 08:04:40407 bool GetStringByPath(GSettings* client,
408 base::StringPiece key,
[email protected]8c20e3d2011-05-19 21:03:57409 std::string* result) {
eroman0070d412017-06-22 22:18:24410 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38411 gchar* value = g_settings_get_string(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57412 if (!value)
413 return false;
414 *result = value;
415 g_free(value);
416 return true;
417 }
thestig0c412e852016-06-30 08:04:40418 bool GetBoolByPath(GSettings* client, base::StringPiece key, bool* result) {
eroman0070d412017-06-22 22:18:24419 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38420 *result = static_cast<bool>(g_settings_get_boolean(client, key.data()));
[email protected]8c20e3d2011-05-19 21:03:57421 return true;
422 }
thestig0c412e852016-06-30 08:04:40423 bool GetIntByPath(GSettings* client, base::StringPiece key, int* result) {
eroman0070d412017-06-22 22:18:24424 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38425 *result = g_settings_get_int(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57426 return true;
427 }
thestig0c412e852016-06-30 08:04:40428 bool GetStringListByPath(GSettings* client,
429 base::StringPiece key,
[email protected]8c20e3d2011-05-19 21:03:57430 std::vector<std::string>* result) {
eroman0070d412017-06-22 22:18:24431 DCHECK(task_runner_->RunsTasksInCurrentSequence());
Tim Brown1c307cc2017-12-08 02:40:38432 gchar** list = g_settings_get_strv(client, key.data());
[email protected]8c20e3d2011-05-19 21:03:57433 if (!list)
434 return false;
435 for (size_t i = 0; list[i]; ++i) {
436 result->push_back(static_cast<char*>(list[i]));
437 g_free(list[i]);
438 }
439 g_free(list);
440 return true;
441 }
442
443 // This is the callback from the debounce timer.
444 void OnDebouncedNotification() {
eroman0070d412017-06-22 22:18:24445 DCHECK(task_runner_->RunsTasksInCurrentSequence());
[email protected]8c20e3d2011-05-19 21:03:57446 CHECK(notify_delegate_);
447 // Forward to a method on the proxy config service delegate object.
448 notify_delegate_->OnCheckProxyConfigSettings();
449 }
450
451 void OnChangeNotification() {
452 // We don't use Reset() because the timer may not yet be running.
453 // (In that case Stop() is a no-op.)
marshall8e5fe942015-03-06 19:22:40454 debounce_timer_->Stop();
455 debounce_timer_->Start(FROM_HERE,
[email protected]8c20e3d2011-05-19 21:03:57456 base::TimeDelta::FromMilliseconds(kDebounceTimeoutMilliseconds),
457 this, &SettingGetterImplGSettings::OnDebouncedNotification);
458 }
459
460 // gsettings notification callback, dispatched on the default glib main loop.
461 static void OnGSettingsChangeNotification(GSettings* client, gchar* key,
462 gpointer user_data) {
463 VLOG(1) << "gsettings change notification for key " << key;
464 // We don't track which key has changed, just that something did change.
465 SettingGetterImplGSettings* setting_getter =
466 reinterpret_cast<SettingGetterImplGSettings*>(user_data);
467 setting_getter->OnChangeNotification();
468 }
469
470 GSettings* client_;
471 GSettings* http_client_;
472 GSettings* https_client_;
473 GSettings* ftp_client_;
474 GSettings* socks_client_;
475 ProxyConfigServiceLinux::Delegate* notify_delegate_;
danakj8a98ca22016-04-16 02:47:36476 std::unique_ptr<base::OneShotTimer> debounce_timer_;
[email protected]8c20e3d2011-05-19 21:03:57477
[email protected]76722472012-05-24 08:26:46478 // Task runner for the thread that we make gsettings calls on. It should
[email protected]8c20e3d2011-05-19 21:03:57479 // be the UI thread and all our methods should be called on this
480 // thread. Only for assertions.
eroman0070d412017-06-22 22:18:24481 scoped_refptr<base::SequencedTaskRunner> task_runner_;
[email protected]8c20e3d2011-05-19 21:03:57482
483 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplGSettings);
484};
485
Tim Brown1c307cc2017-12-08 02:40:38486bool SettingGetterImplGSettings::CheckVersion(
[email protected]8c20e3d2011-05-19 21:03:57487 base::Environment* env) {
Tim Brown1c307cc2017-12-08 02:40:38488 // CheckVersion() must be called *before* Init()!
[email protected]8c20e3d2011-05-19 21:03:57489 DCHECK(!client_);
490
thestig0c412e852016-06-30 08:04:40491 GSettings* client = nullptr;
Tim Brown1c307cc2017-12-08 02:40:38492 if (g_settings_schema_source_lookup(g_settings_schema_source_get_default(),
493 kProxyGSettingsSchema, FALSE)) {
494 client = g_settings_new(kProxyGSettingsSchema);
[email protected]4bbb72d2014-06-06 18:05:51495 }
496 if (!client) {
Tim Brown2a19f3b2017-12-12 01:08:40497 VLOG(1) << "Cannot create gsettings client.";
[email protected]8c20e3d2011-05-19 21:03:57498 return false;
499 }
500 g_object_unref(client);
501
[email protected]8c20e3d2011-05-19 21:03:57502 VLOG(1) << "All gsettings tests OK. Will get proxy config from gsettings.";
503 return true;
504}
505#endif // defined(USE_GIO)
506
eromane44498c2017-06-30 00:02:37507// Converts |value| from a decimal string to an int. If there was a failure
508// parsing, returns |default_value|.
509int StringToIntOrDefault(base::StringPiece value, int default_value) {
510 int result;
511 if (base::StringToInt(value, &result))
512 return result;
513 return default_value;
514}
515
Tim Brown2a19f3b2017-12-12 01:08:40516// This is the KDE version that reads kioslaverc and simulates gsettings.
[email protected]d7395e732009-08-28 23:13:43517// Doing this allows the main Delegate code, as well as the unit tests
518// for it, to stay the same - and the settings map fairly well besides.
gabf4f904e2017-05-10 20:55:02519class SettingGetterImplKDE : public ProxyConfigServiceLinux::SettingGetter {
[email protected]d7395e732009-08-28 23:13:43520 public:
[email protected]573c0502011-05-17 22:19:50521 explicit SettingGetterImplKDE(base::Environment* env_var_getter)
marshall8e5fe942015-03-06 19:22:40522 : inotify_fd_(-1),
thestig0c412e852016-06-30 08:04:40523 notify_delegate_(nullptr),
danakj8c3eb802015-09-24 07:53:00524 debounce_timer_(new base::OneShotTimer()),
marshall8e5fe942015-03-06 19:22:40525 indirect_manual_(false),
526 auto_no_pac_(false),
527 reversed_bypass_list_(false),
528 env_var_getter_(env_var_getter),
thestig0c412e852016-06-30 08:04:40529 file_task_runner_(nullptr) {
[email protected]9a8c4022011-01-25 14:25:33530 // This has to be called on the UI thread (http://crbug.com/69057).
531 base::ThreadRestrictions::ScopedAllowIO allow_io;
532
[email protected]f18fde22010-05-18 23:49:54533 // Derive the location of the kde config dir from the environment.
[email protected]92d2dc82010-04-08 17:49:59534 std::string home;
[email protected]3ba7e082010-08-07 02:57:59535 if (env_var_getter->GetVar("KDEHOME", &home) && !home.empty()) {
[email protected]2e8cfe22010-06-12 00:26:24536 // $KDEHOME is set. Use it unconditionally.
[email protected]6cdfd7f2013-02-08 20:40:15537 kde_config_dir_ = KDEHomeToConfigPath(base::FilePath(home));
[email protected]92d2dc82010-04-08 17:49:59538 } else {
[email protected]2e8cfe22010-06-12 00:26:24539 // $KDEHOME is unset. Try to figure out what to use. This seems to be
[email protected]92d2dc82010-04-08 17:49:59540 // the common case on most distributions.
[email protected]3ba7e082010-08-07 02:57:59541 if (!env_var_getter->GetVar(base::env_vars::kHome, &home))
[email protected]d7395e732009-08-28 23:13:43542 // User has no $HOME? Give up. Later we'll report the failure.
543 return;
[email protected]6b0349ef2010-10-16 04:56:06544 if (base::nix::GetDesktopEnvironment(env_var_getter) ==
545 base::nix::DESKTOP_ENVIRONMENT_KDE3) {
[email protected]92d2dc82010-04-08 17:49:59546 // KDE3 always uses .kde for its configuration.
[email protected]6cdfd7f2013-02-08 20:40:15547 base::FilePath kde_path = base::FilePath(home).Append(".kde");
[email protected]92d2dc82010-04-08 17:49:59548 kde_config_dir_ = KDEHomeToConfigPath(kde_path);
edward.baker53bec302015-10-02 16:57:49549 } else if (base::nix::GetDesktopEnvironment(env_var_getter) ==
550 base::nix::DESKTOP_ENVIRONMENT_KDE4) {
[email protected]92d2dc82010-04-08 17:49:59551 // Some distributions patch KDE4 to use .kde4 instead of .kde, so that
[email protected]fad9c8a52010-06-10 22:30:53552 // both can be installed side-by-side. Sadly they don't all do this, and
553 // they don't always do this: some distributions have started switching
554 // back as well. So if there is a .kde4 directory, check the timestamps
555 // of the config directories within and use the newest one.
[email protected]92d2dc82010-04-08 17:49:59556 // Note that we should currently be running in the UI thread, because in
Tim Brown2a19f3b2017-12-12 01:08:40557 // the gsettings version, that is the only thread that can access the
558 // proxy settings (a gsettings restriction). As noted below, the initial
559 // read of the proxy settings will be done in this thread anyway, so we
560 // check for .kde4 here in this thread as well.
[email protected]6cdfd7f2013-02-08 20:40:15561 base::FilePath kde3_path = base::FilePath(home).Append(".kde");
562 base::FilePath kde3_config = KDEHomeToConfigPath(kde3_path);
563 base::FilePath kde4_path = base::FilePath(home).Append(".kde4");
564 base::FilePath kde4_config = KDEHomeToConfigPath(kde4_path);
[email protected]fad9c8a52010-06-10 22:30:53565 bool use_kde4 = false;
[email protected]dcd16612013-07-15 20:18:09566 if (base::DirectoryExists(kde4_path)) {
[email protected]54124ed02014-01-07 10:06:58567 base::File::Info kde3_info;
568 base::File::Info kde4_info;
[email protected]9eae4e62013-12-04 20:56:49569 if (base::GetFileInfo(kde4_config, &kde4_info)) {
570 if (base::GetFileInfo(kde3_config, &kde3_info)) {
[email protected]fad9c8a52010-06-10 22:30:53571 use_kde4 = kde4_info.last_modified >= kde3_info.last_modified;
572 } else {
573 use_kde4 = true;
574 }
575 }
576 }
577 if (use_kde4) {
[email protected]92d2dc82010-04-08 17:49:59578 kde_config_dir_ = KDEHomeToConfigPath(kde4_path);
579 } else {
[email protected]fad9c8a52010-06-10 22:30:53580 kde_config_dir_ = KDEHomeToConfigPath(kde3_path);
[email protected]92d2dc82010-04-08 17:49:59581 }
edward.baker53bec302015-10-02 16:57:49582 } else {
583 // KDE 5 migrated to ~/.config for storing kioslaverc.
584 kde_config_dir_ = base::FilePath(home).Append(".config");
[email protected]92d2dc82010-04-08 17:49:59585 }
[email protected]d7395e732009-08-28 23:13:43586 }
[email protected]d7395e732009-08-28 23:13:43587 }
588
dcheng67be2b1f2014-10-27 21:47:29589 ~SettingGetterImplKDE() override {
[email protected]d7395e732009-08-28 23:13:43590 // inotify_fd_ should have been closed before now, from
591 // Delegate::OnDestroy(), while running on the file thread. However
592 // on exiting the process, it may happen that Delegate::OnDestroy()
593 // task is left pending on the file loop after the loop was quit,
594 // and pending tasks may then be deleted without being run.
595 // Here in the KDE version, we can safely close the file descriptor
596 // anyway. (Not that it really matters; the process is exiting.)
597 if (inotify_fd_ >= 0)
[email protected]d3066142011-05-10 02:36:20598 ShutDown();
thestig0c412e852016-06-30 08:04:40599 DCHECK_LT(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:43600 }
601
eroman0070d412017-06-22 22:18:24602 bool Init(const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner)
mostynbba063d6032014-10-09 11:01:13603 override {
[email protected]9a8c4022011-01-25 14:25:33604 // This has to be called on the UI thread (http://crbug.com/69057).
605 base::ThreadRestrictions::ScopedAllowIO allow_io;
thestig0c412e852016-06-30 08:04:40606 DCHECK_LT(inotify_fd_, 0);
[email protected]d7395e732009-08-28 23:13:43607 inotify_fd_ = inotify_init();
608 if (inotify_fd_ < 0) {
[email protected]57b765672009-10-13 18:27:40609 PLOG(ERROR) << "inotify_init failed";
[email protected]d7395e732009-08-28 23:13:43610 return false;
611 }
tfarina89b4ae1c2015-12-16 18:59:18612 if (!base::SetNonBlocking(inotify_fd_)) {
613 PLOG(ERROR) << "base::SetNonBlocking failed";
[email protected]d7395e732009-08-28 23:13:43614 close(inotify_fd_);
615 inotify_fd_ = -1;
616 return false;
617 }
eroman0070d412017-06-22 22:18:24618
Gabriel Charette4049d422020-02-29 00:43:27619 constexpr base::TaskTraits kTraits = {base::TaskPriority::USER_VISIBLE,
620 base::MayBlock()};
621 file_task_runner_ = base::ThreadPool::CreateSequencedTaskRunner(kTraits);
eroman0070d412017-06-22 22:18:24622
sergeyu3f923062014-09-05 01:39:40623 // The initial read is done on the current thread, not
624 // |file_task_runner_|, since we will need to have it for
625 // SetUpAndFetchInitialConfig().
[email protected]d7395e732009-08-28 23:13:43626 UpdateCachedSettings();
627 return true;
628 }
629
dcheng67be2b1f2014-10-27 21:47:29630 void ShutDown() override {
[email protected]d7395e732009-08-28 23:13:43631 if (inotify_fd_ >= 0) {
632 ResetCachedSettings();
gabf4f904e2017-05-10 20:55:02633 inotify_watcher_.reset();
[email protected]d7395e732009-08-28 23:13:43634 close(inotify_fd_);
635 inotify_fd_ = -1;
636 }
marshall8e5fe942015-03-06 19:22:40637 debounce_timer_.reset();
[email protected]d7395e732009-08-28 23:13:43638 }
639
dcheng67be2b1f2014-10-27 21:47:29640 bool SetUpNotifications(
mostynbba063d6032014-10-09 11:01:13641 ProxyConfigServiceLinux::Delegate* delegate) override {
thestig0c412e852016-06-30 08:04:40642 DCHECK_GE(inotify_fd_, 0);
eroman0070d412017-06-22 22:18:24643 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]d7395e732009-08-28 23:13:43644 // We can't just watch the kioslaverc file directly, since KDE will write
645 // a new copy of it and then rename it whenever settings are changed and
646 // inotify watches inodes (so we'll be watching the old deleted file after
647 // the first change, and it will never change again). So, we watch the
648 // directory instead. We then act only on changes to the kioslaverc entry.
eroman6b0ca662017-06-22 00:16:36649 // TODO(eroman): What if the file is deleted? (handle with IN_DELETE).
[email protected]d7395e732009-08-28 23:13:43650 if (inotify_add_watch(inotify_fd_, kde_config_dir_.value().c_str(),
sergeyu3f923062014-09-05 01:39:40651 IN_MODIFY | IN_MOVED_TO) < 0) {
[email protected]d7395e732009-08-28 23:13:43652 return false;
sergeyu3f923062014-09-05 01:39:40653 }
[email protected]d7395e732009-08-28 23:13:43654 notify_delegate_ = delegate;
gabf4f904e2017-05-10 20:55:02655 inotify_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Anna Malovaae7007aa2020-03-09 16:48:48656 inotify_fd_,
657 base::BindRepeating(&SettingGetterImplKDE::OnChangeNotification,
658 base::Unretained(this)));
[email protected]d3066142011-05-10 02:36:20659 // Simulate a change to avoid possibly losing updates before this point.
660 OnChangeNotification();
661 return true;
[email protected]d7395e732009-08-28 23:13:43662 }
663
eroman0070d412017-06-22 22:18:24664 const scoped_refptr<base::SequencedTaskRunner>& GetNotificationTaskRunner()
dcheng67be2b1f2014-10-27 21:47:29665 override {
sergeyu3f923062014-09-05 01:39:40666 return file_task_runner_;
[email protected]d7395e732009-08-28 23:13:43667 }
668
dcheng67be2b1f2014-10-27 21:47:29669 bool GetString(StringSetting key, std::string* result) override {
jdoerrie22a91d8b92018-10-05 08:43:26670 auto it = string_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43671 if (it == string_table_.end())
672 return false;
673 *result = it->second;
674 return true;
675 }
dcheng67be2b1f2014-10-27 21:47:29676 bool GetBool(BoolSetting key, bool* result) override {
[email protected]d7395e732009-08-28 23:13:43677 // We don't ever have any booleans.
678 return false;
679 }
dcheng67be2b1f2014-10-27 21:47:29680 bool GetInt(IntSetting key, int* result) override {
[email protected]d7395e732009-08-28 23:13:43681 // We don't ever have any integers. (See AddProxy() below about ports.)
682 return false;
683 }
dcheng67be2b1f2014-10-27 21:47:29684 bool GetStringList(StringListSetting key,
685 std::vector<std::string>* result) override {
jdoerrie22a91d8b92018-10-05 08:43:26686 auto it = strings_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43687 if (it == strings_table_.end())
688 return false;
689 *result = it->second;
690 return true;
691 }
692
dcheng67be2b1f2014-10-27 21:47:29693 bool BypassListIsReversed() override { return reversed_bypass_list_; }
[email protected]a48bf4a2010-06-14 18:24:53694
Shimi Zhang13eace252020-01-31 01:49:19695 bool UseSuffixMatching() override { return true; }
[email protected]1a597192010-07-09 16:58:38696
[email protected]d7395e732009-08-28 23:13:43697 private:
698 void ResetCachedSettings() {
699 string_table_.clear();
700 strings_table_.clear();
701 indirect_manual_ = false;
702 auto_no_pac_ = false;
[email protected]a48bf4a2010-06-14 18:24:53703 reversed_bypass_list_ = false;
[email protected]d7395e732009-08-28 23:13:43704 }
705
[email protected]6cdfd7f2013-02-08 20:40:15706 base::FilePath KDEHomeToConfigPath(const base::FilePath& kde_home) {
[email protected]92d2dc82010-04-08 17:49:59707 return kde_home.Append("share").Append("config");
708 }
709
[email protected]6b5fe742011-05-20 21:46:48710 void AddProxy(StringSetting host_key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:43711 if (value.empty() || value.substr(0, 3) == "//:")
712 // No proxy.
713 return;
[email protected]4b90c202012-04-24 23:27:55714 size_t space = value.find(' ');
715 if (space != std::string::npos) {
716 // Newer versions of KDE use a space rather than a colon to separate the
717 // port number from the hostname. If we find this, we need to convert it.
718 std::string fixed = value;
719 fixed[space] = ':';
720 string_table_[host_key] = fixed;
721 } else {
722 // We don't need to parse the port number out; GetProxyFromSettings()
723 // would only append it right back again. So we just leave the port
724 // number right in the host string.
725 string_table_[host_key] = value;
726 }
[email protected]d7395e732009-08-28 23:13:43727 }
728
[email protected]6b5fe742011-05-20 21:46:48729 void AddHostList(StringListSetting key, const std::string& value) {
[email protected]f18fde22010-05-18 23:49:54730 std::vector<std::string> tokens;
[email protected]f4ebe772013-02-02 00:21:39731 base::StringTokenizer tk(value, ", ");
[email protected]f18fde22010-05-18 23:49:54732 while (tk.GetNext()) {
733 std::string token = tk.token();
734 if (!token.empty())
735 tokens.push_back(token);
736 }
737 strings_table_[key] = tokens;
738 }
739
[email protected]9a3d8d42009-09-03 17:01:46740 void AddKDESetting(const std::string& key, const std::string& value) {
[email protected]d7395e732009-08-28 23:13:43741 if (key == "ProxyType") {
742 const char* mode = "none";
743 indirect_manual_ = false;
744 auto_no_pac_ = false;
eromane44498c2017-06-30 00:02:37745 int int_value = StringToIntOrDefault(value, 0);
[email protected]e83326f2010-07-31 17:29:25746 switch (int_value) {
[email protected]d7395e732009-08-28 23:13:43747 case 1: // Manual configuration.
748 mode = "manual";
749 break;
750 case 2: // PAC URL.
751 mode = "auto";
752 break;
753 case 3: // WPAD.
754 mode = "auto";
755 auto_no_pac_ = true;
756 break;
757 case 4: // Indirect manual via environment variables.
758 mode = "manual";
759 indirect_manual_ = true;
760 break;
eromane44498c2017-06-30 00:02:37761 default: // No proxy, or maybe kioslaverc syntax error.
762 break;
[email protected]d7395e732009-08-28 23:13:43763 }
[email protected]573c0502011-05-17 22:19:50764 string_table_[PROXY_MODE] = mode;
[email protected]d7395e732009-08-28 23:13:43765 } else if (key == "Proxy Config Script") {
[email protected]573c0502011-05-17 22:19:50766 string_table_[PROXY_AUTOCONF_URL] = value;
[email protected]d7395e732009-08-28 23:13:43767 } else if (key == "httpProxy") {
[email protected]573c0502011-05-17 22:19:50768 AddProxy(PROXY_HTTP_HOST, value);
[email protected]d7395e732009-08-28 23:13:43769 } else if (key == "httpsProxy") {
[email protected]573c0502011-05-17 22:19:50770 AddProxy(PROXY_HTTPS_HOST, value);
[email protected]d7395e732009-08-28 23:13:43771 } else if (key == "ftpProxy") {
[email protected]573c0502011-05-17 22:19:50772 AddProxy(PROXY_FTP_HOST, value);
[email protected]bfeb7232012-06-08 00:58:37773 } else if (key == "socksProxy") {
774 // Older versions of KDE configure SOCKS in a weird way involving
775 // LD_PRELOAD and a library that intercepts network calls to SOCKSify
776 // them. We don't support it. KDE 4.8 added a proper SOCKS setting.
777 AddProxy(PROXY_SOCKS_HOST, value);
[email protected]d7395e732009-08-28 23:13:43778 } else if (key == "ReversedException") {
779 // We count "true" or any nonzero number as true, otherwise false.
eromane44498c2017-06-30 00:02:37780 // A failure parsing the integer will also mean false.
781 reversed_bypass_list_ =
782 (value == "true" || StringToIntOrDefault(value, 0) != 0);
[email protected]d7395e732009-08-28 23:13:43783 } else if (key == "NoProxyFor") {
[email protected]573c0502011-05-17 22:19:50784 AddHostList(PROXY_IGNORE_HOSTS, value);
[email protected]d7395e732009-08-28 23:13:43785 } else if (key == "AuthMode") {
786 // Check for authentication, just so we can warn.
eromane44498c2017-06-30 00:02:37787 int mode = StringToIntOrDefault(value, 0);
[email protected]d7395e732009-08-28 23:13:43788 if (mode) {
789 // ProxyConfig does not support authentication parameters, but
790 // Chrome will prompt for the password later. So we ignore this.
791 LOG(WARNING) <<
792 "Proxy authentication parameters ignored, see bug 16709";
793 }
794 }
795 }
796
[email protected]6b5fe742011-05-20 21:46:48797 void ResolveIndirect(StringSetting key) {
jdoerrie22a91d8b92018-10-05 08:43:26798 auto it = string_table_.find(key);
[email protected]d7395e732009-08-28 23:13:43799 if (it != string_table_.end()) {
[email protected]f18fde22010-05-18 23:49:54800 std::string value;
[email protected]3ba7e082010-08-07 02:57:59801 if (env_var_getter_->GetVar(it->second.c_str(), &value))
[email protected]d7395e732009-08-28 23:13:43802 it->second = value;
[email protected]8425adc02010-04-18 17:45:31803 else
804 string_table_.erase(it);
[email protected]d7395e732009-08-28 23:13:43805 }
806 }
807
[email protected]6b5fe742011-05-20 21:46:48808 void ResolveIndirectList(StringListSetting key) {
jdoerrie22a91d8b92018-10-05 08:43:26809 auto it = strings_table_.find(key);
[email protected]f18fde22010-05-18 23:49:54810 if (it != strings_table_.end()) {
811 std::string value;
812 if (!it->second.empty() &&
[email protected]3ba7e082010-08-07 02:57:59813 env_var_getter_->GetVar(it->second[0].c_str(), &value))
[email protected]f18fde22010-05-18 23:49:54814 AddHostList(key, value);
815 else
816 strings_table_.erase(it);
817 }
818 }
819
[email protected]d7395e732009-08-28 23:13:43820 // The settings in kioslaverc could occur in any order, but some affect
821 // others. Rather than read the whole file in and then query them in an
822 // order that allows us to handle that, we read the settings in whatever
823 // order they occur and do any necessary tweaking after we finish.
824 void ResolveModeEffects() {
825 if (indirect_manual_) {
[email protected]573c0502011-05-17 22:19:50826 ResolveIndirect(PROXY_HTTP_HOST);
827 ResolveIndirect(PROXY_HTTPS_HOST);
828 ResolveIndirect(PROXY_FTP_HOST);
829 ResolveIndirectList(PROXY_IGNORE_HOSTS);
[email protected]d7395e732009-08-28 23:13:43830 }
831 if (auto_no_pac_) {
832 // Remove the PAC URL; we're not supposed to use it.
[email protected]573c0502011-05-17 22:19:50833 string_table_.erase(PROXY_AUTOCONF_URL);
[email protected]d7395e732009-08-28 23:13:43834 }
[email protected]d7395e732009-08-28 23:13:43835 }
836
837 // Reads kioslaverc one line at a time and calls AddKDESetting() to add
838 // each relevant name-value pair to the appropriate value table.
839 void UpdateCachedSettings() {
[email protected]6cdfd7f2013-02-08 20:40:15840 base::FilePath kioslaverc = kde_config_dir_.Append("kioslaverc");
[email protected]b9b4a572014-03-17 23:11:12841 base::ScopedFILE input(base::OpenFile(kioslaverc, "r"));
[email protected]d7395e732009-08-28 23:13:43842 if (!input.get())
843 return;
844 ResetCachedSettings();
845 bool in_proxy_settings = false;
846 bool line_too_long = false;
[email protected]9a3d8d42009-09-03 17:01:46847 char line[BUFFER_SIZE];
848 // fgets() will return NULL on EOF or error.
[email protected]d7395e732009-08-28 23:13:43849 while (fgets(line, sizeof(line), input.get())) {
850 // fgets() guarantees the line will be properly terminated.
851 size_t length = strlen(line);
852 if (!length)
853 continue;
854 // This should be true even with CRLF endings.
855 if (line[length - 1] != '\n') {
856 line_too_long = true;
857 continue;
858 }
859 if (line_too_long) {
860 // The previous line had no line ending, but this done does. This is
861 // the end of the line that was too long, so warn here and skip it.
862 LOG(WARNING) << "skipped very long line in " << kioslaverc.value();
863 line_too_long = false;
864 continue;
865 }
866 // Remove the LF at the end, and the CR if there is one.
867 line[--length] = '\0';
868 if (length && line[length - 1] == '\r')
869 line[--length] = '\0';
870 // Now parse the line.
871 if (line[0] == '[') {
872 // Switching sections. All we care about is whether this is
873 // the (a?) proxy settings section, for both KDE3 and KDE4.
874 in_proxy_settings = !strncmp(line, "[Proxy Settings]", 16);
875 } else if (in_proxy_settings) {
876 // A regular line, in the (a?) proxy settings section.
[email protected]9a3d8d42009-09-03 17:01:46877 char* split = strchr(line, '=');
878 // Skip this line if it does not contain an = sign.
879 if (!split)
[email protected]d7395e732009-08-28 23:13:43880 continue;
[email protected]9a3d8d42009-09-03 17:01:46881 // Split the line on the = and advance |split|.
882 *(split++) = 0;
883 std::string key = line;
884 std::string value = split;
[email protected]8af69c6c2014-03-03 19:05:31885 base::TrimWhitespaceASCII(key, base::TRIM_ALL, &key);
886 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value);
[email protected]9a3d8d42009-09-03 17:01:46887 // Skip this line if the key name is empty.
888 if (key.empty())
[email protected]d7395e732009-08-28 23:13:43889 continue;
890 // Is the value name localized?
[email protected]9a3d8d42009-09-03 17:01:46891 if (key[key.length() - 1] == ']') {
892 // Find the matching bracket.
893 length = key.rfind('[');
894 // Skip this line if the localization indicator is malformed.
895 if (length == std::string::npos)
[email protected]d7395e732009-08-28 23:13:43896 continue;
897 // Trim the localization indicator off.
[email protected]9a3d8d42009-09-03 17:01:46898 key.resize(length);
899 // Remove any resulting trailing whitespace.
[email protected]8af69c6c2014-03-03 19:05:31900 base::TrimWhitespaceASCII(key, base::TRIM_TRAILING, &key);
[email protected]9a3d8d42009-09-03 17:01:46901 // Skip this line if the key name is now empty.
902 if (key.empty())
903 continue;
[email protected]d7395e732009-08-28 23:13:43904 }
[email protected]d7395e732009-08-28 23:13:43905 // Now fill in the tables.
[email protected]9a3d8d42009-09-03 17:01:46906 AddKDESetting(key, value);
[email protected]d7395e732009-08-28 23:13:43907 }
908 }
909 if (ferror(input.get()))
910 LOG(ERROR) << "error reading " << kioslaverc.value();
911 ResolveModeEffects();
912 }
913
914 // This is the callback from the debounce timer.
915 void OnDebouncedNotification() {
eroman0070d412017-06-22 22:18:24916 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]b30a3f52010-10-16 01:05:46917 VLOG(1) << "inotify change notification for kioslaverc";
[email protected]d7395e732009-08-28 23:13:43918 UpdateCachedSettings();
[email protected]961ac942011-04-28 18:18:14919 CHECK(notify_delegate_);
[email protected]d7395e732009-08-28 23:13:43920 // Forward to a method on the proxy config service delegate object.
921 notify_delegate_->OnCheckProxyConfigSettings();
922 }
923
924 // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads
925 // from the inotify file descriptor and starts up a debounce timer if
926 // an event for kioslaverc is seen.
927 void OnChangeNotification() {
[email protected]d2e6d592012-02-03 21:49:04928 DCHECK_GE(inotify_fd_, 0);
eroman0070d412017-06-22 22:18:24929 DCHECK(file_task_runner_->RunsTasksInCurrentSequence());
[email protected]d7395e732009-08-28 23:13:43930 char event_buf[(sizeof(inotify_event) + NAME_MAX + 1) * 4];
931 bool kioslaverc_touched = false;
932 ssize_t r;
933 while ((r = read(inotify_fd_, event_buf, sizeof(event_buf))) > 0) {
934 // inotify returns variable-length structures, which is why we have
935 // this strange-looking loop instead of iterating through an array.
936 char* event_ptr = event_buf;
937 while (event_ptr < event_buf + r) {
938 inotify_event* event = reinterpret_cast<inotify_event*>(event_ptr);
939 // The kernel always feeds us whole events.
[email protected]b1f031dd2010-03-02 23:19:33940 CHECK_LE(event_ptr + sizeof(inotify_event), event_buf + r);
941 CHECK_LE(event->name + event->len, event_buf + r);
[email protected]d7395e732009-08-28 23:13:43942 if (!strcmp(event->name, "kioslaverc"))
943 kioslaverc_touched = true;
944 // Advance the pointer just past the end of the filename.
945 event_ptr = event->name + event->len;
946 }
947 // We keep reading even if |kioslaverc_touched| is true to drain the
948 // inotify event queue.
949 }
950 if (!r)
951 // Instead of returning -1 and setting errno to EINVAL if there is not
952 // enough buffer space, older kernels (< 2.6.21) return 0. Simulate the
953 // new behavior (EINVAL) so we can reuse the code below.
954 errno = EINVAL;
955 if (errno != EAGAIN) {
[email protected]57b765672009-10-13 18:27:40956 PLOG(WARNING) << "error reading inotify file descriptor";
[email protected]d7395e732009-08-28 23:13:43957 if (errno == EINVAL) {
958 // Our buffer is not large enough to read the next event. This should
959 // not happen (because its size is calculated to always be sufficiently
960 // large), but if it does we'd warn continuously since |inotify_fd_|
961 // would be forever ready to read. Close it and stop watching instead.
962 LOG(ERROR) << "inotify failure; no longer watching kioslaverc!";
gabf4f904e2017-05-10 20:55:02963 inotify_watcher_.reset();
[email protected]d7395e732009-08-28 23:13:43964 close(inotify_fd_);
965 inotify_fd_ = -1;
966 }
967 }
968 if (kioslaverc_touched) {
eroman6b0ca662017-06-22 00:16:36969 LOG(ERROR) << "kioslaverc_touched";
[email protected]d7395e732009-08-28 23:13:43970 // We don't use Reset() because the timer may not yet be running.
971 // (In that case Stop() is a no-op.)
marshall8e5fe942015-03-06 19:22:40972 debounce_timer_->Stop();
973 debounce_timer_->Start(FROM_HERE, base::TimeDelta::FromMilliseconds(
[email protected]d7395e732009-08-28 23:13:43974 kDebounceTimeoutMilliseconds), this,
[email protected]573c0502011-05-17 22:19:50975 &SettingGetterImplKDE::OnDebouncedNotification);
[email protected]d7395e732009-08-28 23:13:43976 }
977 }
978
[email protected]6b5fe742011-05-20 21:46:48979 typedef std::map<StringSetting, std::string> string_map_type;
980 typedef std::map<StringListSetting,
981 std::vector<std::string> > strings_map_type;
[email protected]d7395e732009-08-28 23:13:43982
983 int inotify_fd_;
gabf4f904e2017-05-10 20:55:02984 std::unique_ptr<base::FileDescriptorWatcher::Controller> inotify_watcher_;
[email protected]d7395e732009-08-28 23:13:43985 ProxyConfigServiceLinux::Delegate* notify_delegate_;
danakj8a98ca22016-04-16 02:47:36986 std::unique_ptr<base::OneShotTimer> debounce_timer_;
[email protected]6cdfd7f2013-02-08 20:40:15987 base::FilePath kde_config_dir_;
[email protected]d7395e732009-08-28 23:13:43988 bool indirect_manual_;
989 bool auto_no_pac_;
[email protected]a48bf4a2010-06-14 18:24:53990 bool reversed_bypass_list_;
[email protected]f18fde22010-05-18 23:49:54991 // We don't own |env_var_getter_|. It's safe to hold a pointer to it, since
992 // both it and us are owned by ProxyConfigServiceLinux::Delegate, and have the
993 // same lifetime.
[email protected]76b90d312010-08-03 03:00:50994 base::Environment* env_var_getter_;
[email protected]d7395e732009-08-28 23:13:43995
996 // We cache these settings whenever we re-read the kioslaverc file.
997 string_map_type string_table_;
998 strings_map_type strings_table_;
999
eroman0070d412017-06-22 22:18:241000 // Task runner for doing blocking file IO on, as well as handling inotify
1001 // events on.
1002 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
[email protected]d7395e732009-08-28 23:13:431003
[email protected]573c0502011-05-17 22:19:501004 DISALLOW_COPY_AND_ASSIGN(SettingGetterImplKDE);
[email protected]861c6c62009-04-20 16:50:561005};
1006
1007} // namespace
1008
[email protected]573c0502011-05-17 22:19:501009bool ProxyConfigServiceLinux::Delegate::GetProxyFromSettings(
[email protected]6b5fe742011-05-20 21:46:481010 SettingGetter::StringSetting host_key,
[email protected]573c0502011-05-17 22:19:501011 ProxyServer* result_server) {
[email protected]861c6c62009-04-20 16:50:561012 std::string host;
[email protected]573c0502011-05-17 22:19:501013 if (!setting_getter_->GetString(host_key, &host) || host.empty()) {
[email protected]861c6c62009-04-20 16:50:561014 // Unset or empty.
1015 return false;
1016 }
1017 // Check for an optional port.
[email protected]d7395e732009-08-28 23:13:431018 int port = 0;
[email protected]6b5fe742011-05-20 21:46:481019 SettingGetter::IntSetting port_key =
[email protected]573c0502011-05-17 22:19:501020 SettingGetter::HostSettingToPortSetting(host_key);
1021 setting_getter_->GetInt(port_key, &port);
[email protected]861c6c62009-04-20 16:50:561022 if (port != 0) {
1023 // If a port is set and non-zero:
Raul Tambre8c1981d2019-02-08 02:22:261024 host += ":" + base::NumberToString(port);
[email protected]861c6c62009-04-20 16:50:561025 }
[email protected]76960f3d2011-04-30 02:15:231026
Tim Brown2a19f3b2017-12-12 01:08:401027 // gsettings settings do not appear to distinguish between SOCKS version. We
[email protected]573c0502011-05-17 22:19:501028 // default to version 5. For more information on this policy decision, see:
[email protected]76960f3d2011-04-30 02:15:231029 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
[email protected]573c0502011-05-17 22:19:501030 ProxyServer::Scheme scheme = (host_key == SettingGetter::PROXY_SOCKS_HOST) ?
1031 ProxyServer::SCHEME_SOCKS5 : ProxyServer::SCHEME_HTTP;
1032 host = FixupProxyHostScheme(scheme, host);
[email protected]87a102b2009-07-14 05:23:301033 ProxyServer proxy_server = ProxyServer::FromURI(host,
1034 ProxyServer::SCHEME_HTTP);
[email protected]861c6c62009-04-20 16:50:561035 if (proxy_server.is_valid()) {
1036 *result_server = proxy_server;
1037 return true;
1038 }
1039 return false;
1040}
1041
Anton Bikineev068d2912021-05-15 20:43:521042absl::optional<ProxyConfigWithAnnotation>
Eric Roman750af4b12018-02-22 22:38:531043ProxyConfigServiceLinux::Delegate::GetConfigFromSettings() {
Ramin Halavatica8d5252018-03-12 05:33:491044 ProxyConfig config;
Eric Roman750af4b12018-02-22 22:38:531045
[email protected]861c6c62009-04-20 16:50:561046 std::string mode;
[email protected]573c0502011-05-17 22:19:501047 if (!setting_getter_->GetString(SettingGetter::PROXY_MODE, &mode)) {
Tim Brown2a19f3b2017-12-12 01:08:401048 // We expect this to always be set, so if we don't see it then we probably
1049 // have a gsettings problem, and so we don't have a valid proxy config.
Anton Bikineev068d2912021-05-15 20:43:521050 return absl::nullopt;
[email protected]861c6c62009-04-20 16:50:561051 }
[email protected]3e44697f2009-05-22 14:37:391052 if (mode == "none") {
[email protected]861c6c62009-04-20 16:50:561053 // Specifically specifies no proxy.
Ramin Halavatica8d5252018-03-12 05:33:491054 return ProxyConfigWithAnnotation(
1055 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]3e44697f2009-05-22 14:37:391056 }
[email protected]861c6c62009-04-20 16:50:561057
[email protected]3e44697f2009-05-22 14:37:391058 if (mode == "auto") {
[email protected]aa3ac2cc2012-06-19 00:28:041059 // Automatic proxy config.
[email protected]861c6c62009-04-20 16:50:561060 std::string pac_url_str;
[email protected]573c0502011-05-17 22:19:501061 if (setting_getter_->GetString(SettingGetter::PROXY_AUTOCONF_URL,
1062 &pac_url_str)) {
[email protected]861c6c62009-04-20 16:50:561063 if (!pac_url_str.empty()) {
[email protected]aa3ac2cc2012-06-19 00:28:041064 // If the PAC URL is actually a file path, then put file:// in front.
1065 if (pac_url_str[0] == '/')
1066 pac_url_str = "file://" + pac_url_str;
[email protected]861c6c62009-04-20 16:50:561067 GURL pac_url(pac_url_str);
1068 if (!pac_url.is_valid())
Anton Bikineev068d2912021-05-15 20:43:521069 return absl::nullopt;
Ramin Halavatica8d5252018-03-12 05:33:491070 config.set_pac_url(pac_url);
1071 return ProxyConfigWithAnnotation(
1072 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561073 }
1074 }
Ramin Halavatica8d5252018-03-12 05:33:491075 config.set_auto_detect(true);
1076 return ProxyConfigWithAnnotation(
1077 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561078 }
1079
[email protected]3e44697f2009-05-22 14:37:391080 if (mode != "manual") {
[email protected]861c6c62009-04-20 16:50:561081 // Mode is unrecognized.
Anton Bikineev068d2912021-05-15 20:43:521082 return absl::nullopt;
[email protected]861c6c62009-04-20 16:50:561083 }
1084 bool use_http_proxy;
[email protected]573c0502011-05-17 22:19:501085 if (setting_getter_->GetBool(SettingGetter::PROXY_USE_HTTP_PROXY,
1086 &use_http_proxy)
[email protected]861c6c62009-04-20 16:50:561087 && !use_http_proxy) {
1088 // Another master switch for some reason. If set to false, then no
1089 // proxy. But we don't panic if the key doesn't exist.
Ramin Halavatica8d5252018-03-12 05:33:491090 return ProxyConfigWithAnnotation(
1091 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561092 }
1093
1094 bool same_proxy = false;
1095 // Indicates to use the http proxy for all protocols. This one may
[email protected]573c0502011-05-17 22:19:501096 // not exist (presumably on older versions); we assume false in that
[email protected]861c6c62009-04-20 16:50:561097 // case.
[email protected]573c0502011-05-17 22:19:501098 setting_getter_->GetBool(SettingGetter::PROXY_USE_SAME_PROXY,
1099 &same_proxy);
[email protected]861c6c62009-04-20 16:50:561100
[email protected]76960f3d2011-04-30 02:15:231101 ProxyServer proxy_for_http;
1102 ProxyServer proxy_for_https;
1103 ProxyServer proxy_for_ftp;
1104 ProxyServer socks_proxy; // (socks)
1105
1106 // This counts how many of the above ProxyServers were defined and valid.
1107 size_t num_proxies_specified = 0;
1108
1109 // Extract the per-scheme proxies. If we failed to parse it, or no proxy was
1110 // specified for the scheme, then the resulting ProxyServer will be invalid.
[email protected]573c0502011-05-17 22:19:501111 if (GetProxyFromSettings(SettingGetter::PROXY_HTTP_HOST, &proxy_for_http))
[email protected]76960f3d2011-04-30 02:15:231112 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501113 if (GetProxyFromSettings(SettingGetter::PROXY_HTTPS_HOST, &proxy_for_https))
[email protected]76960f3d2011-04-30 02:15:231114 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501115 if (GetProxyFromSettings(SettingGetter::PROXY_FTP_HOST, &proxy_for_ftp))
[email protected]76960f3d2011-04-30 02:15:231116 num_proxies_specified++;
[email protected]573c0502011-05-17 22:19:501117 if (GetProxyFromSettings(SettingGetter::PROXY_SOCKS_HOST, &socks_proxy))
[email protected]76960f3d2011-04-30 02:15:231118 num_proxies_specified++;
1119
1120 if (same_proxy) {
1121 if (proxy_for_http.is_valid()) {
1122 // Use the http proxy for all schemes.
Ramin Halavatica8d5252018-03-12 05:33:491123 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
1124 config.proxy_rules().single_proxies.SetSingleProxyServer(proxy_for_http);
[email protected]861c6c62009-04-20 16:50:561125 }
[email protected]76960f3d2011-04-30 02:15:231126 } else if (num_proxies_specified > 0) {
1127 if (socks_proxy.is_valid() && num_proxies_specified == 1) {
1128 // If the only proxy specified was for SOCKS, use it for all schemes.
Ramin Halavatica8d5252018-03-12 05:33:491129 config.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
1130 config.proxy_rules().single_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561131 } else {
[email protected]2189e092013-03-16 18:02:021132 // Otherwise use the indicated proxies per-scheme.
Ramin Halavatica8d5252018-03-12 05:33:491133 config.proxy_rules().type =
Lily Houghtone6b617e2018-01-19 20:13:071134 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME;
Ramin Halavatica8d5252018-03-12 05:33:491135 config.proxy_rules().proxies_for_http.SetSingleProxyServer(
1136 proxy_for_http);
1137 config.proxy_rules().proxies_for_https.SetSingleProxyServer(
1138 proxy_for_https);
1139 config.proxy_rules().proxies_for_ftp.SetSingleProxyServer(proxy_for_ftp);
1140 config.proxy_rules().fallback_proxies.SetSingleProxyServer(socks_proxy);
[email protected]861c6c62009-04-20 16:50:561141 }
1142 }
1143
Ramin Halavatica8d5252018-03-12 05:33:491144 if (config.proxy_rules().empty()) {
[email protected]861c6c62009-04-20 16:50:561145 // Manual mode but we couldn't parse any rules.
Anton Bikineev068d2912021-05-15 20:43:521146 return absl::nullopt;
[email protected]861c6c62009-04-20 16:50:561147 }
1148
1149 // Check for authentication, just so we can warn.
[email protected]d7395e732009-08-28 23:13:431150 bool use_auth = false;
[email protected]573c0502011-05-17 22:19:501151 setting_getter_->GetBool(SettingGetter::PROXY_USE_AUTHENTICATION,
1152 &use_auth);
[email protected]62749f182009-07-15 13:16:541153 if (use_auth) {
1154 // ProxyConfig does not support authentication parameters, but
1155 // Chrome will prompt for the password later. So we ignore
1156 // /system/http_proxy/*auth* settings.
1157 LOG(WARNING) << "Proxy authentication parameters ignored, see bug 16709";
1158 }
[email protected]861c6c62009-04-20 16:50:561159
1160 // Now the bypass list.
[email protected]7541206c2010-02-19 20:24:061161 std::vector<std::string> ignore_hosts_list;
Ramin Halavatica8d5252018-03-12 05:33:491162 config.proxy_rules().bypass_rules.Clear();
[email protected]573c0502011-05-17 22:19:501163 if (setting_getter_->GetStringList(SettingGetter::PROXY_IGNORE_HOSTS,
1164 &ignore_hosts_list)) {
Eric Romanda790f92018-11-07 19:17:151165 for (const auto& rule : ignore_hosts_list) {
Shimi Zhang13eace252020-01-31 01:49:191166 config.proxy_rules().bypass_rules.AddRuleFromString(rule);
[email protected]1a597192010-07-09 16:58:381167 }
[email protected]a8185d02010-06-11 00:19:501168 }
Shimi Zhang13eace252020-01-31 01:49:191169
1170 if (setting_getter_->UseSuffixMatching()) {
1171 RewriteRulesForSuffixMatching(&config.proxy_rules().bypass_rules);
1172 }
1173
[email protected]861c6c62009-04-20 16:50:561174 // Note that there are no settings with semantics corresponding to
[email protected]1a597192010-07-09 16:58:381175 // bypass of local names in GNOME. In KDE, "<local>" is supported
1176 // as a hostname rule.
[email protected]861c6c62009-04-20 16:50:561177
[email protected]a48bf4a2010-06-14 18:24:531178 // KDE allows one to reverse the bypass rules.
Ramin Halavatica8d5252018-03-12 05:33:491179 config.proxy_rules().reverse_bypass = setting_getter_->BypassListIsReversed();
[email protected]a48bf4a2010-06-14 18:24:531180
Ramin Halavatica8d5252018-03-12 05:33:491181 return ProxyConfigWithAnnotation(
1182 config, NetworkTrafficAnnotationTag(traffic_annotation_));
[email protected]861c6c62009-04-20 16:50:561183}
1184
thestig0c412e852016-06-30 08:04:401185ProxyConfigServiceLinux::Delegate::Delegate(
Ramin Halavatica8d5252018-03-12 05:33:491186 std::unique_ptr<base::Environment> env_var_getter,
Anton Bikineev068d2912021-05-15 20:43:521187 absl::optional<std::unique_ptr<SettingGetter>> setting_getter,
1188 absl::optional<NetworkTrafficAnnotationTag> traffic_annotation)
Eric Romancd032fb62018-05-18 21:40:131189 : env_var_getter_(std::move(env_var_getter)) {
1190 if (traffic_annotation) {
1191 traffic_annotation_ =
1192 MutableNetworkTrafficAnnotationTag(traffic_annotation.value());
1193 }
1194
1195 if (setting_getter) {
1196 setting_getter_ = std::move(setting_getter.value());
1197 return;
1198 }
1199
[email protected]573c0502011-05-17 22:19:501200 // Figure out which SettingGetterImpl to use, if any.
thestig0c412e852016-06-30 08:04:401201 switch (base::nix::GetDesktopEnvironment(env_var_getter_.get())) {
Tim Brownd9bd4752017-12-14 20:26:341202 case base::nix::DESKTOP_ENVIRONMENT_CINNAMON:
[email protected]6b0349ef2010-10-16 04:56:061203 case base::nix::DESKTOP_ENVIRONMENT_GNOME:
Tom Andersonac4d6f42017-10-13 20:14:201204 case base::nix::DESKTOP_ENVIRONMENT_PANTHEON:
[email protected]9e6c9bde2012-07-17 23:40:171205 case base::nix::DESKTOP_ENVIRONMENT_UNITY:
[email protected]8c20e3d2011-05-19 21:03:571206#if defined(USE_GIO)
1207 {
danakj8a98ca22016-04-16 02:47:361208 std::unique_ptr<SettingGetterImplGSettings> gs_getter(
1209 new SettingGetterImplGSettings());
1210 // We have to load symbols and check the GNOME version in use to decide
Tim Brown1c307cc2017-12-08 02:40:381211 // if we should use the gsettings getter. See CheckVersion().
1212 if (gs_getter->CheckVersion(env_var_getter_.get()))
inlinechan894515af2016-12-09 02:40:101213 setting_getter_ = std::move(gs_getter);
[email protected]8c20e3d2011-05-19 21:03:571214 }
1215#endif
[email protected]d7395e732009-08-28 23:13:431216 break;
[email protected]6b0349ef2010-10-16 04:56:061217 case base::nix::DESKTOP_ENVIRONMENT_KDE3:
1218 case base::nix::DESKTOP_ENVIRONMENT_KDE4:
edward.baker53bec302015-10-02 16:57:491219 case base::nix::DESKTOP_ENVIRONMENT_KDE5:
Peter Boström08e7ed82021-04-19 17:49:591220 setting_getter_ =
1221 std::make_unique<SettingGetterImplKDE>(env_var_getter_.get());
[email protected]d7395e732009-08-28 23:13:431222 break;
[email protected]6b0349ef2010-10-16 04:56:061223 case base::nix::DESKTOP_ENVIRONMENT_XFCE:
1224 case base::nix::DESKTOP_ENVIRONMENT_OTHER:
[email protected]d7395e732009-08-28 23:13:431225 break;
1226 }
1227}
1228
[email protected]d3066142011-05-10 02:36:201229void ProxyConfigServiceLinux::Delegate::SetUpAndFetchInitialConfig(
sergeyu3f923062014-09-05 01:39:401230 const scoped_refptr<base::SingleThreadTaskRunner>& glib_task_runner,
Ramin Halavatica8d5252018-03-12 05:33:491231 const scoped_refptr<base::SequencedTaskRunner>& main_task_runner,
1232 const NetworkTrafficAnnotationTag& traffic_annotation) {
1233 traffic_annotation_ = MutableNetworkTrafficAnnotationTag(traffic_annotation);
1234
[email protected]3e44697f2009-05-22 14:37:391235 // We should be running on the default glib main loop thread right
Tim Brown2a19f3b2017-12-12 01:08:401236 // now. gsettings can only be accessed from this thread.
eroman0070d412017-06-22 22:18:241237 DCHECK(glib_task_runner->RunsTasksInCurrentSequence());
sergeyu3f923062014-09-05 01:39:401238 glib_task_runner_ = glib_task_runner;
Matt Menke75765062017-11-21 01:21:161239 main_task_runner_ = main_task_runner;
[email protected]3e44697f2009-05-22 14:37:391240
Matt Menke75765062017-11-21 01:21:161241 // If we are passed a NULL |main_task_runner|, then don't set up proxy
eroman0070d412017-06-22 22:18:241242 // setting change notifications. This should not be the usual case but is
1243 // intended to/ simplify test setups.
Matt Menke75765062017-11-21 01:21:161244 if (!main_task_runner_.get())
[email protected]b30a3f52010-10-16 01:05:461245 VLOG(1) << "Monitoring of proxy setting changes is disabled";
[email protected]3e44697f2009-05-22 14:37:391246
1247 // Fetch and cache the current proxy config. The config is left in
Matt Menke75765062017-11-21 01:21:161248 // cached_config_, where GetLatestProxyConfig() running on the main TaskRunner
[email protected]3e44697f2009-05-22 14:37:391249 // will expect to find it. This is safe to do because we return
1250 // before this ProxyConfigServiceLinux is passed on to
Nicolas Arciniegad2013f92020-02-07 23:00:561251 // the ConfiguredProxyResolutionService.
[email protected]d6cb85b2009-07-23 22:10:531252
1253 // Note: It would be nice to prioritize environment variables
Tim Brown2a19f3b2017-12-12 01:08:401254 // and only fall back to gsettings if env vars were unset. But
[email protected]d6cb85b2009-07-23 22:10:531255 // gnome-terminal "helpfully" sets http_proxy and no_proxy, and it
1256 // does so even if the proxy mode is set to auto, which would
1257 // mislead us.
1258
Anton Bikineev068d2912021-05-15 20:43:521259 cached_config_ = absl::nullopt;
Eric Roman750af4b12018-02-22 22:38:531260 if (setting_getter_ && setting_getter_->Init(glib_task_runner)) {
1261 cached_config_ = GetConfigFromSettings();
1262 }
1263 if (cached_config_) {
Ramin Halavatica8d5252018-03-12 05:33:491264 VLOG(1) << "Obtained proxy settings from annotation hash code "
1265 << cached_config_->traffic_annotation().unique_id_hash_code;
[email protected]d3066142011-05-10 02:36:201266
Tim Brown2a19f3b2017-12-12 01:08:401267 // If gsettings proxy mode is "none", meaning direct, then we take
[email protected]d3066142011-05-10 02:36:201268 // that to be a valid config and will not check environment
1269 // variables. The alternative would have been to look for a proxy
Eric Roman750af4b12018-02-22 22:38:531270 // wherever we can find one.
[email protected]d3066142011-05-10 02:36:201271
1272 // Keep a copy of the config for use from this thread for
1273 // comparison with updated settings when we get notifications.
1274 reference_config_ = cached_config_;
[email protected]d3066142011-05-10 02:36:201275
Matt Menke75765062017-11-21 01:21:161276 // We only set up notifications if we have the main and file loops
1277 // available. We do this after getting the initial configuration so that we
1278 // don't have to worry about cancelling it if the initial fetch above fails.
1279 // Note that setting up notifications has the side effect of simulating a
1280 // change, so that we won't lose any updates that may have happened after
1281 // the initial fetch and before setting up notifications. We'll detect the
1282 // common case of no changes in OnCheckProxyConfigSettings() (or sooner) and
1283 // ignore it.
1284 if (main_task_runner.get()) {
eroman0070d412017-06-22 22:18:241285 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461286 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241287 if (!required_loop.get() || required_loop->RunsTasksInCurrentSequence()) {
[email protected]d3066142011-05-10 02:36:201288 // In this case we are already on an acceptable thread.
1289 SetUpNotifications();
[email protected]d7395e732009-08-28 23:13:431290 } else {
[email protected]d3066142011-05-10 02:36:201291 // Post a task to set up notifications. We don't wait for success.
kylecharf4fe5172019-02-15 18:53:491292 required_loop->PostTask(
1293 FROM_HERE,
1294 base::BindOnce(
1295 &ProxyConfigServiceLinux::Delegate::SetUpNotifications, this));
[email protected]d6cb85b2009-07-23 22:10:531296 }
[email protected]d7395e732009-08-28 23:13:431297 }
[email protected]861c6c62009-04-20 16:50:561298 }
[email protected]d6cb85b2009-07-23 22:10:531299
Eric Roman750af4b12018-02-22 22:38:531300 if (!cached_config_) {
[email protected]d6cb85b2009-07-23 22:10:531301 // We fall back on environment variables.
[email protected]3e44697f2009-05-22 14:37:391302 //
[email protected]d3066142011-05-10 02:36:201303 // Consulting environment variables doesn't need to be done from the
1304 // default glib main loop, but it's a tiny enough amount of work.
Eric Roman750af4b12018-02-22 22:38:531305 cached_config_ = GetConfigFromEnv();
1306 if (cached_config_) {
[email protected]b30a3f52010-10-16 01:05:461307 VLOG(1) << "Obtained proxy settings from environment variables";
[email protected]3e44697f2009-05-22 14:37:391308 }
[email protected]861c6c62009-04-20 16:50:561309 }
[email protected]3e44697f2009-05-22 14:37:391310}
1311
[email protected]573c0502011-05-17 22:19:501312// Depending on the SettingGetter in use, this method will be called
Tim Brown2a19f3b2017-12-12 01:08:401313// on either the UI thread (GSettings) or the file thread (KDE).
[email protected]d3066142011-05-10 02:36:201314void ProxyConfigServiceLinux::Delegate::SetUpNotifications() {
eroman0070d412017-06-22 22:18:241315 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461316 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241317 DCHECK(!required_loop.get() || required_loop->RunsTasksInCurrentSequence());
[email protected]573c0502011-05-17 22:19:501318 if (!setting_getter_->SetUpNotifications(this))
[email protected]d3066142011-05-10 02:36:201319 LOG(ERROR) << "Unable to set up proxy configuration change notifications";
1320}
1321
[email protected]119655002010-07-23 06:02:401322void ProxyConfigServiceLinux::Delegate::AddObserver(Observer* observer) {
1323 observers_.AddObserver(observer);
1324}
1325
1326void ProxyConfigServiceLinux::Delegate::RemoveObserver(Observer* observer) {
1327 observers_.RemoveObserver(observer);
1328}
1329
[email protected]3a29593d2011-04-11 10:07:521330ProxyConfigService::ConfigAvailability
Ramin Halavatica8d5252018-03-12 05:33:491331ProxyConfigServiceLinux::Delegate::GetLatestProxyConfig(
1332 ProxyConfigWithAnnotation* config) {
Matt Menke75765062017-11-21 01:21:161333 // This is called from the main TaskRunner.
1334 DCHECK(!main_task_runner_.get() ||
1335 main_task_runner_->RunsTasksInCurrentSequence());
[email protected]3e44697f2009-05-22 14:37:391336
1337 // Simply return the last proxy configuration that glib_default_loop
1338 // notified us of.
Eric Roman750af4b12018-02-22 22:38:531339 *config = GetConfigOrDirect(cached_config_);
[email protected]119655002010-07-23 06:02:401340
[email protected]3a29593d2011-04-11 10:07:521341 // We return CONFIG_VALID to indicate that *config was filled in. It is always
[email protected]119655002010-07-23 06:02:401342 // going to be available since we initialized eagerly on the UI thread.
1343 // TODO(eroman): do lazy initialization instead, so we no longer need
1344 // to construct ProxyConfigServiceLinux on the UI thread.
1345 // In which case, we may return false here.
[email protected]3a29593d2011-04-11 10:07:521346 return CONFIG_VALID;
[email protected]3e44697f2009-05-22 14:37:391347}
1348
[email protected]573c0502011-05-17 22:19:501349// Depending on the SettingGetter in use, this method will be called
Tim Brown2a19f3b2017-12-12 01:08:401350// on either the UI thread (GSettings) or the file thread (KDE).
[email protected]3e44697f2009-05-22 14:37:391351void ProxyConfigServiceLinux::Delegate::OnCheckProxyConfigSettings() {
eroman0070d412017-06-22 22:18:241352 scoped_refptr<base::SequencedTaskRunner> required_loop =
[email protected]76722472012-05-24 08:26:461353 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241354 DCHECK(!required_loop.get() || required_loop->RunsTasksInCurrentSequence());
Anton Bikineev068d2912021-05-15 20:43:521355 absl::optional<ProxyConfigWithAnnotation> new_config =
Ramin Halavatica8d5252018-03-12 05:33:491356 GetConfigFromSettings();
[email protected]3e44697f2009-05-22 14:37:391357
[email protected]119655002010-07-23 06:02:401358 // See if it is different from what we had before.
Eric Roman750af4b12018-02-22 22:38:531359 if (new_config.has_value() != reference_config_.has_value() ||
Eric Roman3e185842018-06-01 18:10:521360 (new_config.has_value() &&
1361 !new_config->value().Equals(reference_config_->value()))) {
Matt Menke75765062017-11-21 01:21:161362 // Post a task to the main TaskRunner with the new configuration, so it can
[email protected]3e44697f2009-05-22 14:37:391363 // update |cached_config_|.
Matt Menke75765062017-11-21 01:21:161364 main_task_runner_->PostTask(
1365 FROM_HERE,
kylecharf4fe5172019-02-15 18:53:491366 base::BindOnce(&ProxyConfigServiceLinux::Delegate::SetNewProxyConfig,
1367 this, new_config));
[email protected]d1f9d472009-08-13 19:59:301368 // Update the thread-private copy in |reference_config_| as well.
1369 reference_config_ = new_config;
[email protected]d3066142011-05-10 02:36:201370 } else {
1371 VLOG(1) << "Detected no-op change to proxy settings. Doing nothing.";
[email protected]3e44697f2009-05-22 14:37:391372 }
1373}
1374
1375void ProxyConfigServiceLinux::Delegate::SetNewProxyConfig(
Anton Bikineev068d2912021-05-15 20:43:521376 const absl::optional<ProxyConfigWithAnnotation>& new_config) {
Matt Menke75765062017-11-21 01:21:161377 DCHECK(main_task_runner_->RunsTasksInCurrentSequence());
[email protected]b30a3f52010-10-16 01:05:461378 VLOG(1) << "Proxy configuration changed";
[email protected]3e44697f2009-05-22 14:37:391379 cached_config_ = new_config;
Eric Roman750af4b12018-02-22 22:38:531380 for (auto& observer : observers_) {
1381 observer.OnProxyConfigChanged(GetConfigOrDirect(new_config),
1382 ProxyConfigService::CONFIG_VALID);
1383 }
[email protected]3e44697f2009-05-22 14:37:391384}
1385
1386void ProxyConfigServiceLinux::Delegate::PostDestroyTask() {
thestig0c412e852016-06-30 08:04:401387 if (!setting_getter_)
[email protected]d7395e732009-08-28 23:13:431388 return;
thestig0c412e852016-06-30 08:04:401389
eroman0070d412017-06-22 22:18:241390 scoped_refptr<base::SequencedTaskRunner> shutdown_loop =
[email protected]76722472012-05-24 08:26:461391 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241392 if (!shutdown_loop.get() || shutdown_loop->RunsTasksInCurrentSequence()) {
[email protected]3e44697f2009-05-22 14:37:391393 // Already on the right thread, call directly.
1394 // This is the case for the unittests.
1395 OnDestroy();
1396 } else {
[email protected]d7395e732009-08-28 23:13:431397 // Post to shutdown thread. Note that on browser shutdown, we may quit
1398 // this MessageLoop and exit the program before ever running this.
kylecharf4fe5172019-02-15 18:53:491399 shutdown_loop->PostTask(
1400 FROM_HERE,
1401 base::BindOnce(&ProxyConfigServiceLinux::Delegate::OnDestroy, this));
[email protected]3e44697f2009-05-22 14:37:391402 }
1403}
1404void ProxyConfigServiceLinux::Delegate::OnDestroy() {
eroman0070d412017-06-22 22:18:241405 scoped_refptr<base::SequencedTaskRunner> shutdown_loop =
[email protected]76722472012-05-24 08:26:461406 setting_getter_->GetNotificationTaskRunner();
eroman0070d412017-06-22 22:18:241407 DCHECK(!shutdown_loop.get() || shutdown_loop->RunsTasksInCurrentSequence());
[email protected]573c0502011-05-17 22:19:501408 setting_getter_->ShutDown();
[email protected]3e44697f2009-05-22 14:37:391409}
1410
1411ProxyConfigServiceLinux::ProxyConfigServiceLinux()
Eric Romancd032fb62018-05-18 21:40:131412 : delegate_(new Delegate(base::Environment::Create(),
Anton Bikineev068d2912021-05-15 20:43:521413 absl::nullopt,
1414 absl::nullopt)) {}
[email protected]3e44697f2009-05-22 14:37:391415
[email protected]8e1845e12010-09-15 19:22:241416ProxyConfigServiceLinux::~ProxyConfigServiceLinux() {
1417 delegate_->PostDestroyTask();
1418}
1419
[email protected]3e44697f2009-05-22 14:37:391420ProxyConfigServiceLinux::ProxyConfigServiceLinux(
Ramin Halavatica8d5252018-03-12 05:33:491421 std::unique_ptr<base::Environment> env_var_getter,
1422 const NetworkTrafficAnnotationTag& traffic_annotation)
Eric Romancd032fb62018-05-18 21:40:131423 : delegate_(new Delegate(std::move(env_var_getter),
Anton Bikineev068d2912021-05-15 20:43:521424 absl::nullopt,
Eric Romancd032fb62018-05-18 21:40:131425 traffic_annotation)) {}
[email protected]9a3d8d42009-09-03 17:01:461426
1427ProxyConfigServiceLinux::ProxyConfigServiceLinux(
thestig0c412e852016-06-30 08:04:401428 std::unique_ptr<base::Environment> env_var_getter,
Ramin Halavatica8d5252018-03-12 05:33:491429 SettingGetter* setting_getter,
1430 const NetworkTrafficAnnotationTag& traffic_annotation)
1431 : delegate_(new Delegate(std::move(env_var_getter),
Eric Romancd032fb62018-05-18 21:40:131432 base::WrapUnique(setting_getter),
Ramin Halavatica8d5252018-03-12 05:33:491433 traffic_annotation)) {}
[email protected]861c6c62009-04-20 16:50:561434
[email protected]e4be2dd2010-12-14 00:44:391435void ProxyConfigServiceLinux::AddObserver(Observer* observer) {
1436 delegate_->AddObserver(observer);
1437}
1438
1439void ProxyConfigServiceLinux::RemoveObserver(Observer* observer) {
1440 delegate_->RemoveObserver(observer);
1441}
1442
[email protected]3a29593d2011-04-11 10:07:521443ProxyConfigService::ConfigAvailability
Ramin Halavatica8d5252018-03-12 05:33:491444ProxyConfigServiceLinux::GetLatestProxyConfig(
1445 ProxyConfigWithAnnotation* config) {
[email protected]e4be2dd2010-12-14 00:44:391446 return delegate_->GetLatestProxyConfig(config);
1447}
1448
[email protected]861c6c62009-04-20 16:50:561449} // namespace net