mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-09 13:07:02 +03:00
Add IP banning
This commit is contained in:
@@ -32,6 +32,10 @@ CREATE TABLE IF NOT EXISTS "categoryVotes" (
|
|||||||
"votes" INTEGER NOT NULL default 0
|
"votes" INTEGER NOT NULL default 0
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS "shadowBannedIPs" (
|
||||||
|
"hashedIP" TEXT NOT NULL PRIMARY KEY
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS "config" (
|
CREATE TABLE IF NOT EXISTS "config" (
|
||||||
"key" TEXT NOT NULL UNIQUE,
|
"key" TEXT NOT NULL UNIQUE,
|
||||||
"value" TEXT NOT NULL
|
"value" TEXT NOT NULL
|
||||||
|
|||||||
@@ -541,14 +541,15 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
//check to see if this user is shadowbanned
|
//check to see if this user is shadowbanned
|
||||||
const shadowBanRow = await db.prepare("get", `SELECT count(*) as "userCount" FROM "shadowBannedUsers" WHERE "userID" = ? LIMIT 1`, [userID]);
|
const shadowBanCount = (await db.prepare("get", `SELECT count(*) as "userCount" FROM "shadowBannedUsers" WHERE "userID" = ? LIMIT 1`, [userID]))?.userCount
|
||||||
|
|| (await db.prepare("get", `SELECT count(*) as "userCount" FROM "shadowBannedIPs" WHERE "hashedIP" = ? LIMIT 1`, [hashedIP]))?.userCount;
|
||||||
const startingVotes = 0;
|
const startingVotes = 0;
|
||||||
const reputation = await getReputation(userID);
|
const reputation = await getReputation(userID);
|
||||||
|
|
||||||
for (const segmentInfo of segments) {
|
for (const segmentInfo of segments) {
|
||||||
// Full segments are always rejected since there can only be one, so shadow hide wouldn't work
|
// Full segments are always rejected since there can only be one, so shadow hide wouldn't work
|
||||||
if (segmentInfo.ignoreSegment
|
if (segmentInfo.ignoreSegment
|
||||||
|| (shadowBanRow.userCount && segmentInfo.actionType === ActionType.Full)) {
|
|| (shadowBanCount && segmentInfo.actionType === ActionType.Full)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -565,7 +566,7 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
|
|||||||
("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "reputation", "shadowHidden", "hashedVideoID", "userAgent", "description")
|
("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "reputation", "shadowHidden", "hashedVideoID", "userAgent", "description")
|
||||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
||||||
videoID, segmentInfo.segment[0], segmentInfo.segment[1], startingVotes, startingLocked, UUID, userID, timeSubmitted, 0
|
videoID, segmentInfo.segment[0], segmentInfo.segment[1], startingVotes, startingLocked, UUID, userID, timeSubmitted, 0
|
||||||
, segmentInfo.category, segmentInfo.actionType, service, videoDuration, reputation, shadowBanRow.userCount, hashedVideoID, userAgent, segmentInfo.description
|
, segmentInfo.category, segmentInfo.actionType, service, videoDuration, reputation, shadowBanCount, hashedVideoID, userAgent, segmentInfo.description
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { db } from "../databases/databases";
|
import { db, privateDB } from "../databases/databases";
|
||||||
import { getHashCache } from "../utils/getHashCache";
|
import { getHashCache } from "../utils/getHashCache";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { config } from "../config";
|
import { config } from "../config";
|
||||||
import { Category, Service, VideoID, VideoIDHash } from "../types/segments.model";
|
import { Category, HashedIP, Service, VideoID, VideoIDHash } from "../types/segments.model";
|
||||||
import { UserID } from "../types/user.model";
|
import { UserID } from "../types/user.model";
|
||||||
import { QueryCacher } from "../utils/queryCacher";
|
import { QueryCacher } from "../utils/queryCacher";
|
||||||
import { isUserVIP } from "../utils/isUserVIP";
|
import { isUserVIP } from "../utils/isUserVIP";
|
||||||
@@ -10,7 +10,7 @@ import { parseCategories } from "../utils/parseParams";
|
|||||||
|
|
||||||
export async function shadowBanUser(req: Request, res: Response): Promise<Response> {
|
export async function shadowBanUser(req: Request, res: Response): Promise<Response> {
|
||||||
const userID = req.query.userID as UserID;
|
const userID = req.query.userID as UserID;
|
||||||
const hashedIP = req.query.hashedIP as string;
|
const hashedIP = req.query.hashedIP as HashedIP;
|
||||||
const adminUserIDInput = req.query.adminUserID as UserID;
|
const adminUserIDInput = req.query.adminUserID as UserID;
|
||||||
const type = req.query.type as string ?? "1";
|
const type = req.query.type as string ?? "1";
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ export async function shadowBanUser(req: Request, res: Response): Promise<Respon
|
|||||||
|
|
||||||
//find all previous submissions and hide them
|
//find all previous submissions and hide them
|
||||||
if (unHideOldSubmissions) {
|
if (unHideOldSubmissions) {
|
||||||
await unHideSubmissions(categories, userID, type);
|
await unHideSubmissionsByUser(categories, userID, type);
|
||||||
}
|
}
|
||||||
} else if (!enabled && row.userCount > 0) {
|
} else if (!enabled && row.userCount > 0) {
|
||||||
//remove them from the shadow ban list
|
//remove them from the shadow ban list
|
||||||
@@ -80,7 +80,7 @@ export async function shadowBanUser(req: Request, res: Response): Promise<Respon
|
|||||||
} else if (enabled && row.userCount > 0) {
|
} else if (enabled && row.userCount > 0) {
|
||||||
// apply unHideOldSubmissions if applicable
|
// apply unHideOldSubmissions if applicable
|
||||||
if (unHideOldSubmissions) {
|
if (unHideOldSubmissions) {
|
||||||
await unHideSubmissions(categories, userID, type);
|
await unHideSubmissionsByUser(categories, userID, type);
|
||||||
return res.sendStatus(200);
|
return res.sendStatus(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,36 +89,43 @@ export async function shadowBanUser(req: Request, res: Response): Promise<Respon
|
|||||||
}
|
}
|
||||||
} else if (hashedIP) {
|
} else if (hashedIP) {
|
||||||
//check to see if this user is already shadowbanned
|
//check to see if this user is already shadowbanned
|
||||||
// let row = await privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedIPs WHERE hashedIP = ?", [hashedIP]);
|
const row = await db.prepare("get", `SELECT count(*) as "userCount" FROM "shadowBannedIPs" WHERE "hashedIP" = ?`, [hashedIP]);
|
||||||
|
|
||||||
// if (enabled && row.userCount == 0) {
|
if (enabled && row.userCount == 0) {
|
||||||
if (enabled) {
|
|
||||||
//add them to the shadow ban list
|
//add them to the shadow ban list
|
||||||
|
|
||||||
//add it to the table
|
//add it to the table
|
||||||
// await privateDB.prepare('run', "INSERT INTO shadowBannedIPs VALUES(?)", [hashedIP]);
|
await db.prepare("run", `INSERT INTO "shadowBannedIPs" VALUES(?)`, [hashedIP]);
|
||||||
|
|
||||||
|
|
||||||
//find all previous submissions and hide them
|
//find all previous submissions and hide them
|
||||||
if (unHideOldSubmissions) {
|
if (unHideOldSubmissions) {
|
||||||
await db.prepare("run", `UPDATE "sponsorTimes" SET "shadowHidden" = ${type} WHERE "timeSubmitted" IN
|
await unHideSubmissionsByIP(categories, hashedIP, type);
|
||||||
(SELECT "privateDB"."timeSubmitted" FROM "sponsorTimes" LEFT JOIN "privateDB"."sponsorTimes" as "privateDB" ON "sponsorTimes"."timeSubmitted"="privateDB"."timeSubmitted"
|
}
|
||||||
WHERE "privateDB"."hashedIP" = ?)`, [hashedIP]);
|
} else if (!enabled) {
|
||||||
|
if (row.userCount > 0) {
|
||||||
|
//remove them from the shadow ban list
|
||||||
|
await db.prepare("run", `DELETE FROM "shadowBannedIPs" WHERE "hashedIP" = ?`, [hashedIP]);
|
||||||
}
|
}
|
||||||
} /*else if (!enabled && row.userCount > 0) {
|
|
||||||
// //remove them from the shadow ban list
|
|
||||||
// await db.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]);
|
|
||||||
|
|
||||||
// //find all previous submissions and unhide them
|
//find all previous submissions and unhide them
|
||||||
// if (unHideOldSubmissions) {
|
if (unHideOldSubmissions) {
|
||||||
// await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE userID = ?", [userID]);
|
await unHideSubmissionsByIP(categories, hashedIP, "0");
|
||||||
// }
|
}
|
||||||
}*/
|
} else if (enabled && row.userCount > 0) {
|
||||||
|
// apply unHideOldSubmissions if applicable
|
||||||
|
if (unHideOldSubmissions) {
|
||||||
|
await unHideSubmissionsByIP(categories, hashedIP, type);
|
||||||
|
return res.sendStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise ban already exists, send 409
|
||||||
|
return res.sendStatus(409);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return res.sendStatus(200);
|
return res.sendStatus(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function unHideSubmissions(categories: string[], userID: UserID, type = "1") {
|
async function unHideSubmissionsByUser(categories: string[], userID: UserID, type = "1") {
|
||||||
if (!["1", "2"].includes(type)) return;
|
if (!["1", "2"].includes(type)) return;
|
||||||
|
|
||||||
await db.prepare("run", `UPDATE "sponsorTimes" SET "shadowHidden" = ${type} WHERE "userID" = ? AND "category" in (${categories.map((c) => `'${c}'`).join(",")})
|
await db.prepare("run", `UPDATE "sponsorTimes" SET "shadowHidden" = ${type} WHERE "userID" = ? AND "category" in (${categories.map((c) => `'${c}'`).join(",")})
|
||||||
@@ -131,3 +138,21 @@ async function unHideSubmissions(categories: string[], userID: UserID, type = "1
|
|||||||
QueryCacher.clearSegmentCache(videoInfo);
|
QueryCacher.clearSegmentCache(videoInfo);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function unHideSubmissionsByIP(categories: string[], hashedIP: HashedIP, type = "1") {
|
||||||
|
if (!["0", "1", "2"].includes(type)) return;
|
||||||
|
|
||||||
|
const submissions = await privateDB.prepare("all", `SELECT "timeSubmitted" FROM "sponsorTimes" WHERE "hashedIP" = ?`, [hashedIP]) as { timeSubmitted: number }[];
|
||||||
|
|
||||||
|
await Promise.all(submissions.map(async (submission) => {
|
||||||
|
(await db.prepare("all", `SELECT "videoID", "hashedVideoID", "service", "votes", "views", "userID" FROM "sponsorTimes" WHERE "timeSubmitted" = ? AND "shadowHidden" >= 1 AND "category" in (${categories.map((c) => `'${c}'`).join(",")})`, [submission.timeSubmitted]))
|
||||||
|
.forEach((videoInfo: { category: Category, videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, userID: UserID }) => {
|
||||||
|
QueryCacher.clearSegmentCache(videoInfo);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
await db.prepare("run", `UPDATE "sponsorTimes" SET "shadowHidden" = ${type} WHERE "timeSubmitted" = ? AND "category" in (${categories.map((c) => `'${c}'`).join(",")})
|
||||||
|
AND NOT EXISTS ( SELECT "videoID", "category" FROM "lockCategories" WHERE
|
||||||
|
"sponsorTimes"."videoID" = "lockCategories"."videoID" AND "sponsorTimes"."service" = "lockCategories"."service" AND "sponsorTimes"."category" = "lockCategories"."category")`, [submission.timeSubmitted]);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { db } from "../../src/databases/databases";
|
import { db, privateDB } from "../../src/databases/databases";
|
||||||
import { getHash } from "../../src/utils/getHash";
|
import { getHash } from "../../src/utils/getHash";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { Category } from "../../src/types/segments.model";
|
import { Category } from "../../src/types/segments.model";
|
||||||
@@ -9,6 +9,8 @@ describe("shadowBanUser", () => {
|
|||||||
const getShadowBanSegments = (userID: string, status: number) => db.prepare("all", `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]);
|
const getShadowBanSegments = (userID: string, status: number) => db.prepare("all", `SELECT "shadowHidden" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]);
|
||||||
const getShadowBanSegmentCategory = (userID: string, status: number): Promise<{shadowHidden: number, category: Category}[]> => db.prepare("all", `SELECT "shadowHidden", "category" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]);
|
const getShadowBanSegmentCategory = (userID: string, status: number): Promise<{shadowHidden: number, category: Category}[]> => db.prepare("all", `SELECT "shadowHidden", "category" FROM "sponsorTimes" WHERE "userID" = ? AND "shadowHidden" = ?`, [userID, status]);
|
||||||
|
|
||||||
|
const getIPShadowBan = (hashedIP: string) => db.prepare("get", `SELECT * FROM "shadowBannedIPs" WHERE "hashedIP" = ?`, [hashedIP]);
|
||||||
|
|
||||||
const endpoint = "/api/shadowBanUser";
|
const endpoint = "/api/shadowBanUser";
|
||||||
const VIPuserID = "shadow-ban-vip";
|
const VIPuserID = "shadow-ban-vip";
|
||||||
const video = "shadowBanVideo";
|
const video = "shadowBanVideo";
|
||||||
@@ -35,6 +37,10 @@ describe("shadowBanUser", () => {
|
|||||||
await db.prepare("run", insertQuery, [video, 10, 10, 2, 1, "shadow-60", "shadowBanned6", 0, 50, "sponsor", "YouTube", 0, videohash]);
|
await db.prepare("run", insertQuery, [video, 10, 10, 2, 1, "shadow-60", "shadowBanned6", 0, 50, "sponsor", "YouTube", 0, videohash]);
|
||||||
await db.prepare("run", insertQuery, ["lockedVideo", 10, 10, 2, 1, "shadow-61", "shadowBanned6", 0, 50, "sponsor", "YouTube", 0, getHash("lockedVideo", 1)]);
|
await db.prepare("run", insertQuery, ["lockedVideo", 10, 10, 2, 1, "shadow-61", "shadowBanned6", 0, 50, "sponsor", "YouTube", 0, getHash("lockedVideo", 1)]);
|
||||||
|
|
||||||
|
await db.prepare("run", insertQuery, [video, 20, 10, 2, 0, "shadow-70", "shadowBanned7", 383848, 50, "sponsor", "YouTube", 0, videohash]);
|
||||||
|
await db.prepare("run", insertQuery, [video, 20, 10, 2, 0, "shadow-71", "shadowBanned7", 2332, 50, "intro", "YouTube", 0, videohash]);
|
||||||
|
await db.prepare("run", insertQuery, [video, 20, 10, 2, 0, "shadow-72", "shadowBanned7", 4923, 50, "interaction", "YouTube", 0, videohash]);
|
||||||
|
|
||||||
await db.prepare("run", `INSERT INTO "shadowBannedUsers" ("userID") VALUES(?)`, ["shadowBanned3"]);
|
await db.prepare("run", `INSERT INTO "shadowBannedUsers" ("userID") VALUES(?)`, ["shadowBanned3"]);
|
||||||
await db.prepare("run", `INSERT INTO "shadowBannedUsers" ("userID") VALUES(?)`, ["shadowBanned4"]);
|
await db.prepare("run", `INSERT INTO "shadowBannedUsers" ("userID") VALUES(?)`, ["shadowBanned4"]);
|
||||||
|
|
||||||
@@ -42,6 +48,11 @@ describe("shadowBanUser", () => {
|
|||||||
[getHash("shadow-ban-vip", 1), "lockedVideo", "skip", "sponsor", "YouTube"]);
|
[getHash("shadow-ban-vip", 1), "lockedVideo", "skip", "sponsor", "YouTube"]);
|
||||||
|
|
||||||
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES(?)`, [getHash(VIPuserID)]);
|
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES(?)`, [getHash(VIPuserID)]);
|
||||||
|
|
||||||
|
const privateInsertQuery = `INSERT INTO "sponsorTimes" ("videoID", "hashedIP", "timeSubmitted", "service") VALUES(?, ?, ?, ?)`;
|
||||||
|
await privateDB.prepare("run", privateInsertQuery, [video, "shadowBannedIP7", 383848, "YouTube"]);
|
||||||
|
await privateDB.prepare("run", privateInsertQuery, [video, "shadowBannedIP7", 2332, "YouTube"]);
|
||||||
|
await privateDB.prepare("run", privateInsertQuery, [video, "shadowBannedIP7", 4923, "YouTube"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should be able to ban user and hide submissions", (done) => {
|
it("Should be able to ban user and hide submissions", (done) => {
|
||||||
@@ -306,4 +317,82 @@ describe("shadowBanUser", () => {
|
|||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should be able to ban user by IP and hide submissions of a specific category", (done) => {
|
||||||
|
const hashedIP = "shadowBannedIP7";
|
||||||
|
const userID = "shadowBanned7";
|
||||||
|
client({
|
||||||
|
method: "POST",
|
||||||
|
url: endpoint,
|
||||||
|
params: {
|
||||||
|
hashedIP,
|
||||||
|
categories: `["sponsor", "intro"]`,
|
||||||
|
adminUserID: VIPuserID,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(async res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const videoRow = await getShadowBanSegments(userID, 1);
|
||||||
|
const normalShadowRow = await getShadowBan(userID);
|
||||||
|
const ipShadowRow = await getIPShadowBan(hashedIP);
|
||||||
|
assert.ok(ipShadowRow);
|
||||||
|
assert.ok(!normalShadowRow);
|
||||||
|
assert.strictEqual(videoRow.length, 2);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should be able to unban user by IP", (done) => {
|
||||||
|
const hashedIP = "shadowBannedIP7";
|
||||||
|
const userID = "shadowBanned7";
|
||||||
|
client({
|
||||||
|
method: "POST",
|
||||||
|
url: endpoint,
|
||||||
|
params: {
|
||||||
|
hashedIP,
|
||||||
|
enabled: false,
|
||||||
|
unHideOldSubmissions: false,
|
||||||
|
adminUserID: VIPuserID,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(async res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const videoRow = await getShadowBanSegments(userID, 1);
|
||||||
|
const normalShadowRow = await getShadowBan(userID);
|
||||||
|
const ipShadowRow = await getIPShadowBan(hashedIP);
|
||||||
|
assert.ok(!ipShadowRow);
|
||||||
|
assert.ok(!normalShadowRow);
|
||||||
|
assert.strictEqual(videoRow.length, 2);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should be able to unban user by IP and unhide specific category", (done) => {
|
||||||
|
const hashedIP = "shadowBannedIP7";
|
||||||
|
const userID = "shadowBanned7";
|
||||||
|
client({
|
||||||
|
method: "POST",
|
||||||
|
url: endpoint,
|
||||||
|
params: {
|
||||||
|
hashedIP,
|
||||||
|
enabled: false,
|
||||||
|
categories: `["sponsor"]`,
|
||||||
|
unHideOldSubmissions: true,
|
||||||
|
adminUserID: VIPuserID,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(async res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const videoRow = await getShadowBanSegments(userID, 1);
|
||||||
|
const normalShadowRow = await getShadowBan(userID);
|
||||||
|
const ipShadowRow = await getIPShadowBan(hashedIP);
|
||||||
|
assert.ok(!ipShadowRow);
|
||||||
|
assert.ok(!normalShadowRow);
|
||||||
|
assert.strictEqual(videoRow.length, 1);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user