mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-22 15:59:41 +08:00
pcre: ,log in an action writes what the pattern matched (#794)
An action word of a pcre or pcre_rewrite rule may end with ,log: every match writes a record of its own carrying the matched part of the buffer, so a header can be watched without changing the request's fate (dunno,log), or a refusal can leave its trace (deny,log). The matched text reaches the log through %T, like any service text. The pcre options apply to such a pattern as to any other, so it can be written case-insensitively and by the line.
This commit is contained in:
1 parent
62b6355040
commit
ab78b0bf69
3 files changed
+110
-1
No files matched your search
@@ -1857,6 +1857,20 @@ FILTER_ACTION - action on match:
|
||||
deny - deny this request without checking the rest of the rules
|
||||
.br
|
||||
dunno - continue with the rest of the rules (useful with pcre_rewrite)
|
||||
.br
|
||||
An action may be followed by ,log (as in allow,log or dunno,log): what
|
||||
the pattern matched is written to the log as a record of its own, one
|
||||
per match, in addition to the connection's own record. Put %T in
|
||||
logformat to have the matched text in the line. The options apply to the
|
||||
pattern as to any other, so for example
|
||||
.br
|
||||
pcre_options PCRE2_CASELESS PCRE2_MULTILINE
|
||||
.br
|
||||
pcre cliheader dunno,log "^user-agent:.*$"
|
||||
.br
|
||||
watches the User-Agent: header whatever its case, with the ^ and $
|
||||
anchors holding for every line of the headers, wherever the header
|
||||
comes, and decides nothing about the request.
|
||||
|
||||
REGEXP - PCRE (Perl) regular expression. Use * if no regexp matching is required.
|
||||
|
||||
|
||||
+13
-1
@@ -5,7 +5,7 @@
|
||||
|
||||
*/
|
||||
|
||||
#include "structures.h"
|
||||
#include "proxy.h"
|
||||
#include <string.h>
|
||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||
#define PCRE2_STATIC
|
||||
@@ -216,6 +216,7 @@ struct pcre_filter_data {
|
||||
pcre2_match_data * match_data;
|
||||
pcre2_match_context * match_context;
|
||||
int action;
|
||||
int log;
|
||||
char * replace;
|
||||
struct ace *acl;
|
||||
};
|
||||
@@ -300,6 +301,7 @@ static FILTER_ACTION pcre_filter_buffer(void *fc, struct clientparam *param, uns
|
||||
char * replace;
|
||||
char * tmpbuf, *target, *newbuf;
|
||||
unsigned char *given = *buf_p;
|
||||
unsigned char *matched;
|
||||
int nreplaces=0;
|
||||
struct pcre_client_data *pcd = (struct pcre_client_data *)fc;
|
||||
#define pcrefd (pcd->pcrefd)
|
||||
@@ -319,6 +321,14 @@ static FILTER_ACTION pcre_filter_buffer(void *fc, struct clientparam *param, uns
|
||||
if(count <= 0) break;
|
||||
ovector = pcre2_get_ovector_pointer(pcd->match_data);
|
||||
if(ovector[0] > (PCRE2_SIZE)*length_p || ovector[1] > (PCRE2_SIZE)*length_p || ovector[1] < ovector[0]) break;
|
||||
/* What the pattern matched, one record per match: with dunno,log
|
||||
it is the whole point of the rule */
|
||||
if(pcrefd->log && (matched = malloc(ovector[1] - ovector[0] + 1))){
|
||||
memcpy(matched, *buf_p + ovector[0], ovector[1] - ovector[0]);
|
||||
matched[ovector[1] - ovector[0]] = 0;
|
||||
dolog(param, matched);
|
||||
free(matched);
|
||||
}
|
||||
if(!(replace = pcrefd->replace) || param->nooverwritefilter) return pcrefd->action;
|
||||
|
||||
replen = *length_p - ovector[1];
|
||||
@@ -494,6 +504,7 @@ int h_pcre(int argc, unsigned char **argv){
|
||||
memset(flt, 0, sizeof(struct pcre_filter_data));
|
||||
memset(newf, 0, sizeof(struct filter));
|
||||
flt->action = action;
|
||||
flt->log = (strstr((char *)argv[2],"log") != 0);
|
||||
flt->re = re;
|
||||
flt->match_data = match_data;
|
||||
flt->acl = acl;
|
||||
@@ -608,6 +619,7 @@ int h_pcre_rewrite(int argc, unsigned char **argv){
|
||||
memset(flt, 0, sizeof(struct pcre_filter_data));
|
||||
memset(newf, 0, sizeof(struct filter));
|
||||
flt->action = action;
|
||||
flt->log = (strstr((char *)argv[2],"log") != 0);
|
||||
flt->re = re;
|
||||
flt->match_data = match_data;
|
||||
flt->acl = acl;
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
"""A pcre action with ,log: what the pattern matched is written to the log.
|
||||
|
||||
dunno,log decides nothing, so a request it matches goes on as if the rule
|
||||
was not there, and every match is a record of its own, carrying what the
|
||||
pattern matched of what the client sent. The options apply to it as to
|
||||
any other pattern, so a header can be watched whatever its case, and by
|
||||
the line, wherever in the headers it comes.
|
||||
"""
|
||||
|
||||
|
||||
def _has_pcre(t):
|
||||
"""Whether this build accepts the pcre commands at all.
|
||||
|
||||
The last line is nonsense on purpose: it makes 3proxy report and exit
|
||||
instead of waiting, and what it says about the line above is the answer.
|
||||
"""
|
||||
out = t.run_config("pcre_probe",
|
||||
'log\npcre request deny "x"\nnot_a_command\n')
|
||||
return "'pcre'" not in out
|
||||
|
||||
|
||||
def run(t):
|
||||
if not t.skip_unless("httpsrv", "pcre"):
|
||||
return
|
||||
if not _has_pcre(t):
|
||||
t.skip("PCRE (this build has no PCRE support)")
|
||||
return
|
||||
|
||||
origin = t.free_port()
|
||||
t.start("pcre_origin", f"""
|
||||
log
|
||||
auth iponly
|
||||
allow *
|
||||
http echo * /echo**
|
||||
httpsrv -p{origin}
|
||||
""", ports=[origin])
|
||||
|
||||
url = f"http://127.0.0.1:{origin}"
|
||||
|
||||
def proxy_with(name, *rules):
|
||||
port = t.free_port()
|
||||
server = t.start(name, "\n".join([
|
||||
'logformat "%H:%M:%S %T"',
|
||||
"log", "flush", "auth iponly", "allow *",
|
||||
*rules, f"proxy -p{port}"]),
|
||||
ports=[port])
|
||||
return f"127.0.0.1:{port}", server
|
||||
|
||||
# --- dunno,log: decided nothing, watched something ---------------------
|
||||
p, s = proxy_with("dunno_log", 'pcre cliheader dunno,log "X-Log-Marker:.*"')
|
||||
t.eq(200, t.http(url + "/echo", proxy=p).status,
|
||||
"a request without the header is served")
|
||||
out = s.output()
|
||||
t.not_contains(out, "watchme-123",
|
||||
"and nothing about it is logged but the connection")
|
||||
t.eq(200, t.http(url + "/echo", proxy=p,
|
||||
headers={"X-Log-Marker": "watchme-123"}).status,
|
||||
"a request a dunno,log rule matches is served all the same")
|
||||
out = t.wait_output(s, "watchme-123")
|
||||
t.contains(out, "X-Log-Marker: watchme-123",
|
||||
"what the pattern matched is written to the log")
|
||||
|
||||
# --- deny,log: refused, and the refusal leaves its trace ----------------
|
||||
p, s = proxy_with("deny_log", 'pcre cliheader deny,log "Forbidden-Marker:.*"')
|
||||
t.eq(200, t.http(url + "/echo", proxy=p).status,
|
||||
"a request without the header is served")
|
||||
t.ne(200, t.http(url + "/echo", proxy=p,
|
||||
headers={"Forbidden-Marker": "closed"}).status,
|
||||
"a request a deny,log rule matches is denied")
|
||||
out = t.wait_output(s, "Forbidden-Marker: closed")
|
||||
t.contains(out, "Forbidden-Marker: closed",
|
||||
"what the pattern matched is written to the log")
|
||||
|
||||
# --- the options apply: any case, any line of the headers ----------------
|
||||
p, s = proxy_with("options_log",
|
||||
"pcre_options PCRE2_CASELESS PCRE2_MULTILINE",
|
||||
'pcre cliheader dunno,log "^x-log-marker:.*$"')
|
||||
t.eq(200, t.http(url + "/echo", proxy=p,
|
||||
headers={"X-Log-Marker": "cased-456"}).status,
|
||||
"a header matches in lower case, on whatever line it comes")
|
||||
out = t.wait_output(s, "cased-456")
|
||||
t.contains(out, "X-Log-Marker: cased-456",
|
||||
"and what it matched is written to the log")
|
||||
Reference in new issue
Block a user