2012-09-15 00:27:46 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "ProxyAutoConfig.h"
|
|
|
|
#include "nsICancelable.h"
|
|
|
|
#include "nsIDNSListener.h"
|
|
|
|
#include "nsIDNSRecord.h"
|
|
|
|
#include "nsIDNSService.h"
|
2017-07-26 21:18:20 +03:00
|
|
|
#include "nsINamed.h"
|
2012-09-15 00:27:46 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIConsoleService.h"
|
2016-09-21 04:42:52 +03:00
|
|
|
#include "nsIURLParser.h"
|
2012-09-15 00:27:46 +04:00
|
|
|
#include "nsJSUtils.h"
|
2013-07-30 03:45:26 +04:00
|
|
|
#include "jsfriendapi.h"
|
2012-09-15 00:27:46 +04:00
|
|
|
#include "prnetdb.h"
|
|
|
|
#include "nsITimer.h"
|
2012-12-24 01:08:43 +04:00
|
|
|
#include "mozilla/net/DNS.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsNetCID.h"
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
// These are some global helper symbols the PAC format requires that we provide that
|
|
|
|
// are initialized as part of the global javascript context used for PAC evaluations.
|
|
|
|
// Additionally dnsResolve(host) and myIpAddress() are supplied in the same context
|
2012-10-09 21:52:26 +04:00
|
|
|
// but are implemented as c++ helpers. alert(msg) is similarly defined.
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
static const char *sPacUtils =
|
|
|
|
"function dnsDomainIs(host, domain) {\n"
|
|
|
|
" return (host.length >= domain.length &&\n"
|
|
|
|
" host.substring(host.length - domain.length) == domain);\n"
|
|
|
|
"}\n"
|
|
|
|
""
|
|
|
|
"function dnsDomainLevels(host) {\n"
|
|
|
|
" return host.split('.').length - 1;\n"
|
|
|
|
"}\n"
|
|
|
|
""
|
2017-06-29 21:35:20 +03:00
|
|
|
"function isValidIpAddress(ipchars) {\n"
|
|
|
|
" var matches = /^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$/.exec(ipchars);\n"
|
|
|
|
" if (matches == null) {\n"
|
|
|
|
" return false;\n"
|
|
|
|
" } else if (matches[1] > 255 || matches[2] > 255 || \n"
|
|
|
|
" matches[3] > 255 || matches[4] > 255) {\n"
|
|
|
|
" return false;\n"
|
|
|
|
" }\n"
|
|
|
|
" return true;\n"
|
|
|
|
"}\n"
|
|
|
|
""
|
2012-09-15 00:27:46 +04:00
|
|
|
"function convert_addr(ipchars) {\n"
|
|
|
|
" var bytes = ipchars.split('.');\n"
|
|
|
|
" var result = ((bytes[0] & 0xff) << 24) |\n"
|
|
|
|
" ((bytes[1] & 0xff) << 16) |\n"
|
|
|
|
" ((bytes[2] & 0xff) << 8) |\n"
|
|
|
|
" (bytes[3] & 0xff);\n"
|
|
|
|
" return result;\n"
|
|
|
|
"}\n"
|
|
|
|
""
|
|
|
|
"function isInNet(ipaddr, pattern, maskstr) {\n"
|
2017-06-29 21:35:20 +03:00
|
|
|
" if (!isValidIpAddress(pattern) || !isValidIpAddress(maskstr)) {\n"
|
|
|
|
" return false;\n"
|
|
|
|
" }\n"
|
|
|
|
" if (!isValidIpAddress(ipaddr)) {\n"
|
2012-09-15 00:27:46 +04:00
|
|
|
" ipaddr = dnsResolve(ipaddr);\n"
|
2017-06-29 21:35:20 +03:00
|
|
|
" if (ipaddr == null) {\n"
|
2012-09-15 00:27:46 +04:00
|
|
|
" return false;\n"
|
2017-06-29 21:35:20 +03:00
|
|
|
" }\n"
|
2012-09-15 00:27:46 +04:00
|
|
|
" }\n"
|
|
|
|
" var host = convert_addr(ipaddr);\n"
|
|
|
|
" var pat = convert_addr(pattern);\n"
|
|
|
|
" var mask = convert_addr(maskstr);\n"
|
|
|
|
" return ((host & mask) == (pat & mask));\n"
|
|
|
|
" \n"
|
|
|
|
"}\n"
|
|
|
|
""
|
|
|
|
"function isPlainHostName(host) {\n"
|
|
|
|
" return (host.search('\\\\.') == -1);\n"
|
|
|
|
"}\n"
|
|
|
|
""
|
|
|
|
"function isResolvable(host) {\n"
|
|
|
|
" var ip = dnsResolve(host);\n"
|
|
|
|
" return (ip != null);\n"
|
|
|
|
"}\n"
|
|
|
|
""
|
|
|
|
"function localHostOrDomainIs(host, hostdom) {\n"
|
|
|
|
" return (host == hostdom) ||\n"
|
|
|
|
" (hostdom.lastIndexOf(host + '.', 0) == 0);\n"
|
|
|
|
"}\n"
|
|
|
|
""
|
|
|
|
"function shExpMatch(url, pattern) {\n"
|
|
|
|
" pattern = pattern.replace(/\\./g, '\\\\.');\n"
|
|
|
|
" pattern = pattern.replace(/\\*/g, '.*');\n"
|
|
|
|
" pattern = pattern.replace(/\\?/g, '.');\n"
|
|
|
|
" var newRe = new RegExp('^'+pattern+'$');\n"
|
|
|
|
" return newRe.test(url);\n"
|
|
|
|
"}\n"
|
|
|
|
""
|
|
|
|
"var wdays = {SUN: 0, MON: 1, TUE: 2, WED: 3, THU: 4, FRI: 5, SAT: 6};\n"
|
|
|
|
"var months = {JAN: 0, FEB: 1, MAR: 2, APR: 3, MAY: 4, JUN: 5, JUL: 6, AUG: 7, SEP: 8, OCT: 9, NOV: 10, DEC: 11};\n"
|
|
|
|
""
|
|
|
|
"function weekdayRange() {\n"
|
|
|
|
" function getDay(weekday) {\n"
|
|
|
|
" if (weekday in wdays) {\n"
|
|
|
|
" return wdays[weekday];\n"
|
|
|
|
" }\n"
|
|
|
|
" return -1;\n"
|
|
|
|
" }\n"
|
|
|
|
" var date = new Date();\n"
|
|
|
|
" var argc = arguments.length;\n"
|
|
|
|
" var wday;\n"
|
|
|
|
" if (argc < 1)\n"
|
|
|
|
" return false;\n"
|
|
|
|
" if (arguments[argc - 1] == 'GMT') {\n"
|
|
|
|
" argc--;\n"
|
|
|
|
" wday = date.getUTCDay();\n"
|
|
|
|
" } else {\n"
|
|
|
|
" wday = date.getDay();\n"
|
|
|
|
" }\n"
|
|
|
|
" var wd1 = getDay(arguments[0]);\n"
|
|
|
|
" var wd2 = (argc == 2) ? getDay(arguments[1]) : wd1;\n"
|
|
|
|
" return (wd1 == -1 || wd2 == -1) ? false\n"
|
2016-04-25 11:26:52 +03:00
|
|
|
" : (wd1 <= wd2) ? (wd1 <= wday && wday <= wd2)\n"
|
|
|
|
" : (wd2 >= wday || wday >= wd1);\n"
|
2012-09-15 00:27:46 +04:00
|
|
|
"}\n"
|
|
|
|
""
|
|
|
|
"function dateRange() {\n"
|
|
|
|
" function getMonth(name) {\n"
|
|
|
|
" if (name in months) {\n"
|
|
|
|
" return months[name];\n"
|
|
|
|
" }\n"
|
|
|
|
" return -1;\n"
|
|
|
|
" }\n"
|
|
|
|
" var date = new Date();\n"
|
|
|
|
" var argc = arguments.length;\n"
|
|
|
|
" if (argc < 1) {\n"
|
|
|
|
" return false;\n"
|
|
|
|
" }\n"
|
|
|
|
" var isGMT = (arguments[argc - 1] == 'GMT');\n"
|
|
|
|
"\n"
|
|
|
|
" if (isGMT) {\n"
|
|
|
|
" argc--;\n"
|
|
|
|
" }\n"
|
|
|
|
" // function will work even without explict handling of this case\n"
|
|
|
|
" if (argc == 1) {\n"
|
|
|
|
" var tmp = parseInt(arguments[0]);\n"
|
|
|
|
" if (isNaN(tmp)) {\n"
|
|
|
|
" return ((isGMT ? date.getUTCMonth() : date.getMonth()) ==\n"
|
|
|
|
" getMonth(arguments[0]));\n"
|
|
|
|
" } else if (tmp < 32) {\n"
|
|
|
|
" return ((isGMT ? date.getUTCDate() : date.getDate()) == tmp);\n"
|
|
|
|
" } else { \n"
|
|
|
|
" return ((isGMT ? date.getUTCFullYear() : date.getFullYear()) ==\n"
|
|
|
|
" tmp);\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" var year = date.getFullYear();\n"
|
|
|
|
" var date1, date2;\n"
|
|
|
|
" date1 = new Date(year, 0, 1, 0, 0, 0);\n"
|
|
|
|
" date2 = new Date(year, 11, 31, 23, 59, 59);\n"
|
|
|
|
" var adjustMonth = false;\n"
|
|
|
|
" for (var i = 0; i < (argc >> 1); i++) {\n"
|
|
|
|
" var tmp = parseInt(arguments[i]);\n"
|
|
|
|
" if (isNaN(tmp)) {\n"
|
|
|
|
" var mon = getMonth(arguments[i]);\n"
|
|
|
|
" date1.setMonth(mon);\n"
|
|
|
|
" } else if (tmp < 32) {\n"
|
|
|
|
" adjustMonth = (argc <= 2);\n"
|
|
|
|
" date1.setDate(tmp);\n"
|
|
|
|
" } else {\n"
|
|
|
|
" date1.setFullYear(tmp);\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" for (var i = (argc >> 1); i < argc; i++) {\n"
|
|
|
|
" var tmp = parseInt(arguments[i]);\n"
|
|
|
|
" if (isNaN(tmp)) {\n"
|
|
|
|
" var mon = getMonth(arguments[i]);\n"
|
|
|
|
" date2.setMonth(mon);\n"
|
|
|
|
" } else if (tmp < 32) {\n"
|
|
|
|
" date2.setDate(tmp);\n"
|
|
|
|
" } else {\n"
|
|
|
|
" date2.setFullYear(tmp);\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" if (adjustMonth) {\n"
|
|
|
|
" date1.setMonth(date.getMonth());\n"
|
|
|
|
" date2.setMonth(date.getMonth());\n"
|
|
|
|
" }\n"
|
|
|
|
" if (isGMT) {\n"
|
|
|
|
" var tmp = date;\n"
|
|
|
|
" tmp.setFullYear(date.getUTCFullYear());\n"
|
|
|
|
" tmp.setMonth(date.getUTCMonth());\n"
|
|
|
|
" tmp.setDate(date.getUTCDate());\n"
|
|
|
|
" tmp.setHours(date.getUTCHours());\n"
|
|
|
|
" tmp.setMinutes(date.getUTCMinutes());\n"
|
|
|
|
" tmp.setSeconds(date.getUTCSeconds());\n"
|
|
|
|
" date = tmp;\n"
|
|
|
|
" }\n"
|
2016-04-25 11:26:52 +03:00
|
|
|
" return (date1 <= date2) ? (date1 <= date) && (date <= date2)\n"
|
|
|
|
" : (date2 >= date) || (date >= date1);\n"
|
2012-09-15 00:27:46 +04:00
|
|
|
"}\n"
|
|
|
|
""
|
|
|
|
"function timeRange() {\n"
|
|
|
|
" var argc = arguments.length;\n"
|
|
|
|
" var date = new Date();\n"
|
|
|
|
" var isGMT= false;\n"
|
|
|
|
""
|
|
|
|
" if (argc < 1) {\n"
|
|
|
|
" return false;\n"
|
|
|
|
" }\n"
|
|
|
|
" if (arguments[argc - 1] == 'GMT') {\n"
|
|
|
|
" isGMT = true;\n"
|
|
|
|
" argc--;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" var hour = isGMT ? date.getUTCHours() : date.getHours();\n"
|
|
|
|
" var date1, date2;\n"
|
|
|
|
" date1 = new Date();\n"
|
|
|
|
" date2 = new Date();\n"
|
|
|
|
"\n"
|
|
|
|
" if (argc == 1) {\n"
|
|
|
|
" return (hour == arguments[0]);\n"
|
|
|
|
" } else if (argc == 2) {\n"
|
|
|
|
" return ((arguments[0] <= hour) && (hour <= arguments[1]));\n"
|
|
|
|
" } else {\n"
|
|
|
|
" switch (argc) {\n"
|
|
|
|
" case 6:\n"
|
|
|
|
" date1.setSeconds(arguments[2]);\n"
|
|
|
|
" date2.setSeconds(arguments[5]);\n"
|
|
|
|
" case 4:\n"
|
|
|
|
" var middle = argc >> 1;\n"
|
|
|
|
" date1.setHours(arguments[0]);\n"
|
|
|
|
" date1.setMinutes(arguments[1]);\n"
|
|
|
|
" date2.setHours(arguments[middle]);\n"
|
|
|
|
" date2.setMinutes(arguments[middle + 1]);\n"
|
|
|
|
" if (middle == 2) {\n"
|
|
|
|
" date2.setSeconds(59);\n"
|
|
|
|
" }\n"
|
|
|
|
" break;\n"
|
|
|
|
" default:\n"
|
|
|
|
" throw 'timeRange: bad number of arguments'\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" if (isGMT) {\n"
|
|
|
|
" date.setFullYear(date.getUTCFullYear());\n"
|
|
|
|
" date.setMonth(date.getUTCMonth());\n"
|
|
|
|
" date.setDate(date.getUTCDate());\n"
|
|
|
|
" date.setHours(date.getUTCHours());\n"
|
|
|
|
" date.setMinutes(date.getUTCMinutes());\n"
|
|
|
|
" date.setSeconds(date.getUTCSeconds());\n"
|
|
|
|
" }\n"
|
2016-04-25 11:26:52 +03:00
|
|
|
" return (date1 <= date2) ? (date1 <= date) && (date <= date2)\n"
|
|
|
|
" : (date2 >= date) || (date >= date1);\n"
|
|
|
|
"\n"
|
2012-09-15 00:27:46 +04:00
|
|
|
"}\n"
|
|
|
|
"";
|
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
// sRunning is defined for the helper functions only while the
|
|
|
|
// Javascript engine is running and the PAC object cannot be deleted
|
|
|
|
// or reset.
|
2014-08-07 19:38:41 +04:00
|
|
|
static uint32_t sRunningIndex = 0xdeadbeef;
|
|
|
|
static ProxyAutoConfig *GetRunning()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sRunningIndex != 0xdeadbeef);
|
|
|
|
return static_cast<ProxyAutoConfig *>(PR_GetThreadPrivate(sRunningIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetRunning(ProxyAutoConfig *arg)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sRunningIndex != 0xdeadbeef);
|
|
|
|
PR_SetThreadPrivate(sRunningIndex, arg);
|
|
|
|
}
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
// The PACResolver is used for dnsResolve()
|
2015-03-21 19:28:04 +03:00
|
|
|
class PACResolver final : public nsIDNSListener
|
2015-03-27 21:52:19 +03:00
|
|
|
, public nsITimerCallback
|
2017-07-26 21:18:20 +03:00
|
|
|
, public nsINamed
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2017-07-05 01:48:00 +03:00
|
|
|
explicit PACResolver(nsIEventTarget *aTarget)
|
2012-09-15 00:27:46 +04:00
|
|
|
: mStatus(NS_ERROR_FAILURE)
|
2017-07-05 01:48:00 +03:00
|
|
|
, mMainThreadEventTarget(aTarget)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
// nsIDNSListener
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD OnLookupComplete(nsICancelable *request,
|
|
|
|
nsIDNSRecord *record,
|
|
|
|
nsresult status) override
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
2012-09-15 00:27:46 +04:00
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
mRequest = nullptr;
|
|
|
|
mStatus = status;
|
|
|
|
mResponse = record;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
// nsITimerCallback
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD Notify(nsITimer *timer) override
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
2018-04-10 23:07:47 +03:00
|
|
|
nsCOMPtr<nsICancelable> request(mRequest);
|
|
|
|
if (request)
|
|
|
|
request->Cancel(NS_ERROR_NET_TIMEOUT);
|
2012-09-15 00:27:46 +04:00
|
|
|
mTimer = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
// nsINamed
|
|
|
|
NS_IMETHOD GetName(nsACString& aName) override
|
|
|
|
{
|
|
|
|
aName.AssignLiteral("PACResolver");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-05 01:48:00 +03:00
|
|
|
nsresult mStatus;
|
|
|
|
nsCOMPtr<nsICancelable> mRequest;
|
|
|
|
nsCOMPtr<nsIDNSRecord> mResponse;
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
|
2014-06-24 20:36:44 +04:00
|
|
|
|
|
|
|
private:
|
2018-04-30 19:46:04 +03:00
|
|
|
~PACResolver() = default;
|
2012-09-15 00:27:46 +04:00
|
|
|
};
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_IMPL_ISUPPORTS(PACResolver, nsIDNSListener, nsITimerCallback, nsINamed)
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
static
|
|
|
|
void PACLogToConsole(nsString &aMessage)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIConsoleService> consoleService =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
if (!consoleService)
|
|
|
|
return;
|
|
|
|
|
|
|
|
consoleService->LogStringMessage(aMessage.get());
|
|
|
|
}
|
|
|
|
|
2016-06-02 09:29:18 +03:00
|
|
|
// Javascript errors and warnings are logged to the main error console
|
2012-09-15 00:27:46 +04:00
|
|
|
static void
|
2016-06-02 09:29:18 +03:00
|
|
|
PACLogErrorOrWarning(const nsAString& aKind, JSErrorReport* aReport)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
2016-06-02 09:29:18 +03:00
|
|
|
nsString formattedMessage(NS_LITERAL_STRING("PAC Execution "));
|
|
|
|
formattedMessage += aKind;
|
|
|
|
formattedMessage += NS_LITERAL_STRING(": ");
|
2016-08-14 14:39:30 +03:00
|
|
|
if (aReport->message())
|
|
|
|
formattedMessage.Append(NS_ConvertUTF8toUTF16(aReport->message().c_str()));
|
2012-09-15 00:27:46 +04:00
|
|
|
formattedMessage += NS_LITERAL_STRING(" [");
|
2016-06-02 09:29:18 +03:00
|
|
|
formattedMessage.Append(aReport->linebuf(), aReport->linebufLength());
|
2012-09-15 00:27:46 +04:00
|
|
|
formattedMessage += NS_LITERAL_STRING("]");
|
|
|
|
PACLogToConsole(formattedMessage);
|
|
|
|
}
|
|
|
|
|
2016-06-02 09:29:18 +03:00
|
|
|
static void
|
2016-08-14 14:39:31 +03:00
|
|
|
PACWarningReporter(JSContext* aCx, JSErrorReport* aReport)
|
2016-06-02 09:29:18 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aReport);
|
|
|
|
MOZ_ASSERT(JSREPORT_IS_WARNING(aReport->flags));
|
|
|
|
|
|
|
|
PACLogErrorOrWarning(NS_LITERAL_STRING("Warning"), aReport);
|
|
|
|
}
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS AutoPACErrorReporter
|
|
|
|
{
|
|
|
|
JSContext* mCx;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AutoPACErrorReporter(JSContext* aCx)
|
|
|
|
: mCx(aCx)
|
|
|
|
{}
|
|
|
|
~AutoPACErrorReporter() {
|
|
|
|
if (!JS_IsExceptionPending(mCx)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JS::RootedValue exn(mCx);
|
|
|
|
if (!JS_GetPendingException(mCx, &exn)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JS_ClearPendingException(mCx);
|
|
|
|
|
|
|
|
js::ErrorReport report(mCx);
|
|
|
|
if (!report.init(mCx, exn, js::ErrorReport::WithSideEffects)) {
|
|
|
|
JS_ClearPendingException(mCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PACLogErrorOrWarning(NS_LITERAL_STRING("Error"), report.report());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
// timeout of 0 means the normal necko timeout strategy, otherwise the dns request
|
|
|
|
// will be canceled after aTimeout milliseconds
|
2012-09-15 00:27:46 +04:00
|
|
|
static
|
2013-08-09 02:53:04 +04:00
|
|
|
bool PACResolve(const nsCString &aHostName, NetAddr *aNetAddr,
|
|
|
|
unsigned int aTimeout)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
2014-08-07 19:38:41 +04:00
|
|
|
if (!GetRunning()) {
|
2012-09-15 00:27:46 +04:00
|
|
|
NS_WARNING("PACResolve without a running ProxyAutoConfig object");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-07 19:38:41 +04:00
|
|
|
return GetRunning()->ResolveAddress(aHostName, aNetAddr, aTimeout);
|
2012-09-15 00:27:46 +04:00
|
|
|
}
|
|
|
|
|
2013-09-22 07:04:57 +04:00
|
|
|
ProxyAutoConfig::ProxyAutoConfig()
|
2016-07-23 20:54:19 +03:00
|
|
|
: mJSContext(nullptr)
|
2013-09-22 07:04:57 +04:00
|
|
|
, mJSNeedsSetup(false)
|
2018-07-17 08:51:20 +03:00
|
|
|
, mShutdown(false)
|
2016-09-21 04:42:52 +03:00
|
|
|
, mIncludePath(false)
|
2017-05-24 09:24:55 +03:00
|
|
|
, mExtraHeapSize(0)
|
2013-09-22 07:04:57 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(ProxyAutoConfig);
|
|
|
|
}
|
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
bool
|
|
|
|
ProxyAutoConfig::ResolveAddress(const nsCString &aHostName,
|
2012-12-24 01:08:43 +04:00
|
|
|
NetAddr *aNetAddr,
|
2012-09-15 00:27:46 +04:00
|
|
|
unsigned int aTimeout)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID);
|
2012-09-15 00:27:46 +04:00
|
|
|
if (!dns)
|
2012-09-15 00:27:46 +04:00
|
|
|
return false;
|
|
|
|
|
2017-07-05 01:48:00 +03:00
|
|
|
RefPtr<PACResolver> helper = new PACResolver(mMainThreadEventTarget);
|
2017-02-15 12:48:15 +03:00
|
|
|
OriginAttributes attrs;
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2017-02-15 12:48:15 +03:00
|
|
|
if (NS_FAILED(dns->AsyncResolveNative(aHostName,
|
|
|
|
nsIDNSService::RESOLVE_PRIORITY_MEDIUM,
|
|
|
|
helper,
|
2017-06-01 23:43:44 +03:00
|
|
|
GetCurrentThreadEventTarget(),
|
2017-02-15 12:48:15 +03:00
|
|
|
attrs,
|
|
|
|
getter_AddRefs(helper->mRequest))))
|
2012-09-15 00:27:46 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (aTimeout && helper->mRequest) {
|
|
|
|
if (!mTimer)
|
2017-10-16 09:12:02 +03:00
|
|
|
mTimer = NS_NewTimer();
|
2012-09-15 00:27:46 +04:00
|
|
|
if (mTimer) {
|
2017-07-05 01:48:00 +03:00
|
|
|
mTimer->SetTarget(mMainThreadEventTarget);
|
2012-09-15 00:27:46 +04:00
|
|
|
mTimer->InitWithCallback(helper, aTimeout, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
helper->mTimer = mTimer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
// Spin the event loop of the pac thread until lookup is complete.
|
|
|
|
// nsPACman is responsible for keeping a queue and only allowing
|
|
|
|
// one PAC execution at a time even when it is called re-entrantly.
|
2017-05-15 16:34:19 +03:00
|
|
|
SpinEventLoopUntil([&, helper]() { return !helper->mRequest; });
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(helper->mStatus) ||
|
2012-09-15 00:27:46 +04:00
|
|
|
NS_FAILED(helper->mResponse->GetNextAddr(0, aNetAddr)))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
bool PACResolveToString(const nsCString &aHostName,
|
|
|
|
nsCString &aDottedDecimal,
|
|
|
|
unsigned int aTimeout)
|
|
|
|
{
|
2012-12-24 01:08:43 +04:00
|
|
|
NetAddr netAddr;
|
2012-09-15 00:27:46 +04:00
|
|
|
if (!PACResolve(aHostName, &netAddr, aTimeout))
|
2012-09-15 00:27:46 +04:00
|
|
|
return false;
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
char dottedDecimal[128];
|
2012-12-24 01:08:43 +04:00
|
|
|
if (!NetAddrToString(&netAddr, dottedDecimal, sizeof(dottedDecimal)))
|
2012-09-15 00:27:46 +04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
aDottedDecimal.Assign(dottedDecimal);
|
2012-09-15 00:27:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dnsResolve(host) javascript implementation
|
|
|
|
static
|
2013-08-02 11:41:57 +04:00
|
|
|
bool PACDnsResolve(JSContext *cx, unsigned int argc, JS::Value *vp)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
2014-02-10 20:42:27 +04:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
NS_WARNING("DNS Resolution From PAC on Main Thread. How did that happen?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-05 14:01:11 +03:00
|
|
|
if (!args.requireAtLeast(cx, "dnsResolve", 1))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
JS::Rooted<JSString*> arg1(cx, JS::ToString(cx, args[0]));
|
|
|
|
if (!arg1)
|
2012-09-15 00:27:46 +04:00
|
|
|
return false;
|
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString hostName;
|
2012-09-15 00:27:46 +04:00
|
|
|
nsAutoCString dottedDecimal;
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
if (!hostName.init(cx, arg1))
|
|
|
|
return false;
|
2012-10-09 21:52:26 +04:00
|
|
|
if (PACResolveToString(NS_ConvertUTF16toUTF8(hostName), dottedDecimal, 0)) {
|
|
|
|
JSString *dottedDecimalString = JS_NewStringCopyZ(cx, dottedDecimal.get());
|
2014-03-26 21:38:29 +04:00
|
|
|
if (!dottedDecimalString) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-10 20:42:27 +04:00
|
|
|
args.rval().setString(dottedDecimalString);
|
2012-10-09 21:52:26 +04:00
|
|
|
}
|
|
|
|
else {
|
2014-02-10 20:42:27 +04:00
|
|
|
args.rval().setNull();
|
2012-10-09 21:52:26 +04:00
|
|
|
}
|
2013-08-02 11:41:57 +04:00
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// myIpAddress() javascript implementation
|
|
|
|
static
|
2013-08-02 11:41:57 +04:00
|
|
|
bool PACMyIpAddress(JSContext *cx, unsigned int argc, JS::Value *vp)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
2014-03-25 22:42:33 +04:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
NS_WARNING("DNS Resolution From PAC on Main Thread. How did that happen?");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-07 19:38:41 +04:00
|
|
|
if (!GetRunning()) {
|
2012-09-15 00:27:46 +04:00
|
|
|
NS_WARNING("PAC myIPAddress without a running ProxyAutoConfig object");
|
2013-08-02 11:41:57 +04:00
|
|
|
return false;
|
2012-09-15 00:27:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-07 19:38:41 +04:00
|
|
|
return GetRunning()->MyIPAddress(args);
|
2012-09-15 00:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// proxyAlert(msg) javascript implementation
|
|
|
|
static
|
2013-08-02 11:41:57 +04:00
|
|
|
bool PACProxyAlert(JSContext *cx, unsigned int argc, JS::Value *vp)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
2014-02-10 20:42:27 +04:00
|
|
|
JS::CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
2015-02-05 14:01:11 +03:00
|
|
|
if (!args.requireAtLeast(cx, "alert", 1))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
JS::Rooted<JSString*> arg1(cx, JS::ToString(cx, args[0]));
|
|
|
|
if (!arg1)
|
2012-09-15 00:27:46 +04:00
|
|
|
return false;
|
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString message;
|
2012-09-15 00:27:46 +04:00
|
|
|
if (!message.init(cx, arg1))
|
|
|
|
return false;
|
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoString alertMessage;
|
2012-09-15 00:27:46 +04:00
|
|
|
alertMessage.SetCapacity(32 + message.Length());
|
|
|
|
alertMessage += NS_LITERAL_STRING("PAC-alert: ");
|
|
|
|
alertMessage += message;
|
|
|
|
PACLogToConsole(alertMessage);
|
|
|
|
|
2014-02-10 20:42:27 +04:00
|
|
|
args.rval().setUndefined(); /* return undefined */
|
2012-09-15 00:27:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-23 01:15:49 +04:00
|
|
|
static const JSFunctionSpec PACGlobalFunctions[] = {
|
2017-08-25 18:04:12 +03:00
|
|
|
JS_FN("dnsResolve", PACDnsResolve, 1, 0),
|
2014-09-18 19:53:30 +04:00
|
|
|
|
|
|
|
// a global "var pacUseMultihomedDNS = true;" will change behavior
|
|
|
|
// of myIpAddress to actively use DNS
|
2017-08-25 18:04:12 +03:00
|
|
|
JS_FN("myIpAddress", PACMyIpAddress, 0, 0),
|
|
|
|
JS_FN("alert", PACProxyAlert, 1, 0),
|
2012-09-15 00:27:46 +04:00
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
// JSContextWrapper is a c++ object that manages the context for the JS engine
|
|
|
|
// used on the PAC thread. It is initialized and destroyed on the PAC thread.
|
|
|
|
class JSContextWrapper
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
|
|
|
public:
|
2017-05-24 09:24:55 +03:00
|
|
|
static JSContextWrapper *Create(uint32_t aExtraHeapSize)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
2017-05-24 09:24:55 +03:00
|
|
|
JSContext* cx = JS_NewContext(sContextHeapSize + aExtraHeapSize);
|
2016-07-23 20:54:19 +03:00
|
|
|
if (NS_WARN_IF(!cx))
|
2014-10-24 11:49:33 +04:00
|
|
|
return nullptr;
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
JSContextWrapper *entry = new JSContextWrapper(cx);
|
2012-09-15 00:27:46 +04:00
|
|
|
if (NS_FAILED(entry->Init())) {
|
|
|
|
delete entry;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext *Context() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *Global() const
|
|
|
|
{
|
|
|
|
return mGlobal;
|
|
|
|
}
|
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
~JSContextWrapper()
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
2014-10-24 11:49:33 +04:00
|
|
|
mGlobal = nullptr;
|
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
MOZ_COUNT_DTOR(JSContextWrapper);
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
if (mContext) {
|
|
|
|
JS_DestroyContext(mContext);
|
2012-09-15 00:27:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetOK()
|
|
|
|
{
|
|
|
|
mOK = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsOK()
|
|
|
|
{
|
|
|
|
return mOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-05-24 09:24:55 +03:00
|
|
|
static const uint32_t sContextHeapSize = 4 << 20; // 4 MB
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
JSContext *mContext;
|
2014-10-24 11:49:33 +04:00
|
|
|
JS::PersistentRooted<JSObject*> mGlobal;
|
2012-09-15 00:27:46 +04:00
|
|
|
bool mOK;
|
|
|
|
|
2013-09-11 16:49:05 +04:00
|
|
|
static const JSClass sGlobalClass;
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
explicit JSContextWrapper(JSContext* cx)
|
|
|
|
: mContext(cx), mGlobal(cx, nullptr), mOK(false)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
2016-07-23 20:54:19 +03:00
|
|
|
MOZ_COUNT_CTOR(JSContextWrapper);
|
2012-09-15 00:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Init()
|
|
|
|
{
|
2013-06-01 07:20:30 +04:00
|
|
|
/*
|
|
|
|
* Not setting this will cause JS_CHECK_RECURSION to report false
|
|
|
|
* positives
|
2014-01-15 00:41:22 +04:00
|
|
|
*/
|
2016-07-05 15:35:23 +03:00
|
|
|
JS_SetNativeStackQuota(mContext, 128 * sizeof(size_t) * 1024);
|
2013-06-01 07:20:30 +04:00
|
|
|
|
2016-07-05 12:06:05 +03:00
|
|
|
JS::SetWarningReporter(mContext, PACWarningReporter);
|
2014-09-10 03:02:10 +04:00
|
|
|
|
2016-06-22 10:47:52 +03:00
|
|
|
if (!JS::InitSelfHostedCode(mContext)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRequest areq(mContext);
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2018-05-17 11:59:45 +03:00
|
|
|
JS::RealmOptions options;
|
2018-06-07 11:02:10 +03:00
|
|
|
options.creationOptions().setNewCompartmentInSystemZone();
|
2013-08-02 05:38:47 +04:00
|
|
|
mGlobal = JS_NewGlobalObject(mContext, &sGlobalClass, nullptr,
|
|
|
|
JS::DontFireOnNewGlobalHook, options);
|
2016-06-02 09:29:18 +03:00
|
|
|
if (!mGlobal) {
|
|
|
|
JS_ClearPendingException(mContext);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2014-01-15 00:41:22 +04:00
|
|
|
JS::Rooted<JSObject*> global(mContext, mGlobal);
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRealm ar(mContext, global);
|
2016-06-02 09:29:18 +03:00
|
|
|
AutoPACErrorReporter aper(mContext);
|
2018-06-13 22:47:47 +03:00
|
|
|
if (!JS::InitRealmStandardClasses(mContext)) {
|
2012-09-15 00:27:46 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2016-06-02 09:29:18 +03:00
|
|
|
}
|
|
|
|
if (!JS_DefineFunctions(mContext, global, PACGlobalFunctions)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2014-01-15 00:41:22 +04:00
|
|
|
JS_FireOnNewGlobalObject(mContext, global);
|
2013-08-02 05:38:47 +04:00
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
static const JSClassOps sJSContextWrapperGlobalClassOps = {
|
2014-11-22 21:23:39 +03:00
|
|
|
nullptr, nullptr, nullptr, nullptr,
|
2015-04-23 16:51:28 +03:00
|
|
|
nullptr, nullptr, nullptr, nullptr,
|
2017-08-25 11:12:16 +03:00
|
|
|
nullptr, nullptr,
|
2014-04-05 03:58:08 +04:00
|
|
|
JS_GlobalObjectTraceHook
|
2012-09-15 00:27:46 +04:00
|
|
|
};
|
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
const JSClass JSContextWrapper::sGlobalClass = {
|
2016-04-01 03:00:01 +03:00
|
|
|
"PACResolutionThreadGlobal",
|
|
|
|
JSCLASS_GLOBAL_FLAGS,
|
2016-07-23 20:54:19 +03:00
|
|
|
&sJSContextWrapperGlobalClassOps
|
2016-04-01 03:00:01 +03:00
|
|
|
};
|
|
|
|
|
2014-08-07 19:38:41 +04:00
|
|
|
void
|
|
|
|
ProxyAutoConfig::SetThreadLocalIndex(uint32_t index)
|
|
|
|
{
|
|
|
|
sRunningIndex = index;
|
|
|
|
}
|
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
nsresult
|
|
|
|
ProxyAutoConfig::Init(const nsCString &aPACURI,
|
2016-09-21 04:42:52 +03:00
|
|
|
const nsCString &aPACScript,
|
2017-05-24 09:24:55 +03:00
|
|
|
bool aIncludePath,
|
2017-07-05 01:48:00 +03:00
|
|
|
uint32_t aExtraHeapSize,
|
|
|
|
nsIEventTarget *aEventTarget)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
|
|
|
mPACURI = aPACURI;
|
|
|
|
mPACScript = sPacUtils;
|
|
|
|
mPACScript.Append(aPACScript);
|
2017-02-09 19:06:00 +03:00
|
|
|
mIncludePath = aIncludePath;
|
2017-05-24 09:24:55 +03:00
|
|
|
mExtraHeapSize = aExtraHeapSize;
|
2017-07-05 01:48:00 +03:00
|
|
|
mMainThreadEventTarget = aEventTarget;
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2014-08-07 19:38:41 +04:00
|
|
|
if (!GetRunning())
|
2012-09-15 00:27:46 +04:00
|
|
|
return SetupJS();
|
|
|
|
|
|
|
|
mJSNeedsSetup = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ProxyAutoConfig::SetupJS()
|
|
|
|
{
|
|
|
|
mJSNeedsSetup = false;
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!GetRunning(), "JIT is running");
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
delete mJSContext;
|
|
|
|
mJSContext = nullptr;
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
if (mPACScript.IsEmpty())
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2016-09-16 07:35:14 +03:00
|
|
|
NS_GetCurrentThread()->SetCanInvokeJS(true);
|
|
|
|
|
2017-05-24 09:24:55 +03:00
|
|
|
mJSContext = JSContextWrapper::Create(mExtraHeapSize);
|
2016-07-23 20:54:19 +03:00
|
|
|
if (!mJSContext)
|
2012-09-15 00:27:46 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
JSContext* cx = mJSContext->Context();
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRequest areq(cx);
|
|
|
|
JSAutoRealm ar(cx, mJSContext->Global());
|
2016-06-02 09:29:18 +03:00
|
|
|
AutoPACErrorReporter aper(cx);
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2013-08-13 01:07:38 +04:00
|
|
|
// check if this is a data: uri so that we don't spam the js console with
|
|
|
|
// huge meaningless strings. this is not on the main thread, so it can't
|
2016-11-02 19:35:49 +03:00
|
|
|
// use nsIURI scheme methods
|
2013-08-13 01:07:38 +04:00
|
|
|
bool isDataURI = nsDependentCSubstring(mPACURI, 0, 5).LowerCaseEqualsASCII("data:", 5);
|
|
|
|
|
2014-08-07 19:38:41 +04:00
|
|
|
SetRunning(this);
|
2016-07-23 20:54:19 +03:00
|
|
|
JS::Rooted<JSObject*> global(cx, mJSContext->Global());
|
2014-03-17 20:17:58 +04:00
|
|
|
JS::CompileOptions options(cx);
|
2013-11-19 15:41:42 +04:00
|
|
|
options.setFileAndLine(mPACURI.get(), 1);
|
2014-06-26 02:35:35 +04:00
|
|
|
JS::Rooted<JSScript*> script(cx);
|
2015-03-19 17:13:33 +03:00
|
|
|
if (!JS_CompileScript(cx, mPACScript.get(), mPACScript.Length(), options,
|
|
|
|
&script) ||
|
2015-03-14 08:36:16 +03:00
|
|
|
!JS_ExecuteScript(cx, script))
|
2014-06-26 02:35:35 +04:00
|
|
|
{
|
2012-09-15 00:27:46 +04:00
|
|
|
nsString alertMessage(NS_LITERAL_STRING("PAC file failed to install from "));
|
2013-08-13 01:07:38 +04:00
|
|
|
if (isDataURI) {
|
|
|
|
alertMessage += NS_LITERAL_STRING("data: URI");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alertMessage += NS_ConvertUTF8toUTF16(mPACURI);
|
|
|
|
}
|
2012-09-15 00:27:46 +04:00
|
|
|
PACLogToConsole(alertMessage);
|
2014-08-07 19:38:41 +04:00
|
|
|
SetRunning(nullptr);
|
2012-09-15 00:27:46 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-08-07 19:38:41 +04:00
|
|
|
SetRunning(nullptr);
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
mJSContext->SetOK();
|
2012-09-15 00:27:46 +04:00
|
|
|
nsString alertMessage(NS_LITERAL_STRING("PAC file installed from "));
|
2013-08-13 01:07:38 +04:00
|
|
|
if (isDataURI) {
|
|
|
|
alertMessage += NS_LITERAL_STRING("data: URI");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alertMessage += NS_ConvertUTF8toUTF16(mPACURI);
|
|
|
|
}
|
2012-09-15 00:27:46 +04:00
|
|
|
PACLogToConsole(alertMessage);
|
|
|
|
|
|
|
|
// we don't need these now
|
|
|
|
mPACScript.Truncate();
|
|
|
|
mPACURI.Truncate();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ProxyAutoConfig::GetProxyForURI(const nsCString &aTestURI,
|
|
|
|
const nsCString &aTestHost,
|
|
|
|
nsACString &result)
|
|
|
|
{
|
|
|
|
if (mJSNeedsSetup)
|
|
|
|
SetupJS();
|
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
if (!mJSContext || !mJSContext->IsOK())
|
2012-09-15 00:27:46 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2016-07-23 20:54:19 +03:00
|
|
|
JSContext *cx = mJSContext->Context();
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRequest areq(cx);
|
|
|
|
JSAutoRealm ar(cx, mJSContext->Global());
|
2016-06-02 09:29:18 +03:00
|
|
|
AutoPACErrorReporter aper(cx);
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
// the sRunning flag keeps a new PAC file from being installed
|
2012-09-15 00:27:46 +04:00
|
|
|
// while the event loop is spinning on a DNS function. Don't early return.
|
2014-08-07 19:38:41 +04:00
|
|
|
SetRunning(this);
|
2012-09-15 00:27:46 +04:00
|
|
|
mRunningHost = aTestHost;
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
2016-09-21 04:42:52 +03:00
|
|
|
nsCString clensedURI = aTestURI;
|
|
|
|
|
|
|
|
if (!mIncludePath) {
|
|
|
|
nsCOMPtr<nsIURLParser> urlParser =
|
|
|
|
do_GetService(NS_STDURLPARSER_CONTRACTID);
|
|
|
|
int32_t pathLen = 0;
|
|
|
|
if (urlParser) {
|
|
|
|
uint32_t schemePos;
|
|
|
|
int32_t schemeLen;
|
|
|
|
uint32_t authorityPos;
|
|
|
|
int32_t authorityLen;
|
|
|
|
uint32_t pathPos;
|
|
|
|
rv = urlParser->ParseURL(aTestURI.get(), aTestURI.Length(),
|
|
|
|
&schemePos, &schemeLen,
|
|
|
|
&authorityPos, &authorityLen,
|
|
|
|
&pathPos, &pathLen);
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (pathLen) {
|
|
|
|
// cut off the path but leave the initial slash
|
|
|
|
pathLen--;
|
|
|
|
}
|
|
|
|
aTestURI.Left(clensedURI, aTestURI.Length() - pathLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::RootedString uriString(cx, JS_NewStringCopyZ(cx, clensedURI.get()));
|
2013-03-06 20:41:43 +04:00
|
|
|
JS::RootedString hostString(cx, JS_NewStringCopyZ(cx, aTestHost.get()));
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
if (uriString && hostString) {
|
2014-02-11 14:59:16 +04:00
|
|
|
JS::AutoValueArray<2> args(cx);
|
|
|
|
args[0].setString(uriString);
|
|
|
|
args[1].setString(hostString);
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2013-05-04 01:49:57 +04:00
|
|
|
JS::Rooted<JS::Value> rval(cx);
|
2016-07-23 20:54:19 +03:00
|
|
|
JS::Rooted<JSObject*> global(cx, mJSContext->Global());
|
2014-02-13 19:33:04 +04:00
|
|
|
bool ok = JS_CallFunctionName(cx, global, "FindProxyForURL", args, &rval);
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
if (ok && rval.isString()) {
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString pacString;
|
2012-09-15 00:27:46 +04:00
|
|
|
if (pacString.init(cx, rval.toString())) {
|
|
|
|
CopyUTF16toUTF8(pacString, result);
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
mRunningHost.Truncate();
|
2014-08-07 19:38:41 +04:00
|
|
|
SetRunning(nullptr);
|
2012-09-15 00:27:46 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProxyAutoConfig::GC()
|
|
|
|
{
|
2016-07-23 20:54:19 +03:00
|
|
|
if (!mJSContext || !mJSContext->IsOK())
|
2012-09-15 00:27:46 +04:00
|
|
|
return;
|
|
|
|
|
2018-05-16 11:53:16 +03:00
|
|
|
JSAutoRealm ar(mJSContext->Context(), mJSContext->Global());
|
2016-07-23 20:54:19 +03:00
|
|
|
JS_MaybeGC(mJSContext->Context());
|
2012-09-15 00:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ProxyAutoConfig::~ProxyAutoConfig()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ProxyAutoConfig);
|
2017-09-04 12:17:44 +03:00
|
|
|
MOZ_ASSERT(mShutdown, "Shutdown must be called before dtor.");
|
2016-07-23 20:54:19 +03:00
|
|
|
NS_ASSERTION(!mJSContext,
|
|
|
|
"~ProxyAutoConfig leaking JS context that "
|
2012-09-15 00:27:46 +04:00
|
|
|
"should have been deleted on pac thread");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ProxyAutoConfig::Shutdown()
|
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread(), "wrong thread for shutdown");
|
2012-09-15 00:27:46 +04:00
|
|
|
|
2017-09-04 12:17:44 +03:00
|
|
|
if (NS_WARN_IF(GetRunning()) || mShutdown) {
|
2012-09-15 00:27:46 +04:00
|
|
|
return;
|
2017-09-04 12:17:44 +03:00
|
|
|
}
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
mShutdown = true;
|
2016-07-23 20:54:19 +03:00
|
|
|
delete mJSContext;
|
|
|
|
mJSContext = nullptr;
|
2012-09-15 00:27:46 +04:00
|
|
|
}
|
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
bool
|
2012-12-24 01:08:43 +04:00
|
|
|
ProxyAutoConfig::SrcAddress(const NetAddr *remoteAddress, nsCString &localAddress)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
|
|
|
PRFileDesc *fd;
|
|
|
|
fd = PR_OpenUDPSocket(remoteAddress->raw.family);
|
|
|
|
if (!fd)
|
|
|
|
return false;
|
|
|
|
|
2012-12-24 01:08:43 +04:00
|
|
|
PRNetAddr prRemoteAddress;
|
|
|
|
NetAddrToPRNetAddr(remoteAddress, &prRemoteAddress);
|
|
|
|
if (PR_Connect(fd, &prRemoteAddress, 0) != PR_SUCCESS) {
|
2012-09-15 00:27:46 +04:00
|
|
|
PR_Close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRNetAddr localName;
|
|
|
|
if (PR_GetSockName(fd, &localName) != PR_SUCCESS) {
|
|
|
|
PR_Close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_Close(fd);
|
2016-02-18 05:55:57 +03:00
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
char dottedDecimal[128];
|
|
|
|
if (PR_NetAddrToString(&localName, dottedDecimal, sizeof(dottedDecimal)) != PR_SUCCESS)
|
|
|
|
return false;
|
2016-02-18 05:55:57 +03:00
|
|
|
|
2012-09-15 00:27:46 +04:00
|
|
|
localAddress.Assign(dottedDecimal);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hostName is run through a dns lookup and then a udp socket is connected
|
|
|
|
// to the result. If that all works, the local IP address of the socket is
|
2014-03-26 21:38:29 +04:00
|
|
|
// returned to the javascript caller and |*aResult| is set to true. Otherwise
|
|
|
|
// |*aResult| is set to false.
|
2012-09-15 00:27:46 +04:00
|
|
|
bool
|
|
|
|
ProxyAutoConfig::MyIPAddressTryHost(const nsCString &hostName,
|
|
|
|
unsigned int timeout,
|
2014-03-26 21:38:29 +04:00
|
|
|
const JS::CallArgs &aArgs,
|
|
|
|
bool* aResult)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
2014-03-26 21:38:29 +04:00
|
|
|
*aResult = false;
|
|
|
|
|
2012-12-24 01:08:43 +04:00
|
|
|
NetAddr remoteAddress;
|
2012-09-15 00:27:46 +04:00
|
|
|
nsAutoCString localDottedDecimal;
|
2016-07-23 20:54:19 +03:00
|
|
|
JSContext *cx = mJSContext->Context();
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
if (PACResolve(hostName, &remoteAddress, timeout) &&
|
|
|
|
SrcAddress(&remoteAddress, localDottedDecimal)) {
|
|
|
|
JSString *dottedDecimalString =
|
|
|
|
JS_NewStringCopyZ(cx, localDottedDecimal.get());
|
2014-03-26 21:38:29 +04:00
|
|
|
if (!dottedDecimalString) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = true;
|
2014-03-25 22:42:33 +04:00
|
|
|
aArgs.rval().setString(dottedDecimalString);
|
2012-09-15 00:27:46 +04:00
|
|
|
}
|
2014-03-26 21:38:29 +04:00
|
|
|
return true;
|
2012-09-15 00:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-03-25 22:42:33 +04:00
|
|
|
ProxyAutoConfig::MyIPAddress(const JS::CallArgs &aArgs)
|
2012-09-15 00:27:46 +04:00
|
|
|
{
|
|
|
|
nsAutoCString remoteDottedDecimal;
|
|
|
|
nsAutoCString localDottedDecimal;
|
2016-07-23 20:54:19 +03:00
|
|
|
JSContext *cx = mJSContext->Context();
|
2014-09-18 19:53:30 +04:00
|
|
|
JS::RootedValue v(cx);
|
2016-07-23 20:54:19 +03:00
|
|
|
JS::Rooted<JSObject*> global(cx, mJSContext->Global());
|
2014-09-18 19:53:30 +04:00
|
|
|
|
|
|
|
bool useMultihomedDNS =
|
|
|
|
JS_GetProperty(cx, global, "pacUseMultihomedDNS", &v) &&
|
|
|
|
!v.isUndefined() && ToBoolean(v);
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
// first, lookup the local address of a socket connected
|
|
|
|
// to the host of uri being resolved by the pac file. This is
|
|
|
|
// v6 safe.. but is the last step like that
|
2014-03-26 21:38:29 +04:00
|
|
|
bool rvalAssigned = false;
|
2014-09-18 19:53:30 +04:00
|
|
|
if (useMultihomedDNS) {
|
|
|
|
if (!MyIPAddressTryHost(mRunningHost, kTimeout, aArgs, &rvalAssigned) ||
|
|
|
|
rvalAssigned) {
|
|
|
|
return rvalAssigned;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we can still do the fancy multi homing thing if the host is a literal
|
|
|
|
PRNetAddr tempAddr;
|
|
|
|
memset(&tempAddr, 0, sizeof(PRNetAddr));
|
|
|
|
if ((PR_StringToNetAddr(mRunningHost.get(), &tempAddr) == PR_SUCCESS) &&
|
|
|
|
(!MyIPAddressTryHost(mRunningHost, kTimeout, aArgs, &rvalAssigned) ||
|
|
|
|
rvalAssigned)) {
|
|
|
|
return rvalAssigned;
|
|
|
|
}
|
2014-03-26 21:38:29 +04:00
|
|
|
}
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
// next, look for a route to a public internet address that doesn't need DNS.
|
|
|
|
// This is the google anycast dns address, but it doesn't matter if it
|
|
|
|
// remains operable (as we don't contact it) as long as the address stays
|
|
|
|
// in commonly routed IP address space.
|
|
|
|
remoteDottedDecimal.AssignLiteral("8.8.8.8");
|
2014-03-26 21:38:29 +04:00
|
|
|
if (!MyIPAddressTryHost(remoteDottedDecimal, 0, aArgs, &rvalAssigned) ||
|
|
|
|
rvalAssigned) {
|
|
|
|
return rvalAssigned;
|
|
|
|
}
|
2016-02-18 05:55:57 +03:00
|
|
|
|
2014-09-18 19:53:30 +04:00
|
|
|
// finally, use the old algorithm based on the local hostname
|
2012-09-15 00:27:46 +04:00
|
|
|
nsAutoCString hostName;
|
|
|
|
nsCOMPtr<nsIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID);
|
2014-09-18 19:53:30 +04:00
|
|
|
// without multihomedDNS use such a short timeout that we are basically
|
|
|
|
// just looking at the cache for raw dotted decimals
|
|
|
|
uint32_t timeout = useMultihomedDNS ? kTimeout : 1;
|
2012-09-15 00:27:46 +04:00
|
|
|
if (dns && NS_SUCCEEDED(dns->GetMyHostName(hostName)) &&
|
2014-09-18 19:53:30 +04:00
|
|
|
PACResolveToString(hostName, localDottedDecimal, timeout)) {
|
2012-09-15 00:27:46 +04:00
|
|
|
JSString *dottedDecimalString =
|
|
|
|
JS_NewStringCopyZ(cx, localDottedDecimal.get());
|
2014-03-26 21:38:29 +04:00
|
|
|
if (!dottedDecimalString) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-25 22:42:33 +04:00
|
|
|
aArgs.rval().setString(dottedDecimalString);
|
2012-09-15 00:27:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// next try a couple RFC 1918 variants.. maybe there is a
|
|
|
|
// local route
|
|
|
|
remoteDottedDecimal.AssignLiteral("192.168.0.1");
|
2014-03-26 21:38:29 +04:00
|
|
|
if (!MyIPAddressTryHost(remoteDottedDecimal, 0, aArgs, &rvalAssigned) ||
|
|
|
|
rvalAssigned) {
|
|
|
|
return rvalAssigned;
|
|
|
|
}
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
// more RFC 1918
|
|
|
|
remoteDottedDecimal.AssignLiteral("10.0.0.1");
|
2014-03-26 21:38:29 +04:00
|
|
|
if (!MyIPAddressTryHost(remoteDottedDecimal, 0, aArgs, &rvalAssigned) ||
|
|
|
|
rvalAssigned) {
|
|
|
|
return rvalAssigned;
|
|
|
|
}
|
2012-09-15 00:27:46 +04:00
|
|
|
|
|
|
|
// who knows? let's fallback to localhost
|
|
|
|
localDottedDecimal.AssignLiteral("127.0.0.1");
|
|
|
|
JSString *dottedDecimalString =
|
|
|
|
JS_NewStringCopyZ(cx, localDottedDecimal.get());
|
2014-03-26 21:38:29 +04:00
|
|
|
if (!dottedDecimalString) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-25 22:42:33 +04:00
|
|
|
aArgs.rval().setString(dottedDecimalString);
|
2012-09-15 00:27:46 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
Bug 356831 - Proxy autodiscovery doesn't check DHCP (option 252) r=bagder,valentin
This patch addresses an issue with Firefox's proxy detection on networks which
do not have their a proxy auto-configuration (PAC) file hosted at
http://wpad/wpad.dat, and instead make use of DHCP option 252 for broadcasting
the address of the PAC file. See https://findproxyforurl.com/wpad-introduction/
for an introduction to the protocol.
Prior to this patch, proxy auto-detect missed out the DHCP query stage, and just
looked for a PAC file at http://wpad/wpad.dat
This patch only addresses the issue for Firefox on Windows, although it defines a
DHCP client interface which could be implemented on other platforms.
The high-level components of this patch are:
* nsIDHCPClient.idl - this is an interface which has been defined for querying the
DHCP server.
* nsPACMan.cpp - where previously when the PAC URL was simply set to a constant of
http://wpad/wpad.dat, it now dispatches an asynchronous command to the proxy
thread. The class ExecutePACThreadAction has been augmented to include an
instruction to 'ConfigureWPAD' (Configure Web-proxy auto-detect), and a new class,
'ConfigureWPADComplete' has been created to relay the result (the URL of the PAC
file) back to the nsPACMan object.
* nsProtocolProxyService.cpp
Minor changes to reflect the fact that the PAC URL not being set does not always
mean there is no PAC to be used; instead it could be in the process of being
detected.
* TestPACMan.cpp
This is a new file, and tests only the DHCP auto-detect functionality.
Some tests use multiple threads, as they test the non-blocking proxy detection.
* DHCPUtils.cpp
A class containing the main logic for querying DHCP.
* WindowsNetworkFunctionsWrapper.cpp
A very thin wrapper around the Windows API calls needed by DHCPUtils.
This class was introduced so it could be mocked out in tests.
* nsWindowsDHCPClient.cpp
* An implementation of the interface defined in nsIDHCPClient.idl. Fairly thin:
most logic is implemented in DHCPUtils.
* TestDHCPUtils.cpp
Tests for DHCPUtils and nsWindowsDHCPClient
MozReview-Commit-ID: 4xFQz3tOLEx
--HG--
extra : rebase_source : dfd5c588406a8b0d92f91cc8a0038ca722b7140a
2018-06-08 01:07:28 +03:00
|
|
|
} // namespace mozilla
|