168 lines
4.2 KiB
Go
168 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"errors"
|
|
"html/template"
|
|
"log"
|
|
"math/rand/v2"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/dustin/go-humanize"
|
|
)
|
|
|
|
type Pages struct {
|
|
Templates *template.Template
|
|
Qts []string
|
|
Placeholders []string
|
|
}
|
|
|
|
type HomePageData struct {
|
|
Expiries []Expiry
|
|
Qt string
|
|
PastePlaceholder string
|
|
FileMaxSize string
|
|
PasteMaxSize string
|
|
}
|
|
|
|
func render(pages *Pages, w http.ResponseWriter, name string, data any) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
if err := pages.Templates.ExecuteTemplate(w, name, data); err != nil {
|
|
log.Printf("render %s: %v", name, err)
|
|
}
|
|
}
|
|
|
|
func staticPage(pages *Pages, name string) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
render(pages, w, name, getRandomQt(pages.Qts))
|
|
}
|
|
}
|
|
|
|
func resultPage(pages *Pages, title string, message string, statusCode ...int) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
code := 200
|
|
if len(statusCode) > 0 {
|
|
code = statusCode[0]
|
|
}
|
|
w.WriteHeader(code)
|
|
|
|
render(pages, w, "result.html", map[string]any{
|
|
"Qt": getRandomQt(pages.Qts),
|
|
"Title": title,
|
|
"Message": message,
|
|
})
|
|
}
|
|
}
|
|
|
|
func getRandomQt(qts []string) string {
|
|
if len(qts) > 0 {
|
|
return qts[rand.IntN(len(qts))]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func loadPages() (*Pages, error) {
|
|
templates, err := template.New("").Funcs(template.FuncMap{
|
|
"hasPrefix": strings.HasPrefix,
|
|
}).ParseGlob("templates/*.html")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pages := &Pages{
|
|
Templates: templates,
|
|
Placeholders: []string{"cmon, paste something already!"},
|
|
}
|
|
|
|
qtFiles, err := os.ReadDir("qts")
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return nil, err
|
|
}
|
|
for _, qt := range qtFiles {
|
|
if qt.IsDir() {
|
|
continue
|
|
}
|
|
|
|
switch strings.ToLower(filepath.Ext(qt.Name())) {
|
|
case ".jpeg", ".jpg", ".jxl", ".png", ".webp", ".avif", ".gif":
|
|
pages.Qts = append(pages.Qts, "/qts/"+url.PathEscape(qt.Name()))
|
|
}
|
|
}
|
|
|
|
pastePlaceholders, err := os.ReadFile("paste-placeholders.txt")
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return nil, err
|
|
}
|
|
|
|
placeholders := strings.TrimSpace(strings.ReplaceAll(string(pastePlaceholders), "\r\n", "\n"))
|
|
if placeholders != "" {
|
|
pages.Placeholders = strings.Split(placeholders, "\n")
|
|
}
|
|
return pages, nil
|
|
}
|
|
|
|
func homeHandler(pages *Pages, cfg Config) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
data := HomePageData{
|
|
Expiries: cfg.Expiries,
|
|
FileMaxSize: humanize.Bytes(uint64(cfg.MaxFileBytes)),
|
|
PasteMaxSize: humanize.Bytes(uint64(cfg.MaxPasteBytes)),
|
|
Qt: getRandomQt(pages.Qts),
|
|
}
|
|
|
|
if len(pages.Placeholders) > 0 {
|
|
data.PastePlaceholder = pages.Placeholders[rand.IntN(len(pages.Placeholders))]
|
|
}
|
|
|
|
render(pages, w, "home.html", data)
|
|
}
|
|
}
|
|
|
|
func faqHandler(pages *Pages, cfg Config) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
render(pages, w, "faq.html", map[string]any{
|
|
"Expiries": cfg.Expiries,
|
|
"Host": cfg.Host,
|
|
"NormalizeMIMETypes": cfg.NormalizeMIMETypes,
|
|
"Qt": getRandomQt(pages.Qts),
|
|
})
|
|
}
|
|
}
|
|
|
|
func reportPageHandler(pages *Pages, store *ChallengeStore, cfg Config) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
id, challenge, err := store.Issue(cfg.PoWDifficulty)
|
|
if err != nil {
|
|
http.Error(w, "failed to generate PoW challenge", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
render(pages, w, "report.html", map[string]any{
|
|
"Qt": getRandomQt(pages.Qts),
|
|
"Challenge": map[string]any{
|
|
"Data": hex.EncodeToString(challenge.Data),
|
|
"Difficulty": challenge.Difficulty,
|
|
"ExpiresAt": challenge.ExpiresAt,
|
|
"Id": id,
|
|
},
|
|
"ReportOptions": cfg.ReportOptions,
|
|
})
|
|
}
|
|
}
|
|
|
|
func (pages *Pages) HTTPError(w http.ResponseWriter, r *http.Request, message string, status int) {
|
|
if strings.Contains(r.Header.Get("Accept"), "text/html") {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
w.WriteHeader(status)
|
|
_ = pages.Templates.ExecuteTemplate(w, "result.html", map[string]any{
|
|
"Title": status,
|
|
"Message": message,
|
|
})
|
|
return
|
|
}
|
|
http.Error(w, message, status)
|
|
}
|