mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-07-21 03:28:59 +02:00
130 feature custom html error page integration on 403 like captcha but for ban (#145)
* ✨ Add ban html template * 📝 Add doc for custom ban page * ⚰️ Remove old code due to merge * ✏️ Fix merge remaining in html * ✨ render banTemplate from html/template to string * 🚨 : fix lint --------- Co-authored-by: max.lerebourg <max.lerebourg@monisnap.com>
This commit is contained in:
co-authored by
max.lerebourg
parent
cb4f95d5d0
commit
b8dd883bb6
+15
-10
@@ -8,7 +8,6 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
htmlTemplate "html/template"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -73,7 +72,7 @@ type Bouncer struct {
|
|||||||
customHeader string
|
customHeader string
|
||||||
crowdsecStreamRoute string
|
crowdsecStreamRoute string
|
||||||
crowdsecHeader string
|
crowdsecHeader string
|
||||||
banTemplate *htmlTemplate.Template
|
banTemplateString string
|
||||||
clientPoolStrategy *ip.PoolStrategy
|
clientPoolStrategy *ip.PoolStrategy
|
||||||
serverPoolStrategy *ip.PoolStrategy
|
serverPoolStrategy *ip.PoolStrategy
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
@@ -120,9 +119,17 @@ func New(ctx context.Context, next http.Handler, config *configuration.Config, n
|
|||||||
}
|
}
|
||||||
config.CrowdsecLapiKey = apiKey
|
config.CrowdsecLapiKey = apiKey
|
||||||
}
|
}
|
||||||
var banTemplate *htmlTemplate.Template
|
|
||||||
|
var banTemplateString string
|
||||||
if config.BanHTMLFilePath != "" {
|
if config.BanHTMLFilePath != "" {
|
||||||
banTemplate, _ = configuration.GetHTMLTemplate(config.BanHTMLFilePath)
|
var buf bytes.Buffer
|
||||||
|
banTemplate, _ := configuration.GetHTMLTemplate(config.BanHTMLFilePath)
|
||||||
|
err = banTemplate.Execute(&buf, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(fmt.Sprintf("New:banTemplate is bad formatted %s", err.Error()))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
banTemplateString = buf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
bouncer := &Bouncer{
|
bouncer := &Bouncer{
|
||||||
@@ -144,10 +151,10 @@ func New(ctx context.Context, next http.Handler, config *configuration.Config, n
|
|||||||
updateInterval: config.UpdateIntervalSeconds,
|
updateInterval: config.UpdateIntervalSeconds,
|
||||||
customHeader: config.ForwardedHeadersCustomName,
|
customHeader: config.ForwardedHeadersCustomName,
|
||||||
defaultDecisionTimeout: config.DefaultDecisionSeconds,
|
defaultDecisionTimeout: config.DefaultDecisionSeconds,
|
||||||
|
banTemplateString: banTemplateString,
|
||||||
crowdsecStreamRoute: crowdsecStreamRoute,
|
crowdsecStreamRoute: crowdsecStreamRoute,
|
||||||
crowdsecHeader: crowdsecHeader,
|
crowdsecHeader: crowdsecHeader,
|
||||||
log: log,
|
log: log,
|
||||||
banTemplate: banTemplate,
|
|
||||||
serverPoolStrategy: &ip.PoolStrategy{
|
serverPoolStrategy: &ip.PoolStrategy{
|
||||||
Checker: serverChecker,
|
Checker: serverChecker,
|
||||||
},
|
},
|
||||||
@@ -318,11 +325,9 @@ type Login struct {
|
|||||||
|
|
||||||
func handleBanServeHTTP(bouncer *Bouncer, rw http.ResponseWriter) {
|
func handleBanServeHTTP(bouncer *Bouncer, rw http.ResponseWriter) {
|
||||||
rw.WriteHeader(http.StatusForbidden)
|
rw.WriteHeader(http.StatusForbidden)
|
||||||
if bouncer.banTemplate != nil {
|
if bouncer.banTemplateString != "" {
|
||||||
err := bouncer.banTemplate.Execute(rw, map[string]string{"caca": "caca"})
|
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
if err != nil {
|
fmt.Fprint(rw, bouncer.banTemplateString)
|
||||||
bouncer.log.Info(fmt.Sprintf("handleBanServeHTTP banTemplateServe %s", err.Error()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user