blob: 71e63b4be530efd7aa1a22561b975fbc52e10798 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import fetch from "node-fetch";
TheCodedProfb5e9d552023-01-29 15:43:26 -05002import fs, { writeFileSync, createReadStream } from "fs";
pineafan63fc5e22022-08-04 22:04:10 +01003import generateFileName from "../utils/temp/generateFileName.js";
4import Tesseract from "node-tesseract-ocr";
5import type Discord from "discord.js";
pineafan3a02ea32022-08-11 21:35:04 +01006import client from "../utils/client.js";
TheCodedProfb5e9d552023-01-29 15:43:26 -05007import { createHash } from "crypto";
pineafan813bdf42022-07-24 10:39:10 +01008
Skyler Grey75ea9172022-08-06 10:22:23 +01009interface NSFWSchema {
10 nsfw: boolean;
TheCodedProf5b53a8c2023-02-03 15:40:26 -050011 errored?: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +010012}
13interface MalwareSchema {
14 safe: boolean;
TheCodedProf5b53a8c2023-02-03 15:40:26 -050015 errored?: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +010016}
pineafan813bdf42022-07-24 10:39:10 +010017
pineafan02ba0232022-07-24 22:16:15 +010018export async function testNSFW(link: string): Promise<NSFWSchema> {
TheCodedProfb5e9d552023-01-29 15:43:26 -050019 const [p, hash] = await saveAttachment(link);
Skyler Greyda16adf2023-03-05 10:22:12 +000020 const alreadyHaveCheck = await client.database.scanCache.read(hash);
21 if (alreadyHaveCheck) return { nsfw: alreadyHaveCheck.data };
TheCodedProf633866f2023-02-03 17:06:00 -050022 const data = new URLSearchParams();
Skyler Greyda16adf2023-03-05 10:22:12 +000023 const r = createReadStream(p);
TheCodedProf633866f2023-02-03 17:06:00 -050024 data.append("file", r.read(fs.statSync(p).size));
pineafan3a02ea32022-08-11 21:35:04 +010025 const result = await fetch("https://unscan.p.rapidapi.com/", {
26 method: "POST",
27 headers: {
28 "X-RapidAPI-Key": client.config.rapidApiKey,
29 "X-RapidAPI-Host": "unscan.p.rapidapi.com"
30 },
31 body: data
32 })
Skyler Greyda16adf2023-03-05 10:22:12 +000033 .then((response) =>
34 response.status === 200 ? (response.json() as Promise<NSFWSchema>) : { nsfw: false, errored: true }
35 )
pineafan3a02ea32022-08-11 21:35:04 +010036 .catch((err) => {
37 console.error(err);
TheCodedProf5b53a8c2023-02-03 15:40:26 -050038 return { nsfw: false, errored: true };
pineafan3a02ea32022-08-11 21:35:04 +010039 });
Skyler Greyda16adf2023-03-05 10:22:12 +000040 if (!result.errored) {
TheCodedProf5b53a8c2023-02-03 15:40:26 -050041 client.database.scanCache.write(hash, result.nsfw);
42 }
pineafan3a02ea32022-08-11 21:35:04 +010043 return { nsfw: result.nsfw };
pineafan813bdf42022-07-24 10:39:10 +010044}
45
pineafan02ba0232022-07-24 22:16:15 +010046export async function testMalware(link: string): Promise<MalwareSchema> {
TheCodedProfb5e9d552023-01-29 15:43:26 -050047 const [p, hash] = await saveAttachment(link);
Skyler Greyda16adf2023-03-05 10:22:12 +000048 const alreadyHaveCheck = await client.database.scanCache.read(hash);
49 if (alreadyHaveCheck) return { safe: alreadyHaveCheck.data };
TheCodedProfb5e9d552023-01-29 15:43:26 -050050 const data = new URLSearchParams();
PineaFanb0d0c242023-02-05 10:59:45 +000051 const f = createReadStream(p);
TheCodedProfb5e9d552023-01-29 15:43:26 -050052 data.append("file", f.read(fs.statSync(p).size));
pineafan3a02ea32022-08-11 21:35:04 +010053 const result = await fetch("https://unscan.p.rapidapi.com/malware", {
54 method: "POST",
55 headers: {
56 "X-RapidAPI-Key": client.config.rapidApiKey,
57 "X-RapidAPI-Host": "unscan.p.rapidapi.com"
58 },
59 body: data
60 })
Skyler Greyda16adf2023-03-05 10:22:12 +000061 .then((response) =>
62 response.status === 200 ? (response.json() as Promise<MalwareSchema>) : { safe: true, errored: true }
63 )
pineafan3a02ea32022-08-11 21:35:04 +010064 .catch((err) => {
65 console.error(err);
TheCodedProf5b53a8c2023-02-03 15:40:26 -050066 return { safe: true, errored: true };
pineafan3a02ea32022-08-11 21:35:04 +010067 });
TheCodedProf5b53a8c2023-02-03 15:40:26 -050068 if (!result.errored) {
69 client.database.scanCache.write(hash, result.safe);
70 }
pineafan3a02ea32022-08-11 21:35:04 +010071 return { safe: result.safe };
72}
73
74export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> {
Skyler Greyda16adf2023-03-05 10:22:12 +000075 const alreadyHaveCheck = await client.database.scanCache.read(link);
76 if (alreadyHaveCheck) return { safe: alreadyHaveCheck.data, tags: [] };
TheCodedProfb5e9d552023-01-29 15:43:26 -050077 const scanned: { safe?: boolean; tags?: string[] } = await fetch("https://unscan.p.rapidapi.com/link", {
pineafan3a02ea32022-08-11 21:35:04 +010078 method: "POST",
79 headers: {
80 "X-RapidAPI-Key": client.config.rapidApiKey,
81 "X-RapidAPI-Host": "unscan.p.rapidapi.com"
82 },
83 body: `{"link":"${link}"}`
84 })
85 .then((response) => response.json() as Promise<MalwareSchema>)
86 .catch((err) => {
87 console.error(err);
88 return { safe: true, tags: [] };
89 });
TheCodedProfb5e9d552023-01-29 15:43:26 -050090 client.database.scanCache.write(link, scanned.safe ?? true, []);
pineafan3a02ea32022-08-11 21:35:04 +010091 return {
92 safe: scanned.safe ?? true,
93 tags: scanned.tags ?? []
94 };
pineafan813bdf42022-07-24 10:39:10 +010095}
96
TheCodedProfb5e9d552023-01-29 15:43:26 -050097export async function saveAttachment(link: string): Promise<[string, string]> {
Skyler Greyda16adf2023-03-05 10:22:12 +000098 const image = await (await fetch(link)).arrayBuffer();
Skyler Grey75ea9172022-08-06 10:22:23 +010099 const fileName = generateFileName(link.split("/").pop()!.split(".").pop()!);
TheCodedProf5b53a8c2023-02-03 15:40:26 -0500100 const enc = new TextDecoder("utf-8");
101 writeFileSync(fileName, new DataView(image), "base64");
Skyler Greyda16adf2023-03-05 10:22:12 +0000102 return [fileName, createHash("sha512").update(enc.decode(image), "base64").digest("base64")];
pineafan813bdf42022-07-24 10:39:10 +0100103}
104
pineafan813bdf42022-07-24 10:39:10 +0100105const linkTypes = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100106 PHISHING: "Links designed to trick users into clicking on them.",
107 DATING: "Dating sites.",
108 TRACKERS: "Websites that store or track personal information.",
109 ADVERTISEMENTS: "Websites only for ads.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100110 FACEBOOK: "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)",
Skyler Grey75ea9172022-08-06 10:22:23 +0100111 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 +0100112 "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100113 "IP GRABBERS": "Websites that store your IP address, which shows your approximate location.",
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 PORN: "Websites that include pornography.",
115 GAMBLING: "Gambling sites, often scams.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100116 MALWARE: "Websites which download files designed to break or slow down your device.",
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 PIRACY: "Sites which include illegally downloaded material.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100118 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 +0100119 REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.",
120 SCAMS: "Sites which are designed to trick you into doing something.",
121 TORRENT: "Websites that download torrent files.",
122 HATE: "Websites that spread hate towards groups or individuals.",
123 JUNK: "Websites that are designed to make you waste time."
pineafan63fc5e22022-08-04 22:04:10 +0100124};
pineafan813bdf42022-07-24 10:39:10 +0100125export { linkTypes };
126
pineafan63fc5e22022-08-04 22:04:10 +0100127export async function LinkCheck(message: Discord.Message): Promise<string[]> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100128 const links =
129 message.content.match(
130 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
131 ) ?? [];
132 const detections: { tags: string[]; safe: boolean }[] = [];
133 const promises: Promise<void>[] = links.map(async (element) => {
pineafan63fc5e22022-08-04 22:04:10 +0100134 let returned;
pineafan813bdf42022-07-24 10:39:10 +0100135 try {
Skyler Grey11236ba2022-08-08 21:13:33 +0100136 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 +0100137 returned = await testLink(element);
138 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 detections.push({ tags: [], safe: true });
pineafan63fc5e22022-08-04 22:04:10 +0100140 return;
141 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100142 detections.push({ tags: returned.tags, safe: returned.safe });
pineafan813bdf42022-07-24 10:39:10 +0100143 });
144 await Promise.all(promises);
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 const detectionsTypes = detections
146 .map((element) => {
Skyler Grey11236ba2022-08-08 21:13:33 +0100147 const type = Object.keys(linkTypes).find((type) => element.tags.includes(type));
Skyler Grey75ea9172022-08-06 10:22:23 +0100148 if (type) return type;
149 // if (!element.safe) return "UNSAFE"
150 return undefined;
151 })
152 .filter((element) => element !== undefined);
pineafan63fc5e22022-08-04 22:04:10 +0100153 return detectionsTypes as string[];
pineafan813bdf42022-07-24 10:39:10 +0100154}
155
pineafan63fc5e22022-08-04 22:04:10 +0100156export async function NSFWCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100157 try {
TheCodedProfb5e9d552023-01-29 15:43:26 -0500158 return (await testNSFW(element)).nsfw;
pineafan813bdf42022-07-24 10:39:10 +0100159 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100160 return false;
pineafan813bdf42022-07-24 10:39:10 +0100161 }
162}
163
Skyler Grey11236ba2022-08-08 21:13:33 +0100164export async function SizeCheck(element: { height: number | null; width: number | null }): Promise<boolean> {
pineafan63fc5e22022-08-04 22:04:10 +0100165 if (element.height === null || element.width === null) return true;
166 if (element.height < 20 || element.width < 20) return false;
167 return true;
pineafan813bdf42022-07-24 10:39:10 +0100168}
169
pineafan63fc5e22022-08-04 22:04:10 +0100170export async function MalwareCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100171 try {
pineafan63fc5e22022-08-04 22:04:10 +0100172 return (await testMalware(element)).safe;
pineafan813bdf42022-07-24 10:39:10 +0100173 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100174 return true;
pineafan813bdf42022-07-24 10:39:10 +0100175 }
176}
177
Skyler Grey11236ba2022-08-08 21:13:33 +0100178export function TestString(string: string, soft: string[], strict: string[]): object | null {
Skyler Grey75ea9172022-08-06 10:22:23 +0100179 for (const word of strict) {
pineafan813bdf42022-07-24 10:39:10 +0100180 if (string.toLowerCase().includes(word)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100181 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100182 }
183 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100184 for (const word of soft) {
185 for (const word2 of string.match(/[a-z]+/gi) ?? []) {
pineafane23c4ec2022-07-27 21:56:27 +0100186 if (word2 === word) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100187 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100188 }
189 }
190 }
pineafan63fc5e22022-08-04 22:04:10 +0100191 return null;
pineafan813bdf42022-07-24 10:39:10 +0100192}
193
pineafan63fc5e22022-08-04 22:04:10 +0100194export async function TestImage(url: string): Promise<string | null> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100195 const text = await Tesseract.recognize(url, {
196 lang: "eng",
197 oem: 1,
198 psm: 3
199 });
pineafan813bdf42022-07-24 10:39:10 +0100200 return text;
201}