1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/net/firefox_proxy_settings.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_tokenizer.h"
12 #include "base/strings/string_util.h"
13 #include "base/values.h"
14 #include "chrome/common/importer/firefox_importer_utils.h"
15 #include "net/proxy/proxy_config.h"
19 const char* const kNetworkProxyTypeKey
= "network.proxy.type";
20 const char* const kHTTPProxyKey
= "network.proxy.http";
21 const char* const kHTTPProxyPortKey
= "network.proxy.http_port";
22 const char* const kSSLProxyKey
= "network.proxy.ssl";
23 const char* const kSSLProxyPortKey
= "network.proxy.ssl_port";
24 const char* const kFTPProxyKey
= "network.proxy.ftp";
25 const char* const kFTPProxyPortKey
= "network.proxy.ftp_port";
26 const char* const kGopherProxyKey
= "network.proxy.gopher";
27 const char* const kGopherProxyPortKey
= "network.proxy.gopher_port";
28 const char* const kSOCKSHostKey
= "network.proxy.socks";
29 const char* const kSOCKSHostPortKey
= "network.proxy.socks_port";
30 const char* const kSOCKSVersionKey
= "network.proxy.socks_version";
31 const char* const kAutoconfigURL
= "network.proxy.autoconfig_url";
32 const char* const kNoProxyListKey
= "network.proxy.no_proxies_on";
33 const char* const kPrefFileName
= "prefs.js";
35 FirefoxProxySettings::ProxyConfig
IntToProxyConfig(int type
) {
38 return FirefoxProxySettings::MANUAL
;
40 return FirefoxProxySettings::AUTO_FROM_URL
;
42 return FirefoxProxySettings::AUTO_DETECT
;
44 return FirefoxProxySettings::SYSTEM
;
46 LOG(ERROR
) << "Unknown Firefox proxy config type: " << type
;
47 return FirefoxProxySettings::NO_PROXY
;
51 FirefoxProxySettings::SOCKSVersion
IntToSOCKSVersion(int type
) {
54 return FirefoxProxySettings::V4
;
56 return FirefoxProxySettings::V5
;
58 LOG(ERROR
) << "Unknown Firefox proxy config type: " << type
;
59 return FirefoxProxySettings::UNKNONW
;
63 // Parses the prefs found in the file |pref_file| and puts the key/value pairs
64 // in |prefs|. Keys are strings, and values can be strings, booleans or
65 // integers. Returns true if it succeeded, false otherwise (in which case
66 // |prefs| is not filled).
67 // Note: for strings, only valid UTF-8 string values are supported. If a
68 // key/pair is not valid UTF-8, it is ignored and will not appear in |prefs|.
69 bool ParsePrefFile(const base::FilePath
& pref_file
,
70 base::DictionaryValue
* prefs
) {
71 // The string that is before a pref key.
72 const std::string kUserPrefString
= "user_pref(\"";
74 if (!base::ReadFileToString(pref_file
, &contents
))
77 for (const std::string
& line
:
78 base::SplitString(contents
, "\n", base::KEEP_WHITESPACE
,
79 base::SPLIT_WANT_NONEMPTY
)) {
80 size_t start_key
= line
.find(kUserPrefString
);
81 if (start_key
== std::string::npos
)
82 continue; // Could be a comment or a blank line.
83 start_key
+= kUserPrefString
.length();
84 size_t stop_key
= line
.find('"', start_key
);
85 if (stop_key
== std::string::npos
) {
86 LOG(ERROR
) << "Invalid key found in Firefox pref file '" <<
87 pref_file
.value() << "' line is '" << line
<< "'.";
90 std::string key
= line
.substr(start_key
, stop_key
- start_key
);
91 size_t start_value
= line
.find(',', stop_key
+ 1);
92 if (start_value
== std::string::npos
) {
93 LOG(ERROR
) << "Invalid value found in Firefox pref file '" <<
94 pref_file
.value() << "' line is '" << line
<< "'.";
97 size_t stop_value
= line
.find(");", start_value
+ 1);
98 if (stop_value
== std::string::npos
) {
99 LOG(ERROR
) << "Invalid value found in Firefox pref file '" <<
100 pref_file
.value() << "' line is '" << line
<< "'.";
103 std::string value
= line
.substr(start_value
+ 1,
104 stop_value
- start_value
- 1);
105 base::TrimWhitespace(value
, base::TRIM_ALL
, &value
);
106 // Value could be a boolean.
107 bool is_value_true
= base::LowerCaseEqualsASCII(value
, "true");
108 if (is_value_true
|| base::LowerCaseEqualsASCII(value
, "false")) {
109 prefs
->SetBoolean(key
, is_value_true
);
113 // Value could be a string.
114 if (value
.size() >= 2U &&
115 value
[0] == '"' && value
[value
.size() - 1] == '"') {
116 value
= value
.substr(1, value
.size() - 2);
117 // ValueString only accept valid UTF-8. Simply ignore that entry if it is
119 if (base::IsStringUTF8(value
))
120 prefs
->SetString(key
, value
);
122 VLOG(1) << "Non UTF8 value for key " << key
<< ", ignored.";
126 // Or value could be an integer.
128 if (base::StringToInt(value
, &int_value
)) {
129 prefs
->SetInteger(key
, int_value
);
133 LOG(ERROR
) << "Invalid value found in Firefox pref file '"
134 << pref_file
.value() << "' value is '" << value
<< "'.";
141 FirefoxProxySettings::FirefoxProxySettings() {
145 FirefoxProxySettings::~FirefoxProxySettings() {
148 void FirefoxProxySettings::Reset() {
149 config_type_
= NO_PROXY
;
151 http_proxy_port_
= 0;
156 gopher_proxy_
.clear();
157 gopher_proxy_port_
= 0;
160 socks_version_
= UNKNONW
;
161 proxy_bypass_list_
.clear();
162 autoconfig_url_
.clear();
166 bool FirefoxProxySettings::GetSettings(FirefoxProxySettings
* settings
) {
170 base::FilePath profile_path
= GetFirefoxProfilePath();
171 if (profile_path
.empty())
173 base::FilePath pref_file
= profile_path
.AppendASCII(kPrefFileName
);
174 return GetSettingsFromFile(pref_file
, settings
);
177 bool FirefoxProxySettings::ToProxyConfig(net::ProxyConfig
* config
) {
178 switch (config_type()) {
180 *config
= net::ProxyConfig::CreateDirect();
183 *config
= net::ProxyConfig::CreateAutoDetect();
186 *config
= net::ProxyConfig::CreateFromCustomPacURL(
187 GURL(autoconfig_url()));
190 // Can't convert this directly to a ProxyConfig.
193 // Handled outside of the switch (since it is a lot of code.)
200 // The rest of this funciton is for handling the MANUAL case.
201 DCHECK_EQ(MANUAL
, config_type());
203 *config
= net::ProxyConfig();
204 config
->proxy_rules().type
=
205 net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME
;
207 if (!http_proxy().empty()) {
208 config
->proxy_rules().proxies_for_http
.SetSingleProxyServer(
210 net::ProxyServer::SCHEME_HTTP
,
211 net::HostPortPair(http_proxy(), http_proxy_port())));
214 if (!ftp_proxy().empty()) {
215 config
->proxy_rules().proxies_for_ftp
.SetSingleProxyServer(
217 net::ProxyServer::SCHEME_HTTP
,
218 net::HostPortPair(ftp_proxy(), ftp_proxy_port())));
221 if (!ssl_proxy().empty()) {
222 config
->proxy_rules().proxies_for_https
.SetSingleProxyServer(
224 net::ProxyServer::SCHEME_HTTP
,
225 net::HostPortPair(ssl_proxy(), ssl_proxy_port())));
228 if (!socks_host().empty()) {
229 net::ProxyServer::Scheme proxy_scheme
= V5
== socks_version() ?
230 net::ProxyServer::SCHEME_SOCKS5
: net::ProxyServer::SCHEME_SOCKS4
;
232 config
->proxy_rules().fallback_proxies
.SetSingleProxyServer(
235 net::HostPortPair(socks_host(), socks_port())));
238 config
->proxy_rules().bypass_rules
.ParseFromStringUsingSuffixMatching(
239 base::JoinString(proxy_bypass_list_
, ";"));
245 bool FirefoxProxySettings::GetSettingsFromFile(const base::FilePath
& pref_file
,
246 FirefoxProxySettings
* settings
) {
247 base::DictionaryValue dictionary
;
248 if (!ParsePrefFile(pref_file
, &dictionary
))
252 if (!dictionary
.GetInteger(kNetworkProxyTypeKey
, &proxy_type
))
253 return true; // No type means no proxy.
255 settings
->config_type_
= IntToProxyConfig(proxy_type
);
256 if (settings
->config_type_
== AUTO_FROM_URL
) {
257 if (!dictionary
.GetStringASCII(kAutoconfigURL
,
258 &(settings
->autoconfig_url_
))) {
259 LOG(ERROR
) << "Failed to retrieve Firefox proxy autoconfig URL";
264 if (settings
->config_type_
== MANUAL
) {
265 if (!dictionary
.GetStringASCII(kHTTPProxyKey
, &(settings
->http_proxy_
)))
266 LOG(ERROR
) << "Failed to retrieve Firefox proxy HTTP host";
267 if (!dictionary
.GetInteger(kHTTPProxyPortKey
,
268 &(settings
->http_proxy_port_
))) {
269 LOG(ERROR
) << "Failed to retrieve Firefox proxy HTTP port";
271 if (!dictionary
.GetStringASCII(kSSLProxyKey
, &(settings
->ssl_proxy_
)))
272 LOG(ERROR
) << "Failed to retrieve Firefox proxy SSL host";
273 if (!dictionary
.GetInteger(kSSLProxyPortKey
, &(settings
->ssl_proxy_port_
)))
274 LOG(ERROR
) << "Failed to retrieve Firefox proxy SSL port";
275 if (!dictionary
.GetStringASCII(kFTPProxyKey
, &(settings
->ftp_proxy_
)))
276 LOG(ERROR
) << "Failed to retrieve Firefox proxy FTP host";
277 if (!dictionary
.GetInteger(kFTPProxyPortKey
, &(settings
->ftp_proxy_port_
)))
278 LOG(ERROR
) << "Failed to retrieve Firefox proxy SSL port";
279 if (!dictionary
.GetStringASCII(kGopherProxyKey
, &(settings
->gopher_proxy_
)))
280 LOG(ERROR
) << "Failed to retrieve Firefox proxy gopher host";
281 if (!dictionary
.GetInteger(kGopherProxyPortKey
,
282 &(settings
->gopher_proxy_port_
))) {
283 LOG(ERROR
) << "Failed to retrieve Firefox proxy gopher port";
285 if (!dictionary
.GetStringASCII(kSOCKSHostKey
, &(settings
->socks_host_
)))
286 LOG(ERROR
) << "Failed to retrieve Firefox SOCKS host";
287 if (!dictionary
.GetInteger(kSOCKSHostPortKey
, &(settings
->socks_port_
)))
288 LOG(ERROR
) << "Failed to retrieve Firefox SOCKS port";
290 if (dictionary
.GetInteger(kSOCKSVersionKey
, &socks_version
))
291 settings
->socks_version_
= IntToSOCKSVersion(socks_version
);
293 std::string proxy_bypass
;
294 if (dictionary
.GetStringASCII(kNoProxyListKey
, &proxy_bypass
) &&
295 !proxy_bypass
.empty()) {
296 base::StringTokenizer
string_tok(proxy_bypass
, ",");
297 while (string_tok
.GetNext()) {
298 std::string token
= string_tok
.token();
299 base::TrimWhitespaceASCII(token
, base::TRIM_ALL
, &token
);
301 settings
->proxy_bypass_list_
.push_back(token
);