From 05bfa634cf8c42123c8c0a19e65aff7320dadf41 Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Tue, 13 Jul 2021 21:10:39 -0700 Subject: added regex blacklist for posts in admin page --- cacheDatabase.go | 6 ++++ client.go | 6 ++++ database.go | 51 ++++++++++++++++++++++++++++++++++ databaseschema.psql | 4 +++ main.go | 80 ++++++++++++++++++++++++++++++++++++++++++++++++----- static/admin.html | 77 --------------------------------------------------- static/faq.html | 2 +- static/nadmin.html | 53 +++++++++++++++++++++++------------ 8 files changed, 176 insertions(+), 103 deletions(-) delete mode 100644 static/admin.html diff --git a/cacheDatabase.go b/cacheDatabase.go index 7ab3b9e..deaed2e 100644 --- a/cacheDatabase.go +++ b/cacheDatabase.go @@ -5,6 +5,12 @@ import "database/sql" import _ "github.com/lib/pq" func WriteObjectToCache(db *sql.DB, obj ObjectBase) ObjectBase { + + if(IsPostBlacklist(db, obj.Content)){ + fmt.Println("\n\nBlacklist post blocked\n\n") + return obj + } + if len(obj.Attachment) > 0 { if obj.Preview.Href != "" { WritePreviewToCache(db, *obj.Preview) diff --git a/client.go b/client.go index de0168f..a46f2a1 100644 --- a/client.go +++ b/client.go @@ -63,6 +63,7 @@ type AdminPage struct { Reported []Report Domain string IsLocal bool + PostBlacklist []PostBlacklist } type Report struct { @@ -84,6 +85,11 @@ type NewsItem struct { Time int } +type PostBlacklist struct { + Id int + Regex string +} + func IndexGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { t := template.Must(template.New("").Funcs(template.FuncMap{ "mod": func(i, j int) bool { return i%j == 0 }, diff --git a/database.go b/database.go index 5668a4d..0e53142 100644 --- a/database.go +++ b/database.go @@ -1598,3 +1598,54 @@ func WriteNewsToDB(db *sql.DB, news NewsItem) { CheckError(err, "error writing news item") } + +func WriteRegexBlacklistDB(db *sql.DB, regex string) { + query := `select from postblacklist where regex=$1` + + rows, err := db.Query(query, regex) + + CheckError(err, "error select from postblacklist db") + + var re string + defer rows.Close() + rows.Next() + rows.Scan(&re) + + if re != "" { + return + } + + query = `insert into postblacklist (regex) values ($1)` + + _, err = db.Exec(query, regex) + + CheckError(err, "error inserting postblacklist into db") +} + +func GetRegexBlacklistDB(db *sql.DB) []PostBlacklist { + query := `select id, regex from postblacklist` + + rows, err := db.Query(query) + + CheckError(err, "error with select all from postblacklist db") + + var List []PostBlacklist + + defer rows.Close() + for rows.Next() { + var temp PostBlacklist + rows.Scan(&temp.Id, &temp.Regex) + + List = append(List, temp) + } + + return List +} + +func DeleteRegexBlacklistDB(db *sql.DB, id int) { + query := `delete from postblacklist where id=$1` + + _, err := db.Exec(query, id) + + CheckError(err, "error with delete from postblacklist") +} diff --git a/databaseschema.psql b/databaseschema.psql index 02c229b..8c0c0d4 100644 --- a/databaseschema.psql +++ b/databaseschema.psql @@ -227,3 +227,7 @@ ALTER TABLE actor ADD COLUMN IF NOT EXISTS publicKeyPem varchar(100) default ''; ALTER TABLE activitystream ADD COLUMN IF NOT EXISTS sensitive boolean default false; ALTER TABLE cacheactivitystream ADD COLUMN IF NOT EXISTS sensitive boolean default false; +CREATE TABLE IF NOT EXISTS postblacklist( +id serial primary key, +regex varchar(200) +); \ No newline at end of file diff --git a/main.go b/main.go index b520aa7..d606674 100644 --- a/main.go +++ b/main.go @@ -322,6 +322,12 @@ func main() { file, header, _ := r.FormFile("file") + if(IsPostBlacklist(db, r.FormValue("comment"))){ + fmt.Println("\n\nBlacklist post blocked\n\n") + http.Redirect(w, r, Domain + "/", http.StatusMovedPermanently) + return + } + if(file != nil && header.Size > (7 << 20)){ w.Write([]byte("7MB max file size")) return @@ -559,7 +565,9 @@ func main() { adminData.Board.TP = TP adminData.Board.Post.Actor = actor.Id - + + + t.ExecuteTemplate(w, "layout", adminData) } else if admin || actor.Id == Domain { @@ -591,7 +599,9 @@ func main() { adminData.Boards = Boards - adminData.Board.Post.Actor = actor.Id + adminData.Board.Post.Actor = actor.Id + + adminData.PostBlacklist = GetRegexBlacklistDB(db) t.ExecuteTemplate(w, "layout", adminData) } @@ -1203,14 +1213,56 @@ func main() { if Domain != "https://fchan.xyz" { return } - - go AddInstanceToIndexDB(db, actor) }) + http.HandleFunc("/blacklist", func(w http.ResponseWriter, r *http.Request) { + + id, _ := GetPasswordFromSession(r) + + actor := GetActorFromDB(db, Domain) + + if id == "" || (id != actor.Id && id != Domain) { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + + if r.Method == "GET" { + id := r.URL.Query().Get("remove") + + if id != "" { + i, _ := strconv.Atoi(id) + DeleteRegexBlacklistDB(db, i) + } + + } else { + regex := r.FormValue("regex") + testCase := r.FormValue("testCase") + + if regex == "" { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + + r.ParseForm() + + re := regexp.MustCompile(regex) + + if testCase == "" { + WriteRegexBlacklistDB(db, regex) + } else if re.MatchString(testCase) { + WriteRegexBlacklistDB(db, regex) + } + } + + http.Redirect(w, r, "/" + *Key + "#regex", http.StatusSeeOther) + }) + http.HandleFunc("/api/media", func(w http.ResponseWriter, r *http.Request) { - RouteImages(w, r.URL.Query().Get("hash")) + if r.URL.Query().Get("hash") != "" { + RouteImages(w, r.URL.Query().Get("hash")) + } }) fmt.Println("Server for " + Domain + " running on port " + Port) @@ -2590,8 +2642,8 @@ func RouteImages(w http.ResponseWriter, media string) { resp, err := http.DefaultClient.Do(req) - CheckError(err, "error with Route Images resp") - + CheckError(err, "error with Route Images resp") + defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) @@ -2603,3 +2655,17 @@ func RouteImages(w http.ResponseWriter, media string) { w.Write(body) } + +func IsPostBlacklist(db *sql.DB, comment string) bool { + postblacklist := GetRegexBlacklistDB(db) + + for _, e := range postblacklist { + re := regexp.MustCompile(e.Regex) + + if re.MatchString(comment) { + return true + } + } + + return false +} diff --git a/static/admin.html b/static/admin.html deleted file mode 100644 index f238ec0..0000000 --- a/static/admin.html +++ /dev/null @@ -1,77 +0,0 @@ - - -
-Soon™.
v0.0.8-dev
+v0.0.9-dev