blob: 435ce4c204158abf29840f6b4bcbcca951a31ae1 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2// @ts-ignore
3import * 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
pineafan02ba0232022-07-24 22:16:15 +010010interface NSFWSchema { nsfw: boolean }
11interface MalwareSchema { safe: boolean }
pineafan813bdf42022-07-24 10:39:10 +010012
pineafan02ba0232022-07-24 22:16:15 +010013export async function testNSFW(link: string): Promise<NSFWSchema> {
pineafan63fc5e22022-08-04 22:04:10 +010014 const p = await saveAttachment(link);
15 const result = await us.nsfw.file(p);
16 return result;
pineafan813bdf42022-07-24 10:39:10 +010017}
18
pineafan02ba0232022-07-24 22:16:15 +010019export async function testMalware(link: string): Promise<MalwareSchema> {
pineafan63fc5e22022-08-04 22:04:10 +010020 const p = await saveAttachment(link);
21 const result = await us.malware.file(p);
22 return result;
pineafan813bdf42022-07-24 10:39:10 +010023}
24
pineafan63fc5e22022-08-04 22:04:10 +010025export async function saveAttachment(link: string): Promise<string> {
26 const image = (await (await fetch(link)).buffer()).toString("base64");
27 const fileName = generateFileName((link.split("/").pop() as string).split(".").pop() as string);
28 writeFileSync(fileName, image, "base64");
29 return fileName;
pineafan813bdf42022-07-24 10:39:10 +010030}
31
pineafan63fc5e22022-08-04 22:04:10 +010032export async function testLink(link: string): Promise<{safe: boolean, tags: string[]}> {
33 return await us.link.scan(link);
pineafan813bdf42022-07-24 10:39:10 +010034}
35
36
37const linkTypes = {
38 "PHISHING": "Links designed to trick users into clicking on them.",
39 "DATING": "Dating sites.",
40 "TRACKERS": "Websites that store or track personal information.",
41 "ADVERTISEMENTS": "Websites only for ads.",
42 "FACEBOOK": "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)",
43 "AMP": "AMP pages. (AMP is a technology that allows websites to be served by Google. Read more on /privacy)",
44 "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.",
45 "IP GRABBERS": "Websites that store your IP address, which shows your approximate location.",
46 "PORN": "Websites that include pornography.",
47 "GAMBLING": "Gambling sites, often scams.",
48 "MALWARE": "Websites which download files designed to break or slow down your device.",
49 "PIRACY": "Sites which include illegally downloaded material.",
50 "RANSOMWARE": "Websites which download a program that can steal your data and make you pay to get it back.",
51 "REDIRECTS": "Sites like bit.ly which could redirect to a malicious site.",
52 "SCAMS": "Sites which are designed to trick you into doing something.",
53 "TORRENT": "Websites that download torrent files.",
54 "HATE": "Websites that spread hate towards groups or individuals.",
pineafan63fc5e22022-08-04 22:04:10 +010055 "JUNK": "Websites that are designed to make you waste time."
56};
pineafan813bdf42022-07-24 10:39:10 +010057export { linkTypes };
58
59
pineafan63fc5e22022-08-04 22:04:10 +010060export async function LinkCheck(message: Discord.Message): Promise<string[]> {
61 const links = message.content.match(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi) ?? [];
62 const detections: {tags: string[], safe: boolean}[] = [];
63 const promises: Promise<void>[] = links.map(async element => {
64 let returned;
pineafan813bdf42022-07-24 10:39:10 +010065 try {
pineafan63fc5e22022-08-04 22:04:10 +010066 if (element.match(/https?:\/\/[a-zA-Z]+\.?discord(app)?\.(com|net)\/?/)) return; // Also matches discord.net, not enough of a bug
67 returned = await testLink(element);
68 } catch {
69 detections.push({tags: [], safe: true});
70 return;
71 }
72 if (returned) { detections.push({tags: returned.tags || [], safe: returned.safe}); }
73 else { detections.push({tags: [], safe: true}); }
pineafan813bdf42022-07-24 10:39:10 +010074 });
75 await Promise.all(promises);
pineafan63fc5e22022-08-04 22:04:10 +010076 const detectionsTypes = detections.map(element => {
77 const type = Object.keys(linkTypes).find(type => element.tags.includes(type));
78 if (type) return type;
pineafan813bdf42022-07-24 10:39:10 +010079 // if (!element.safe) return "UNSAFE"
pineafan63fc5e22022-08-04 22:04:10 +010080 return undefined;
81 }).filter(element => element !== undefined);
82 return detectionsTypes as string[];
pineafan813bdf42022-07-24 10:39:10 +010083}
84
pineafan63fc5e22022-08-04 22:04:10 +010085export async function NSFWCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +010086 try {
pineafan63fc5e22022-08-04 22:04:10 +010087 const test = (await testNSFW(element));
88 return test.nsfw;
pineafan813bdf42022-07-24 10:39:10 +010089 } catch {
pineafan63fc5e22022-08-04 22:04:10 +010090 return false;
pineafan813bdf42022-07-24 10:39:10 +010091 }
92}
93
pineafan63fc5e22022-08-04 22:04:10 +010094export async function SizeCheck(element: {height: number | null, width: number | null}): Promise<boolean> {
95 if (element.height === null || element.width === null) return true;
96 if (element.height < 20 || element.width < 20) return false;
97 return true;
pineafan813bdf42022-07-24 10:39:10 +010098}
99
pineafan63fc5e22022-08-04 22:04:10 +0100100export async function MalwareCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100101 try {
pineafan63fc5e22022-08-04 22:04:10 +0100102 return (await testMalware(element)).safe;
pineafan813bdf42022-07-24 10:39:10 +0100103 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100104 return true;
pineafan813bdf42022-07-24 10:39:10 +0100105 }
106}
107
pineafan63fc5e22022-08-04 22:04:10 +0100108export function TestString(string: string, soft: string[], strict: string[]): object | null {
109 for(const word of strict || []) {
pineafan813bdf42022-07-24 10:39:10 +0100110 if (string.toLowerCase().includes(word)) {
pineafan63fc5e22022-08-04 22:04:10 +0100111 return {word: word, type: "strict"};
pineafan813bdf42022-07-24 10:39:10 +0100112 }
113 }
pineafan63fc5e22022-08-04 22:04:10 +0100114 for(const word of soft) {
115 for(const word2 of string.match(/[a-z]+/gi) || []) {
pineafane23c4ec2022-07-27 21:56:27 +0100116 if (word2 === word) {
pineafan63fc5e22022-08-04 22:04:10 +0100117 return {word: word, type: "strict"};
pineafan813bdf42022-07-24 10:39:10 +0100118 }
119 }
120 }
pineafan63fc5e22022-08-04 22:04:10 +0100121 return null;
pineafan813bdf42022-07-24 10:39:10 +0100122}
123
pineafan63fc5e22022-08-04 22:04:10 +0100124export async function TestImage(url: string): Promise<string | null> {
125 const text = await Tesseract.recognize(url, {lang: "eng", oem: 1, psm: 3});
pineafan813bdf42022-07-24 10:39:10 +0100126 return text;
127}