mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-15 23:26:36 +08:00
实现WAF
This commit is contained in:
58
internal/waf/waf_test.go
Normal file
58
internal/waf/waf_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package waf
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWAF_MatchRequest(t *testing.T) {
|
||||
a := assert.NewAssertion(t)
|
||||
|
||||
set := NewRuleSet()
|
||||
set.Name = "Name_Age"
|
||||
set.Connector = RuleConnectorAnd
|
||||
set.Rules = []*Rule{
|
||||
{
|
||||
Param: "${arg.name}",
|
||||
Operator: RuleOperatorEqString,
|
||||
Value: "lu",
|
||||
},
|
||||
{
|
||||
Param: "${arg.age}",
|
||||
Operator: RuleOperatorEq,
|
||||
Value: "20",
|
||||
},
|
||||
}
|
||||
set.Action = ActionBlock
|
||||
|
||||
group := NewRuleGroup()
|
||||
group.AddRuleSet(set)
|
||||
group.IsInbound = true
|
||||
|
||||
waf := NewWAF()
|
||||
waf.AddRuleGroup(group)
|
||||
err := waf.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
waf.OnAction(func(action ActionString) (goNext bool) {
|
||||
return action != ActionBlock
|
||||
})
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "http://teaos.cn/hello?name=lu&age=20", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
goNext, _, set, err := waf.MatchRequest(req, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if set == nil {
|
||||
t.Log("not match")
|
||||
return
|
||||
}
|
||||
t.Log("goNext:", goNext, "set:", set.Name)
|
||||
a.IsFalse(goNext)
|
||||
}
|
||||
Reference in New Issue
Block a user