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
This commit is contained in:
2026-11-20 21:31:37 -05:00
parent ee03411154
commit 5c2427b096
14 changed files with 470 additions and 15 deletions
+68 -1
View File
@@ -15,9 +15,11 @@ import (
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
"unicode/utf8"
"uuid"
"gorm.io/gorm"
)
@@ -418,7 +420,6 @@ func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
// continue to delete from database IF the file doesnt exist on disk
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
log.Println("remove upload file:", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
@@ -443,3 +444,69 @@ func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
w.WriteHeader(http.StatusNoContent)
}
}
func reportUploadHandler(db *gorm.DB, cfg Config, pages *Pages, cStore *ChallengeStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "Invalid form data", http.StatusBadRequest)
return
}
powResult := strings.TrimSpace(r.PostForm.Get("pow_result"))
powNonceForm := strings.TrimSpace(r.PostForm.Get("pow_nonce"))
powChallengeId := strings.TrimSpace(r.PostForm.Get("pow_challenge_id"))
uploadId := strings.TrimSpace(r.PostForm.Get("upload_id"))
reportReason := strings.TrimSpace(r.PostForm.Get("report_reason"))
if powResult == "" || powNonceForm == "" || powChallengeId == "" {
resultPage(pages, "400, bad request", "proof of work is required", 400)(w, r)
return
}
if uploadId == "" || (len(uploadId) != 10 && len(uploadId) != 16) {
resultPage(pages, "400, bad request", "invalid upload id provided", 400)(w, r)
return
}
if reportReason == "" || !slices.Contains(cfg.ReportOptions, reportReason) {
resultPage(pages, "400, bad request", "invalid reason provided", 400)(w, r)
return
}
powNonce, err := strconv.Atoi(powNonceForm)
if err != nil {
log.Println("failed to parse pow nonce:", err)
resultPage(pages, "400, bad request", "the nonce provided is not a valid integer", 400)(w, r)
return
}
clientError, err := cStore.Verify(powChallengeId, powResult, powNonce)
if err != nil && !clientError {
log.Println("failed to verify pow:", err)
resultPage(pages, "500, internal server error", "server went kaput", 500)(w, r)
return
}
if err != nil && clientError {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
report := &Report{
ID: uuid.NewV7().String(),
UploadID: uploadId,
Reason: reportReason,
}
err = insertReport(db.WithContext(r.Context()), report)
if err != nil {
if errors.Is(err, gorm.ErrForeignKeyViolated) {
resultPage(pages, "404, upload not found", "the upload you are reporting was not found", 404)(w, r)
return
}
log.Println("failed to insert report:", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
resultPage(pages, "success!", "your report has been successfully submitted", 200)(w, r)
}
}