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

View File

@ -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}`;
}

View File

@ -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
*/

View File

@ -27,3 +27,8 @@ export async function getCacheAllowed(ip) {
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 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;

View File

@ -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;

View File

@ -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;