diff options
author | npmccallum <npmccallum@c587cffe-e639-0410-9787-d7902ae8ed56> | 2011-02-06 15:13:18 +0000 |
---|---|---|
committer | npmccallum <npmccallum@c587cffe-e639-0410-9787-d7902ae8ed56> | 2011-02-06 15:13:18 +0000 |
commit | 1ce42ef5642b2fd381d8373ad73c7d6ffa574759 (patch) | |
tree | 36355c4a5465abf09a1baf80e4297646822bdf04 | |
parent | eb18877cb8f3e458c0c7999ae39ef7e485cd57e6 (diff) | |
download | libproxy-1ce42ef5642b2fd381d8373ad73c7d6ffa574759.tar.gz |
added natus pacrunner
git-svn-id: http://libproxy.googlecode.com/svn/trunk@779 c587cffe-e639-0410-9787-d7902ae8ed56
-rw-r--r-- | libproxy/cmake/modules.cmk | 4 | ||||
-rw-r--r-- | libproxy/cmake/modules/pacrunner_natus.cmk | 7 | ||||
-rw-r--r-- | libproxy/modules/pacrunner_natus.cpp | 119 |
3 files changed, 129 insertions, 1 deletions
diff --git a/libproxy/cmake/modules.cmk b/libproxy/cmake/modules.cmk index 6558b93..dfbb9e4 100644 --- a/libproxy/cmake/modules.cmk +++ b/libproxy/cmake/modules.cmk @@ -14,11 +14,12 @@ include(cmake/modules/config_kde4.cmk) include(cmake/modules/config_macosx.cmk) include(cmake/modules/network_networkmanager.cmk) include(cmake/modules/pacrunner_mozjs.cmk) +include(cmake/modules/pacrunner_natus.cmk) include(cmake/modules/pacrunner_webkit.cmk) # Build the pacrunner into libproxy unless we are building for multiple engines set(BIPR 1) -if(MOZJS_FOUND AND WEBKIT_FOUND) +if((MOZJS_FOUND AND WEBKIT_FOUND) OR (MOZJS_FOUND AND NATUS_FOUND) OR (WEBKIT_FOUND AND NATUS_FOUND)) set(BIPR 0) endif() @@ -36,6 +37,7 @@ px_module(ignore_hostname 1 1) px_module(ignore_ip 1 1) px_module(network_networkmanager "${NM_FOUND}" 0 ${NM_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(wpad_dns_alias 1 1) message("") diff --git a/libproxy/cmake/modules/pacrunner_natus.cmk b/libproxy/cmake/modules/pacrunner_natus.cmk new file mode 100644 index 0000000..7b14c93 --- /dev/null +++ b/libproxy/cmake/modules/pacrunner_natus.cmk @@ -0,0 +1,7 @@ +if(WIN32) + set(NATUS_FOUND 0) +elseif(APPLE) + set(NATUS_FOUND 0) +else() + px_check_modules(NATUS natus) +endif()
\ No newline at end of file diff --git a/libproxy/modules/pacrunner_natus.cpp b/libproxy/modules/pacrunner_natus.cpp new file mode 100644 index 0000000..aec8457 --- /dev/null +++ b/libproxy/modules/pacrunner_natus.cpp @@ -0,0 +1,119 @@ +/******************************************************************************* + * libproxy - A library for proxy configuration + * Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com> + * + * 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" +using namespace libproxy; + +#define I_ACKNOWLEDGE_THAT_NATUS_IS_NOT_STABLE +#include <natus/natus.h> +#include "pacutils.h" + +#ifndef INET_ADDRSTRLEN +#define INET_ADDRSTRLEN 16 +#endif + +#ifndef INET6_ADDRSTRLEN +#define INET6_ADDRSTRLEN 46 +#endif + +using namespace natus; + +static Value dnsResolve(Value& ths, Value& fnc, vector<Value>& arg) { + Value exc = checkArguments(ths, arg, "s"); + if (exc.isException()) return exc; + + // Look it up + struct addrinfo *info; + if (getaddrinfo(arg[0].toString().c_str(), NULL, NULL, &info)) + return NULL; + + // Try for IPv4 + char* tmp = new char[INET6_ADDRSTRLEN+1]; + if (getnameinfo(info->ai_addr, info->ai_addrlen, + tmp, INET6_ADDRSTRLEN+1, + NULL, 0, + NI_NUMERICHOST)) { + freeaddrinfo(info); + delete tmp; + return NULL; + } + freeaddrinfo(info); + + // Create the return value + Value ret = ths.newString(tmp); + delete tmp; + return ret; +} + +static Value myIpAddress(Value& ths, Value& fnc, vector<Value>& arg) { + char hostname[1024]; + if (!gethostname(hostname, 1023)) { + vector<Value> dnsargs; + dnsargs.push_back(ths.newString(hostname)); + return dnsResolve(ths, fnc, dnsargs); + } + return ths.newString("Unable to find hostname!").toException(); +} + +class webkit_pacrunner : public pacrunner { +public: + webkit_pacrunner(string pac, const url& pacurl) throw (bad_alloc) : pacrunner(pac, pacurl) { + Value exc; + + // Create the basic context + if (!eng.initialize()) goto error; + glb = this->eng.newGlobal(); + if (glb.isException()) goto error; + + // Add dnsResolve into the context + if (!glb.set("dnsResolve", glb.newFunction(dnsResolve))) goto error; + + // Add myIpAddress into the context + if (!glb.set("myIpAddress", glb.newFunction(myIpAddress))) goto error; + + // Add all other routines into the context + exc = glb.evaluate(JAVASCRIPT_ROUTINES); + if (exc.isException()) goto error; + + // Add the PAC into the context + exc = glb.evaluate(pac.c_str(), pacurl.to_string()); + if (exc.isException()) goto error; + return; + + error: + throw bad_alloc(); + } + + string run(const url& url_) throw (bad_alloc) { + vector<Value> args; + args.push_back(glb.newString(url_.to_string())); + args.push_back(glb.newString(url_.get_host())); + + Value res = glb.call("FindProxyForURL", args); + if (res.isString() && !res.isException()) + return res.toString(); + return ""; + } + +private: + Engine eng; + Value glb; +}; + +PX_PACRUNNER_MODULE_EZ(webkit, "JSObjectMakeFunctionWithCallback", "webkit"); |