You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1014 B
TypeScript
40 lines
1014 B
TypeScript
4 years ago
|
import * as db from '../service/db';
|
||
4 years ago
|
import { Bdoc } from '../interface';
|
||
5 years ago
|
|
||
|
const coll = db.collection('blacklist');
|
||
5 years ago
|
|
||
4 years ago
|
export async function add(ip: string): Promise<Bdoc> {
|
||
4 years ago
|
/**
|
||
|
* Add a ip into blacklist.
|
||
|
* @param {string} ip
|
||
|
* @returns {Promise<Bdoc>}
|
||
|
*/
|
||
5 years ago
|
const expireAt = new Date(new Date().getTime() + 365 * 24 * 60 * 60 * 1000);
|
||
4 years ago
|
const res = await coll.findOneAndUpdate(
|
||
|
{ _id: ip },
|
||
|
{ $set: { expireAt } },
|
||
|
{ upsert: true, returnOriginal: false },
|
||
|
);
|
||
|
return res.value;
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function get(ip: string): Promise<Bdoc | null> {
|
||
|
/**
|
||
|
* Get a ip, return null if not.
|
||
|
* @param {string} ip
|
||
|
* @returns {Promise<Bdoc>}
|
||
|
*/
|
||
5 years ago
|
return coll.findOne({ _id: ip });
|
||
|
}
|
||
5 years ago
|
|
||
4 years ago
|
export function del(ip: string) {
|
||
5 years ago
|
return coll.deleteOne({ _id: ip });
|
||
|
}
|
||
|
|
||
4 years ago
|
function ensureIndexes() {
|
||
5 years ago
|
return coll.createIndex('expireAt', { expireAfterSeconds: 0 });
|
||
|
}
|
||
|
|
||
4 years ago
|
global.Hydro.postInit.push(ensureIndexes);
|
||
|
global.Hydro.model.blacklist = { add, get, del };
|