From 486580b21ca75e6ad9dbd81208a75b0d8628db98 Mon Sep 17 00:00:00 2001 From: Flummi Date: Thu, 20 Jun 2024 04:20:28 +0200 Subject: [PATCH 01/10] https://github.com/nodejs/node/pull/52104 --- src/inc/config.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inc/config.mjs b/src/inc/config.mjs index ee7c7e0..b45a2ea 100644 --- a/src/inc/config.mjs +++ b/src/inc/config.mjs @@ -1,4 +1,4 @@ -import _config from "../../config.json" assert { type: "json" }; +import _config from "../../config.json" with { type: "json" }; let config = JSON.parse(JSON.stringify(_config)); From 2ff1842d097c1dc9c2a87f3016295e58af422e99 Mon Sep 17 00:00:00 2001 From: Flummi Date: Mon, 24 Jun 2024 07:53:00 +0200 Subject: [PATCH 02/10] admin schmadmin --- src/inc/lib.mjs | 32 ++++--- src/inc/routes/admin.mjs | 21 ++--- src/inc/routes/apiv2/index.mjs | 6 +- src/inc/routes/apiv2/settings.mjs | 2 +- src/inc/routes/apiv2/tags.mjs | 6 +- src/inc/routes/index.mjs | 2 +- src/inc/routes/search.mjs | 2 +- src/index.mjs | 2 +- views/item.html | 4 +- views/snippets/navbar.html | 2 +- views/snippets/navbar2.html | 133 ------------------------------ 11 files changed, 40 insertions(+), 172 deletions(-) delete mode 100644 views/snippets/navbar2.html diff --git a/src/inc/lib.mjs b/src/inc/lib.mjs index 707a0de..45aa0d2 100644 --- a/src/inc/lib.mjs +++ b/src/inc/lib.mjs @@ -134,15 +134,6 @@ export default new class { const derivedKey = await scrypt(str, salt, 64); return crypto.timingSafeEqual(keyBuffer, derivedKey); }; - async auth(req, res, next) { - if(!req.session) { - return res.reply({ - code: 401, - body: "401 - Unauthorized" - }); - } - return next(); - }; async getTags(itemid) { const tags = await db` select "tags".id, "tags".tag, "tags".normalized, "user".user @@ -217,6 +208,27 @@ export default new class { TABLE_NAME='user_options' and COLUMN_NAME = 'avatar' `)[0].avatar; - } + }; + + // meddlware + async auth(req, res, next) { + if(!req.session || !req.session.admin) { + return res.reply({ + code: 401, + body: "401 - Unauthorized" + }); + } + return next(); + }; + + async loggedin(req, res, next) { + if(!req.session) { + return res.reply({ + code: 401, + body: "401 - Unauthorized" + }); + } + return next(); + }; }; diff --git a/src/inc/routes/admin.mjs b/src/inc/routes/admin.mjs index 436bf48..60ad38a 100644 --- a/src/inc/routes/admin.mjs +++ b/src/inc/routes/admin.mjs @@ -3,18 +3,7 @@ import lib from "../lib.mjs"; import { exec } from "child_process"; import { promises as fs } from "fs"; -const auth = async (req, res, next) => { - if(!req.session) { - return res.reply({ - code: 401, - body: "401 - Unauthorized" - }); - } - return next(); -}; - export default (router, tpl) => { - router.get(/^\/login(\/)?$/, async (req, res) => { if(req.cookies.session) { return res.reply({ @@ -72,7 +61,7 @@ export default (router, tpl) => { }).end(); }); - router.get(/^\/logout$/, auth, async (req, res) => { + router.get(/^\/logout$/, lib.loggedin, async (req, res) => { const usersession = await db` select * from "user_sessions" @@ -103,7 +92,7 @@ export default (router, tpl) => { }); }); - router.get(/^\/admin(\/)?$/, auth, async (req, res) => { // frontpage + router.get(/^\/admin(\/)?$/, lib.auth, async (req, res) => { // frontpage res.reply({ body: tpl.render("admin", { @@ -114,7 +103,7 @@ export default (router, tpl) => { }); }); - router.get(/^\/admin\/sessions(\/)?$/, auth, async (req, res) => { + router.get(/^\/admin\/sessions(\/)?$/, lib.auth, async (req, res) => { const rows = await db` select "user_sessions".*, "user".user from "user_sessions" @@ -132,7 +121,7 @@ export default (router, tpl) => { }); }); - router.get(/^\/admin\/log(\/)?$/, auth, async (req, res) => { + router.get(/^\/admin\/log(\/)?$/, lib.auth, async (req, res) => { exec("journalctl -qeu f0ck --no-pager", (err, stdout) => { res.reply({ body: tpl.render("admin/log", { @@ -143,7 +132,7 @@ export default (router, tpl) => { }); }); - router.get(/^\/admin\/recover\/?/, auth, async (req, res) => { + router.get(/^\/admin\/recover\/?/, lib.auth, async (req, res) => { if(req.url.qs?.id) { const id = +req.url.qs.id; const f0ck = await db` diff --git a/src/inc/routes/apiv2/index.mjs b/src/inc/routes/apiv2/index.mjs index c58e7c2..7fec014 100644 --- a/src/inc/routes/apiv2/index.mjs +++ b/src/inc/routes/apiv2/index.mjs @@ -139,7 +139,7 @@ export default router => { // tags lol - group.put(/\/admin\/tags\/(?.*)/, lib.auth, async (req, res) => { + group.put(/\/admin\/tags\/(?.*)/, lib.loggedin, async (req, res) => { if(!req.params.tagname || !req.post.newtag) { return res.json({ success: false, @@ -187,7 +187,7 @@ export default router => { return res.json(q, tagname === newtag ? 200 : 201); // created (modified) }); - group.get(/\/admin\/tags\/suggest$/, lib.auth, async (req, res) => { + group.get(/\/admin\/tags\/suggest$/, lib.loggedin, async (req, res) => { const reply = { success: false, suggestions: {} @@ -267,7 +267,7 @@ export default router => { }); }); - group.post(/\/admin\/togglefav$/, lib.auth, async (req, res) => { + group.post(/\/admin\/togglefav$/, lib.loggedin, async (req, res) => { const postid = +req.post.postid; let favs = await db` diff --git a/src/inc/routes/apiv2/settings.mjs b/src/inc/routes/apiv2/settings.mjs index dfea483..e0ff897 100644 --- a/src/inc/routes/apiv2/settings.mjs +++ b/src/inc/routes/apiv2/settings.mjs @@ -3,7 +3,7 @@ import lib from '../../lib.mjs'; export default router => { router.group(/^\/api\/v2\/settings/, group => { - group.put(/\/setAvatar/, lib.auth, async (req, res) => { + group.put(/\/setAvatar/, lib.loggedin, async (req, res) => { if(!req.post.avatar) { return res.json({ msg: 'no avatar provided', diff --git a/src/inc/routes/apiv2/tags.mjs b/src/inc/routes/apiv2/tags.mjs index b551576..fa0d4b3 100644 --- a/src/inc/routes/apiv2/tags.mjs +++ b/src/inc/routes/apiv2/tags.mjs @@ -3,7 +3,7 @@ import lib from '../../lib.mjs'; export default router => { router.group(/^\/api\/v2\/admin\/(?\d+)\/tags/, group => { - group.get(/$/, lib.auth, async (req, res) => { + group.get(/$/, lib.loggedin, async (req, res) => { // get tags if(!req.params.postid) { return res.json({ @@ -18,7 +18,7 @@ export default router => { }); }); - group.post(/$/, lib.auth, async (req, res) => { + group.post(/$/, lib.loggedin, async (req, res) => { // assign and/or create tag if(!req.params.postid || !req.post.tagname) { return res.json({ @@ -80,7 +80,7 @@ export default router => { }); }); - group.put(/\/toggle$/, lib.auth, async (req, res) => { + group.put(/\/toggle$/, lib.loggedin, async (req, res) => { // xD if(!req.params.postid) { return res.json({ diff --git a/src/inc/routes/index.mjs b/src/inc/routes/index.mjs index c15b934..00f9dd3 100644 --- a/src/inc/routes/index.mjs +++ b/src/inc/routes/index.mjs @@ -100,7 +100,7 @@ export default (router, tpl) => { }); }); - router.get(/^\/mode\/(\d)/, auth, async (req, res) => { + router.get(/^\/mode\/(\d)/, lib.loggedin, async (req, res) => { const mode = +req.url.split[1]; let referertmp = req.headers.referer; let referer = ""; diff --git a/src/inc/routes/search.mjs b/src/inc/routes/search.mjs index 5ffad83..45d3aa1 100644 --- a/src/inc/routes/search.mjs +++ b/src/inc/routes/search.mjs @@ -5,7 +5,7 @@ import search from "../routeinc/search.mjs"; const _eps = 20; export default (router, tpl) => { - router.get(/^\/search(\/)?$/, lib.auth, async (req, res) => { + router.get(/^\/search(\/)?$/, lib.loggedin, async (req, res) => { let ret; let tag = req.url.qs.tag ?? []; let page = req.url.qs.page ?? 1; diff --git a/src/index.mjs b/src/index.mjs index 5b04e8e..810178b 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -71,7 +71,7 @@ process.on('unhandledRejection', err => { if(req.cookies.session) { const user = await db` - select "user".id, "user".login, "user".user, "user".level, "user_sessions".id as sess_id, "user_options".* + select "user".id, "user".login, "user".user, "user".admin, "user_sessions".id as sess_id, "user_options".* from "user_sessions" left join "user" on "user".id = "user_sessions".user_id left join "user_options" on "user_options".user_id = "user_sessions".user_id diff --git a/views/item.html b/views/item.html index bc3ae41..318d22f 100644 --- a/views/item.html +++ b/views/item.html @@ -14,7 +14,7 @@
- + @if(session.admin)@endif
@endif @@ -83,7 +83,7 @@ @if(typeof item.tags !== "undefined") @each(item.tags as tag) - {!! tag.tag !!}@if(session) ×@endif + {!! tag.tag !!}@if(session.admin) ×@endif @endeach @endif diff --git a/views/snippets/navbar.html b/views/snippets/navbar.html index 94b3d23..1922c3d 100644 --- a/views/snippets/navbar.html +++ b/views/snippets/navbar.html @@ -12,7 +12,7 @@
  • my f0cks
  • my favs
  • search
  • -
  • Admin
  • + @if(session.admin)
  • Admin
  • @endif
  • About
  • ranking
  • settings
  • diff --git a/views/snippets/navbar2.html b/views/snippets/navbar2.html deleted file mode 100644 index 09c64c7..0000000 --- a/views/snippets/navbar2.html +++ /dev/null @@ -1,133 +0,0 @@ -@if(session) - -@else - -@endif From c79cca18cfa37dd54b2e95c46f916d57d5f0f946 Mon Sep 17 00:00:00 2001 From: Flummi Date: Mon, 24 Jun 2024 08:41:13 +0200 Subject: [PATCH 03/10] fix total f0cks in profile --- src/inc/routeinc/f0cklib.mjs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/inc/routeinc/f0cklib.mjs b/src/inc/routeinc/f0cklib.mjs index 38ab8e0..aabab30 100644 --- a/src/inc/routeinc/f0cklib.mjs +++ b/src/inc/routeinc/f0cklib.mjs @@ -7,12 +7,13 @@ import url from "url"; const globalfilter = cfg.nsfp.map(n => `tag_id = ${n}`).join(' or '); export default { - getf0cks: async (o = { user, tag, mime, page, mode, fav, session }) => { + getf0cks: async (o = { user, tag, mime, page, mode, fav, session, limit }) => { const user = o.user ? decodeURI(o.user) : null; const tag = lib.parseTag(o.tag ?? null); const mime = o.mime ?? null; const page = +(o.page ?? 1); const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%"; + const eps = o.limit ?? cfg.websrv.eps; const tmp = { user, tag, mime, smime, page, mode: o.mode }; const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0); @@ -70,7 +71,7 @@ export default { group by items.id, tags.tag, ta.tag_id order by items.id desc offset ${offset} - limit ${cfg.websrv.eps} + limit ${eps} `; const cheat = []; From 058fe94fd1315113b93164f679010bc0b3c139d3 Mon Sep 17 00:00:00 2001 From: Flummi Date: Mon, 24 Jun 2024 08:41:46 +0200 Subject: [PATCH 04/10] admin the second lol --- f0ck.sql | 2 +- src/inc/routes/index.mjs | 8 +++++--- src/inc/routes/ranking.mjs | 4 ++-- views/ranking.html | 4 ++-- views/snippets/navbar.html | 2 +- views/user.html | 2 +- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/f0ck.sql b/f0ck.sql index 625ca3e..819d60f 100644 --- a/f0ck.sql +++ b/f0ck.sql @@ -131,7 +131,7 @@ CREATE TABLE public."user" ( login character varying(255) NOT NULL, "user" character varying(255) NOT NULL, password character varying(167) NOT NULL, - level integer NOT NULL + admin boolean NOT NULL ); ALTER TABLE public."user" OWNER TO f0ck; diff --git a/src/inc/routes/index.mjs b/src/inc/routes/index.mjs index 00f9dd3..155015e 100644 --- a/src/inc/routes/index.mjs +++ b/src/inc/routes/index.mjs @@ -14,7 +14,7 @@ export default (router, tpl) => { const user = decodeURIComponent(req.params.user); const query = await db` - select "user".user, "user".created_at, user_options.* + select "user".user, "user".admin, "user".created_at, user_options.* from user_options left join "user" on "user".id = user_options.user_id where "user".user ilike ${user} @@ -35,13 +35,15 @@ export default (router, tpl) => { user: user, mode: req.session.mode, fav: false, - session: !!req.session + session: !!req.session, + limit: 99999999 }); const favs = await f0cklib.getf0cks({ user: user, mode: req.session.mode, fav: true, - session: !!req.session + session: !!req.session, + limit: 99999999 }); const count = { diff --git a/src/inc/routes/ranking.mjs b/src/inc/routes/ranking.mjs index 4cafc46..48da3e3 100644 --- a/src/inc/routes/ranking.mjs +++ b/src/inc/routes/ranking.mjs @@ -8,13 +8,13 @@ export default (router, tpl) => { try { const list = await db` select - "user".user, + "user".user, "user".admin, coalesce("user_options".avatar, ${await lib.getDefaultAvatar()}) as avatar, count(distinct(tag_id, item_id)) as count from "tags_assign" left join "user" on "user".id = "tags_assign".user_id left join "user_options" on "user_options".user_id = "user".id - group by "user".user, "user_options".avatar + group by "user".user, "user_options".avatar, "user".admin order by count desc `; const stats = await lib.countf0cks(); diff --git a/views/ranking.html b/views/ranking.html index c8ac9f9..39bc6ad 100644 --- a/views/ranking.html +++ b/views/ranking.html @@ -11,8 +11,8 @@ @for(let i = 0; i < list.length; i++) {{ i + 1 }} - - {!! list[i].user !!} + + @if(list[i].admin)⭐ @endif{!! list[i].user !!} {{ list[i].count }} @endfor diff --git a/views/snippets/navbar.html b/views/snippets/navbar.html index 1922c3d..d3dea70 100644 --- a/views/snippets/navbar.html +++ b/views/snippets/navbar.html @@ -5,7 +5,7 @@