add sql functions for whitelists and bans

This commit is contained in:
HF 2022-08-05 22:38:57 +02:00
parent a41c286372
commit 86d773c3f0
7 changed files with 124 additions and 25 deletions

View File

@ -46,8 +46,8 @@ const ModalRoot = () => {
const [Content, name] = COMPONENTS[windowType]; const [Content, name] = COMPONENTS[windowType];
return ( return (
(render || open) (render || open) && (
&& [ <>
<div <div
className={(open && render) className={(open && render)
? 'OverlayModal show' ? 'OverlayModal show'
@ -55,7 +55,7 @@ const ModalRoot = () => {
onTransitionEnd={onTransitionEnd} onTransitionEnd={onTransitionEnd}
tabIndex={-1} tabIndex={-1}
onClick={() => dispatch(closeWindow(0))} onClick={() => dispatch(closeWindow(0))}
/>, />
<div <div
className={(open && render) ? 'Modal show' : 'Modal'} className={(open && render) ? 'Modal show' : 'Modal'}
> >
@ -82,7 +82,8 @@ const ModalRoot = () => {
<Content windowId={0} /> <Content windowId={0} />
</div> </div>
</div>, </div>,
] </>
)
); );
}; };

View File

@ -107,6 +107,10 @@ export async function executeIIDAction(action, iid, logger = null) {
} }
return `${iidPart} would have gotten captcha anyway`; return `${iidPart} would have gotten captcha anyway`;
} }
case 'ban':
case 'unban':
case 'Whitelist':
case 'unwhitelist':
default: default:
return `Failed to ${action} ${iid}`; return `Failed to ${action} ${iid}`;
} }

View File

@ -6,8 +6,9 @@ import fetch from '../utils/proxiedFetch';
import { getIPv6Subnet } from '../utils/ip'; import { getIPv6Subnet } from '../utils/ip';
import whois from '../utils/whois'; import whois from '../utils/whois';
import { Whitelist, IPInfo } from '../data/sql'; import { IPInfo } from '../data/sql';
import { isIPBanned } from '../data/sql/Ban'; import { isIPBanned } from '../data/sql/Ban';
import { isWhitelisted } from '../data/sql/Whitelist';
import { import {
cacheAllowed, cacheAllowed,
getCacheAllowed, 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 * dummy function to include if you don't want any proxycheck
*/ */

View File

@ -27,3 +27,8 @@ export async function getCacheAllowed(ip) {
status: cache, status: cache,
}; };
} }
export function cleanCacheForIP(ip) {
const key = `${PREFIX}:${ip}`;
return client.del(key);
}

View File

@ -1,6 +1,8 @@
import { DataTypes } from 'sequelize'; import { DataTypes } from 'sequelize';
import sequelize from './sequelize'; import sequelize from './sequelize';
import RegUser from './RegUser';
const Ban = sequelize.define('Blacklist', { const Ban = sequelize.define('Blacklist', {
ip: { ip: {
type: DataTypes.CHAR(39), type: DataTypes.CHAR(39),
@ -33,6 +35,13 @@ const Ban = sequelize.define('Blacklist', {
updatedAt: false, updatedAt: false,
}); });
/*
* check if ip is whitelisted
* @param ip
* @return boolean
*/
export async function isIPBanned(ip) { export async function isIPBanned(ip) {
const count = await Ban const count = await Ban
.count({ .count({
@ -41,4 +50,58 @@ export async function isIPBanned(ip) {
return count !== 0; 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; export default Ban;

View File

@ -1,7 +1,5 @@
/** /**
* Created by HF
* *
* https://github.com/sequelize/sequelize/issues/1485#issuecomment-243822779
*/ */
import { DataTypes } from 'sequelize'; import { DataTypes } from 'sequelize';
@ -9,13 +7,53 @@ import sequelize from './sequelize';
const Whitelist = sequelize.define('Whitelist', { const Whitelist = sequelize.define('Whitelist', {
ip: { ip: {
type: DataTypes.CHAR(39), type: DataTypes.CHAR(39),
allowNull: false, allowNull: false,
primaryKey: true, 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; export default Whitelist;

View File

@ -118,11 +118,13 @@ router.post('/', upload.single('image'), async (req, res, next) => {
} }
if (req.body.iidaction) { if (req.body.iidaction) {
const { const {
iidaction, iid, iidaction, iid, reason, time,
} = req.body; } = req.body;
const ret = await executeIIDAction( const ret = await executeIIDAction(
iidaction, iidaction,
iid, iid,
reason,
time,
); );
res.status(200).send(ret); res.status(200).send(ret);
return; return;