blob: bd60f54670c35c1ea4578a479c98aa608dc6d711 [file] [log] [blame]
Skyler Grey75ea9172022-08-06 10:22:23 +01001// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01002import * as us from "unscan";
3import fetch from "node-fetch";
4import { writeFileSync } from "fs";
5import generateFileName from "../utils/temp/generateFileName.js";
6import Tesseract from "node-tesseract-ocr";
7import type Discord from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +01008
Skyler Grey75ea9172022-08-06 10:22:23 +01009interface NSFWSchema {
10 nsfw: boolean;
11}
12interface MalwareSchema {
13 safe: boolean;
14}
pineafan813bdf42022-07-24 10:39:10 +010015
pineafan02ba0232022-07-24 22:16:15 +010016export async function testNSFW(link: string): Promise<NSFWSchema> {
pineafan63fc5e22022-08-04 22:04:10 +010017 const p = await saveAttachment(link);
18 const result = await us.nsfw.file(p);
Skyler Greyc634e2b2022-08-06 17:50:48 +010019 return { nsfw: result.nsfw ?? false };
pineafan813bdf42022-07-24 10:39:10 +010020}
21
pineafan02ba0232022-07-24 22:16:15 +010022export async function testMalware(link: string): Promise<MalwareSchema> {
pineafan63fc5e22022-08-04 22:04:10 +010023 const p = await saveAttachment(link);
24 const result = await us.malware.file(p);
Skyler Greyc634e2b2022-08-06 17:50:48 +010025 return { safe: result.safe ?? true };
pineafan813bdf42022-07-24 10:39:10 +010026}
27
pineafan63fc5e22022-08-04 22:04:10 +010028export async function saveAttachment(link: string): Promise<string> {
29 const image = (await (await fetch(link)).buffer()).toString("base64");
Skyler Grey75ea9172022-08-06 10:22:23 +010030 const fileName = generateFileName(link.split("/").pop()!.split(".").pop()!);
pineafan63fc5e22022-08-04 22:04:10 +010031 writeFileSync(fileName, image, "base64");
32 return fileName;
pineafan813bdf42022-07-24 10:39:10 +010033}
34
Skyler Grey75ea9172022-08-06 10:22:23 +010035const defaultLinkTestResult: { safe: boolean; tags: string[] } = {
36 safe: true,
37 tags: []
38};
Skyler Grey11236ba2022-08-08 21:13:33 +010039export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> {
40 const scanned: { safe?: boolean; tags?: string[] } | undefined = await us.link.scan(link);
Skyler Grey75ea9172022-08-06 10:22:23 +010041 if (scanned === undefined) return defaultLinkTestResult;
42 return {
43 safe: scanned.safe ?? defaultLinkTestResult.safe,
44 tags: scanned.tags ?? defaultLinkTestResult.tags
45 };
pineafan813bdf42022-07-24 10:39:10 +010046}
47
pineafan813bdf42022-07-24 10:39:10 +010048const linkTypes = {
Skyler Grey75ea9172022-08-06 10:22:23 +010049 PHISHING: "Links designed to trick users into clicking on them.",
50 DATING: "Dating sites.",
51 TRACKERS: "Websites that store or track personal information.",
52 ADVERTISEMENTS: "Websites only for ads.",
Skyler Grey11236ba2022-08-08 21:13:33 +010053 FACEBOOK: "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)",
Skyler Grey75ea9172022-08-06 10:22:23 +010054 AMP: "AMP pages. (AMP is a technology that allows websites to be served by Google. Read more on /privacy)",
pineafan813bdf42022-07-24 10:39:10 +010055 "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.",
Skyler Grey11236ba2022-08-08 21:13:33 +010056 "IP GRABBERS": "Websites that store your IP address, which shows your approximate location.",
Skyler Grey75ea9172022-08-06 10:22:23 +010057 PORN: "Websites that include pornography.",
58 GAMBLING: "Gambling sites, often scams.",
Skyler Grey11236ba2022-08-08 21:13:33 +010059 MALWARE: "Websites which download files designed to break or slow down your device.",
Skyler Grey75ea9172022-08-06 10:22:23 +010060 PIRACY: "Sites which include illegally downloaded material.",
Skyler Grey11236ba2022-08-08 21:13:33 +010061 RANSOMWARE: "Websites which download a program that can steal your data and make you pay to get it back.",
Skyler Grey75ea9172022-08-06 10:22:23 +010062 REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.",
63 SCAMS: "Sites which are designed to trick you into doing something.",
64 TORRENT: "Websites that download torrent files.",
65 HATE: "Websites that spread hate towards groups or individuals.",
66 JUNK: "Websites that are designed to make you waste time."
pineafan63fc5e22022-08-04 22:04:10 +010067};
pineafan813bdf42022-07-24 10:39:10 +010068export { linkTypes };
69
pineafan63fc5e22022-08-04 22:04:10 +010070export async function LinkCheck(message: Discord.Message): Promise<string[]> {
Skyler Grey75ea9172022-08-06 10:22:23 +010071 const links =
72 message.content.match(
73 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
74 ) ?? [];
75 const detections: { tags: string[]; safe: boolean }[] = [];
76 const promises: Promise<void>[] = links.map(async (element) => {
pineafan63fc5e22022-08-04 22:04:10 +010077 let returned;
pineafan813bdf42022-07-24 10:39:10 +010078 try {
Skyler Grey11236ba2022-08-08 21:13:33 +010079 if (element.match(/https?:\/\/[a-zA-Z]+\.?discord(app)?\.(com|net)\/?/)) return; // Also matches discord.net, not enough of a bug
pineafan63fc5e22022-08-04 22:04:10 +010080 returned = await testLink(element);
81 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010082 detections.push({ tags: [], safe: true });
pineafan63fc5e22022-08-04 22:04:10 +010083 return;
84 }
Skyler Grey75ea9172022-08-06 10:22:23 +010085 detections.push({ tags: returned.tags, safe: returned.safe });
pineafan813bdf42022-07-24 10:39:10 +010086 });
87 await Promise.all(promises);
Skyler Grey75ea9172022-08-06 10:22:23 +010088 const detectionsTypes = detections
89 .map((element) => {
Skyler Grey11236ba2022-08-08 21:13:33 +010090 const type = Object.keys(linkTypes).find((type) => element.tags.includes(type));
Skyler Grey75ea9172022-08-06 10:22:23 +010091 if (type) return type;
92 // if (!element.safe) return "UNSAFE"
93 return undefined;
94 })
95 .filter((element) => element !== undefined);
pineafan63fc5e22022-08-04 22:04:10 +010096 return detectionsTypes as string[];
pineafan813bdf42022-07-24 10:39:10 +010097}
98
pineafan63fc5e22022-08-04 22:04:10 +010099export async function NSFWCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100100 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100101 const test = await testNSFW(element);
pineafan63fc5e22022-08-04 22:04:10 +0100102 return test.nsfw;
pineafan813bdf42022-07-24 10:39:10 +0100103 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100104 return false;
pineafan813bdf42022-07-24 10:39:10 +0100105 }
106}
107
Skyler Grey11236ba2022-08-08 21:13:33 +0100108export async function SizeCheck(element: { height: number | null; width: number | null }): Promise<boolean> {
pineafan63fc5e22022-08-04 22:04:10 +0100109 if (element.height === null || element.width === null) return true;
110 if (element.height < 20 || element.width < 20) return false;
111 return true;
pineafan813bdf42022-07-24 10:39:10 +0100112}
113
pineafan63fc5e22022-08-04 22:04:10 +0100114export async function MalwareCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100115 try {
pineafan63fc5e22022-08-04 22:04:10 +0100116 return (await testMalware(element)).safe;
pineafan813bdf42022-07-24 10:39:10 +0100117 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100118 return true;
pineafan813bdf42022-07-24 10:39:10 +0100119 }
120}
121
Skyler Grey11236ba2022-08-08 21:13:33 +0100122export function TestString(string: string, soft: string[], strict: string[]): object | null {
Skyler Grey75ea9172022-08-06 10:22:23 +0100123 for (const word of strict) {
pineafan813bdf42022-07-24 10:39:10 +0100124 if (string.toLowerCase().includes(word)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100125 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100126 }
127 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100128 for (const word of soft) {
129 for (const word2 of string.match(/[a-z]+/gi) ?? []) {
pineafane23c4ec2022-07-27 21:56:27 +0100130 if (word2 === word) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100132 }
133 }
134 }
pineafan63fc5e22022-08-04 22:04:10 +0100135 return null;
pineafan813bdf42022-07-24 10:39:10 +0100136}
137
pineafan63fc5e22022-08-04 22:04:10 +0100138export async function TestImage(url: string): Promise<string | null> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 const text = await Tesseract.recognize(url, {
140 lang: "eng",
141 oem: 1,
142 psm: 3
143 });
pineafan813bdf42022-07-24 10:39:10 +0100144 return text;
145}