Add a CGI handler for /wpad.dat
authorFabian Keil <fk@fabiankeil.de>
Mon, 1 Mar 2021 11:22:06 +0000 (12:22 +0100)
committerFabian Keil <fk@fabiankeil.de>
Wed, 10 Nov 2021 16:46:52 +0000 (17:46 +0100)
... that returns a Proxy Auto-Configuration (PAC) file.

Among other things, it can be used to instruct clients
through DHCP to use Privoxy as proxy.

For example with the dnsmasq option:
dhcp-option=252,http://config.privoxy.org/wpad.dat

Initial patch by Richard Schneidt.

cgi.c
cgisimple.c
cgisimple.h
templates/wpad.dat [new file with mode: 0644]

diff --git a/cgi.c b/cgi.c
index 6c78806..cb1f072 100644 (file)
--- a/cgi.c
+++ b/cgi.c
@@ -221,6 +221,9 @@ static const struct cgi_dispatcher cgi_dispatchers[] = {
    { "user-manual",
           cgi_send_user_manual,
           NULL, TRUE /* Send user-manual */ },
+   { "wpad.dat",
+         cgi_send_wpad,
+         NULL, TRUE /* Send wpad.dat proxy autoconfiguration file */ },
    { NULL, /* NULL Indicates end of list and default page */
          cgi_error_404,
          NULL, TRUE /* Unknown CGI page */ }
index e465be3..9650627 100644 (file)
@@ -870,6 +870,50 @@ jb_err cgi_send_stylesheet(struct client_state *csp,
 }
 
 
+/*********************************************************************
+ *
+ * Function    :  cgi_send_wpad
+ *
+ * Description :  CGI function that sends a Proxy Auto-Configuration
+ *                (PAC) file.
+ *
+ * Parameters  :
+ *          1  :  csp = Current client state (buffers, headers, etc...)
+ *          2  :  rsp = http_response data structure for output
+ *          3  :  parameters = map of cgi parameters
+ *
+ * CGI Parameters : None
+ *
+ * Returns     :  JB_ERR_OK on success
+ *                JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+jb_err cgi_send_wpad(struct client_state *csp,
+                     struct http_response *rsp,
+                     const struct map *parameters)
+{
+   struct map *exports;
+
+   assert(csp);
+   assert(rsp);
+   assert(parameters);
+
+   if (NULL == (exports = default_exports(csp, NULL)))
+   {
+      return JB_ERR_MEMORY;
+   }
+
+   if (enlist(rsp->headers, "Content-Type: application/x-ns-proxy-autoconfig"))
+   {
+      free_map(exports);
+      return JB_ERR_MEMORY;
+   }
+
+   return template_fill_for_cgi(csp, "wpad.dat", exports, rsp);
+
+}
+
+
 /*********************************************************************
  *
  * Function    :  cgi_send_url_info_osd
index 01e6a9e..ab975dc 100644 (file)
@@ -84,6 +84,9 @@ extern jb_err cgi_send_default_favicon (struct client_state *csp,
 extern jb_err cgi_send_stylesheet(struct client_state *csp,
                                   struct http_response *rsp,
                                   const struct map *parameters);
+extern jb_err cgi_send_wpad(struct client_state *csp,
+                            struct http_response *rsp,
+                            const struct map *parameters);
 extern jb_err cgi_send_url_info_osd(struct client_state *csp,
                                     struct http_response *rsp,
                                     const struct map *parameters);
diff --git a/templates/wpad.dat b/templates/wpad.dat
new file mode 100644 (file)
index 0000000..d47d778
--- /dev/null
@@ -0,0 +1,11 @@
+function FindProxyForURL(url, host) {
+    var proxy = "PROXY @my-ip-address@:@my-port@; DIRECT";
+    var direct = "DIRECT";
+    if (isPlainHostName(host)) {
+        return direct;
+    }
+    if (url.substring(0, 4) == "ftp:" || url.substring(0, 6) == "rsync:") {
+        return direct;
+    }
+    return proxy;
+}