github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/action_page.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package waf
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
     7  	"net/http"
     8  )
     9  
    10  type PageAction struct {
    11  	BaseAction
    12  
    13  	UseDefault bool   `yaml:"useDefault" json:"useDefault"`
    14  	Status     int    `yaml:"status" json:"status"`
    15  	Body       string `yaml:"body" json:"body"`
    16  }
    17  
    18  func (this *PageAction) Init(waf *WAF) error {
    19  	if waf.DefaultPageAction != nil {
    20  		if this.Status <= 0 || this.UseDefault {
    21  			this.Status = waf.DefaultPageAction.Status
    22  		}
    23  		if len(this.Body) == 0 || this.UseDefault {
    24  			this.Body = waf.DefaultPageAction.Body
    25  		}
    26  	}
    27  
    28  	if this.Status <= 0 {
    29  		this.Status = http.StatusForbidden
    30  	}
    31  	return nil
    32  }
    33  
    34  func (this *PageAction) Code() string {
    35  	return ActionPage
    36  }
    37  
    38  func (this *PageAction) IsAttack() bool {
    39  	return false
    40  }
    41  
    42  // WillChange determine if the action will change the request
    43  func (this *PageAction) WillChange() bool {
    44  	return true
    45  }
    46  
    47  // Perform the action
    48  func (this *PageAction) Perform(waf *WAF, group *RuleGroup, set *RuleSet, request requests.Request, writer http.ResponseWriter) PerformResult {
    49  	if writer == nil {
    50  		return PerformResult{}
    51  	}
    52  
    53  	request.ProcessResponseHeaders(writer.Header(), this.Status)
    54  	writer.Header().Set("Content-Type", "text/html; charset=utf-8")
    55  	writer.WriteHeader(this.Status)
    56  
    57  	var body = this.Body
    58  	if len(body) == 0 {
    59  		body = `<!DOCTYPE html>
    60  <html lang="en">
    61  <head>
    62  	<title>403 Forbidden</title>
    63  	<style>
    64  		address { line-height: 1.8; }
    65  	</style>
    66  </head>
    67  <body>
    68  <h1>403 Forbidden By WAF</h1>
    69  <address>Connection: ${remoteAddr} (Client) -&gt; ${serverAddr} (Server)</address>
    70  <address>Request ID: ${requestId}</address>
    71  </body>
    72  </html>`
    73  	}
    74  	_, _ = writer.Write([]byte(request.Format(body)))
    75  
    76  	return PerformResult{}
    77  }