|
Added
Link Here
|
| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* ***** BEGIN LICENSE BLOCK ***** |
| 3 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 4 |
* |
| 5 |
* The contents of this file are subject to the Mozilla Public License Version |
| 6 |
* 1.1 (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* http://www.mozilla.org/MPL/ |
| 9 |
* |
| 10 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 11 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 12 |
* for the specific language governing rights and limitations under the |
| 13 |
* License. |
| 14 |
* |
| 15 |
* The Original Code is mozilla.org code. |
| 16 |
* |
| 17 |
* The Initial Developer of the Original Code is |
| 18 |
* Netscape Communications Corporation. |
| 19 |
* Portions created by the Initial Developer are Copyright (C) 1998 |
| 20 |
* the Initial Developer. All Rights Reserved. |
| 21 |
* |
| 22 |
* Contributor(s): |
| 23 |
* Robert O'Callahan (rocallahan@novell.com) |
| 24 |
* |
| 25 |
* Alternatively, the contents of this file may be used under the terms of |
| 26 |
* either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 |
* of those above. If you wish to allow use of your version of this file only |
| 30 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 |
* use your version of this file under the terms of the MPL, indicate your |
| 32 |
* decision by deleting the provisions above and replace them with the notice |
| 33 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 |
* the provisions above, a recipient may use your version of this file under |
| 35 |
* the terms of any one of the MPL, the GPL or the LGPL. |
| 36 |
* |
| 37 |
* ***** END LICENSE BLOCK ***** */ |
| 38 |
|
| 39 |
#include "nsISystemProxySettings.h" |
| 40 |
#include "nsIGenericFactory.h" |
| 41 |
#include "nsIServiceManager.h" |
| 42 |
#include "nsIGConfService.h" |
| 43 |
#include "nsIURI.h" |
| 44 |
#include "nsReadableUtils.h" |
| 45 |
#include "nsArray.h" |
| 46 |
#include "prnetdb.h" |
| 47 |
#include "prenv.h" |
| 48 |
#include "nsPrintfCString.h" |
| 49 |
#include "nsNetUtil.h" |
| 50 |
#include "nsISupportsPrimitives.h" |
| 51 |
|
| 52 |
class nsUnixSystemProxySettings : public nsISystemProxySettings { |
| 53 |
public: |
| 54 |
NS_DECL_ISUPPORTS |
| 55 |
NS_DECL_NSISYSTEMPROXYSETTINGS |
| 56 |
|
| 57 |
nsUnixSystemProxySettings() {} |
| 58 |
nsresult Init(); |
| 59 |
|
| 60 |
private: |
| 61 |
~nsUnixSystemProxySettings() {} |
| 62 |
|
| 63 |
nsCOMPtr<nsIGConfService> mGConf; |
| 64 |
}; |
| 65 |
|
| 66 |
NS_IMPL_ISUPPORTS1(nsUnixSystemProxySettings, nsISystemProxySettings) |
| 67 |
|
| 68 |
nsresult |
| 69 |
nsUnixSystemProxySettings::Init() |
| 70 |
{ |
| 71 |
// If this is a GNOME session, load gconf and try to use its preferences. |
| 72 |
// If gconf is not available (which would be stupid) we'll proceed as if this |
| 73 |
// was not a GNOME session, using *_PROXY environment variables. |
| 74 |
const char* sessionType = PR_GetEnv("DESKTOP_SESSION"); |
| 75 |
if (sessionType && !strcmp(sessionType, "gnome")) { |
| 76 |
mGConf = do_GetService(NS_GCONFSERVICE_CONTRACTID); |
| 77 |
} |
| 78 |
return NS_OK; |
| 79 |
} |
| 80 |
|
| 81 |
static PRBool |
| 82 |
IsProxyMode(nsIGConfService* aGConf, const char* aMode) |
| 83 |
{ |
| 84 |
nsCAutoString mode; |
| 85 |
return NS_SUCCEEDED(aGConf->GetString(NS_LITERAL_CSTRING("/system/proxy/mode"), mode)) && |
| 86 |
mode.EqualsASCII(aMode); |
| 87 |
} |
| 88 |
|
| 89 |
nsresult |
| 90 |
nsUnixSystemProxySettings::GetPACURI(nsACString& aResult) |
| 91 |
{ |
| 92 |
if (!mGConf || !IsProxyMode(mGConf, "auto")) |
| 93 |
return NS_ERROR_FAILURE; |
| 94 |
return mGConf->GetString(NS_LITERAL_CSTRING("/system/proxy/autoconfig_url"), |
| 95 |
aResult); |
| 96 |
} |
| 97 |
|
| 98 |
static PRBool |
| 99 |
IsInNoProxyList(const nsACString& aHost, PRInt32 aPort, const char* noProxyVal) |
| 100 |
{ |
| 101 |
NS_ASSERTION(aPort >= 0, "Negative port?"); |
| 102 |
|
| 103 |
nsCAutoString noProxy(noProxyVal); |
| 104 |
if (noProxy.EqualsLiteral("*")) |
| 105 |
return PR_TRUE; |
| 106 |
|
| 107 |
noProxy.StripWhitespace(); |
| 108 |
|
| 109 |
nsReadingIterator<char> pos; |
| 110 |
nsReadingIterator<char> end; |
| 111 |
noProxy.BeginReading(pos); |
| 112 |
noProxy.EndReading(end); |
| 113 |
while (pos != end) { |
| 114 |
nsReadingIterator<char> last = pos; |
| 115 |
nsReadingIterator<char> nextPos; |
| 116 |
if (FindCharInReadable(',', last, end)) { |
| 117 |
nextPos = last; |
| 118 |
++nextPos; |
| 119 |
} else { |
| 120 |
last = end; |
| 121 |
nextPos = end; |
| 122 |
} |
| 123 |
|
| 124 |
nsReadingIterator<char> colon = pos; |
| 125 |
PRInt32 port = -1; |
| 126 |
if (FindCharInReadable(':', colon, last)) { |
| 127 |
++colon; |
| 128 |
nsDependentCSubstring portStr(colon, last); |
| 129 |
nsCAutoString portStr2(portStr); |
| 130 |
PRInt32 err; |
| 131 |
port = portStr2.ToInteger(&err); |
| 132 |
if (err != 0) { |
| 133 |
port = -2; // don't match any port, so we ignore this pattern |
| 134 |
} |
| 135 |
--colon; |
| 136 |
} else { |
| 137 |
colon = last; |
| 138 |
} |
| 139 |
|
| 140 |
if (port == -1 || port == aPort) { |
| 141 |
nsDependentCSubstring hostStr(pos, colon); |
| 142 |
if (StringEndsWith(aHost, hostStr, nsCaseInsensitiveCStringComparator())) |
| 143 |
return PR_TRUE; |
| 144 |
} |
| 145 |
|
| 146 |
pos = nextPos; |
| 147 |
} |
| 148 |
|
| 149 |
return PR_FALSE; |
| 150 |
} |
| 151 |
|
| 152 |
static void SetProxyResult(const char* aType, const nsACString& aHost, |
| 153 |
PRInt32 aPort, nsACString& aResult) |
| 154 |
{ |
| 155 |
aResult.AppendASCII(aType); |
| 156 |
aResult.Append(' '); |
| 157 |
aResult.Append(aHost); |
| 158 |
aResult.Append(':'); |
| 159 |
aResult.Append(nsPrintfCString("%d", aPort)); |
| 160 |
} |
| 161 |
|
| 162 |
static nsresult |
| 163 |
GetProxyForURIFromEnvironment(const nsACString& aScheme, |
| 164 |
const nsACString& aHost, |
| 165 |
PRInt32 aPort, |
| 166 |
nsACString& aResult) |
| 167 |
{ |
| 168 |
nsCAutoString envVar; |
| 169 |
envVar.Append(aScheme); |
| 170 |
envVar.AppendLiteral("_proxy"); |
| 171 |
const char* proxyVal = PR_GetEnv(envVar.get()); |
| 172 |
if (!proxyVal) { |
| 173 |
proxyVal = PR_GetEnv("all_proxy"); |
| 174 |
if (!proxyVal) { |
| 175 |
// Return failure so that the caller can detect the failure and |
| 176 |
// fall back to other proxy detection (e.g., WPAD) |
| 177 |
return NS_ERROR_FAILURE; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
const char* noProxyVal = PR_GetEnv("no_proxy"); |
| 182 |
if (noProxyVal && IsInNoProxyList(aHost, aPort, noProxyVal)) { |
| 183 |
aResult.AppendLiteral("DIRECT"); |
| 184 |
return NS_OK; |
| 185 |
} |
| 186 |
|
| 187 |
// Use our URI parser to crack the proxy URI |
| 188 |
nsCOMPtr<nsIURI> proxyURI; |
| 189 |
nsresult rv = NS_NewURI(getter_AddRefs(proxyURI), proxyVal); |
| 190 |
if (NS_FAILED(rv)) |
| 191 |
return rv; |
| 192 |
|
| 193 |
// Is there a way to specify "socks://" or something in these environment |
| 194 |
// variables? I can't find any documentation. |
| 195 |
PRBool isHTTP; |
| 196 |
rv = proxyURI->SchemeIs("http", &isHTTP); |
| 197 |
if (NS_FAILED(rv)) |
| 198 |
return rv; |
| 199 |
if (!isHTTP) |
| 200 |
return NS_ERROR_FAILURE; |
| 201 |
|
| 202 |
nsCAutoString proxyHost; |
| 203 |
rv = proxyURI->GetHost(proxyHost); |
| 204 |
if (NS_FAILED(rv)) |
| 205 |
return rv; |
| 206 |
PRInt32 proxyPort; |
| 207 |
rv = proxyURI->GetPort(&proxyPort); |
| 208 |
if (NS_FAILED(rv)) |
| 209 |
return rv; |
| 210 |
|
| 211 |
SetProxyResult("PROXY", proxyHost, proxyPort, aResult); |
| 212 |
return NS_OK; |
| 213 |
} |
| 214 |
|
| 215 |
static nsresult |
| 216 |
SetProxyResultFromGConf(nsIGConfService* aGConf, const char* aKeyBase, |
| 217 |
const char* aType, nsACString& aResult) |
| 218 |
{ |
| 219 |
nsCAutoString hostKey; |
| 220 |
hostKey.AppendASCII(aKeyBase); |
| 221 |
hostKey.AppendLiteral("host"); |
| 222 |
nsCAutoString host; |
| 223 |
nsresult rv = aGConf->GetString(hostKey, host); |
| 224 |
if (NS_FAILED(rv)) |
| 225 |
return rv; |
| 226 |
if (host.IsEmpty()) |
| 227 |
return NS_ERROR_FAILURE; |
| 228 |
|
| 229 |
nsCAutoString portKey; |
| 230 |
portKey.AppendASCII(aKeyBase); |
| 231 |
portKey.AppendLiteral("port"); |
| 232 |
PRInt32 port; |
| 233 |
rv = aGConf->GetInt(portKey, &port); |
| 234 |
if (NS_FAILED(rv)) |
| 235 |
return rv; |
| 236 |
|
| 237 |
SetProxyResult(aType, host, port, aResult); |
| 238 |
return NS_OK; |
| 239 |
} |
| 240 |
|
| 241 |
/* copied from nsProtocolProxyService.cpp --- we should share this! */ |
| 242 |
static void |
| 243 |
proxy_MaskIPv6Addr(PRIPv6Addr &addr, PRUint16 mask_len) |
| 244 |
{ |
| 245 |
if (mask_len == 128) |
| 246 |
return; |
| 247 |
|
| 248 |
if (mask_len > 96) { |
| 249 |
addr.pr_s6_addr32[3] = PR_htonl( |
| 250 |
PR_ntohl(addr.pr_s6_addr32[3]) & (~0L << (128 - mask_len))); |
| 251 |
} |
| 252 |
else if (mask_len > 64) { |
| 253 |
addr.pr_s6_addr32[3] = 0; |
| 254 |
addr.pr_s6_addr32[2] = PR_htonl( |
| 255 |
PR_ntohl(addr.pr_s6_addr32[2]) & (~0L << (96 - mask_len))); |
| 256 |
} |
| 257 |
else if (mask_len > 32) { |
| 258 |
addr.pr_s6_addr32[3] = 0; |
| 259 |
addr.pr_s6_addr32[2] = 0; |
| 260 |
addr.pr_s6_addr32[1] = PR_htonl( |
| 261 |
PR_ntohl(addr.pr_s6_addr32[1]) & (~0L << (64 - mask_len))); |
| 262 |
} |
| 263 |
else { |
| 264 |
addr.pr_s6_addr32[3] = 0; |
| 265 |
addr.pr_s6_addr32[2] = 0; |
| 266 |
addr.pr_s6_addr32[1] = 0; |
| 267 |
addr.pr_s6_addr32[0] = PR_htonl( |
| 268 |
PR_ntohl(addr.pr_s6_addr32[0]) & (~0L << (32 - mask_len))); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
static PRBool ConvertToIPV6Addr(const nsACString& aName, |
| 273 |
PRIPv6Addr* aAddr) |
| 274 |
{ |
| 275 |
PRNetAddr addr; |
| 276 |
if (PR_StringToNetAddr(PromiseFlatCString(aName).get(), &addr) != PR_SUCCESS) |
| 277 |
return PR_FALSE; |
| 278 |
|
| 279 |
PRIPv6Addr ipv6; |
| 280 |
// convert parsed address to IPv6 |
| 281 |
if (addr.raw.family == PR_AF_INET) { |
| 282 |
// convert to IPv4-mapped address |
| 283 |
PR_ConvertIPv4AddrToIPv6(addr.inet.ip, &ipv6); |
| 284 |
} else if (addr.raw.family == PR_AF_INET6) { |
| 285 |
// copy the address |
| 286 |
memcpy(&ipv6, &addr.ipv6.ip, sizeof(PRIPv6Addr)); |
| 287 |
} else { |
| 288 |
return PR_FALSE; |
| 289 |
} |
| 290 |
|
| 291 |
return PR_TRUE; |
| 292 |
} |
| 293 |
|
| 294 |
static PRBool GConfIgnoreHost(const nsACString& aIgnore, |
| 295 |
const nsACString& aHost) |
| 296 |
{ |
| 297 |
if (aIgnore.Equals(aHost, nsCaseInsensitiveCStringComparator())) |
| 298 |
return PR_TRUE; |
| 299 |
|
| 300 |
if (StringBeginsWith(aIgnore, NS_LITERAL_CSTRING("*")) && |
| 301 |
StringEndsWith(aHost, nsDependentCSubstring(aIgnore, 1), |
| 302 |
nsCaseInsensitiveCStringComparator())) |
| 303 |
return PR_TRUE; |
| 304 |
|
| 305 |
PRInt32 mask = 128; |
| 306 |
nsReadingIterator<char> start; |
| 307 |
nsReadingIterator<char> slash; |
| 308 |
nsReadingIterator<char> end; |
| 309 |
aIgnore.BeginReading(start); |
| 310 |
aIgnore.BeginReading(slash); |
| 311 |
aIgnore.EndReading(end); |
| 312 |
if (FindCharInReadable('/', slash, end)) { |
| 313 |
++slash; |
| 314 |
nsDependentCSubstring maskStr(slash, end); |
| 315 |
nsCAutoString maskStr2(maskStr); |
| 316 |
PRInt32 err; |
| 317 |
mask = maskStr2.ToInteger(&err); |
| 318 |
if (err != 0) { |
| 319 |
mask = 128; |
| 320 |
} |
| 321 |
--slash; |
| 322 |
} else { |
| 323 |
slash = end; |
| 324 |
} |
| 325 |
|
| 326 |
PRIPv6Addr ignoreAddr, hostAddr; |
| 327 |
if (!ConvertToIPV6Addr(aIgnore, &ignoreAddr) || |
| 328 |
!ConvertToIPV6Addr(aHost, &hostAddr)) |
| 329 |
return PR_FALSE; |
| 330 |
|
| 331 |
proxy_MaskIPv6Addr(ignoreAddr, mask); |
| 332 |
proxy_MaskIPv6Addr(hostAddr, mask); |
| 333 |
|
| 334 |
return memcmp(&ignoreAddr, &hostAddr, sizeof(PRIPv6Addr)) == 0; |
| 335 |
} |
| 336 |
|
| 337 |
static nsresult |
| 338 |
GetProxyForURIFromGConf(nsIGConfService* aGConf, |
| 339 |
const nsACString& aScheme, |
| 340 |
const nsACString& aHost, |
| 341 |
PRInt32 aPort, |
| 342 |
nsACString& aResult) |
| 343 |
{ |
| 344 |
if (!IsProxyMode(aGConf, "manual")) { |
| 345 |
aResult.AppendLiteral("DIRECT"); |
| 346 |
return NS_OK; |
| 347 |
} |
| 348 |
|
| 349 |
nsCOMPtr<nsIArray> ignoreList; |
| 350 |
if (NS_SUCCEEDED(aGConf->GetStringList(NS_LITERAL_CSTRING("/system/http_proxy/ignore_hosts"), |
| 351 |
getter_AddRefs(ignoreList))) && ignoreList) { |
| 352 |
PRUint32 len = 0; |
| 353 |
ignoreList->GetLength(&len); |
| 354 |
for (PRUint32 i = 0; i < len; ++i) { |
| 355 |
nsCOMPtr<nsISupportsCString> str = do_QueryElementAt(ignoreList, i); |
| 356 |
if (str) { |
| 357 |
nsCAutoString s; |
| 358 |
if (NS_SUCCEEDED(str->GetData(s)) && !s.IsEmpty()) { |
| 359 |
if (GConfIgnoreHost(s, aHost)) { |
| 360 |
aResult.AppendLiteral("DIRECT"); |
| 361 |
return NS_OK; |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
nsresult rv = SetProxyResultFromGConf(aGConf, "/system/proxy/socks_", "SOCKS", aResult); |
| 369 |
if (NS_SUCCEEDED(rv)) |
| 370 |
return rv; |
| 371 |
|
| 372 |
if (aScheme.LowerCaseEqualsLiteral("http")) { |
| 373 |
rv = SetProxyResultFromGConf(aGConf, "/system/http_proxy/", "PROXY", aResult); |
| 374 |
} else if (aScheme.LowerCaseEqualsLiteral("https")) { |
| 375 |
rv = SetProxyResultFromGConf(aGConf, "/system/proxy/secure_", "PROXY", aResult); |
| 376 |
} else if (aScheme.LowerCaseEqualsLiteral("ftp")) { |
| 377 |
rv = SetProxyResultFromGConf(aGConf, "/system/proxy/ftp_", "PROXY", aResult); |
| 378 |
} else { |
| 379 |
rv = NS_ERROR_FAILURE; |
| 380 |
} |
| 381 |
|
| 382 |
if (NS_FAILED(rv)) { |
| 383 |
aResult.AppendLiteral("DIRECT"); |
| 384 |
} |
| 385 |
return NS_OK; |
| 386 |
} |
| 387 |
|
| 388 |
nsresult |
| 389 |
nsUnixSystemProxySettings::GetProxyForURI(nsIURI* aURI, nsACString& aResult) |
| 390 |
{ |
| 391 |
nsCAutoString scheme; |
| 392 |
nsresult rv = aURI->GetScheme(scheme); |
| 393 |
if (NS_FAILED(rv)) |
| 394 |
return rv; |
| 395 |
|
| 396 |
nsCAutoString host; |
| 397 |
rv = aURI->GetHost(host); |
| 398 |
if (NS_FAILED(rv)) |
| 399 |
return rv; |
| 400 |
|
| 401 |
PRInt32 port; |
| 402 |
rv = aURI->GetPort(&port); |
| 403 |
if (NS_FAILED(rv)) |
| 404 |
return rv; |
| 405 |
|
| 406 |
if (!mGConf) |
| 407 |
return GetProxyForURIFromEnvironment(scheme, host, port, aResult); |
| 408 |
|
| 409 |
return GetProxyForURIFromGConf(mGConf, scheme, host, port, aResult); |
| 410 |
} |
| 411 |
|
| 412 |
#define NS_UNIXSYSTEMPROXYSERVICE_CID /* 0fa3158c-d5a7-43de-9181-a285e74cf1d4 */\ |
| 413 |
{ 0x0fa3158c, 0xd5a7, 0x43de, \ |
| 414 |
{0x91, 0x81, 0xa2, 0x85, 0xe7, 0x4c, 0xf1, 0xd4 } } |
| 415 |
|
| 416 |
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsUnixSystemProxySettings, Init) |
| 417 |
|
| 418 |
static const nsModuleComponentInfo components[] = { |
| 419 |
{ "Unix System Proxy Settings Service", |
| 420 |
NS_UNIXSYSTEMPROXYSERVICE_CID, |
| 421 |
NS_SYSTEMPROXYSETTINGS_CONTRACTID, |
| 422 |
nsUnixSystemProxySettingsConstructor } |
| 423 |
}; |
| 424 |
|
| 425 |
NS_IMPL_NSGETMODULE(unixproxy, components) |