Using multiple threads in reading logs via rule message example

This commit is contained in:
Felipe Zimmerle 2017-06-03 16:13:48 -03:00
parent 8fbb9e8128
commit 2a5085255e
No known key found for this signature in database
GPG Key ID: E6DFB08CE8B11277
3 changed files with 111 additions and 62 deletions

View File

@ -16,12 +16,96 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include <unistd.h>
#define NUM_THREADS 100
char request_header[] = "" \
"GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1\n\r" \
"Host: net.tutsplus.com\n\r" \
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5)" \
" Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)\n\r" \
"Accept: text/html,application/xhtml+xml,application/xml; " \
"q=0.9,*/*;q=0.8\n\r" \
"Accept-Language: en-us,en;q=0.5\n\r" \
"Accept-Encoding: gzip,deflate\n\r" \
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n\r" \
"Keep-Alive: 300\n\r" \
"Connection: keep-alive\n\r" \
"Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120\n\r" \
"Pragma: no-cache\n\r" \
"Cache-Control: no-cache\n\r";
char request_uri[] = "/test.pl?param1=test&para2=test2";
char request_body[] = "";
char response_headers[] = "" \
"HTTP/1.1 200 OK\n\r" \
"Content-Type: text/xml; charset=utf-8\n\r" \
"Content-Length: length\n\r";
char response_body[] = "" \
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\r" \
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " \
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " \
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n\r" \
" <soap:Body>\n\r" \
" <EnlightenResponse xmlns=\"http://clearforest.com/\">\n\r" \
" <EnlightenResult>string</EnlightenResult>\n\r" \
" </EnlightenResponse>\n\r" \
" </soap:Body>\n\r" \
"</soap:Envelope>\n\r";
char ip[] = "200.249.12.31";
#include "modsecurity/rule_message.h" #include "modsecurity/rule_message.h"
#ifndef EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_ #ifndef EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
#define EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_ #define EXAMPLES_READING_LOGS_VIA_RULE_MESSAGE_READING_LOGS_VIA_RULE_MESSAGE_H_
struct data_ms {
modsecurity::ModSecurity *modsec;
modsecurity::Rules *rules;
};
static void *process_request(void *data) {
struct data_ms *a = (struct data_ms *)data;
modsecurity::ModSecurity *modsec = a->modsec;
modsecurity::Rules *rules = a->rules;
int z = 0;
for (z = 0; z < 10000; z++) {
modsecurity::Transaction *modsecTransaction = \
new modsecurity::Transaction(modsec, rules, NULL);
modsecTransaction->processConnection(ip, 12345, "127.0.0.1", 80);
modsecTransaction->processURI(request_uri, "GET", "1.1");
usleep(10);
modsecTransaction->addRequestHeader("Host",
"net.tutsplus.com");
modsecTransaction->processRequestHeaders();
modsecTransaction->processRequestBody();
modsecTransaction->addResponseHeader("HTTP/1.1",
"200 OK");
modsecTransaction->processResponseHeaders(200, "HTTP 1.2");
modsecTransaction->appendResponseBody(
(const unsigned char*)response_body,
strlen((const char*)response_body));
modsecTransaction->processResponseBody();
modsecTransaction->processLogging();
delete modsecTransaction;
}
pthread_exit(NULL);
return NULL;
}
class ReadingLogsViaRuleMessage { class ReadingLogsViaRuleMessage {
public: public:
ReadingLogsViaRuleMessage(char *request_header, ReadingLogsViaRuleMessage(char *request_header,
@ -41,6 +125,11 @@ class ReadingLogsViaRuleMessage {
{ } { }
int process() { int process() {
pthread_t threads[NUM_THREADS];
int i;
struct data_ms dms;
void *status;
modsecurity::ModSecurity *modsec; modsecurity::ModSecurity *modsec;
modsecurity::Rules *rules; modsecurity::Rules *rules;
modsecurity::ModSecurityIntervention it; modsecurity::ModSecurityIntervention it;
@ -58,27 +147,24 @@ class ReadingLogsViaRuleMessage {
return -1; return -1;
} }
modsecurity::Transaction *modsecTransaction = \ dms.modsec = modsec;
new modsecurity::Transaction(modsec, rules, NULL); dms.rules = rules;
modsecTransaction->processConnection(m_ip, 12345, "127.0.0.1", 80);
modsecTransaction->processURI(m_request_uri, "GET", "1.1");
modsecTransaction->addRequestHeader("Host", for (i = 0; i < NUM_THREADS; i++) {
"net.tutsplus.com"); pthread_create(&threads[i], NULL, process_request, (void *)&dms);
modsecTransaction->processRequestHeaders(); //process_request((void *)&dms);
modsecTransaction->processRequestBody(); }
modsecTransaction->addResponseHeader("HTTP/1.1",
"200 OK"); usleep(10000);
modsecTransaction->processResponseHeaders(200, "HTTP 1.2");
modsecTransaction->appendResponseBody( for (i=0; i < NUM_THREADS; i++) {
(const unsigned char*)m_response_body, pthread_join(threads[i], &status);
strlen((const char*)m_response_body)); std::cout << "Main: completed thread id :" << i << std::endl;
modsecTransaction->processResponseBody(); }
modsecTransaction->processLogging();
delete modsecTransaction;
delete rules; delete rules;
delete modsec; delete modsec;
pthread_exit(NULL);
return 0; return 0;
end: end:
return -1; return -1;

View File

@ -18,60 +18,26 @@
#include <modsecurity/modsecurity.h> #include <modsecurity/modsecurity.h>
#include <modsecurity/rules.h> #include <modsecurity/rules.h>
#include "examples/reading_logs_via_rule_message/reading_logs_via_rule_message.h" #include "examples/reading_logs_via_rule_message/reading_logs_via_rule_message.h"
char request_header[] = "" \
"GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1\n\r" \
"Host: net.tutsplus.com\n\r" \
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5)" \
" Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)\n\r" \
"Accept: text/html,application/xhtml+xml,application/xml; " \
"q=0.9,*/*;q=0.8\n\r" \
"Accept-Language: en-us,en;q=0.5\n\r" \
"Accept-Encoding: gzip,deflate\n\r" \
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n\r" \
"Keep-Alive: 300\n\r" \
"Connection: keep-alive\n\r" \
"Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120\n\r" \
"Pragma: no-cache\n\r" \
"Cache-Control: no-cache\n\r";
char request_uri[] = "/test.pl?param1=test&para2=test2";
char request_body[] = "";
char response_headers[] = "" \
"HTTP/1.1 200 OK\n\r" \
"Content-Type: text/xml; charset=utf-8\n\r" \
"Content-Length: length\n\r";
char response_body[] = "" \
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\r" \
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " \
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " \
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n\r" \
" <soap:Body>\n\r" \
" <EnlightenResponse xmlns=\"http://clearforest.com/\">\n\r" \
" <EnlightenResult>string</EnlightenResult>\n\r" \
" </EnlightenResponse>\n\r" \
" </soap:Body>\n\r" \
"</soap:Envelope>\n\r";
char ip[] = "200.249.12.31";
int main(int argc, char **argv) { int main(int argc, char **argv) {
(*argv)++; *argv++;
if (*argv == NULL) { if (*argv == NULL) {
(*argv)--; *argv--;
std::cout << "Use " << *argv << " test-case-file.conf"; std::cout << "Use " << *argv << " test-case-file.conf";
std::cout << std::endl << std::endl; std::cout << std::endl << std::endl;
return -1; return -1;
} }
std::string rules(*argv); std::string rules(*argv);
ReadingLogsViaRuleMessage rlvrm(request_header, request_uri, request_body, ReadingLogsViaRuleMessage rlvrm(request_header, request_uri, request_body,
response_headers, response_body, ip, rules); response_headers, response_body, ip, rules);
rlvrm.process(); rlvrm.process();
pthread_exit(NULL);
return 0; return 0;
} }

View File

@ -38,10 +38,7 @@ namespace backend {
InMemoryPerProcess::InMemoryPerProcess() { InMemoryPerProcess::InMemoryPerProcess() {
this->reserve(1000); this->reserve(1000);
if (pthread_mutex_init(&m_lock, NULL) != 0) pthread_mutex_init(&m_lock, NULL);
{
printf("\n mutex init failed\n");
}
} }
InMemoryPerProcess::~InMemoryPerProcess() { InMemoryPerProcess::~InMemoryPerProcess() {