add adminfunctions

This commit is contained in:
HF 2022-08-06 05:55:27 +02:00
parent a5d25a8217
commit be8f94b368
5 changed files with 227 additions and 169 deletions

View File

@ -114,7 +114,7 @@ function Admintools() {
selectIPAction(sel.options[sel.selectedIndex].value);
}}
>
{['ban', 'unban', 'whitelist', 'unwhitelist', 'iidtoip']
{['iidtoip']
.map((opt) => (
<option
value={opt}

View File

@ -7,14 +7,26 @@
import sharp from 'sharp';
import Sequelize from 'sequelize';
import redis from '../data/redis/client';
import { getIPv6Subnet } from '../utils/ip';
import isIPAllowed from './isAllowed';
import { validateCoorRange } from '../utils/validation';
import CanvasCleaner from './CanvasCleaner';
import { Whitelist, RegUser } from '../data/sql';
import { getIPofIID } from '../data/sql/IPInfo';
import { RegUser } from '../data/sql';
import {
cleanCacheForIP,
} from '../data/redis/isAllowedCache';
import { forceCaptcha } from '../data/redis/captcha';
import {
isWhitelisted,
whitelistIP,
unwhitelistIP,
} from '../data/sql/Whitelist';
import {
getBanInfo,
banIP,
unbanIP,
} from '../data/sql/Ban';
import { getInfoToIp, getIPofIID } from '../data/sql/IPInfo';
// eslint-disable-next-line import/no-unresolved
import canvases from './canvases.json';
import {
@ -56,29 +68,12 @@ export async function executeIPAction(action, ips, logger = null) {
out += `Couln't parse ${action} ${ip}\n`;
continue;
}
const ipKey = getIPv6Subnet(ip);
const key = `isprox:${ipKey}`;
if (logger) logger(`${action} ${ip}`);
switch (action) {
case 'whitelist':
await Whitelist.findOrCreate({
where: { ip: ipKey },
});
await redis.set(key, 'n', {
EX: 24 * 3600,
});
break;
case 'unwhitelist':
await Whitelist.destroy({
where: { ip: ipKey },
});
await redis.del(key);
break;
default:
out += `Failed to ${action} ${ip}\n`;
return `Failed to ${action} ${ip}\n`;
}
out += `Succseefully did ${action} ${ip}\n`;
}
return out;
}
@ -89,7 +84,14 @@ export async function executeIPAction(action, ips, logger = null) {
* @param iid already sanizized iid
* @return text of success
*/
export async function executeIIDAction(action, iid, logger = null) {
export async function executeIIDAction(
action,
iid,
reason,
expire,
muid,
logger = null,
) {
const ip = await getIPofIID(iid);
if (!ip) {
return `Could not resolve ${iid}`;
@ -97,6 +99,36 @@ export async function executeIIDAction(action, iid, logger = null) {
const iidPart = iid.slice(0, iid.indexOf('-'));
switch (action) {
case 'status': {
const allowed = await isIPAllowed(ip, true);
let out = `Allowed to place: ${allowed.allowed}`;
const info = await getInfoToIp(ip);
out += `Country: ${info.country}\n`
+ `CIDR: ${info.cidr}\n`
+ `org: ${info.org || 'N/A'}\n`
+ `desc: ${info.descr || 'N/A'}\n`
+ `asn: ${info.asn}\n`
+ `proxy: ${info.isProxy}\n`;
if (info.pcheck) {
const { pcheck } = info;
out += `pc: ${pcheck.slice(0, pcheck.indexOf(','))}\n`;
}
const whitelisted = await isWhitelisted(ip);
out += `whitelisted: ${whitelisted}\n`;
const ban = await getBanInfo(ip);
if (!ban) {
out += 'banned: false\n';
} else {
out += `reason: ${ban.reason}\n`;
if (ban.expires) {
out += `expires: ${ban.expires.toLocaleString()}\n`;
}
if (ban.mod) {
out += `by: @[${ban.mod.name}](${ban.mod.id})\n`;
}
}
return out;
}
case 'givecaptcha': {
const succ = await forceCaptcha(ip);
if (succ === null) {
@ -107,10 +139,44 @@ export async function executeIIDAction(action, iid, logger = null) {
}
return `${iidPart} would have gotten captcha anyway`;
}
case 'ban':
case 'unban':
case 'Whitelist':
case 'unwhitelist':
case 'ban': {
if (expire && expire < Date.now()) {
return 'No valid expiration time';
}
if (!reason) {
return 'No reason specified';
}
const ret = await banIP(ip, reason, expire || null, muid);
if (ret) {
await cleanCacheForIP(ip);
return 'Successfully banned user';
}
return 'User is already banned';
}
case 'unban': {
const ret = await unbanIP(ip);
if (ret) {
await cleanCacheForIP(ip);
return 'Successfully unbanned user';
}
return 'User is not banned';
}
case 'Whitelist': {
const ret = await whitelistIP(ip);
if (ret) {
await cleanCacheForIP(ip);
return 'Successfully whitelisted user';
}
return 'User is already whitelisted';
}
case 'unwhitelist': {
const ret = await unwhitelistIP(ip);
if (ret) {
await cleanCacheForIP(ip);
return 'Successfully removed user from whitelist';
}
return 'User is not on whitelist';
}
default:
return `Failed to ${action} ${iid}`;
}

View File

@ -115,7 +115,7 @@ async function saveIPInfo(ip, whoisRet, isProxy, info) {
* @param ip IP to check
* @return true if proxy or blacklisted, false if not or whitelisted
*/
export async function withoutCache(f, ip) {
async function withoutCache(f, ip) {
const ipKey = getIPv6Subnet(ip);
let allowed = true;
let status = -2;

View File

@ -37,6 +37,7 @@ const IPInfo = sequelize.define('IPInfo', {
asn: {
type: DataTypes.CHAR(12),
defaultValue: 'N/A',
allowNull: false,
},
@ -55,7 +56,7 @@ const IPInfo = sequelize.define('IPInfo', {
}, {
getterMethods: {
isProxy() {
return !!this.proxy;
return (this.proxy === 1);
},
},
@ -128,6 +129,10 @@ export async function getIdsToIps(ips) {
return ipToIdMap;
}
export async function getInfoToIp(ip) {
return IPInfo.findByPk(ip);
}
export async function getInfoToIps(ips) {
const ipToIdMap = new Map();
if (!ips.length || ips.length > 300) {

View File

@ -89,107 +89,103 @@ router.post('/', upload.single('image'), async (req, res, next) => {
);
};
try {
if (req.body.cleanerstat) {
const ret = CanvasCleaner.reportStatus();
res.status(200);
res.json(ret);
return;
}
if (req.body.cleanercancel) {
const ret = CanvasCleaner.stop();
res.status(200).send(ret);
return;
}
if (req.body.watchaction) {
const {
watchaction, ulcoor, brcoor, time, iid, canvasid,
} = req.body;
const ret = await executeWatchAction(
watchaction,
ulcoor,
brcoor,
time,
iid,
canvasid,
);
res.status(200).json(ret);
return;
}
if (req.body.iidaction) {
const {
iidaction, iid, reason, time,
} = req.body;
const ret = await executeIIDAction(
iidaction,
iid,
reason,
time,
);
res.status(200).send(ret);
return;
}
if (req.body.cleaneraction) {
const {
cleaneraction, ulcoor, brcoor, canvasid,
} = req.body;
const [ret, msg] = await executeCleanerAction(
cleaneraction,
ulcoor,
brcoor,
canvasid,
aLogger,
);
res.status(ret).send(msg);
return;
}
if (req.body.imageaction) {
const { imageaction, coords, canvasid } = req.body;
const [ret, msg] = await executeImageAction(
imageaction,
req.file,
coords,
canvasid,
aLogger,
);
res.status(ret).send(msg);
return;
}
if (req.body.protaction) {
const {
protaction, ulcoor, brcoor, canvasid,
} = req.body;
const [ret, msg] = await executeProtAction(
protaction,
ulcoor,
brcoor,
canvasid,
aLogger,
);
res.status(ret).send(msg);
return;
}
if (req.body.rollback) {
// rollback is date as YYYYMMdd
const {
rollback, ulcoor, brcoor, canvasid,
} = req.body;
const [ret, msg] = await executeRollback(
rollback,
ulcoor,
brcoor,
canvasid,
aLogger,
(req.user.userlvl === 1),
);
res.status(ret).send(msg);
return;
}
next();
} catch (error) {
next(error);
if (req.body.cleanerstat) {
const ret = CanvasCleaner.reportStatus();
res.status(200);
res.json(ret);
return;
}
if (req.body.cleanercancel) {
const ret = CanvasCleaner.stop();
res.status(200).send(ret);
return;
}
if (req.body.watchaction) {
const {
watchaction, ulcoor, brcoor, time, iid, canvasid,
} = req.body;
const ret = await executeWatchAction(
watchaction,
ulcoor,
brcoor,
time,
iid,
canvasid,
);
res.status(200).json(ret);
return;
}
if (req.body.iidaction) {
const {
iidaction, iid, reason, time,
} = req.body;
const ret = await executeIIDAction(
iidaction,
iid,
reason,
time,
req.user.id,
);
res.status(200).send(ret);
return;
}
if (req.body.cleaneraction) {
const {
cleaneraction, ulcoor, brcoor, canvasid,
} = req.body;
const [ret, msg] = await executeCleanerAction(
cleaneraction,
ulcoor,
brcoor,
canvasid,
aLogger,
);
res.status(ret).send(msg);
return;
}
if (req.body.imageaction) {
const { imageaction, coords, canvasid } = req.body;
const [ret, msg] = await executeImageAction(
imageaction,
req.file,
coords,
canvasid,
aLogger,
);
res.status(ret).send(msg);
return;
}
if (req.body.protaction) {
const {
protaction, ulcoor, brcoor, canvasid,
} = req.body;
const [ret, msg] = await executeProtAction(
protaction,
ulcoor,
brcoor,
canvasid,
aLogger,
);
res.status(ret).send(msg);
return;
}
if (req.body.rollback) {
// rollback is date as YYYYMMdd
const {
rollback, ulcoor, brcoor, canvasid,
} = req.body;
const [ret, msg] = await executeRollback(
rollback,
ulcoor,
brcoor,
canvasid,
aLogger,
(req.user.userlvl === 1),
);
res.status(ret).send(msg);
return;
}
next();
});
@ -213,51 +209,42 @@ router.post('/', async (req, res, next) => {
logger.info(`ADMIN> ${req.user.regUser.name}[${req.user.id}]> ${text}`);
};
try {
if (req.body.ipaction) {
const ret = await executeIPAction(
req.body.ipaction,
req.body.ip,
aLogger,
);
res.status(200).send(ret);
return;
}
if (req.body.modlist) {
const ret = await getModList();
res.status(200);
res.json(ret);
return;
}
if (req.body.remmod) {
try {
const ret = await removeMod(req.body.remmod);
res.status(200).send(ret);
} catch (e) {
res.status(400).send(e.message);
}
return;
}
if (req.body.makemod) {
try {
const ret = await makeMod(req.body.makemod);
res.status(200);
res.json(ret);
} catch (e) {
res.status(400).send(e.message);
}
return;
}
next();
} catch (error) {
next(error);
if (req.body.ipaction) {
const ret = await executeIPAction(
req.body.ipaction,
req.body.ip,
aLogger,
);
res.status(200).send(ret);
return;
}
if (req.body.modlist) {
const ret = await getModList();
res.status(200);
res.json(ret);
return;
}
if (req.body.remmod) {
const ret = await removeMod(req.body.remmod);
res.status(200).send(ret);
return;
}
if (req.body.makemod) {
const ret = await makeMod(req.body.makemod);
res.status(200);
res.json(ret);
return;
}
next();
});
router.use(async (req, res) => {
res.status(400).send('Invalid request');
});
// eslint-disable-next-line no-unused-vars
router.use((err, req, res, next) => {
res.status(400).send(err.message);
});
export default router;