-
-
Notifications
You must be signed in to change notification settings - Fork 230
/
main.go
62 lines (49 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/corazawaf/coraza/v3"
txhttp "github.com/corazawaf/coraza/v3/http"
"github.com/corazawaf/coraza/v3/types"
)
func exampleHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain")
resBody := "Hello world, transaction not disrupted."
if body := os.Getenv("RESPONSE_BODY"); body != "" {
resBody = body
}
if h := os.Getenv("RESPONSE_HEADERS"); h != "" {
key, val, _ := strings.Cut(h, ":")
w.Header().Set(key, val)
}
// The server generates the response
w.Write([]byte(resBody))
}
func main() {
waf := createWAF()
http.Handle("/", txhttp.WrapHandler(waf, http.HandlerFunc(exampleHandler)))
fmt.Println("Server is running. Listening port: 8090")
log.Fatal(http.ListenAndServe(":8090", nil))
}
func createWAF() coraza.WAF {
directivesFile := "./default.conf"
if s := os.Getenv("DIRECTIVES_FILE"); s != "" {
directivesFile = s
}
waf, err := coraza.NewWAF(
coraza.NewWAFConfig().
WithErrorCallback(logError).
WithDirectivesFromFile(directivesFile),
)
if err != nil {
log.Fatal(err)
}
return waf
}
func logError(error types.MatchedRule) {
msg := error.ErrorLog()
fmt.Printf("[logError][%s] %s\n", error.Rule().Severity(), msg)
}