blob: 2f6f52611f1e2aefe145b77341497f3c4578d0cf [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};
39export async function testLink(
40 link: string
41): Promise<{ safe: boolean; tags: string[] }> {
42 const scanned: { safe?: boolean; tags?: string[] } | undefined =
43 await us.link.scan(link);
44 if (scanned === undefined) return defaultLinkTestResult;
45 return {
46 safe: scanned.safe ?? defaultLinkTestResult.safe,
47 tags: scanned.tags ?? defaultLinkTestResult.tags
48 };
pineafan813bdf42022-07-24 10:39:10 +010049}
50
pineafan813bdf42022-07-24 10:39:10 +010051const linkTypes = {
Skyler Grey75ea9172022-08-06 10:22:23 +010052 PHISHING: "Links designed to trick users into clicking on them.",
53 DATING: "Dating sites.",
54 TRACKERS: "Websites that store or track personal information.",
55 ADVERTISEMENTS: "Websites only for ads.",
56 FACEBOOK:
57 "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)",
58 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 +010059 "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.",
Skyler Grey75ea9172022-08-06 10:22:23 +010060 "IP GRABBERS":
61 "Websites that store your IP address, which shows your approximate location.",
62 PORN: "Websites that include pornography.",
63 GAMBLING: "Gambling sites, often scams.",
64 MALWARE:
65 "Websites which download files designed to break or slow down your device.",
66 PIRACY: "Sites which include illegally downloaded material.",
67 RANSOMWARE:
68 "Websites which download a program that can steal your data and make you pay to get it back.",
69 REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.",
70 SCAMS: "Sites which are designed to trick you into doing something.",
71 TORRENT: "Websites that download torrent files.",
72 HATE: "Websites that spread hate towards groups or individuals.",
73 JUNK: "Websites that are designed to make you waste time."
pineafan63fc5e22022-08-04 22:04:10 +010074};
pineafan813bdf42022-07-24 10:39:10 +010075export { linkTypes };
76
pineafan63fc5e22022-08-04 22:04:10 +010077export async function LinkCheck(message: Discord.Message): Promise<string[]> {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 const links =
79 message.content.match(
80 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
81 ) ?? [];
82 const detections: { tags: string[]; safe: boolean }[] = [];
83 const promises: Promise<void>[] = links.map(async (element) => {
pineafan63fc5e22022-08-04 22:04:10 +010084 let returned;
pineafan813bdf42022-07-24 10:39:10 +010085 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010086 if (
87 element.match(
88 /https?:\/\/[a-zA-Z]+\.?discord(app)?\.(com|net)\/?/
89 )
90 )
91 return; // Also matches discord.net, not enough of a bug
pineafan63fc5e22022-08-04 22:04:10 +010092 returned = await testLink(element);
93 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010094 detections.push({ tags: [], safe: true });
pineafan63fc5e22022-08-04 22:04:10 +010095 return;
96 }
Skyler Grey75ea9172022-08-06 10:22:23 +010097 detections.push({ tags: returned.tags, safe: returned.safe });
pineafan813bdf42022-07-24 10:39:10 +010098 });
99 await Promise.all(promises);
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 const detectionsTypes = detections
101 .map((element) => {
102 const type = Object.keys(linkTypes).find((type) =>
103 element.tags.includes(type)
104 );
105 if (type) return type;
106 // if (!element.safe) return "UNSAFE"
107 return undefined;
108 })
109 .filter((element) => element !== undefined);
pineafan63fc5e22022-08-04 22:04:10 +0100110 return detectionsTypes as string[];
pineafan813bdf42022-07-24 10:39:10 +0100111}
112
pineafan63fc5e22022-08-04 22:04:10 +0100113export async function NSFWCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100114 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 const test = await testNSFW(element);
pineafan63fc5e22022-08-04 22:04:10 +0100116 return test.nsfw;
pineafan813bdf42022-07-24 10:39:10 +0100117 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100118 return false;
pineafan813bdf42022-07-24 10:39:10 +0100119 }
120}
121
Skyler Grey75ea9172022-08-06 10:22:23 +0100122export async function SizeCheck(element: {
123 height: number | null;
124 width: number | null;
125}): Promise<boolean> {
pineafan63fc5e22022-08-04 22:04:10 +0100126 if (element.height === null || element.width === null) return true;
127 if (element.height < 20 || element.width < 20) return false;
128 return true;
pineafan813bdf42022-07-24 10:39:10 +0100129}
130
pineafan63fc5e22022-08-04 22:04:10 +0100131export async function MalwareCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100132 try {
pineafan63fc5e22022-08-04 22:04:10 +0100133 return (await testMalware(element)).safe;
pineafan813bdf42022-07-24 10:39:10 +0100134 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100135 return true;
pineafan813bdf42022-07-24 10:39:10 +0100136 }
137}
138
Skyler Grey75ea9172022-08-06 10:22:23 +0100139export function TestString(
140 string: string,
141 soft: string[],
142 strict: string[]
143): object | null {
144 for (const word of strict) {
pineafan813bdf42022-07-24 10:39:10 +0100145 if (string.toLowerCase().includes(word)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100147 }
148 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 for (const word of soft) {
150 for (const word2 of string.match(/[a-z]+/gi) ?? []) {
pineafane23c4ec2022-07-27 21:56:27 +0100151 if (word2 === word) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100152 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100153 }
154 }
155 }
pineafan63fc5e22022-08-04 22:04:10 +0100156 return null;
pineafan813bdf42022-07-24 10:39:10 +0100157}
158
pineafan63fc5e22022-08-04 22:04:10 +0100159export async function TestImage(url: string): Promise<string | null> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100160 const text = await Tesseract.recognize(url, {
161 lang: "eng",
162 oem: 1,
163 psm: 3
164 });
pineafan813bdf42022-07-24 10:39:10 +0100165 return text;
166}