1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/net/firefox_proxy_settings.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chrome/common/importer/firefox_importer_utils.h"
#include "net/proxy/proxy_config.h"
namespace {
const char* const kNetworkProxyTypeKey = "network.proxy.type";
const char* const kHTTPProxyKey = "network.proxy.http";
const char* const kHTTPProxyPortKey = "network.proxy.http_port";
const char* const kSSLProxyKey = "network.proxy.ssl";
const char* const kSSLProxyPortKey = "network.proxy.ssl_port";
const char* const kFTPProxyKey = "network.proxy.ftp";
const char* const kFTPProxyPortKey = "network.proxy.ftp_port";
const char* const kGopherProxyKey = "network.proxy.gopher";
const char* const kGopherProxyPortKey = "network.proxy.gopher_port";
const char* const kSOCKSHostKey = "network.proxy.socks";
const char* const kSOCKSHostPortKey = "network.proxy.socks_port";
const char* const kSOCKSVersionKey = "network.proxy.socks_version";
const char* const kAutoconfigURL = "network.proxy.autoconfig_url";
const char* const kNoProxyListKey = "network.proxy.no_proxies_on";
const char* const kPrefFileName = "prefs.js";
FirefoxProxySettings::ProxyConfig IntToProxyConfig(int type) {
switch (type) {
case 1:
return FirefoxProxySettings::MANUAL;
case 2:
return FirefoxProxySettings::AUTO_FROM_URL;
case 4:
return FirefoxProxySettings::AUTO_DETECT;
case 5:
return FirefoxProxySettings::SYSTEM;
default:
LOG(ERROR) << "Unknown Firefox proxy config type: " << type;
return FirefoxProxySettings::NO_PROXY;
}
}
FirefoxProxySettings::SOCKSVersion IntToSOCKSVersion(int type) {
switch (type) {
case 4:
return FirefoxProxySettings::V4;
case 5:
return FirefoxProxySettings::V5;
default:
LOG(ERROR) << "Unknown Firefox proxy config type: " << type;
return FirefoxProxySettings::UNKNONW;
}
}
// Parses the prefs found in the file |pref_file| and puts the key/value pairs
// in |prefs|. Keys are strings, and values can be strings, booleans or
// integers. Returns true if it succeeded, false otherwise (in which case
// |prefs| is not filled).
// Note: for strings, only valid UTF-8 string values are supported. If a
// key/pair is not valid UTF-8, it is ignored and will not appear in |prefs|.
bool ParsePrefFile(const base::FilePath& pref_file,
base::DictionaryValue* prefs) {
// The string that is before a pref key.
const std::string kUserPrefString = "user_pref(\"";
std::string contents;
if (!base::ReadFileToString(pref_file, &contents))
return false;
std::vector<std::string> lines;
Tokenize(contents, "\n", &lines);
for (std::vector<std::string>::const_iterator iter = lines.begin();
iter != lines.end(); ++iter) {
const std::string& line = *iter;
size_t start_key = line.find(kUserPrefString);
if (start_key == std::string::npos)
continue; // Could be a comment or a blank line.
start_key += kUserPrefString.length();
size_t stop_key = line.find('"', start_key);
if (stop_key == std::string::npos) {
LOG(ERROR) << "Invalid key found in Firefox pref file '" <<
pref_file.value() << "' line is '" << line << "'.";
continue;
}
std::string key = line.substr(start_key, stop_key - start_key);
size_t start_value = line.find(',', stop_key + 1);
if (start_value == std::string::npos) {
LOG(ERROR) << "Invalid value found in Firefox pref file '" <<
pref_file.value() << "' line is '" << line << "'.";
continue;
}
size_t stop_value = line.find(");", start_value + 1);
if (stop_value == std::string::npos) {
LOG(ERROR) << "Invalid value found in Firefox pref file '" <<
pref_file.value() << "' line is '" << line << "'.";
continue;
}
std::string value = line.substr(start_value + 1,
stop_value - start_value - 1);
base::TrimWhitespace(value, base::TRIM_ALL, &value);
// Value could be a boolean.
bool is_value_true = LowerCaseEqualsASCII(value, "true");
if (is_value_true || LowerCaseEqualsASCII(value, "false")) {
prefs->SetBoolean(key, is_value_true);
continue;
}
// Value could be a string.
if (value.size() >= 2U &&
value[0] == '"' && value[value.size() - 1] == '"') {
value = value.substr(1, value.size() - 2);
// ValueString only accept valid UTF-8. Simply ignore that entry if it is
// not UTF-8.
if (base::IsStringUTF8(value))
prefs->SetString(key, value);
else
VLOG(1) << "Non UTF8 value for key " << key << ", ignored.";
continue;
}
// Or value could be an integer.
int int_value = 0;
if (base::StringToInt(value, &int_value)) {
prefs->SetInteger(key, int_value);
continue;
}
LOG(ERROR) << "Invalid value found in Firefox pref file '"
<< pref_file.value() << "' value is '" << value << "'.";
}
return true;
}
} // namespace
FirefoxProxySettings::FirefoxProxySettings() {
Reset();
}
FirefoxProxySettings::~FirefoxProxySettings() {
}
void FirefoxProxySettings::Reset() {
config_type_ = NO_PROXY;
http_proxy_.clear();
http_proxy_port_ = 0;
ssl_proxy_.clear();
ssl_proxy_port_ = 0;
ftp_proxy_.clear();
ftp_proxy_port_ = 0;
gopher_proxy_.clear();
gopher_proxy_port_ = 0;
socks_host_.clear();
socks_port_ = 0;
socks_version_ = UNKNONW;
proxy_bypass_list_.clear();
autoconfig_url_.clear();
}
// static
bool FirefoxProxySettings::GetSettings(FirefoxProxySettings* settings) {
DCHECK(settings);
settings->Reset();
base::FilePath profile_path = GetFirefoxProfilePath();
if (profile_path.empty())
return false;
base::FilePath pref_file = profile_path.AppendASCII(kPrefFileName);
return GetSettingsFromFile(pref_file, settings);
}
bool FirefoxProxySettings::ToProxyConfig(net::ProxyConfig* config) {
switch (config_type()) {
case NO_PROXY:
*config = net::ProxyConfig::CreateDirect();
return true;
case AUTO_DETECT:
*config = net::ProxyConfig::CreateAutoDetect();
return true;
case AUTO_FROM_URL:
*config = net::ProxyConfig::CreateFromCustomPacURL(
GURL(autoconfig_url()));
return true;
case SYSTEM:
// Can't convert this directly to a ProxyConfig.
return false;
case MANUAL:
// Handled outside of the switch (since it is a lot of code.)
break;
default:
NOTREACHED();
return false;
}
// The rest of this funciton is for handling the MANUAL case.
DCHECK_EQ(MANUAL, config_type());
*config = net::ProxyConfig();
config->proxy_rules().type =
net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
if (!http_proxy().empty()) {
config->proxy_rules().proxies_for_http.SetSingleProxyServer(
net::ProxyServer(
net::ProxyServer::SCHEME_HTTP,
net::HostPortPair(http_proxy(), http_proxy_port())));
}
if (!ftp_proxy().empty()) {
config->proxy_rules().proxies_for_ftp.SetSingleProxyServer(
net::ProxyServer(
net::ProxyServer::SCHEME_HTTP,
net::HostPortPair(ftp_proxy(), ftp_proxy_port())));
}
if (!ssl_proxy().empty()) {
config->proxy_rules().proxies_for_https.SetSingleProxyServer(
net::ProxyServer(
net::ProxyServer::SCHEME_HTTP,
net::HostPortPair(ssl_proxy(), ssl_proxy_port())));
}
if (!socks_host().empty()) {
net::ProxyServer::Scheme proxy_scheme = V5 == socks_version() ?
net::ProxyServer::SCHEME_SOCKS5 : net::ProxyServer::SCHEME_SOCKS4;
config->proxy_rules().fallback_proxies.SetSingleProxyServer(
net::ProxyServer(
proxy_scheme,
net::HostPortPair(socks_host(), socks_port())));
}
config->proxy_rules().bypass_rules.ParseFromStringUsingSuffixMatching(
JoinString(proxy_bypass_list_, ';'));
return true;
}
// static
bool FirefoxProxySettings::GetSettingsFromFile(const base::FilePath& pref_file,
FirefoxProxySettings* settings) {
base::DictionaryValue dictionary;
if (!ParsePrefFile(pref_file, &dictionary))
return false;
int proxy_type = 0;
if (!dictionary.GetInteger(kNetworkProxyTypeKey, &proxy_type))
return true; // No type means no proxy.
settings->config_type_ = IntToProxyConfig(proxy_type);
if (settings->config_type_ == AUTO_FROM_URL) {
if (!dictionary.GetStringASCII(kAutoconfigURL,
&(settings->autoconfig_url_))) {
LOG(ERROR) << "Failed to retrieve Firefox proxy autoconfig URL";
}
return true;
}
if (settings->config_type_ == MANUAL) {
if (!dictionary.GetStringASCII(kHTTPProxyKey, &(settings->http_proxy_)))
LOG(ERROR) << "Failed to retrieve Firefox proxy HTTP host";
if (!dictionary.GetInteger(kHTTPProxyPortKey,
&(settings->http_proxy_port_))) {
LOG(ERROR) << "Failed to retrieve Firefox proxy HTTP port";
}
if (!dictionary.GetStringASCII(kSSLProxyKey, &(settings->ssl_proxy_)))
LOG(ERROR) << "Failed to retrieve Firefox proxy SSL host";
if (!dictionary.GetInteger(kSSLProxyPortKey, &(settings->ssl_proxy_port_)))
LOG(ERROR) << "Failed to retrieve Firefox proxy SSL port";
if (!dictionary.GetStringASCII(kFTPProxyKey, &(settings->ftp_proxy_)))
LOG(ERROR) << "Failed to retrieve Firefox proxy FTP host";
if (!dictionary.GetInteger(kFTPProxyPortKey, &(settings->ftp_proxy_port_)))
LOG(ERROR) << "Failed to retrieve Firefox proxy SSL port";
if (!dictionary.GetStringASCII(kGopherProxyKey, &(settings->gopher_proxy_)))
LOG(ERROR) << "Failed to retrieve Firefox proxy gopher host";
if (!dictionary.GetInteger(kGopherProxyPortKey,
&(settings->gopher_proxy_port_))) {
LOG(ERROR) << "Failed to retrieve Firefox proxy gopher port";
}
if (!dictionary.GetStringASCII(kSOCKSHostKey, &(settings->socks_host_)))
LOG(ERROR) << "Failed to retrieve Firefox SOCKS host";
if (!dictionary.GetInteger(kSOCKSHostPortKey, &(settings->socks_port_)))
LOG(ERROR) << "Failed to retrieve Firefox SOCKS port";
int socks_version;
if (dictionary.GetInteger(kSOCKSVersionKey, &socks_version))
settings->socks_version_ = IntToSOCKSVersion(socks_version);
std::string proxy_bypass;
if (dictionary.GetStringASCII(kNoProxyListKey, &proxy_bypass) &&
!proxy_bypass.empty()) {
base::StringTokenizer string_tok(proxy_bypass, ",");
while (string_tok.GetNext()) {
std::string token = string_tok.token();
base::TrimWhitespaceASCII(token, base::TRIM_ALL, &token);
if (!token.empty())
settings->proxy_bypass_list_.push_back(token);
}
}
}
return true;
}
|