pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 2 | // @ts-ignore |
| 3 | import * as us from "unscan"; |
| 4 | import fetch from "node-fetch"; |
| 5 | import { writeFileSync } from "fs"; |
| 6 | import generateFileName from "../utils/temp/generateFileName.js"; |
| 7 | import Tesseract from "node-tesseract-ocr"; |
| 8 | import type Discord from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 9 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 10 | interface NSFWSchema { nsfw: boolean } |
| 11 | interface MalwareSchema { safe: boolean } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 12 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 13 | export async function testNSFW(link: string): Promise<NSFWSchema> { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | const p = await saveAttachment(link); |
| 15 | const result = await us.nsfw.file(p); |
| 16 | return result; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 17 | } |
| 18 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 19 | export async function testMalware(link: string): Promise<MalwareSchema> { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | const p = await saveAttachment(link); |
| 21 | const result = await us.malware.file(p); |
| 22 | return result; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 23 | } |
| 24 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | export 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; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 30 | } |
| 31 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 32 | export async function testLink(link: string): Promise<{safe: boolean, tags: string[]}> { |
| 33 | return await us.link.scan(link); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | |
| 37 | const 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.", |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 55 | "JUNK": "Websites that are designed to make you waste time." |
| 56 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 57 | export { linkTypes }; |
| 58 | |
| 59 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 60 | export 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; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 65 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 66 | 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}); } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 74 | }); |
| 75 | await Promise.all(promises); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 76 | const detectionsTypes = detections.map(element => { |
| 77 | const type = Object.keys(linkTypes).find(type => element.tags.includes(type)); |
| 78 | if (type) return type; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 79 | // if (!element.safe) return "UNSAFE" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 80 | return undefined; |
| 81 | }).filter(element => element !== undefined); |
| 82 | return detectionsTypes as string[]; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 83 | } |
| 84 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 85 | export async function NSFWCheck(element: string): Promise<boolean> { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 86 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 87 | const test = (await testNSFW(element)); |
| 88 | return test.nsfw; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 89 | } catch { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 90 | return false; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 94 | export 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; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 98 | } |
| 99 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 100 | export async function MalwareCheck(element: string): Promise<boolean> { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 101 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | return (await testMalware(element)).safe; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 103 | } catch { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 104 | return true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 108 | export function TestString(string: string, soft: string[], strict: string[]): object | null { |
| 109 | for(const word of strict || []) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 110 | if (string.toLowerCase().includes(word)) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 111 | return {word: word, type: "strict"}; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 112 | } |
| 113 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 114 | for(const word of soft) { |
| 115 | for(const word2 of string.match(/[a-z]+/gi) || []) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 116 | if (word2 === word) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 117 | return {word: word, type: "strict"}; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 121 | return null; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 122 | } |
| 123 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 124 | export async function TestImage(url: string): Promise<string | null> { |
| 125 | const text = await Tesseract.recognize(url, {lang: "eng", oem: 1, psm: 3}); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 126 | return text; |
| 127 | } |