check / check (push) Failing after 1m48s
A deny of 403 from waf is answered with refused.html, a deny of 429 with ratelimited.html, each as text/html with waf's other headers kept. Both pages are made once at start and held in memory. With SWW_RPROXY_CONTACT_EMAIL set, their "contact the site's operator" is a mailto link to it; the value is taken as given.
72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package rproxy
|
|
|
|
import (
|
|
"embed"
|
|
"html"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// pageFiles are the pages a refused visitor gets, as HTML with nothing
|
|
// external: refused.html for a 403, ratelimited.html for a 429.
|
|
//
|
|
//go:embed refused.html ratelimited.html
|
|
var pageFiles embed.FS
|
|
|
|
// contactSentence is the sentence of each page that becomes a link to
|
|
// SWW_RPROXY_CONTACT_EMAIL when that is set.
|
|
const contactSentence = "contact the site's operator"
|
|
|
|
// htmlContentType is the Content-Type of the pages.
|
|
const htmlContentType = "text/html; charset=utf-8"
|
|
|
|
// blockPages are the pages a refused visitor gets, ready to send, made once
|
|
// at start.
|
|
type blockPages struct {
|
|
refused []byte
|
|
rateLimited []byte
|
|
}
|
|
|
|
// newBlockPages makes the pages. With contactEmail set, each page's
|
|
// contactSentence is a mailto link to it.
|
|
func newBlockPages(contactEmail string) blockPages {
|
|
return blockPages{
|
|
refused: readPage("refused.html", contactEmail),
|
|
rateLimited: readPage("ratelimited.html", contactEmail),
|
|
}
|
|
}
|
|
|
|
// readPage reads the page in name from pageFiles, linking its
|
|
// contactSentence to contactEmail unless that is "". The files are part of
|
|
// the binary, so a missing one is a bug, and a panic.
|
|
func readPage(name, contactEmail string) []byte {
|
|
contents, err := pageFiles.ReadFile(name)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if contactEmail == "" {
|
|
return contents
|
|
}
|
|
|
|
link := `<a href="mailto:` + html.EscapeString(contactEmail) + `">` +
|
|
contactSentence + `</a>`
|
|
|
|
return []byte(
|
|
strings.Replace(string(contents), contactSentence, link, 1),
|
|
)
|
|
}
|
|
|
|
// forStatus returns the page for a refusal with status, and whether there is
|
|
// one: the rate-limited page for a 429, the refused page for a 403.
|
|
func (p blockPages) forStatus(status int) ([]byte, bool) {
|
|
switch status {
|
|
case http.StatusForbidden:
|
|
return p.refused, true
|
|
case http.StatusTooManyRequests:
|
|
return p.rateLimited, true
|
|
default:
|
|
return nil, false
|
|
}
|
|
}
|