From 82de52fd11e5edc162e1f356890f8de5a9da4c15 Mon Sep 17 00:00:00 2001 From: Felipe Zipitria Date: Mon, 7 Nov 2022 23:09:44 -0300 Subject: [PATCH] feat: add simple dump Signed-off-by: Felipe Zipitria --- libcoraza/coraza.go | 17 +++++++++++++---- libcoraza/coraza_test.go | 8 ++++---- tests/simple_get.c | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/libcoraza/coraza.go b/libcoraza/coraza.go index 689ab85..0d95a3d 100644 --- a/libcoraza/coraza.go +++ b/libcoraza/coraza.go @@ -20,7 +20,7 @@ typedef struct coraza_intervention_t typedef uint64_t coraza_waf_t; typedef uint64_t coraza_transaction_t; -typedef void (*coraza_log_cb) (const void *); +typedef void (*coraza_log_cb) (void *, const void *); void send_log_to_cb(coraza_log_cb cb, const char *msg); #endif */ @@ -61,7 +61,7 @@ func coraza_new_waf() C.coraza_waf_t { * @returns pointer to transaction */ //export coraza_new_transaction -func coraza_new_transaction(waf C.coraza_waf_t, logCb unsafe.Pointer) C.coraza_transaction_t { +func coraza_new_transaction(waf C.coraza_waf_t) C.coraza_transaction_t { w := ptrToWaf(waf) tx := w.NewTransaction(context.Background()) ptr := transactionToPtr(tx) @@ -70,9 +70,9 @@ func coraza_new_transaction(waf C.coraza_waf_t, logCb unsafe.Pointer) C.coraza_t } //export coraza_new_transaction_with_id -func coraza_new_transaction_with_id(waf C.coraza_waf_t, id *C.char, logCb unsafe.Pointer) C.coraza_transaction_t { +func coraza_new_transaction_with_id(waf C.coraza_waf_t, id *C.char) C.coraza_transaction_t { idd := C.GoString(id) - txPtr := coraza_new_transaction(waf, logCb) + txPtr := coraza_new_transaction(waf) tx := ptrToTransaction(txPtr) tx.ID = idd tx.Variables.UniqueID.Set(idd) @@ -254,6 +254,15 @@ func coraza_rules_merge(w1 C.coraza_waf_t, w2 C.coraza_waf_t, er **C.char) C.int return 0 } +//export coraza_rules_dump +func coraza_rules_dump(w C.coraza_waf_t) C.int { + waf := ptrToWaf(w) + for _, r := range waf.Rules.GetRules() { + fmt.Fprintln(os.Stderr, "%v\n", r) + } + return 0 +} + //export coraza_request_body_from_file func coraza_request_body_from_file(t C.coraza_transaction_t, file *C.char) C.int { tx := ptrToTransaction(t) diff --git a/libcoraza/coraza_test.go b/libcoraza/coraza_test.go index 25a4da2..d45abc2 100644 --- a/libcoraza/coraza_test.go +++ b/libcoraza/coraza_test.go @@ -47,7 +47,7 @@ func TestAddRulesToWaf(t *testing.T) { func TestTransactionInitialization(t *testing.T) { waf := coraza_new_waf() - tt := coraza_new_transaction(waf, nil) + tt := coraza_new_transaction(waf) if tt == 0 { t.Fatal("Transaction initialization failed") } @@ -66,7 +66,7 @@ func TestTransactionInitialization(t *testing.T) { func TestTxCleaning(t *testing.T) { waf := coraza_new_waf() - txPtr := coraza_new_transaction(waf, nil) + txPtr := coraza_new_transaction(waf) tx := ptrToTransaction(txPtr) if tx == nil || tx.ID == "" { t.Fatal("Transaction ID is empty") @@ -80,7 +80,7 @@ func TestTxCleaning(t *testing.T) { func BenchmarkTransactionCreation(b *testing.B) { waf := coraza_new_waf() for i := 0; i < b.N; i++ { - coraza_new_transaction(waf, nil) + coraza_new_transaction(waf) } } @@ -88,7 +88,7 @@ func BenchmarkTransactionProcessing(b *testing.B) { waf := coraza_new_waf() coraza_rules_add(waf, stringToC(`SecRule UNIQUE_ID "" "id:1"`), nil) for i := 0; i < b.N; i++ { - txPtr := coraza_new_transaction(waf, nil) + txPtr := coraza_new_transaction(waf) tx := ptrToTransaction(txPtr) tx.ProcessConnection("127.0.0.1", 55555, "127.0.0.1", 80) tx.ProcessURI("https://www.example.com/some?params=123", "GET", "HTTP/1.1") diff --git a/tests/simple_get.c b/tests/simple_get.c index f5acf5f..6b10e22 100644 --- a/tests/simple_get.c +++ b/tests/simple_get.c @@ -34,7 +34,7 @@ int main() printf("%d rules compiled\n", coraza_rules_count(waf)); printf("Creating transaction...\n"); - tx = coraza_new_transaction(waf, NULL); + tx = coraza_new_transaction(waf); if(tx == 0) { printf("Failed to create transaction\n"); return 1;