Files
minibin/pages.go
T
whoswhip 5c2427b096 reports!!!! and PoW!!!!!!!
this has taken quite a while (8 ish hours) but reports and proof of work challenges are completely implemented

the proof of work challenges are bit iffy and could be gpu accelerated which is bad, so i plan to eventually switch to something memory hard like argon2id

references used for pow:
https://dev.to/ratmd/simple-proof-of-work-in-javascript-3kgm
https://anubis.techaro.lol/docs/design/how-anubis-works
https://anubis.techaro.lol/docs/design/why-proof-of-work

as well as some other posts or repos found when searching
2026-11-20 21:31:37 -05:00

155 lines
3.8 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,
})
}
}