Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 2d0b072

Browse files
committed
[waf]规则集动作支持跳转到下一个规则分组或者下一个规则集
1 parent cafff6a commit 2d0b072

39 files changed

+533
-336
lines changed

teaproxy/request_waf.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package teaproxy
22

33
import (
4-
"github.com/TeaWeb/code/teawaf/actions"
4+
"github.com/TeaWeb/code/teawaf"
55
"github.com/iwind/TeaGo/logs"
66
"net/http"
77
)
@@ -18,7 +18,7 @@ func (this *Request) callWAFRequest(writer *ResponseWriter) (blocked bool) {
1818
}
1919

2020
if ruleSet != nil {
21-
if ruleSet.Action != actions.ActionAllow {
21+
if ruleSet.Action != teawaf.ActionAllow {
2222
this.SetAttr("waf_action", ruleSet.Action)
2323
this.SetAttr("waf_group", group.Id)
2424
this.SetAttr("waf_ruleset", ruleSet.Id)
@@ -43,7 +43,7 @@ func (this *Request) callWAFResponse(resp *http.Response, writer *ResponseWriter
4343
}
4444

4545
if ruleSet != nil {
46-
if ruleSet.Action != actions.ActionAllow {
46+
if ruleSet.Action != teawaf.ActionAllow {
4747
this.SetAttr("waf_action", ruleSet.Action)
4848
this.SetAttr("waf_group", group.Id)
4949
this.SetAttr("waf_ruleset", ruleSet.Id)

teastats/waf_block_all_period.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package teastats
22

33
import (
44
"github.com/TeaWeb/code/tealogs/accesslogs"
5-
"github.com/TeaWeb/code/teawaf/actions"
5+
"github.com/TeaWeb/code/teawaf"
66
"github.com/iwind/TeaGo/logs"
77
"github.com/iwind/TeaGo/maps"
88
"strings"
@@ -72,7 +72,7 @@ func (this *WAFBlockAllPeriodFilter) Filter(accessLog *accesslogs.AccessLog) {
7272
if !ok {
7373
return
7474
}
75-
if wafAction != actions.ActionBlock {
75+
if wafAction != teawaf.ActionBlock {
7676
return
7777
}
7878

teawaf/action_allow.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package teawaf
2+
3+
import (
4+
"github.com/TeaWeb/code/teawaf/requests"
5+
"net/http"
6+
)
7+
8+
type AllowAction struct {
9+
}
10+
11+
func (this *AllowAction) Perform(waf *WAF, request *requests.Request, writer http.ResponseWriter) (allow bool) {
12+
// do nothing
13+
return true
14+
}

teawaf/actions/action_block.go renamed to teawaf/action_block.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package actions
1+
package teawaf
22

33
import (
44
"github.com/TeaWeb/code/teautils"
5+
"github.com/TeaWeb/code/teawaf/requests"
56
"github.com/iwind/TeaGo/Tea"
67
"github.com/iwind/TeaGo/logs"
78
"io"
@@ -23,7 +24,7 @@ type BlockAction struct {
2324
URL string `yaml:"url" json:"url"`
2425
}
2526

26-
func (this *BlockAction) Perform(request *http.Request, writer http.ResponseWriter) (allow bool) {
27+
func (this *BlockAction) Perform(waf *WAF, request *requests.Request, writer http.ResponseWriter) (allow bool) {
2728
if writer != nil {
2829
if this.StatusCode > 0 {
2930
writer.WriteHeader(this.StatusCode)

teawaf/actions/action_captcha.go renamed to teawaf/action_captcha.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package actions
1+
package teawaf
22

33
import (
44
"bytes"
55
"encoding/base64"
66
"fmt"
7+
"github.com/TeaWeb/code/teawaf/requests"
78
"github.com/dchest/captcha"
89
"github.com/iwind/TeaGo/logs"
910
"github.com/iwind/TeaGo/types"
@@ -21,7 +22,7 @@ const (
2122
type CaptchaAction struct {
2223
}
2324

24-
func (this *CaptchaAction) Perform(request *http.Request, writer http.ResponseWriter) (allow bool) {
25+
func (this *CaptchaAction) Perform(waf *WAF, request *requests.Request, writer http.ResponseWriter) (allow bool) {
2526
// TEAWEB_CAPTCHA:
2627
cookie, err := request.Cookie("TEAWEB_WAF_CAPTCHA")
2728
if err == nil && cookie != nil && len(cookie.Value) > 32 {
@@ -48,7 +49,7 @@ func (this *CaptchaAction) Perform(request *http.Request, writer http.ResponseWr
4849
Path: "/", // all of dirs
4950
})
5051

51-
http.Redirect(writer, request, request.URL.String(), http.StatusTemporaryRedirect)
52+
http.Redirect(writer, request.Raw(), request.URL.String(), http.StatusTemporaryRedirect)
5253

5354
return false
5455
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
package actions
1+
package teawaf
2+
3+
import "reflect"
24

35
// action definition
46
type ActionDefinition struct {
57
Name string
68
Code ActionString
79
Description string
810
Instance ActionInterface
11+
Type reflect.Type
912
}

teawaf/action_go_group.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package teawaf
2+
3+
import (
4+
"github.com/TeaWeb/code/teawaf/requests"
5+
"github.com/iwind/TeaGo/logs"
6+
"net/http"
7+
)
8+
9+
type GoGroupAction struct {
10+
GroupId string `yaml:"groupId" json:"groupId"`
11+
}
12+
13+
func (this *GoGroupAction) Perform(waf *WAF, request *requests.Request, writer http.ResponseWriter) (allow bool) {
14+
group := waf.FindRuleGroup(this.GroupId)
15+
if group == nil || !group.On {
16+
return true
17+
}
18+
19+
b, set, err := group.MatchRequest(request)
20+
if err != nil {
21+
logs.Error(err)
22+
return true
23+
}
24+
25+
if !b {
26+
return true
27+
}
28+
29+
actionObject := FindActionInstance(set.Action, set.ActionOptions)
30+
if actionObject == nil {
31+
return true
32+
}
33+
return actionObject.Perform(waf, request, writer)
34+
}

teawaf/action_go_set.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package teawaf
2+
3+
import (
4+
"github.com/TeaWeb/code/teawaf/requests"
5+
"github.com/iwind/TeaGo/logs"
6+
"net/http"
7+
)
8+
9+
type GoSetAction struct {
10+
GroupId string `yaml:"groupId" json:"groupId"`
11+
SetId string `yaml:"setId" json:"setId"`
12+
}
13+
14+
func (this *GoSetAction) Perform(waf *WAF, request *requests.Request, writer http.ResponseWriter) (allow bool) {
15+
group := waf.FindRuleGroup(this.GroupId)
16+
if group == nil || !group.On {
17+
return true
18+
}
19+
set := group.FindRuleSet(this.SetId)
20+
if set == nil || !set.On {
21+
return true
22+
}
23+
24+
b, err := set.MatchRequest(request)
25+
if err != nil {
26+
logs.Error(err)
27+
return true
28+
}
29+
if !b {
30+
return true
31+
}
32+
actionObject := FindActionInstance(set.Action, set.ActionOptions)
33+
if actionObject == nil {
34+
return true
35+
}
36+
return actionObject.Perform(waf, request, writer)
37+
}

teawaf/actions/action_instance.go renamed to teawaf/action_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package actions
1+
package teawaf
22

33
type Action struct {
44

teawaf/action_log.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package teawaf
2+
3+
import (
4+
"github.com/TeaWeb/code/teawaf/requests"
5+
"net/http"
6+
)
7+
8+
type LogAction struct {
9+
}
10+
11+
func (this *LogAction) Perform(waf *WAF, request *requests.Request, writer http.ResponseWriter) (allow bool) {
12+
return true
13+
}

0 commit comments

Comments
 (0)