comparison mod_firewall/test.lib.lua @ 2585:02c6ae745c4f

mod_firewall: Add 'test' subcommand to read stanzas from stdin and test them against rules
author Matthew Wild <mwild1@gmail.com>
date Sun, 26 Feb 2017 09:58:07 +0000
parents
children b50f7b9fdbbb
comparison
equal deleted inserted replaced
2584:d64fc9c3cffd 2585:02c6ae745c4f
1 local set = require "util.set";
2
3 local xmppstream = require "util.xmppstream";
4
5 local function stderr(...)
6 io.stderr:write("** ", table.concat({...}, "\t", 1, select("#", ...)), "\n");
7 end
8
9 return function (arg)
10 require "net.http".request = function (url, ex, cb)
11 stderr("Making HTTP request to "..url);
12 local body_table = {};
13 local ok, response_status, response_headers = require "ssl.https".request({
14 url = url;
15 headers = ex.headers;
16 method = ex.body and "POST" or "GET";
17 sink = ltn12.sink.table(body_table);
18 source = ex.body and ltn12.source.string(ex.body) or nil;
19 });
20 stderr("HTTP response "..response_status);
21 cb(table.concat(body_table), response_status, { headers = response_headers });
22 return true;
23 end;
24
25 local stats_dropped, stats_passed = 0, 0;
26
27 load_unload_scripts(set.new(arg));
28 local session = { notopen = true };
29 local stream_callbacks = { default_ns = "jabber:client" };
30
31 function stream_callbacks.streamopened()
32 session.notopen = nil;
33 end
34 function stream_callbacks.streamclosed()
35 end
36 function stream_callbacks.error(session, error_name, error_message)
37 stderr("Fatal error parsing XML stream: "..error_name..": "..tostring(error_message))
38 assert(false);
39 end
40 function stream_callbacks.handlestanza(session, stanza)
41 if not module:fire_event("firewall/chains/deliver", { origin = session, stanza = stanza }) then
42 stats_passed = stats_passed + 1;
43 print(stanza);
44 print("");
45 else
46 stats_dropped = stats_dropped + 1;
47 end
48 end
49
50 local stream = xmppstream.new(session, stream_callbacks);
51 stream:feed("<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client'>");
52 local line_count = 0;
53 for line in io.lines() do
54 line_count = line_count + 1;
55 local ok, err = stream:feed(line.."\n");
56 if not ok then
57 stderr("Fatal XML parse error on line "..line_count..": "..err);
58 return 1;
59 end
60 end
61
62 stderr("Summary");
63 stderr("-------");
64 stderr("");
65 stderr(stats_dropped + stats_passed, "processed");
66 stderr(stats_passed, "passed");
67 stderr(stats_dropped, "droppped");
68 stderr(line_count, "input lines");
69 stderr("");
70 end