2009-04-25 01:29:32 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-04-25 01:29:32 -07:00
|
|
|
|
|
|
|
#include <windows.h>
|
2012-07-09 17:53:17 -07:00
|
|
|
#include <ras.h>
|
2012-06-13 18:12:36 -07:00
|
|
|
#include <wininet.h>
|
2009-04-25 01:29:32 -07:00
|
|
|
|
2013-01-10 03:44:15 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-07-09 17:53:17 -07:00
|
|
|
#include "mozilla/Util.h"
|
2009-04-25 01:29:32 -07:00
|
|
|
#include "nsISystemProxySettings.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2010-06-10 11:11:11 -07:00
|
|
|
#include "mozilla/ModuleUtils.h"
|
2009-04-25 01:29:32 -07:00
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
|
2013-01-10 03:44:15 -08:00
|
|
|
class nsWindowsSystemProxySettings MOZ_FINAL : public nsISystemProxySettings
|
2009-04-25 01:29:32 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISYSTEMPROXYSETTINGS
|
|
|
|
|
|
|
|
nsWindowsSystemProxySettings() {};
|
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsWindowsSystemProxySettings() {};
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool MatchOverride(const nsACString& aHost);
|
|
|
|
bool PatternMatch(const nsACString& aHost, const nsACString& aOverride);
|
2009-04-25 01:29:32 -07:00
|
|
|
};
|
|
|
|
|
2012-09-14 13:27:46 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsWindowsSystemProxySettings, nsISystemProxySettings)
|
2012-09-14 13:27:46 -07:00
|
|
|
|
2012-09-14 13:27:46 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWindowsSystemProxySettings::GetMainThreadOnly(bool *aMainThreadOnly)
|
2009-04-25 01:29:32 -07:00
|
|
|
{
|
2012-09-14 13:27:46 -07:00
|
|
|
*aMainThreadOnly = false;
|
|
|
|
return NS_OK;
|
2012-09-13 14:42:29 -07:00
|
|
|
}
|
|
|
|
|
2012-09-14 13:27:46 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWindowsSystemProxySettings::Init()
|
2012-09-13 14:42:29 -07:00
|
|
|
{
|
2012-09-14 13:27:46 -07:00
|
|
|
return NS_OK;
|
2012-09-17 08:54:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void SetProxyResult(const char* aType, const nsACString& aHostPort,
|
|
|
|
nsACString& aResult)
|
|
|
|
{
|
2012-09-14 13:27:46 -07:00
|
|
|
aResult.AssignASCII(aType);
|
|
|
|
aResult.Append(' ');
|
|
|
|
aResult.Append(aHostPort);
|
2009-04-25 01:29:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void SetProxyResultDirect(nsACString& aResult)
|
|
|
|
{
|
|
|
|
// For whatever reason, a proxy is not to be used.
|
|
|
|
aResult.AssignASCII("DIRECT");
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static nsresult ReadInternetOption(uint32_t aOption, uint32_t& aFlags,
|
2012-07-09 17:53:17 -07:00
|
|
|
nsAString& aValue)
|
2012-06-13 18:12:36 -07:00
|
|
|
{
|
2012-07-09 17:53:17 -07:00
|
|
|
DWORD connFlags = 0;
|
|
|
|
WCHAR connName[RAS_MaxEntryName + 1];
|
2013-01-31 04:47:58 -08:00
|
|
|
MOZ_SEH_TRY {
|
|
|
|
InternetGetConnectedStateExW(&connFlags, connName,
|
|
|
|
mozilla::ArrayLength(connName), 0);
|
|
|
|
} MOZ_SEH_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-06-13 18:12:36 -07:00
|
|
|
|
2012-07-09 17:53:17 -07:00
|
|
|
INTERNET_PER_CONN_OPTIONW options[2];
|
2012-09-12 19:24:00 -07:00
|
|
|
options[0].dwOption = INTERNET_PER_CONN_FLAGS_UI;
|
2012-07-09 17:53:17 -07:00
|
|
|
options[1].dwOption = aOption;
|
2012-06-13 18:12:36 -07:00
|
|
|
|
|
|
|
INTERNET_PER_CONN_OPTION_LISTW list;
|
|
|
|
list.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
|
2012-07-09 17:53:17 -07:00
|
|
|
list.pszConnection = connFlags & INTERNET_CONNECTION_MODEM ?
|
|
|
|
connName : NULL;
|
|
|
|
list.dwOptionCount = mozilla::ArrayLength(options);
|
2012-06-13 18:12:36 -07:00
|
|
|
list.dwOptionError = 0;
|
2012-07-09 17:53:17 -07:00
|
|
|
list.pOptions = options;
|
2012-06-13 18:12:36 -07:00
|
|
|
|
|
|
|
unsigned long size = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
|
|
|
|
if (!InternetQueryOptionW(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION,
|
|
|
|
&list, &size)) {
|
2012-09-12 19:24:00 -07:00
|
|
|
if (GetLastError() != ERROR_INVALID_PARAMETER) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
options[0].dwOption = INTERNET_PER_CONN_FLAGS;
|
|
|
|
size = sizeof(INTERNET_PER_CONN_OPTION_LISTW);
|
2013-01-14 04:27:33 -08:00
|
|
|
MOZ_SEH_TRY {
|
2013-01-12 12:30:15 -08:00
|
|
|
if (!InternetQueryOptionW(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION,
|
|
|
|
&list, &size)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-01-31 04:47:58 -08:00
|
|
|
} MOZ_SEH_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
|
2012-09-12 19:24:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-06-13 18:12:36 -07:00
|
|
|
}
|
|
|
|
|
2012-07-09 17:53:17 -07:00
|
|
|
aFlags = options[0].Value.dwValue;
|
|
|
|
aValue.Assign(options[1].Value.pszValue);
|
|
|
|
GlobalFree(options[1].Value.pszValue);
|
2012-06-13 18:12:36 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2009-04-25 01:29:32 -07:00
|
|
|
nsWindowsSystemProxySettings::MatchOverride(const nsACString& aHost)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags = 0;
|
2009-04-25 01:29:32 -07:00
|
|
|
nsAutoString buf;
|
|
|
|
|
2012-07-09 17:53:17 -07:00
|
|
|
rv = ReadInternetOption(INTERNET_PER_CONN_PROXY_BYPASS, flags, buf);
|
2009-04-25 01:29:32 -07:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-04-25 01:29:32 -07:00
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 cbuf(buf);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host(aHost);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t start = 0;
|
|
|
|
int32_t end = cbuf.Length();
|
2009-04-25 01:29:32 -07:00
|
|
|
|
|
|
|
// Windows formats its proxy override list in the form:
|
|
|
|
// server;server;server where 'server' is a server name pattern or IP
|
|
|
|
// address, or "<local>". "<local>" must be translated to
|
|
|
|
// "localhost;127.0.0.1".
|
|
|
|
// In a server name pattern, a '*' character matches any substring and
|
|
|
|
// all other characters must match themselves; the whole pattern must match
|
|
|
|
// the whole hostname.
|
|
|
|
while (true) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t delimiter = cbuf.FindCharInSet(" ;", start);
|
2009-04-25 01:29:32 -07:00
|
|
|
if (delimiter == -1)
|
|
|
|
delimiter = end;
|
|
|
|
|
|
|
|
if (delimiter != start) {
|
2012-09-01 19:35:17 -07:00
|
|
|
const nsAutoCString override(Substring(cbuf, start,
|
2009-04-25 01:29:32 -07:00
|
|
|
delimiter - start));
|
|
|
|
if (override.EqualsLiteral("<local>")) {
|
|
|
|
// This override matches local addresses.
|
|
|
|
if (host.EqualsLiteral("localhost") ||
|
|
|
|
host.EqualsLiteral("127.0.0.1"))
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-04-25 01:29:32 -07:00
|
|
|
} else if (PatternMatch(host, override)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-04-25 01:29:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delimiter == end)
|
|
|
|
break;
|
|
|
|
start = ++delimiter;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-04-25 01:29:32 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2009-04-25 01:29:32 -07:00
|
|
|
nsWindowsSystemProxySettings::PatternMatch(const nsACString& aHost,
|
|
|
|
const nsACString& aOverride)
|
|
|
|
{
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString host(aHost);
|
|
|
|
nsAutoCString override(aOverride);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t overrideLength = override.Length();
|
|
|
|
int32_t tokenStart = 0;
|
|
|
|
int32_t offset = 0;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool star = false;
|
2009-04-25 01:29:32 -07:00
|
|
|
|
|
|
|
while (tokenStart < overrideLength) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t tokenEnd = override.FindChar('*', tokenStart);
|
2009-04-25 01:29:32 -07:00
|
|
|
if (tokenEnd == tokenStart) {
|
2011-10-17 07:59:28 -07:00
|
|
|
star = true;
|
2009-04-25 01:29:32 -07:00
|
|
|
tokenStart++;
|
2009-05-06 13:44:11 -07:00
|
|
|
// If the character following the '*' is a '.' character then skip
|
|
|
|
// it so that "*.foo.com" allows "foo.com".
|
|
|
|
if (override.FindChar('.', tokenStart) == tokenStart)
|
|
|
|
tokenStart++;
|
2009-04-25 01:29:32 -07:00
|
|
|
} else {
|
|
|
|
if (tokenEnd == -1)
|
|
|
|
tokenEnd = overrideLength;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString token(Substring(override, tokenStart,
|
2009-04-25 01:29:32 -07:00
|
|
|
tokenEnd - tokenStart));
|
|
|
|
offset = host.Find(token, offset);
|
|
|
|
if (offset == -1 || (!star && offset))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
|
|
|
star = false;
|
2009-04-25 01:29:32 -07:00
|
|
|
tokenStart = tokenEnd;
|
|
|
|
offset += token.Length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (star || (offset == host.Length()));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsWindowsSystemProxySettings::GetPACURI(nsACString& aResult)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags = 0;
|
2012-07-09 17:53:17 -07:00
|
|
|
nsAutoString buf;
|
2012-06-13 18:12:36 -07:00
|
|
|
|
2012-07-09 17:53:17 -07:00
|
|
|
rv = ReadInternetOption(INTERNET_PER_CONN_AUTOCONFIG_URL, flags, buf);
|
2012-06-13 18:12:36 -07:00
|
|
|
if (!(flags & PROXY_TYPE_AUTO_PROXY_URL)) {
|
|
|
|
aResult.Truncate();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-04-25 01:29:32 -07:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
aResult = NS_ConvertUTF16toUTF8(buf);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-09-14 13:27:46 -07:00
|
|
|
nsWindowsSystemProxySettings::GetProxyForURI(const nsACString & aSpec,
|
|
|
|
const nsACString & aScheme,
|
|
|
|
const nsACString & aHost,
|
|
|
|
const int32_t aPort,
|
|
|
|
nsACString & aResult)
|
2009-04-25 01:29:32 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags = 0;
|
2012-07-09 17:53:17 -07:00
|
|
|
nsAutoString buf;
|
2009-04-25 01:29:32 -07:00
|
|
|
|
2012-07-09 17:53:17 -07:00
|
|
|
rv = ReadInternetOption(INTERNET_PER_CONN_PROXY_SERVER, flags, buf);
|
|
|
|
if (NS_FAILED(rv) || !(flags & PROXY_TYPE_PROXY)) {
|
2009-04-25 01:29:32 -07:00
|
|
|
SetProxyResultDirect(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-14 13:27:46 -07:00
|
|
|
if (MatchOverride(aHost)) {
|
2009-04-25 01:29:32 -07:00
|
|
|
SetProxyResultDirect(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 cbuf(buf);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString prefix;
|
2012-09-14 13:27:46 -07:00
|
|
|
ToLowerCase(aScheme, prefix);
|
2009-04-25 01:29:32 -07:00
|
|
|
|
|
|
|
prefix.Append('=');
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString specificProxy;
|
|
|
|
nsAutoCString defaultProxy;
|
|
|
|
nsAutoCString socksProxy;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t start = 0;
|
|
|
|
int32_t end = cbuf.Length();
|
2009-04-25 01:29:32 -07:00
|
|
|
|
|
|
|
while (true) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t delimiter = cbuf.FindCharInSet(" ;", start);
|
2009-04-25 01:29:32 -07:00
|
|
|
if (delimiter == -1)
|
|
|
|
delimiter = end;
|
|
|
|
|
|
|
|
if (delimiter != start) {
|
2012-09-01 19:35:17 -07:00
|
|
|
const nsAutoCString proxy(Substring(cbuf, start,
|
2009-04-25 01:29:32 -07:00
|
|
|
delimiter - start));
|
|
|
|
if (proxy.FindChar('=') == -1) {
|
|
|
|
// If a proxy name is listed by itself, it is used as the
|
|
|
|
// default proxy for any protocols that do not have a specific
|
|
|
|
// proxy specified.
|
|
|
|
// (http://msdn.microsoft.com/en-us/library/aa383996%28VS.85%29.aspx)
|
|
|
|
defaultProxy = proxy;
|
|
|
|
} else if (proxy.Find(prefix) == 0) {
|
|
|
|
// To list a proxy for a specific protocol, the string must
|
|
|
|
// follow the format "<protocol>=<protocol>://<proxy_name>".
|
|
|
|
// (http://msdn.microsoft.com/en-us/library/aa383996%28VS.85%29.aspx)
|
|
|
|
specificProxy = Substring(proxy, prefix.Length());
|
|
|
|
break;
|
|
|
|
} else if (proxy.Find("socks=") == 0) {
|
|
|
|
// SOCKS proxy.
|
|
|
|
socksProxy = Substring(proxy, 5); // "socks=" length.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delimiter == end)
|
|
|
|
break;
|
|
|
|
start = ++delimiter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!specificProxy.IsEmpty())
|
|
|
|
SetProxyResult("PROXY", specificProxy, aResult); // Protocol-specific proxy.
|
|
|
|
else if (!defaultProxy.IsEmpty())
|
|
|
|
SetProxyResult("PROXY", defaultProxy, aResult); // Default proxy.
|
|
|
|
else if (!socksProxy.IsEmpty())
|
|
|
|
SetProxyResult("SOCKS", socksProxy, aResult); // SOCKS proxy.
|
|
|
|
else
|
|
|
|
SetProxyResultDirect(aResult); // Direct connection.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NS_WINDOWSSYSTEMPROXYSERVICE_CID /* 4e22d3ea-aaa2-436e-ada4-9247de57d367 */\
|
|
|
|
{ 0x4e22d3ea, 0xaaa2, 0x436e, \
|
|
|
|
{0xad, 0xa4, 0x92, 0x47, 0xde, 0x57, 0xd3, 0x67 } }
|
|
|
|
|
|
|
|
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsWindowsSystemProxySettings, Init)
|
2010-06-10 11:11:11 -07:00
|
|
|
NS_DEFINE_NAMED_CID(NS_WINDOWSSYSTEMPROXYSERVICE_CID);
|
2009-04-25 01:29:32 -07:00
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
static const mozilla::Module::CIDEntry kSysProxyCIDs[] = {
|
|
|
|
{ &kNS_WINDOWSSYSTEMPROXYSERVICE_CID, false, NULL, nsWindowsSystemProxySettingsConstructor },
|
|
|
|
{ NULL }
|
2009-04-25 01:29:32 -07:00
|
|
|
};
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
static const mozilla::Module::ContractIDEntry kSysProxyContracts[] = {
|
|
|
|
{ NS_SYSTEMPROXYSETTINGS_CONTRACTID, &kNS_WINDOWSSYSTEMPROXYSERVICE_CID },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const mozilla::Module kSysProxyModule = {
|
|
|
|
mozilla::Module::kVersion,
|
|
|
|
kSysProxyCIDs,
|
|
|
|
kSysProxyContracts
|
|
|
|
};
|
|
|
|
|
|
|
|
NSMODULE_DEFN(nsWindowsProxyModule) = &kSysProxyModule;
|