blob: c8d59f003bb0b189106d65a59401fbd107903a7c [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import fetch from "node-fetch";
pineafan3a02ea32022-08-11 21:35:04 +01002import FormData from "form-data";
TheCodedProfb5e9d552023-01-29 15:43:26 -05003import fs, { writeFileSync, createReadStream } from "fs";
pineafan63fc5e22022-08-04 22:04:10 +01004import generateFileName from "../utils/temp/generateFileName.js";
5import Tesseract from "node-tesseract-ocr";
6import type Discord from "discord.js";
pineafan3a02ea32022-08-11 21:35:04 +01007import client from "../utils/client.js";
TheCodedProfb5e9d552023-01-29 15:43:26 -05008import { createHash } from "crypto";
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> {
TheCodedProfb5e9d552023-01-29 15:43:26 -050018 const [p, hash] = await saveAttachment(link);
19 console.log("Checking an image")
20 let alreadyHaveCheck = await client.database.scanCache.read(hash)
21 if(alreadyHaveCheck) return { nsfw: alreadyHaveCheck.data };
22 console.log("Was not in db")
pineafan3a02ea32022-08-11 21:35:04 +010023 const data = new FormData();
24 console.log(link);
25 data.append("file", createReadStream(p));
26 const result = await fetch("https://unscan.p.rapidapi.com/", {
27 method: "POST",
28 headers: {
29 "X-RapidAPI-Key": client.config.rapidApiKey,
30 "X-RapidAPI-Host": "unscan.p.rapidapi.com"
31 },
32 body: data
33 })
34 .then((response) => response.json() as Promise<NSFWSchema>)
35 .catch((err) => {
36 console.error(err);
37 return { nsfw: false };
38 });
39 console.log(result);
TheCodedProfb5e9d552023-01-29 15:43:26 -050040 client.database.scanCache.write(hash, result.nsfw);
pineafan3a02ea32022-08-11 21:35:04 +010041 return { nsfw: result.nsfw };
pineafan813bdf42022-07-24 10:39:10 +010042}
43
pineafan02ba0232022-07-24 22:16:15 +010044export async function testMalware(link: string): Promise<MalwareSchema> {
TheCodedProfb5e9d552023-01-29 15:43:26 -050045 const [p, hash] = await saveAttachment(link);
46 let alreadyHaveCheck = await client.database.scanCache.read(hash)
47 if(alreadyHaveCheck) return { safe: alreadyHaveCheck.data };
48 const data = new URLSearchParams();
49 let f = createReadStream(p);
50 data.append("file", f.read(fs.statSync(p).size));
pineafan3a02ea32022-08-11 21:35:04 +010051 console.log(link);
52 const result = await fetch("https://unscan.p.rapidapi.com/malware", {
53 method: "POST",
54 headers: {
55 "X-RapidAPI-Key": client.config.rapidApiKey,
56 "X-RapidAPI-Host": "unscan.p.rapidapi.com"
57 },
58 body: data
59 })
60 .then((response) => response.json() as Promise<MalwareSchema>)
61 .catch((err) => {
62 console.error(err);
63 return { safe: true };
64 });
65 console.log(result);
TheCodedProfb5e9d552023-01-29 15:43:26 -050066 client.database.scanCache.write(hash, result.safe);
pineafan3a02ea32022-08-11 21:35:04 +010067 return { safe: result.safe };
68}
69
70export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> {
71 console.log(link);
TheCodedProfb5e9d552023-01-29 15:43:26 -050072 let alreadyHaveCheck = await client.database.scanCache.read(link)
73 if(alreadyHaveCheck) return { safe: alreadyHaveCheck.data, tags: [] };
74 const scanned: { safe?: boolean; tags?: string[] } = await fetch("https://unscan.p.rapidapi.com/link", {
pineafan3a02ea32022-08-11 21:35:04 +010075 method: "POST",
76 headers: {
77 "X-RapidAPI-Key": client.config.rapidApiKey,
78 "X-RapidAPI-Host": "unscan.p.rapidapi.com"
79 },
80 body: `{"link":"${link}"}`
81 })
82 .then((response) => response.json() as Promise<MalwareSchema>)
83 .catch((err) => {
84 console.error(err);
85 return { safe: true, tags: [] };
86 });
87 console.log(scanned);
TheCodedProfb5e9d552023-01-29 15:43:26 -050088 client.database.scanCache.write(link, scanned.safe ?? true, []);
pineafan3a02ea32022-08-11 21:35:04 +010089 return {
90 safe: scanned.safe ?? true,
91 tags: scanned.tags ?? []
92 };
pineafan813bdf42022-07-24 10:39:10 +010093}
94
TheCodedProfb5e9d552023-01-29 15:43:26 -050095export async function saveAttachment(link: string): Promise<[string, string]> {
TheCodedProfa112f612023-01-28 18:06:45 -050096 const image = (await fetch(link)).arrayBuffer().toString();
Skyler Grey75ea9172022-08-06 10:22:23 +010097 const fileName = generateFileName(link.split("/").pop()!.split(".").pop()!);
pineafan63fc5e22022-08-04 22:04:10 +010098 writeFileSync(fileName, image, "base64");
TheCodedProfb5e9d552023-01-29 15:43:26 -050099 return [fileName, createHash('sha512').update(image, 'base64').digest('base64')];
pineafan813bdf42022-07-24 10:39:10 +0100100}
101
pineafan813bdf42022-07-24 10:39:10 +0100102const linkTypes = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 PHISHING: "Links designed to trick users into clicking on them.",
104 DATING: "Dating sites.",
105 TRACKERS: "Websites that store or track personal information.",
106 ADVERTISEMENTS: "Websites only for ads.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100107 FACEBOOK: "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)",
Skyler Grey75ea9172022-08-06 10:22:23 +0100108 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 +0100109 "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100110 "IP GRABBERS": "Websites that store your IP address, which shows your approximate location.",
Skyler Grey75ea9172022-08-06 10:22:23 +0100111 PORN: "Websites that include pornography.",
112 GAMBLING: "Gambling sites, often scams.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100113 MALWARE: "Websites which download files designed to break or slow down your device.",
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 PIRACY: "Sites which include illegally downloaded material.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100115 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 +0100116 REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.",
117 SCAMS: "Sites which are designed to trick you into doing something.",
118 TORRENT: "Websites that download torrent files.",
119 HATE: "Websites that spread hate towards groups or individuals.",
120 JUNK: "Websites that are designed to make you waste time."
pineafan63fc5e22022-08-04 22:04:10 +0100121};
pineafan813bdf42022-07-24 10:39:10 +0100122export { linkTypes };
123
pineafan63fc5e22022-08-04 22:04:10 +0100124export async function LinkCheck(message: Discord.Message): Promise<string[]> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100125 const links =
126 message.content.match(
127 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
128 ) ?? [];
129 const detections: { tags: string[]; safe: boolean }[] = [];
130 const promises: Promise<void>[] = links.map(async (element) => {
pineafan63fc5e22022-08-04 22:04:10 +0100131 let returned;
pineafan813bdf42022-07-24 10:39:10 +0100132 try {
Skyler Grey11236ba2022-08-08 21:13:33 +0100133 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 +0100134 returned = await testLink(element);
135 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +0100136 detections.push({ tags: [], safe: true });
pineafan63fc5e22022-08-04 22:04:10 +0100137 return;
138 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 detections.push({ tags: returned.tags, safe: returned.safe });
pineafan813bdf42022-07-24 10:39:10 +0100140 });
141 await Promise.all(promises);
Skyler Grey75ea9172022-08-06 10:22:23 +0100142 const detectionsTypes = detections
143 .map((element) => {
Skyler Grey11236ba2022-08-08 21:13:33 +0100144 const type = Object.keys(linkTypes).find((type) => element.tags.includes(type));
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 if (type) return type;
146 // if (!element.safe) return "UNSAFE"
147 return undefined;
148 })
149 .filter((element) => element !== undefined);
pineafan63fc5e22022-08-04 22:04:10 +0100150 return detectionsTypes as string[];
pineafan813bdf42022-07-24 10:39:10 +0100151}
152
pineafan63fc5e22022-08-04 22:04:10 +0100153export async function NSFWCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100154 try {
TheCodedProfb5e9d552023-01-29 15:43:26 -0500155 return (await testNSFW(element)).nsfw;
pineafan813bdf42022-07-24 10:39:10 +0100156 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100157 return false;
pineafan813bdf42022-07-24 10:39:10 +0100158 }
159}
160
Skyler Grey11236ba2022-08-08 21:13:33 +0100161export async function SizeCheck(element: { height: number | null; width: number | null }): Promise<boolean> {
pineafan63fc5e22022-08-04 22:04:10 +0100162 if (element.height === null || element.width === null) return true;
163 if (element.height < 20 || element.width < 20) return false;
164 return true;
pineafan813bdf42022-07-24 10:39:10 +0100165}
166
pineafan63fc5e22022-08-04 22:04:10 +0100167export async function MalwareCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100168 try {
pineafan63fc5e22022-08-04 22:04:10 +0100169 return (await testMalware(element)).safe;
pineafan813bdf42022-07-24 10:39:10 +0100170 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100171 return true;
pineafan813bdf42022-07-24 10:39:10 +0100172 }
173}
174
Skyler Grey11236ba2022-08-08 21:13:33 +0100175export function TestString(string: string, soft: string[], strict: string[]): object | null {
Skyler Grey75ea9172022-08-06 10:22:23 +0100176 for (const word of strict) {
pineafan813bdf42022-07-24 10:39:10 +0100177 if (string.toLowerCase().includes(word)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100178 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100179 }
180 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100181 for (const word of soft) {
182 for (const word2 of string.match(/[a-z]+/gi) ?? []) {
pineafane23c4ec2022-07-27 21:56:27 +0100183 if (word2 === word) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100184 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100185 }
186 }
187 }
pineafan63fc5e22022-08-04 22:04:10 +0100188 return null;
pineafan813bdf42022-07-24 10:39:10 +0100189}
190
pineafan63fc5e22022-08-04 22:04:10 +0100191export async function TestImage(url: string): Promise<string | null> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100192 const text = await Tesseract.recognize(url, {
193 lang: "eng",
194 oem: 1,
195 psm: 3
196 });
pineafan813bdf42022-07-24 10:39:10 +0100197 return text;
198}