Add ability to ban ips
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/dominicf2001/comfychan/internal/util"
|
"github.com/dominicf2001/comfychan/internal/util"
|
||||||
)
|
)
|
||||||
@@ -95,7 +96,8 @@ func GetThread(db *sql.DB, threadId int) (Thread, error) {
|
|||||||
|
|
||||||
func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
|
func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
|
||||||
rows, err := db.Query(`
|
rows, err := db.Query(`
|
||||||
SELECT id, thread_id, author, body, created_at, media_path, ip_hash, number, thumb_path
|
SELECT id, thread_id, author, body, created_at, media_path,
|
||||||
|
ip_hash, number, thumb_path, banned
|
||||||
FROM posts
|
FROM posts
|
||||||
WHERE thread_id = ?`, threadId)
|
WHERE thread_id = ?`, threadId)
|
||||||
|
|
||||||
@@ -107,7 +109,9 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
|
|||||||
var result []Post
|
var result []Post
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var p Post
|
var p Post
|
||||||
err := rows.Scan(&p.Id, &p.ThreadId, &p.Author, &p.Body, &p.CreatedAt, &p.MediaPath, &p.IpHash, &p.Number, &p.ThumbPath)
|
err := rows.Scan(
|
||||||
|
&p.Id, &p.ThreadId, &p.Author, &p.Body, &p.CreatedAt, &p.MediaPath,
|
||||||
|
&p.IpHash, &p.Number, &p.ThumbPath, &p.Banned)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -117,15 +121,35 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
|
|||||||
return result, rows.Err()
|
return result, rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetPost(db *sql.DB, postId int) (Post, error) {
|
||||||
|
row := db.QueryRow(`
|
||||||
|
SELECT id, thread_id, author, body, created_at, media_path,
|
||||||
|
ip_hash, number, thumb_path, banned
|
||||||
|
FROM posts
|
||||||
|
WHERE id = ?`, postId)
|
||||||
|
|
||||||
|
var r Post
|
||||||
|
err := row.Scan(
|
||||||
|
&r.Id, &r.ThreadId, &r.Author, &r.Body, &r.CreatedAt, &r.MediaPath,
|
||||||
|
&r.IpHash, &r.Number, &r.ThumbPath, &r.Banned)
|
||||||
|
if err != nil {
|
||||||
|
return Post{}, err
|
||||||
|
}
|
||||||
|
return r, row.Err()
|
||||||
|
}
|
||||||
|
|
||||||
func GetOriginalPost(db *sql.DB, threadId int) (Post, error) {
|
func GetOriginalPost(db *sql.DB, threadId int) (Post, error) {
|
||||||
row := db.QueryRow(`
|
row := db.QueryRow(`
|
||||||
SELECT id, thread_id, author, body, created_at, media_path, ip_hash, number, thumb_path
|
SELECT id, thread_id, author, body, created_at, media_path,
|
||||||
|
ip_hash, number, thumb_path, banned
|
||||||
FROM posts
|
FROM posts
|
||||||
WHERE thread_id = ?
|
WHERE thread_id = ?
|
||||||
ORDER BY created_at ASC LIMIT 1`, threadId)
|
ORDER BY created_at ASC LIMIT 1`, threadId)
|
||||||
|
|
||||||
var r Post
|
var r Post
|
||||||
err := row.Scan(&r.Id, &r.ThreadId, &r.Author, &r.Body, &r.CreatedAt, &r.MediaPath, &r.IpHash, &r.Number, &r.ThumbPath)
|
err := row.Scan(
|
||||||
|
&r.Id, &r.ThreadId, &r.Author, &r.Body, &r.CreatedAt, &r.MediaPath,
|
||||||
|
&r.IpHash, &r.Number, &r.ThumbPath, &r.Banned)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Post{}, err
|
return Post{}, err
|
||||||
}
|
}
|
||||||
@@ -324,3 +348,10 @@ func GetAdmin(db *sql.DB, username string) (Admin, error) {
|
|||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BanIp(db *sql.DB, ip string, reason string, expiration time.Time) error {
|
||||||
|
_, err := db.Exec(`
|
||||||
|
INSERT INTO bans (ip_hash, reason, expiration)
|
||||||
|
VALUES (?, ?, ?)`, ip, reason, expiration)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ type Post struct {
|
|||||||
ThumbPath string
|
ThumbPath string
|
||||||
IpHash string
|
IpHash string
|
||||||
Number int
|
Number int
|
||||||
|
Banned bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Admin struct {
|
type Admin struct {
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ CREATE TABLE IF NOT EXISTS posts (
|
|||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
thread_id INTEGER NOT NULL,
|
thread_id INTEGER NOT NULL,
|
||||||
number INTEGER NOT NULL ,
|
number INTEGER NOT NULL ,
|
||||||
|
banned BOOLEAN NOT NULL DEFAULT 0,
|
||||||
author TEXT DEFAULT 'Anonymous',
|
author TEXT DEFAULT 'Anonymous',
|
||||||
body TEXT NOT NULL,
|
body TEXT NOT NULL,
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
@@ -39,6 +40,13 @@ CREATE TABLE IF NOT EXISTS admins (
|
|||||||
password TEXT NOT NULL
|
password TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS bans (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
ip_hash TEXT NOT NULL,
|
||||||
|
reason TEXT NOT NULL,
|
||||||
|
expiration DATETIME NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
-- ======================
|
-- ======================
|
||||||
-- Seed data
|
-- Seed data
|
||||||
-- ======================
|
-- ======================
|
||||||
|
|||||||
+40
@@ -544,6 +544,46 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// bans the ip stored in the post id
|
||||||
|
r.Post("/ban/{postId}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
postIdStr := chi.URLParam(r, "postId")
|
||||||
|
postId, err := strconv.Atoi(postIdStr)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Invalid post id", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = db.Exec(`UPDATE posts SET banned = 1 WHERE id = ?`, postId)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed update post to banned", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
post, err := database.GetPost(db, postId)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("GetPost: ", err)
|
||||||
|
http.Error(w, "Failed to get post: "+postIdStr, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ipToBan := post.IpHash
|
||||||
|
reason := r.FormValue("reason")
|
||||||
|
|
||||||
|
expirationInput := r.FormValue("expiration")
|
||||||
|
expiration, err := time.Parse("2006-01-02T15:04", expirationInput)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Invalid expiration datetime value", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = database.BanIp(db, ipToBan, reason, expiration)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("BanIp: ", err)
|
||||||
|
http.Error(w, "Failed to ban ip: ", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
// -----------------
|
// -----------------
|
||||||
|
|||||||
@@ -246,6 +246,12 @@ body {
|
|||||||
color: #ff0000;
|
color: #ff0000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.post-banned-message {
|
||||||
|
margin-top: 2px;
|
||||||
|
font-weight: bold;
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
|
|
||||||
.greentext {
|
.greentext {
|
||||||
color: #789922
|
color: #789922
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,11 +22,44 @@ templ PostAdminDialog(post database.Post) {
|
|||||||
_="on htmx:afterRequest trigger refreshPosts on body"
|
_="on htmx:afterRequest trigger refreshPosts on body"
|
||||||
hx-confirm="Are you sure you wish to delete this post?"
|
hx-confirm="Are you sure you wish to delete this post?"
|
||||||
>Delete</button>
|
>Delete</button>
|
||||||
|
<button
|
||||||
|
class="link-button"
|
||||||
|
_={ fmt.Sprintf(`on click call #%s-dialog-2.showModal()`, elPostId) }
|
||||||
|
>IP ban</button>
|
||||||
<button
|
<button
|
||||||
class="admin-dialog-close-btn link-button"
|
class="admin-dialog-close-btn link-button"
|
||||||
_={ fmt.Sprintf("on click call #%s-dialog.close()", elPostId) }
|
_={ fmt.Sprintf("on click call #%s-dialog.close()", elPostId) }
|
||||||
>Close</button>
|
>Close</button>
|
||||||
</dialog>
|
</dialog>
|
||||||
|
<dialog
|
||||||
|
id={ elPostId + "-dialog-2" }
|
||||||
|
class="admin-dialog"
|
||||||
|
>
|
||||||
|
<h1>Ban info</h1>
|
||||||
|
<form
|
||||||
|
hx-post={ fmt.Sprintf("/admin/ban/%d", post.Id) }
|
||||||
|
hx-swap="none"
|
||||||
|
_="on htmx:afterRequest trigger refreshPosts on body"
|
||||||
|
hx-confirm="Are you sure you wish to ban this post's ip?"
|
||||||
|
>
|
||||||
|
<div style="margin-bottom: 5px;">
|
||||||
|
<span>Reason: </span>
|
||||||
|
<input name="reason"/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<span>Days: </span>
|
||||||
|
<input type="datetime-local" name="expiration"/>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="link-button"
|
||||||
|
>Ban</button>
|
||||||
|
</form>
|
||||||
|
<button
|
||||||
|
class="admin-dialog-close-btn link-button"
|
||||||
|
_={ fmt.Sprintf("on click call #%s-dialog-2.close()", elPostId) }
|
||||||
|
>Cancel</button>
|
||||||
|
</dialog>
|
||||||
}
|
}
|
||||||
|
|
||||||
templ PostOriginal(post database.Post, thread database.Thread) {
|
templ PostOriginal(post database.Post, thread database.Thread) {
|
||||||
@@ -130,6 +163,9 @@ templ PostReply(post database.Post, threadContext ThreadContext) {
|
|||||||
}
|
}
|
||||||
<p class="post-body">
|
<p class="post-body">
|
||||||
@templ.Raw(util.EnrichPost(post.Body))
|
@templ.Raw(util.EnrichPost(post.Body))
|
||||||
|
if post.Banned {
|
||||||
|
<strong class="post-banned-message">(USER WAS BANNED FOR THIS POST)</strong>
|
||||||
|
}
|
||||||
</p>
|
</p>
|
||||||
@PostAdminDialog(post)
|
@PostAdminDialog(post)
|
||||||
</article>
|
</article>
|
||||||
|
|||||||
Reference in New Issue
Block a user