From c8b549115a87e6d3cf3c9a039c0cf0f8863b9278 Mon Sep 17 00:00:00 2001 From: Dominic Ferrando Date: Sun, 20 Apr 2025 20:54:34 -0400 Subject: [PATCH] Store hashed ips. Display unique ip count in catalog --- internal/database/handlers.go | 58 +++++++++++++++++++++++++---------- internal/database/models.go | 1 + internal/database/seed.sql | 1 + internal/util/cooldowns.go | 7 +++++ internal/util/posts.go | 9 ++++++ web/main.go | 6 ++-- 6 files changed, 63 insertions(+), 19 deletions(-) diff --git a/internal/database/handlers.go b/internal/database/handlers.go index bd3503f..a53c15b 100644 --- a/internal/database/handlers.go +++ b/internal/database/handlers.go @@ -5,7 +5,10 @@ import ( ) func GetBoards(db *sql.DB) ([]Board, error) { - rows, err := db.Query(`select id, name, slug, tag from boards order by slug`) + rows, err := db.Query(` + SELECT id, name, slug, tag + FROM boards ORDER BY slug`) + if err != nil { return nil, err } @@ -25,7 +28,10 @@ func GetBoards(db *sql.DB) ([]Board, error) { } func GetBoard(db *sql.DB, slug string) (Board, error) { - row := db.QueryRow(`SELECT id, name, slug, tag FROM boards WHERE slug = ?`, slug) + row := db.QueryRow(` + SELECT id, name, slug, tag + FROM boards + WHERE slug = ?`, slug) var result Board err := row.Scan(&result.Id, &result.Name, &result.Slug, &result.Tag) @@ -37,8 +43,11 @@ func GetBoard(db *sql.DB, slug string) (Board, error) { } func GetThreads(db *sql.DB, boardSlug string) ([]Thread, error) { - rows, err := db.Query( - `SELECT id, board_slug, subject, created_at, bumped_at FROM threads WHERE board_slug = ?`, boardSlug) + rows, err := db.Query(` + SELECT id, board_slug, subject, created_at, bumped_at + FROM threads + WHERE board_slug = ?`, boardSlug) + if err != nil { return nil, err } @@ -58,7 +67,10 @@ func GetThreads(db *sql.DB, boardSlug string) ([]Thread, error) { } func GetThread(db *sql.DB, threadId int) (Thread, error) { - row := db.QueryRow(`SELECT id, board_slug, subject, created_at, bumped_at FROM threads WHERE id = ?`, threadId) + row := db.QueryRow(` + SELECT id, board_slug, subject, created_at, bumped_at + FROM threads + WHERE id = ?`, threadId) var thread Thread err := row.Scan(&thread.Id, &thread.BoardSlug, &thread.Subject, &thread.CreatedAt, &thread.BumpedAt) @@ -70,7 +82,11 @@ func GetThread(db *sql.DB, threadId int) (Thread, error) { } func GetPosts(db *sql.DB, threadId int) ([]Post, error) { - rows, err := db.Query(`SELECT id, thread_id, author, body, created_at, media_path FROM posts WHERE thread_id = ?`, threadId) + rows, err := db.Query(` + SELECT id, thread_id, author, body, created_at, media_path, ip_hash + FROM posts + WHERE thread_id = ?`, threadId) + if err != nil { return nil, err } @@ -79,7 +95,7 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) { var result []Post for rows.Next() { var p Post - err := rows.Scan(&p.Id, &p.ThreadId, &p.Author, &p.Body, &p.CreatedAt, &p.MediaPath) + err := rows.Scan(&p.Id, &p.ThreadId, &p.Author, &p.Body, &p.CreatedAt, &p.MediaPath, &p.IpHash) if err != nil { return nil, err } @@ -90,27 +106,31 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) { } func GetOriginalPost(db *sql.DB, threadId int) (Post, error) { - row := db.QueryRow(`SELECT id, thread_id, author, body, created_at, media_path + row := db.QueryRow(` + SELECT id, thread_id, author, body, created_at, media_path, ip_hash FROM posts WHERE thread_id = ? ORDER BY created_at ASC LIMIT 1`, threadId) - var result Post - err := row.Scan(&result.Id, &result.ThreadId, &result.Author, &result.Body, &result.CreatedAt, &result.MediaPath) + var r Post + err := row.Scan(&r.Id, &r.ThreadId, &r.Author, &r.Body, &r.CreatedAt, &r.MediaPath, &r.IpHash) if err != nil { return Post{}, err } - return result, row.Err() + return r, row.Err() } -func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaPath string) error { +func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaPath string, ip_hash string) error { tx, err := db.Begin() if err != nil { return err } defer tx.Rollback() - res, err := tx.Exec(`INSERT INTO threads (board_slug, subject) VALUES (?, ?) RETURNING id`, boardSlug, subject) + res, err := tx.Exec(` + INSERT INTO threads (board_slug, subject) + VALUES (?, ?) RETURNING id`, boardSlug, subject) + if err != nil { return err } @@ -120,7 +140,10 @@ func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaP return err } - _, err = tx.Exec(`INSERT INTO posts (thread_id, body, media_path) VALUES (?, ?, ?)`, threadId, body, mediaPath) + _, err = tx.Exec(` + INSERT INTO posts (thread_id, body, media_path, ip_hash) + VALUES (?, ?, ?, ?)`, threadId, body, mediaPath, ip_hash) + if err != nil { return err } @@ -132,8 +155,11 @@ func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaP return nil } -func PutPost(db *sql.DB, threadId int, body string, mediaPath string) error { - _, err := db.Exec(`INSERT INTO posts (thread_id, body, media_path) VALUES (?, ?, ?)`, threadId, body, mediaPath) +func PutPost(db *sql.DB, threadId int, body string, mediaPath string, ip_hash string) error { + _, err := db.Exec(` + INSERT INTO posts (thread_id, body, media_path, ip_hash) + VALUES (?, ?, ?, ?)`, threadId, body, mediaPath, ip_hash) + if err != nil { return err } diff --git a/internal/database/models.go b/internal/database/models.go index c1d5999..15f1424 100644 --- a/internal/database/models.go +++ b/internal/database/models.go @@ -24,4 +24,5 @@ type Post struct { Body string CreatedAt time.Time MediaPath string + IpHash string } diff --git a/internal/database/seed.sql b/internal/database/seed.sql index 35ef6f8..2ae1306 100644 --- a/internal/database/seed.sql +++ b/internal/database/seed.sql @@ -27,6 +27,7 @@ CREATE TABLE IF NOT EXISTS posts ( body TEXT NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, media_path TEXT NOT NULL DEFAULT '', + ip_hash TEXT NOT NULL, FOREIGN KEY (thread_id) REFERENCES threads(id) ON DELETE CASCADE ); diff --git a/internal/util/cooldowns.go b/internal/util/cooldowns.go index ba78d89..2a40a1b 100644 --- a/internal/util/cooldowns.go +++ b/internal/util/cooldowns.go @@ -1,6 +1,8 @@ package util import ( + "crypto/sha256" + "encoding/hex" "net" "net/http" "strings" @@ -43,3 +45,8 @@ func GetIP(r *http.Request) string { } return ip } + +func HashIp(ip string) string { + checksum := sha256.Sum256([]byte(ip)) + return hex.EncodeToString(checksum[:]) +} diff --git a/internal/util/posts.go b/internal/util/posts.go index 6f74d56..614d603 100644 --- a/internal/util/posts.go +++ b/internal/util/posts.go @@ -12,6 +12,7 @@ import ( "strings" "github.com/disintegration/imaging" + "github.com/dominicf2001/comfychan/internal/database" ) // 10 MB memory limit @@ -102,3 +103,11 @@ func SavePostFile(file *multipart.File, filename string) error { return nil } + +func SumUniquePostIps(posts []database.Post) int { + uniqueIpHashes := map[string]bool{} + for _, post := range posts { + uniqueIpHashes[post.IpHash] = true + } + return len(uniqueIpHashes) +} diff --git a/web/main.go b/web/main.go index da55f8d..6190a69 100644 --- a/web/main.go +++ b/web/main.go @@ -150,7 +150,7 @@ func main() { return } - if err := database.PutThread(db, slug, subject, body, filename); err != nil { + if err := database.PutThread(db, slug, subject, body, filename, util.HashIp(ip)); err != nil { http.Error(w, "Failed to create thread", http.StatusInternalServerError) log.Printf("PutThread: %v", err) return @@ -201,7 +201,7 @@ func main() { mediaPath = filename } - if err := database.PutPost(db, threadId, body, mediaPath); err != nil { + if err := database.PutPost(db, threadId, body, mediaPath, util.HashIp(ip)); err != nil { http.Error(w, "Failed to create post", http.StatusInternalServerError) log.Printf("PutPost: %v", err) return @@ -246,7 +246,7 @@ func main() { ThreadURL: fmt.Sprintf("/%s/threads/%d", slug, thread.Id), MediaPath: op.MediaPath, ReplyCount: len(posts), - IpCount: 0, // TODO + IpCount: util.SumUniquePostIps(posts), }) }