From 86d773c3f0d533279c8ff2a7d06994407fad2596 Mon Sep 17 00:00:00 2001 From: HF Date: Fri, 5 Aug 2022 22:38:57 +0200 Subject: [PATCH] add sql functions for whitelists and bans --- src/components/ModalRoot.jsx | 9 +++-- src/core/adminfunctions.js | 4 ++ src/core/isAllowed.js | 18 +-------- src/data/redis/isAllowedCache.js | 5 +++ src/data/sql/Ban.js | 63 ++++++++++++++++++++++++++++++++ src/data/sql/Whitelist.js | 46 +++++++++++++++++++++-- src/routes/api/modtools.js | 4 +- 7 files changed, 124 insertions(+), 25 deletions(-) diff --git a/src/components/ModalRoot.jsx b/src/components/ModalRoot.jsx index d811f74..4e551e8 100644 --- a/src/components/ModalRoot.jsx +++ b/src/components/ModalRoot.jsx @@ -46,8 +46,8 @@ const ModalRoot = () => { const [Content, name] = COMPONENTS[windowType]; return ( - (render || open) - && [ + (render || open) && ( + <>
{ onTransitionEnd={onTransitionEnd} tabIndex={-1} onClick={() => dispatch(closeWindow(0))} - />, + />
@@ -82,7 +82,8 @@ const ModalRoot = () => {
, - ] + + ) ); }; diff --git a/src/core/adminfunctions.js b/src/core/adminfunctions.js index aca9ea1..07bcc35 100644 --- a/src/core/adminfunctions.js +++ b/src/core/adminfunctions.js @@ -107,6 +107,10 @@ export async function executeIIDAction(action, iid, logger = null) { } return `${iidPart} would have gotten captcha anyway`; } + case 'ban': + case 'unban': + case 'Whitelist': + case 'unwhitelist': default: return `Failed to ${action} ${iid}`; } diff --git a/src/core/isAllowed.js b/src/core/isAllowed.js index 0fd5467..96212c8 100644 --- a/src/core/isAllowed.js +++ b/src/core/isAllowed.js @@ -6,8 +6,9 @@ import fetch from '../utils/proxiedFetch'; import { getIPv6Subnet } from '../utils/ip'; import whois from '../utils/whois'; -import { Whitelist, IPInfo } from '../data/sql'; +import { IPInfo } from '../data/sql'; import { isIPBanned } from '../data/sql/Ban'; +import { isWhitelisted } from '../data/sql/Whitelist'; import { cacheAllowed, getCacheAllowed, @@ -86,21 +87,6 @@ async function getProxyCheck(ip) { ]; } -/* - * check MYSQL Whitelist table - * @param ip IP to check - * @return true if whitelisted - */ -async function isWhitelisted(ip) { - const count = await Whitelist - .count({ - where: { - ip, - }, - }); - return count !== 0; -} - /* * dummy function to include if you don't want any proxycheck */ diff --git a/src/data/redis/isAllowedCache.js b/src/data/redis/isAllowedCache.js index aa47712..f01c807 100644 --- a/src/data/redis/isAllowedCache.js +++ b/src/data/redis/isAllowedCache.js @@ -27,3 +27,8 @@ export async function getCacheAllowed(ip) { status: cache, }; } + +export function cleanCacheForIP(ip) { + const key = `${PREFIX}:${ip}`; + return client.del(key); +} diff --git a/src/data/sql/Ban.js b/src/data/sql/Ban.js index c03fa2b..4726368 100644 --- a/src/data/sql/Ban.js +++ b/src/data/sql/Ban.js @@ -1,6 +1,8 @@ import { DataTypes } from 'sequelize'; import sequelize from './sequelize'; +import RegUser from './RegUser'; + const Ban = sequelize.define('Blacklist', { ip: { type: DataTypes.CHAR(39), @@ -33,6 +35,13 @@ const Ban = sequelize.define('Blacklist', { updatedAt: false, }); + + +/* + * check if ip is whitelisted + * @param ip + * @return boolean + */ export async function isIPBanned(ip) { const count = await Ban .count({ @@ -41,4 +50,58 @@ export async function isIPBanned(ip) { return count !== 0; } +/* + * get information of ban + * @param ip + * @return + */ +export async function getBanInfo(ip) { + const ban = await Ban.findByPk(ip, { + include: [{ + model: RegUser, + as: 'mod', + foreignKey: 'muid', + attributes: ['id', 'name'], + }], + }); + return ban; +} + +/* + * ban ip + * @param ip + * @return true if banned + * false if already banned + */ +export async function banIP( + ip, + reason, + expiresTs, + muid, +) { + const expires = (expiresTs) ? new Date(expiresTs) : null; + const [, created] = await Ban.findOrCreate({ + where: { ip }, + defaults: { + reason, + expires, + muid, + }, + }); + return created; +} + +/* + * unban ip + * @param ip + * @return true if unbanned, + * false if ip wasn't banned anyway + */ +export async function unbanIP(ip) { + const count = await Ban.destroy({ + where: { ip }, + }); + return !!count; +} + export default Ban; diff --git a/src/data/sql/Whitelist.js b/src/data/sql/Whitelist.js index dc5a727..bb9a1ee 100644 --- a/src/data/sql/Whitelist.js +++ b/src/data/sql/Whitelist.js @@ -1,7 +1,5 @@ /** - * Created by HF * - * https://github.com/sequelize/sequelize/issues/1485#issuecomment-243822779 */ import { DataTypes } from 'sequelize'; @@ -9,13 +7,53 @@ import sequelize from './sequelize'; const Whitelist = sequelize.define('Whitelist', { - ip: { type: DataTypes.CHAR(39), allowNull: false, primaryKey: true, }, - +}, { + timestamps: true, + updatedAt: false, }); +/* + * check if ip is whitelisted + * @param ip + * @return boolean + */ +export async function isWhitelisted(ip) { + const count = await Whitelist + .count({ + where: { ip }, + }); + return count !== 0; +} + +/* + * whitelist ip + * @param ip + * @return true if whitelisted, + * false if it was already whitelisted + */ +export async function whitelistIP(ip) { + const [, created] = await Whitelist.findOrCreate({ + where: { ip }, + }); + return created; +} + +/* + * remove ip from whitelist + * @param ip + * @return true if unwhitelisted, + * false if ip wasn't whitelisted anyway + */ +export async function unwhitelistIP(ip) { + const count = await Whitelist.destroy({ + where: { ip }, + }); + return !!count; +} + export default Whitelist; diff --git a/src/routes/api/modtools.js b/src/routes/api/modtools.js index 4f29b81..9c3f4aa 100644 --- a/src/routes/api/modtools.js +++ b/src/routes/api/modtools.js @@ -118,11 +118,13 @@ router.post('/', upload.single('image'), async (req, res, next) => { } if (req.body.iidaction) { const { - iidaction, iid, + iidaction, iid, reason, time, } = req.body; const ret = await executeIIDAction( iidaction, iid, + reason, + time, ); res.status(200).send(ret); return;