summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominique Leuenberger <dimstar@opensuse.org>2021-07-27 19:49:44 +0200
committerGitHub <noreply@github.com>2021-07-27 19:49:44 +0200
commitbcde153760ee885d2c633994e78ac447f8ec0713 (patch)
treef005f41930c092232c7f9c59330887d621ce341d
parent3c44b7b38300707c4c58f5fa0a596086187feef9 (diff)
parentc2dcf154c748cb482fd8d2c3374eb1c259c5b05a (diff)
downloadlibproxy-git-bcde153760ee885d2c633994e78ac447f8ec0713.tar.gz
Merge pull request #161 from zhaofengli/duktape
Add Duktape support
-rw-r--r--INSTALL1
-rw-r--r--libproxy/cmake/modules.cmk9
-rw-r--r--libproxy/cmake/modules/pacrunner_duktape.cmk21
-rw-r--r--libproxy/cmake/modules/pacrunner_mozjs.cmk2
-rw-r--r--libproxy/modules/config_envvar.cpp6
-rw-r--r--libproxy/modules/pacrunner_duktape.cpp138
-rw-r--r--libproxy/modules/pacrunner_mozjs.cpp1
-rw-r--r--libproxy/modules/pacrunner_natus.cpp4
-rw-r--r--libproxy/modules/pacrunner_webkit.cpp4
9 files changed, 181 insertions, 5 deletions
diff --git a/INSTALL b/INSTALL
index ab292cf..b5fb987 100644
--- a/INSTALL
+++ b/INSTALL
@@ -120,6 +120,7 @@ WITH_WEBKIT: Default to ON. Enable Webkit Java Script engine.
WITH_WEBKIT3: Default to OFF: Enable Webkit GTK3 support.
+WITH_DUKTAPE: Defaults to ON. Enable Duktape JavaScript engine.
Bindings Path:
==============
diff --git a/libproxy/cmake/modules.cmk b/libproxy/cmake/modules.cmk
index 11d2d43..6eaaa01 100644
--- a/libproxy/cmake/modules.cmk
+++ b/libproxy/cmake/modules.cmk
@@ -18,10 +18,13 @@ include(cmake/modules/network_networkmanager.cmk)
include(cmake/modules/pacrunner_mozjs.cmk)
include(cmake/modules/pacrunner_natus.cmk)
include(cmake/modules/pacrunner_webkit.cmk)
+include(cmake/modules/pacrunner_duktape.cmk)
# Build the pacrunner into libproxy unless we are building for multiple engines
set(BIPR 1 CACHE BOOL "Build internal pacrunner? (Always false when building more than one PR")
-if((MOZJS_FOUND AND WEBKIT_FOUND) OR (MOZJS_FOUND AND NATUS_FOUND) OR (WEBKIT_FOUND AND NATUS_FOUND))
+
+math(EXPR NUM_PACRUNNER "${MOZJS_FOUND}+${NATUS_FOUND}+${WEBKIT_FOUND}+${DUKTAPE_FOUND}")
+if(NUM_PACRUNNER GREATER 1)
set(BIPR 0)
endif()
@@ -44,8 +47,10 @@ if(NM_FOUND OR NMold_FOUND)
set(NM_BUILD 1)
endif(NM_FOUND OR NMold_FOUND)
px_module(network_networkmanager "${NM_BUILD}" 0 ${NM_LIBRARIES}${NMold_LIBRARIES})
-px_module(pacrunner_mozjs 0 ${BIPR} ${MOZJS_LIBRARIES})
+px_module(pacrunner_mozjs "${MOZJS_FOUND}" ${BIPR} ${MOZJS_LIBRARIES})
px_module(pacrunner_natus "${NATUS_FOUND}" ${BIPR} ${NATUS_LIBRARIES})
px_module(pacrunner_webkit "${WEBKIT_FOUND}" ${BIPR} ${WEBKIT_LIBRARIES})
+px_module(pacrunner_duktape "${DUKTAPE_FOUND}" ${BIPR} ${DUKTAPE_LIBRARIES})
px_module(wpad_dns_alias 1 1)
+message("${NUM_PACRUNNER} pacrunner(s) are enabled")
message("")
diff --git a/libproxy/cmake/modules/pacrunner_duktape.cmk b/libproxy/cmake/modules/pacrunner_duktape.cmk
new file mode 100644
index 0000000..e7918a8
--- /dev/null
+++ b/libproxy/cmake/modules/pacrunner_duktape.cmk
@@ -0,0 +1,21 @@
+if(WIN32)
+ set(DUKTAPE_FOUND 0)
+else()
+ set(DUKTAPE_INCLUDE_DIR "DUKTAPE_INCLUDE_DIR-NOTFOUND" CACHE PATH "Path to Duktape headers")
+ set(DUKTAPE_LIBRARIES "DUKTAPE_LIBRARIES-NOTFOUND" CACHE FILEPATH "Path to Duktape libraries")
+
+ option(WITH_DUKTAPE "Search for Duktape package" ON)
+
+ if(WITH_DUKTAPE)
+ px_check_modules(DUKTAPE "duktape")
+
+ if(NOT DUKTAPE_FOUND AND DUKTAPE_INCLUDE_DIR AND DUKTAPE_LIBRARIES)
+ message("Duktape headers: " ${DUKTAPE_INCLUDE_DIR})
+ message("Duktape libraries: " ${DUKTAPE_LIBRARIES})
+ set(DUKTAPE_FOUND 1)
+ include_directories(${DUKTAPE_INCLUDE_DIR})
+ link_directories(${DUKTAPE_LIBRARIES})
+ link_libraries(duktape)
+ endif()
+ endif()
+endif()
diff --git a/libproxy/cmake/modules/pacrunner_mozjs.cmk b/libproxy/cmake/modules/pacrunner_mozjs.cmk
index 2cc3c51..117d789 100644
--- a/libproxy/cmake/modules/pacrunner_mozjs.cmk
+++ b/libproxy/cmake/modules/pacrunner_mozjs.cmk
@@ -7,7 +7,7 @@ if(WIN32)
include_directories("${MOZJS_INCLUDE_DIR}")
endif()
elseif(NOT APPLE)
- option(WITH_MOZJS "Search for MOZJS package" ON)
+ option(WITH_MOZJS "Search for MOZJS package" OFF)
if (WITH_MOZJS)
pkg_search_module(MOZJS mozjs-68)
if(MOZJS_FOUND)
diff --git a/libproxy/modules/config_envvar.cpp b/libproxy/modules/config_envvar.cpp
index 2c4217e..544819b 100644
--- a/libproxy/modules/config_envvar.cpp
+++ b/libproxy/modules/config_envvar.cpp
@@ -28,6 +28,12 @@ public:
const char *proxy = NULL;
vector<url> response;
+ // If _PX_DEBUG_PACURL is set, use it as the PAC URL.
+ if (proxy = getenv("_PX_DEBUG_PACURL")) {
+ response.push_back(url(string("pac+") + proxy));
+ return response;
+ }
+
// If the URL is an ftp url, try to read the ftp proxy
if (dst.get_scheme() == "ftp") {
if (!(proxy = getenv("ftp_proxy")))
diff --git a/libproxy/modules/pacrunner_duktape.cpp b/libproxy/modules/pacrunner_duktape.cpp
new file mode 100644
index 0000000..8288e13
--- /dev/null
+++ b/libproxy/modules/pacrunner_duktape.cpp
@@ -0,0 +1,138 @@
+/*******************************************************************************
+ * libproxy - A library for proxy configuration
+ * Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
+ * Copyright (C) 2021 Zhaofeng Li <hello@zhaofeng.li>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ ******************************************************************************/
+
+#include "../extension_pacrunner.hpp"
+#include <unistd.h> // gethostname
+using namespace libproxy;
+
+#include <duktape.h>
+#include "pacutils.h"
+
+static duk_ret_t dnsResolve(duk_context *ctx) {
+ if (duk_get_top(ctx) != 1) {
+ // Invalid number of arguments
+ return 0;
+ }
+
+ // We do not need to free the string - It's managed by Duktape.
+ const char *hostname = duk_get_string(ctx, 0);
+ if (!hostname) {
+ return 0;
+ }
+
+ // Look it up
+ struct addrinfo *info;
+ if (getaddrinfo(hostname, NULL, NULL, &info)) {
+ return 0;
+ }
+
+ // Try for IPv4
+ char tmp[INET6_ADDRSTRLEN+1];
+ if (getnameinfo(info->ai_addr, info->ai_addrlen,
+ tmp, INET6_ADDRSTRLEN+1,
+ NULL, 0,
+ NI_NUMERICHOST)) {
+ freeaddrinfo(info);
+ duk_push_null(ctx);
+ return 1;
+ }
+ freeaddrinfo(info);
+
+ // Create the return value
+ duk_push_string(ctx, tmp);
+ return 1;
+}
+
+static duk_ret_t myIpAddress(duk_context *ctx) {
+ char hostname[1024];
+ hostname[sizeof(hostname) - 1] = '\0';
+
+ if (!gethostname(hostname, sizeof(hostname) - 1)) {
+ duk_push_string(ctx, hostname);
+ return dnsResolve(ctx);
+ }
+
+ return duk_error(ctx, DUK_ERR_ERROR, "Unable to find hostname!");
+}
+
+class duktape_pacrunner : public pacrunner {
+public:
+ duktape_pacrunner(string pac, const url& pacurl) : pacrunner(pac, pacurl) {
+ this->ctx = duk_create_heap_default();
+ if (!this->ctx) goto error;
+ duk_push_c_function(this->ctx, dnsResolve, 1);
+ duk_put_global_string(this->ctx, "dnsResolve");
+
+ duk_push_c_function(this->ctx, myIpAddress, 1);
+ duk_put_global_string(this->ctx, "myIpAddress");
+
+ // Add other routines
+ duk_push_string(this->ctx, JAVASCRIPT_ROUTINES);
+ if (duk_peval_noresult(this->ctx)) {
+ goto error;
+ }
+
+ // Add the PAC into the context
+ duk_push_string(this->ctx, pac.c_str());
+ if (duk_peval_noresult(this->ctx)) {
+ goto error;
+ }
+
+ return;
+ error:
+ duk_destroy_heap(this->ctx);
+ throw bad_alloc();
+ }
+
+ ~duktape_pacrunner() {
+ duk_destroy_heap(this->ctx);
+ }
+
+ string run(const url& url_) override {
+ string url = url_.to_string();
+ string host = url_.get_host();
+
+ duk_get_global_string(this->ctx, "FindProxyForURL");
+ duk_push_string(this->ctx, url.c_str());
+ duk_push_string(this->ctx, host.c_str());
+ duk_int_t result = duk_pcall(this->ctx, 2);
+
+ if (result == 0) {
+ // Success
+ const char *proxy = duk_get_string(this->ctx, 0);
+ if (!proxy) {
+ duk_pop(this->ctx);
+ return "";
+ }
+ string proxyString = string(proxy);
+ duk_pop(this->ctx);
+ return proxyString;
+ } else {
+ // Something happened. The top of the stack is an error.
+ duk_pop(this->ctx);
+ return "";
+ }
+ }
+
+private:
+ duk_context *ctx;
+};
+
+PX_PACRUNNER_MODULE_EZ(duktape, "duk_create_heap_default", "duktape");
diff --git a/libproxy/modules/pacrunner_mozjs.cpp b/libproxy/modules/pacrunner_mozjs.cpp
index a8fb59a..129d4b5 100644
--- a/libproxy/modules/pacrunner_mozjs.cpp
+++ b/libproxy/modules/pacrunner_mozjs.cpp
@@ -93,6 +93,7 @@ static bool dnsResolve(JSContext *cx, unsigned argc, JS::Value *vp) {
static bool myIpAddress(JSContext *cx, unsigned argc, JS::Value *vp) {
JS::CallArgs argv=JS::CallArgsFromVp(argc,vp);
char *hostname = (char *) JS_malloc(cx, 1024);
+ hostname[1023] = '\0';
if (!gethostname(hostname, 1023)) {
JSString *myhost = JS_NewStringCopyN(cx, hostname, strlen(hostname));
diff --git a/libproxy/modules/pacrunner_natus.cpp b/libproxy/modules/pacrunner_natus.cpp
index 1505578..e1dc6c1 100644
--- a/libproxy/modules/pacrunner_natus.cpp
+++ b/libproxy/modules/pacrunner_natus.cpp
@@ -56,7 +56,9 @@ static Value dnsResolve(Value& ths, Value& fnc, vector<Value>& arg) {
static Value myIpAddress(Value& ths, Value& fnc, vector<Value>& arg) {
char hostname[1024];
- if (!gethostname(hostname, 1023)) {
+ hostname[sizeof(hostname) - 1] = '\0';
+
+ if (!gethostname(hostname, sizeof(hostname) - 1)) {
vector<Value> dnsargs;
dnsargs.push_back(ths.newString(hostname));
return dnsResolve(ths, fnc, dnsargs);
diff --git a/libproxy/modules/pacrunner_webkit.cpp b/libproxy/modules/pacrunner_webkit.cpp
index b79a9d6..d907806 100644
--- a/libproxy/modules/pacrunner_webkit.cpp
+++ b/libproxy/modules/pacrunner_webkit.cpp
@@ -86,7 +86,9 @@ static JSValueRef dnsResolve(JSContextRef ctx, JSObjectRef /*func*/, JSObjectRef
static JSValueRef myIpAddress(JSContextRef ctx, JSObjectRef func, JSObjectRef self, size_t /*argc*/, const JSValueRef[] /*argv*/, JSValueRef* /*exception*/)
{
char hostname[1024];
- if (!gethostname(hostname, 1023)) {
+ hostname[sizeof(hostname) - 1] = '\0';
+
+ if (!gethostname(hostname, sizeof(hostname) - 1)) {
JSStringRef str = JSStringCreateWithUTF8CString(hostname);
JSValueRef val = JSValueMakeString(ctx, str);
JSStringRelease(str);