Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add simple dump #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions libcoraza/coraza.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions libcoraza/coraza_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
Expand All @@ -80,15 +80,15 @@ 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)
}
}

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")
Expand Down
2 changes: 1 addition & 1 deletion tests/simple_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down