summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhaofeng Li <hello@zhaofeng.li>2021-06-13 20:35:17 -0700
committerZhaofeng Li <hello@zhaofeng.li>2021-07-13 10:49:59 -0700
commitd61d1c1fcbe8df6c40fc317bac96e2f0f375b146 (patch)
tree765af3057e364180c1934e9c5688f8159ee0f24f
parent66b30ef388a45f66f9b118d51cc76b53fdb8bbf4 (diff)
downloadlibproxy-git-d61d1c1fcbe8df6c40fc317bac96e2f0f375b146.tar.gz
Add Duktape pacrunner module
-rw-r--r--INSTALL1
-rw-r--r--libproxy/cmake/modules.cmk2
-rw-r--r--libproxy/cmake/modules/pacrunner_duktape.cmk21
-rw-r--r--libproxy/modules/pacrunner_duktape.cpp138
4 files changed, 162 insertions, 0 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 0794a6e..476e9c5 100644
--- a/libproxy/cmake/modules.cmk
+++ b/libproxy/cmake/modules.cmk
@@ -18,6 +18,7 @@ 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")
@@ -47,5 +48,6 @@ px_module(network_networkmanager "${NM_BUILD}" 0 ${NM_LIBRARIES}${NM
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("")
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/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");