Files
EdgeNode/internal/waf/action_page.go

79 lines
1.7 KiB
Go
Raw Permalink Normal View History

2024-05-17 18:30:33 +08:00
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
2021-10-25 19:42:12 +08:00
package waf
import (
"net/http"
2024-07-27 15:42:50 +08:00
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
2021-10-25 19:42:12 +08:00
)
type PageAction struct {
BaseAction
UseDefault bool `yaml:"useDefault" json:"useDefault"`
Status int `yaml:"status" json:"status"`
Body string `yaml:"body" json:"body"`
2021-10-25 19:42:12 +08:00
}
func (this *PageAction) Init(waf *WAF) error {
if waf.DefaultPageAction != nil {
if this.Status <= 0 || this.UseDefault {
this.Status = waf.DefaultPageAction.Status
}
if len(this.Body) == 0 || this.UseDefault {
this.Body = waf.DefaultPageAction.Body
}
}
2023-05-28 17:11:33 +08:00
if this.Status <= 0 {
this.Status = http.StatusForbidden
}
2021-10-25 19:42:12 +08:00
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) PerformResult {
if writer == nil {
return PerformResult{}
}
request.ProcessResponseHeaders(writer.Header(), this.Status)
2021-10-25 19:42:12 +08:00
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
writer.WriteHeader(this.Status)
var body = this.Body
if len(body) == 0 {
body = `<!DOCTYPE html>
<html lang="en">
<head>
<title>403 Forbidden</title>
<style>
address { line-height: 1.8; }
</style>
</head>
<body>
<h1>403 Forbidden By WAF</h1>
<address>Connection: ${remoteAddr} (Client) -&gt; ${serverAddr} (Server)</address>
<address>Request ID: ${requestId}</address>
</body>
</html>`
}
_, _ = writer.Write([]byte(request.Format(body)))
2021-10-25 19:42:12 +08:00
return PerformResult{}
2021-10-25 19:42:12 +08:00
}