blob: 162b85a8fc34ae844bd2308fb6b3ce92555628fc [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Skyler Grey75ea9172022-08-06 10:22:23 +01002// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01003import * as us from "unscan";
4import fetch from "node-fetch";
5import { writeFileSync } from "fs";
6import generateFileName from "../utils/temp/generateFileName.js";
7import Tesseract from "node-tesseract-ocr";
8import type Discord from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +01009
Skyler Grey75ea9172022-08-06 10:22:23 +010010interface NSFWSchema {
11 nsfw: boolean;
12}
13interface MalwareSchema {
14 safe: boolean;
15}
pineafan813bdf42022-07-24 10:39:10 +010016
pineafan02ba0232022-07-24 22:16:15 +010017export async function testNSFW(link: string): Promise<NSFWSchema> {
pineafan63fc5e22022-08-04 22:04:10 +010018 const p = await saveAttachment(link);
19 const result = await us.nsfw.file(p);
20 return result;
pineafan813bdf42022-07-24 10:39:10 +010021}
22
pineafan02ba0232022-07-24 22:16:15 +010023export async function testMalware(link: string): Promise<MalwareSchema> {
pineafan63fc5e22022-08-04 22:04:10 +010024 const p = await saveAttachment(link);
25 const result = await us.malware.file(p);
26 return result;
pineafan813bdf42022-07-24 10:39:10 +010027}
28
pineafan63fc5e22022-08-04 22:04:10 +010029export async function saveAttachment(link: string): Promise<string> {
30 const image = (await (await fetch(link)).buffer()).toString("base64");
Skyler Grey75ea9172022-08-06 10:22:23 +010031 const fileName = generateFileName(link.split("/").pop()!.split(".").pop()!);
pineafan63fc5e22022-08-04 22:04:10 +010032 writeFileSync(fileName, image, "base64");
33 return fileName;
pineafan813bdf42022-07-24 10:39:10 +010034}
35
Skyler Grey75ea9172022-08-06 10:22:23 +010036const defaultLinkTestResult: { safe: boolean; tags: string[] } = {
37 safe: true,
38 tags: []
39};
40export async function testLink(
41 link: string
42): Promise<{ safe: boolean; tags: string[] }> {
43 const scanned: { safe?: boolean; tags?: string[] } | undefined =
44 await us.link.scan(link);
45 if (scanned === undefined) return defaultLinkTestResult;
46 return {
47 safe: scanned.safe ?? defaultLinkTestResult.safe,
48 tags: scanned.tags ?? defaultLinkTestResult.tags
49 };
pineafan813bdf42022-07-24 10:39:10 +010050}
51
pineafan813bdf42022-07-24 10:39:10 +010052const linkTypes = {
Skyler Grey75ea9172022-08-06 10:22:23 +010053 PHISHING: "Links designed to trick users into clicking on them.",
54 DATING: "Dating sites.",
55 TRACKERS: "Websites that store or track personal information.",
56 ADVERTISEMENTS: "Websites only for ads.",
57 FACEBOOK:
58 "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)",
59 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 +010060 "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.",
Skyler Grey75ea9172022-08-06 10:22:23 +010061 "IP GRABBERS":
62 "Websites that store your IP address, which shows your approximate location.",
63 PORN: "Websites that include pornography.",
64 GAMBLING: "Gambling sites, often scams.",
65 MALWARE:
66 "Websites which download files designed to break or slow down your device.",
67 PIRACY: "Sites which include illegally downloaded material.",
68 RANSOMWARE:
69 "Websites which download a program that can steal your data and make you pay to get it back.",
70 REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.",
71 SCAMS: "Sites which are designed to trick you into doing something.",
72 TORRENT: "Websites that download torrent files.",
73 HATE: "Websites that spread hate towards groups or individuals.",
74 JUNK: "Websites that are designed to make you waste time."
pineafan63fc5e22022-08-04 22:04:10 +010075};
pineafan813bdf42022-07-24 10:39:10 +010076export { linkTypes };
77
pineafan63fc5e22022-08-04 22:04:10 +010078export async function LinkCheck(message: Discord.Message): Promise<string[]> {
Skyler Grey75ea9172022-08-06 10:22:23 +010079 const links =
80 message.content.match(
81 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
82 ) ?? [];
83 const detections: { tags: string[]; safe: boolean }[] = [];
84 const promises: Promise<void>[] = links.map(async (element) => {
pineafan63fc5e22022-08-04 22:04:10 +010085 let returned;
pineafan813bdf42022-07-24 10:39:10 +010086 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010087 if (
88 element.match(
89 /https?:\/\/[a-zA-Z]+\.?discord(app)?\.(com|net)\/?/
90 )
91 )
92 return; // Also matches discord.net, not enough of a bug
pineafan63fc5e22022-08-04 22:04:10 +010093 returned = await testLink(element);
94 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010095 detections.push({ tags: [], safe: true });
pineafan63fc5e22022-08-04 22:04:10 +010096 return;
97 }
Skyler Grey75ea9172022-08-06 10:22:23 +010098 detections.push({ tags: returned.tags, safe: returned.safe });
pineafan813bdf42022-07-24 10:39:10 +010099 });
100 await Promise.all(promises);
Skyler Grey75ea9172022-08-06 10:22:23 +0100101 const detectionsTypes = detections
102 .map((element) => {
103 const type = Object.keys(linkTypes).find((type) =>
104 element.tags.includes(type)
105 );
106 if (type) return type;
107 // if (!element.safe) return "UNSAFE"
108 return undefined;
109 })
110 .filter((element) => element !== undefined);
pineafan63fc5e22022-08-04 22:04:10 +0100111 return detectionsTypes as string[];
pineafan813bdf42022-07-24 10:39:10 +0100112}
113
pineafan63fc5e22022-08-04 22:04:10 +0100114export async function NSFWCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100115 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 const test = await testNSFW(element);
pineafan63fc5e22022-08-04 22:04:10 +0100117 return test.nsfw;
pineafan813bdf42022-07-24 10:39:10 +0100118 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100119 return false;
pineafan813bdf42022-07-24 10:39:10 +0100120 }
121}
122
Skyler Grey75ea9172022-08-06 10:22:23 +0100123export async function SizeCheck(element: {
124 height: number | null;
125 width: number | null;
126}): Promise<boolean> {
pineafan63fc5e22022-08-04 22:04:10 +0100127 if (element.height === null || element.width === null) return true;
128 if (element.height < 20 || element.width < 20) return false;
129 return true;
pineafan813bdf42022-07-24 10:39:10 +0100130}
131
pineafan63fc5e22022-08-04 22:04:10 +0100132export async function MalwareCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100133 try {
pineafan63fc5e22022-08-04 22:04:10 +0100134 return (await testMalware(element)).safe;
pineafan813bdf42022-07-24 10:39:10 +0100135 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100136 return true;
pineafan813bdf42022-07-24 10:39:10 +0100137 }
138}
139
Skyler Grey75ea9172022-08-06 10:22:23 +0100140export function TestString(
141 string: string,
142 soft: string[],
143 strict: string[]
144): object | null {
145 for (const word of strict) {
pineafan813bdf42022-07-24 10:39:10 +0100146 if (string.toLowerCase().includes(word)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100147 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100148 }
149 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100150 for (const word of soft) {
151 for (const word2 of string.match(/[a-z]+/gi) ?? []) {
pineafane23c4ec2022-07-27 21:56:27 +0100152 if (word2 === word) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100153 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100154 }
155 }
156 }
pineafan63fc5e22022-08-04 22:04:10 +0100157 return null;
pineafan813bdf42022-07-24 10:39:10 +0100158}
159
pineafan63fc5e22022-08-04 22:04:10 +0100160export async function TestImage(url: string): Promise<string | null> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100161 const text = await Tesseract.recognize(url, {
162 lang: "eng",
163 oem: 1,
164 psm: 3
165 });
pineafan813bdf42022-07-24 10:39:10 +0100166 return text;
167}