WAF增加显示网页动作

This commit is contained in:
GoEdgeLab
2021-10-25 19:42:12 +08:00
parent d8943f3816
commit 40f0fca878
5 changed files with 55 additions and 4 deletions

View File

@@ -56,7 +56,7 @@ func (this *HTTPRequest) doWebsocket() {
} }
clientConn, _, err := this.writer.Hijack() clientConn, _, err := this.writer.Hijack()
if err != nil { if err != nil || clientConn == nil {
this.write50x(err, http.StatusInternalServerError) this.write50x(err, http.StatusInternalServerError)
return return
} }

View File

@@ -15,7 +15,7 @@ func (this *BaseAction) CloseConn(writer http.ResponseWriter) error {
hijack, ok := writer.(http.Hijacker) hijack, ok := writer.(http.Hijacker)
if ok { if ok {
conn, _, err := hijack.Hijack() conn, _, err := hijack.Hijack()
if err == nil { if err == nil && conn != nil {
return conn.Close() return conn.Close()
} }
} }

View File

@@ -0,0 +1,41 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package waf
import (
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
"net/http"
)
type PageAction struct {
BaseAction
Status int `yaml:"status" json:"status"`
Body string `yaml:"body" json:"body"`
}
func (this *PageAction) Init(waf *WAF) error {
return nil
}
func (this *PageAction) Code() string {
return ActionPage
}
func (this *PageAction) IsAttack() bool {
return false
}
// WillChange determine if the action will change the request
func (this *PageAction) WillChange() bool {
return true
}
// Perform the action
func (this *PageAction) Perform(waf *WAF, group *RuleGroup, set *RuleSet, request requests.Request, writer http.ResponseWriter) (allow bool) {
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
writer.WriteHeader(this.Status)
_, _ = writer.Write([]byte(request.Format(this.Body)))
return false
}

View File

@@ -13,6 +13,7 @@ const (
ActionPost307 ActionString = "post_307" // 针对POST的307重定向认证 ActionPost307 ActionString = "post_307" // 针对POST的307重定向认证
ActionRecordIP ActionString = "record_ip" // 记录IP ActionRecordIP ActionString = "record_ip" // 记录IP
ActionTag ActionString = "tag" // 标签 ActionTag ActionString = "tag" // 标签
ActionPage ActionString = "page" // 显示网页
ActionAllow ActionString = "allow" // allow ActionAllow ActionString = "allow" // allow
ActionGoGroup ActionString = "go_group" // go to next rule group ActionGoGroup ActionString = "go_group" // go to next rule group
ActionGoSet ActionString = "go_set" // go to next rule set ActionGoSet ActionString = "go_set" // go to next rule set
@@ -73,6 +74,12 @@ var AllActions = []*ActionDefinition{
Instance: new(TagAction), Instance: new(TagAction),
Type: reflect.TypeOf(new(TagAction)).Elem(), Type: reflect.TypeOf(new(TagAction)).Elem(),
}, },
{
Name: "显示页面",
Code: ActionPage,
Instance: new(PageAction),
Type: reflect.TypeOf(new(PageAction)).Elem(),
},
{ {
Name: "跳到下一个规则分组", Name: "跳到下一个规则分组",
Code: ActionGoGroup, Code: ActionGoGroup,

View File

@@ -67,13 +67,16 @@ func (this *RuleSet) Init(waf *WAF) error {
instance := FindActionInstance(action.Code, action.Options) instance := FindActionInstance(action.Code, action.Options)
if instance == nil { if instance == nil {
remotelogs.Error("WAF_RULE_SET", "can not find instance for action '"+action.Code+"'") remotelogs.Error("WAF_RULE_SET", "can not find instance for action '"+action.Code+"'")
} else { continue
this.actionInstances = append(this.actionInstances, instance)
} }
err := instance.Init(waf) err := instance.Init(waf)
if err != nil { if err != nil {
remotelogs.Error("WAF_RULE_SET", "init action '"+action.Code+"' failed: "+err.Error()) remotelogs.Error("WAF_RULE_SET", "init action '"+action.Code+"' failed: "+err.Error())
continue
} }
this.actionInstances = append(this.actionInstances, instance)
} }
// sort actions // sort actions