From: Fabian Keil <fk@fabiankeil.de> Date: Wed, 25 May 2016 10:50:55 +0000 (+0000) Subject: Let dynamic filters and taggers support a $listen-address variable X-Git-Tag: v_3_0_25~13 X-Git-Url: http://www.privoxy.org/gitweb/%22https:/faq/@default-cgi@/static/@url@?a=commitdiff_plain;h=afac6cca62fd37496b45e002c6c18da532e001ef;p=privoxy.git Let dynamic filters and taggers support a $listen-address variable ... which contains the address the request came in on. For external filters the variable is called $PRIVOXY_LISTEN_ADDRESS. Original patch contributed by pursievro. --- diff --git a/default.filter b/default.filter index 46408c08..9815feac 100644 --- a/default.filter +++ b/default.filter @@ -2,7 +2,7 @@ # # File : $Source: /cvsroot/ijbswa/current/default.filter,v $ # -# $Id: default.filter,v 1.91 2014/06/20 09:46:13 fabiankeil Exp $ +# $Id: default.filter,v 1.92 2014/10/17 14:45:10 fabiankeil Exp $ # # Purpose : Rules to process the content of web pages # @@ -57,7 +57,8 @@ # quoting. # # 'D' (Dynamic) allows the use of variables. Supported variables are: -# $host, $origin (the IP address the request came from), $path and $url. +# $host, $listen-address, $origin (the IP address the request came +# from), $path and $url. # # Note that '$' is a bad choice as delimiter for dynamic filters as you # might end up with unintended variables if you use a variable name diff --git a/doc/source/user-manual.sgml b/doc/source/user-manual.sgml index 0a8588df..4151b425 100644 --- a/doc/source/user-manual.sgml +++ b/doc/source/user-manual.sgml @@ -36,7 +36,7 @@ This file belongs into ijbswa.sourceforge.net:/home/groups/i/ij/ijbswa/htdocs/ - $Id: user-manual.sgml,v 2.210 2016/05/03 13:20:20 fabiankeil Exp $ + $Id: user-manual.sgml,v 2.211 2016/05/22 12:42:11 fabiankeil Exp $ Copyright (C) 2001-2016 Privoxy Developers https://www.privoxy.org/ See LICENSE. @@ -62,7 +62,7 @@ </subscript> </pubdate> -<pubdate>$Id: user-manual.sgml,v 2.210 2016/05/03 13:20:20 fabiankeil Exp $</pubdate> +<pubdate>$Id: user-manual.sgml,v 2.211 2016/05/22 12:42:11 fabiankeil Exp $</pubdate> <!-- @@ -6773,8 +6773,10 @@ stupid-server.example.com/</screen> <para> The non-standard option letter <literal>D</literal> (dynamic) allows to use the variables $host, $origin (the IP address the request came from), - $path and $url. They will be replaced with the value they refer to before - the filter is executed. + $path, $url and $listen-address (the address on which Privoxy accepted the + client request. Example: 127.0.0.1:8118). + They will be replaced with the value they refer to before the filter + is executed. </para> <para> @@ -7531,9 +7533,10 @@ pre-defined filters for your convenience: </para> <para> External filters read the content from STDIN and write the rewritten - content to STDOUT. The environment variables PRIVOXY_URL, PRIVOXY_PATH, - PRIVOXY_HOST, PRIVOXY_ORIGIN can be used to get some details about the - client request. + content to STDOUT. + The environment variables PRIVOXY_URL, PRIVOXY_PATH, PRIVOXY_HOST, + PRIVOXY_ORIGIN, PRIVOXY_LISTEN_ADDRESS can be used to get some details + about the client request. </para> <para> &my-app; will temporary store the content to filter in the diff --git a/filters.c b/filters.c index c3aa3fd3..d4cec754 100644 --- a/filters.c +++ b/filters.c @@ -1,4 +1,4 @@ -const char filters_rcs[] = "$Id: filters.c,v 1.200 2016/02/26 12:29:38 fabiankeil Exp $"; +const char filters_rcs[] = "$Id: filters.c,v 1.201 2016/03/17 10:40:53 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/filters.c,v $ @@ -935,6 +935,7 @@ pcrs_job *compile_dynamic_pcrs_job_list(const struct client_state *csp, const st {"path", csp->http->path, 1}, {"host", csp->http->host, 1}, {"origin", csp->ip_addr_str, 1}, + {"listen-address", csp->listen_addr_str, 1}, {NULL, NULL, 1} }; @@ -1771,6 +1772,7 @@ static void set_privoxy_variables(const struct client_state *csp) { "PRIVOXY_PATH", csp->http->path }, { "PRIVOXY_HOST", csp->http->host }, { "PRIVOXY_ORIGIN", csp->ip_addr_str }, + { "PRIVOXY_LISTEN_ADDRESS", csp->listen_addr_str }, }; for (i = 0; i < SZ(env); i++) diff --git a/jbsockets.c b/jbsockets.c index 48f43c92..c1a40809 100644 --- a/jbsockets.c +++ b/jbsockets.c @@ -1,4 +1,4 @@ -const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.134 2015/11/06 13:37:35 fabiankeil Exp $"; +const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.135 2016/01/16 12:33:35 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jbsockets.c,v $ @@ -1221,6 +1221,7 @@ int accept_connection(struct client_state * csp, jb_socket fds[]) int max_selected_socket; fd_set selected_fds; jb_socket fd; + size_t listen_addr_size; c_length = sizeof(client); @@ -1348,6 +1349,25 @@ int accept_connection(struct client_state * csp, jb_socket fds[]) csp->ip_addr_long = ntohl(client.sin_addr.s_addr); #endif /* def HAVE_RFC2553 */ + /* + * Save the name and port of the accepting socket for later lookup. + * + * The string needs space for strlen(...) + 7 characters: + * strlen(haddr[i]) + 1 (':') + 5 (port digits) + 1 ('\0') + */ + listen_addr_size = strlen(csp->config->haddr[i]) + 7; + csp->listen_addr_str = malloc_or_die(listen_addr_size); + retval = snprintf(csp->listen_addr_str, listen_addr_size, + "%s:%d", csp->config->haddr[i], csp->config->hport[i]); + if ((-1 == retval) || listen_addr_size <= retval) + { + log_error(LOG_LEVEL_ERROR, + "Server name (%s) and port number (%d) ASCII decimal representation" + "don't fit into %d bytes", + csp->config->haddr[i], csp->config->hport[i], listen_addr_size); + return 0; + } + return 1; } diff --git a/jcc.c b/jcc.c index ad210ab2..082bb02e 100644 --- a/jcc.c +++ b/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 1.442 2016/03/17 10:40:53 fabiankeil Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 1.443 2016/05/22 12:43:07 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $ @@ -3992,6 +3992,12 @@ static void listen_loop(void) "Waiting for the next client connection. Currently active threads: %d", active_threads); + /* + * This config may be outdated, but for accept_connection() + * it's fresh enough. + */ + csp->config = config; + if (!accept_connection(csp, bfds)) { log_error(LOG_LEVEL_CONNECT, "accept failed: %E"); @@ -4051,6 +4057,7 @@ static void listen_loop(void) "Connection from %s on socket %d dropped due to ACL", csp->ip_addr_str, csp->cfd); close_socket(csp->cfd); freez(csp->ip_addr_str); + freez(csp->listen_addr_str); freez(csp_list); continue; } @@ -4066,6 +4073,7 @@ static void listen_loop(void) strlen(TOO_MANY_CONNECTIONS_RESPONSE)); close_socket(csp->cfd); freez(csp->ip_addr_str); + freez(csp->listen_addr_str); freez(csp_list); continue; } diff --git a/loaders.c b/loaders.c index 92e0353f..1fdeabfc 100644 --- a/loaders.c +++ b/loaders.c @@ -1,4 +1,4 @@ -const char loaders_rcs[] = "$Id: loaders.c,v 1.103 2016/05/08 10:45:32 fabiankeil Exp $"; +const char loaders_rcs[] = "$Id: loaders.c,v 1.104 2016/05/22 12:43:07 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/loaders.c,v $ @@ -185,6 +185,7 @@ unsigned int sweep(void) #ifdef FEATURE_CLIENT_TAGS freez(csp->client_address); #endif + freez(csp->listen_addr_str); freez(csp->client_iob->buf); freez(csp->iob->buf); freez(csp->error_message); diff --git a/project.h b/project.h index 32a63d52..92141377 100644 --- a/project.h +++ b/project.h @@ -1,7 +1,7 @@ #ifndef PROJECT_H_INCLUDED #define PROJECT_H_INCLUDED /** Version string. */ -#define PROJECT_H_VERSION "$Id: project.h,v 1.214 2016/03/30 11:13:25 fabiankeil Exp $" +#define PROJECT_H_VERSION "$Id: project.h,v 1.215 2016/05/22 12:43:07 fabiankeil Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/project.h,v $ @@ -927,6 +927,10 @@ struct client_state unsigned long ip_addr_long; #endif /* def HAVE_RFC2553 */ + /** The host name and port (as a string of the form '<hostname>:<port>') + of the server socket to which the client connected. */ + char *listen_addr_str; + /** The URL that was requested */ struct http_request http[1]; diff --git a/user.filter b/user.filter index b138ff29..976953c6 100644 --- a/user.filter +++ b/user.filter @@ -2,7 +2,7 @@ # # File : $Source: /cvsroot/ijbswa/current/user.filter,v $ # -# $Id: user.filter,v 1.2 2006/07/18 14:48:47 david__schmidt Exp $ +# $Id: user.filter,v 1.3 2008/05/21 20:17:03 fabiankeil Exp $ # # Purpose : Rules to process the content of web pages # @@ -63,7 +63,8 @@ # quoting. # # 'D' (Dynamic) allows the use of variables. Supported variables are: -# $host, $origin (the IP address the request came from), $path and $url. +# $host, $listen-address, $origin (the IP address the request came +# from), $path and $url. # # Note that '$' is a bad choice as delimiter for dynamic filters as you # might end up with unintended variables if you use a variable name