Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 1 | // @ts-expect-error |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 2 | import * as us from "unscan"; |
| 3 | import fetch from "node-fetch"; |
| 4 | import { writeFileSync } from "fs"; |
| 5 | import generateFileName from "../utils/temp/generateFileName.js"; |
| 6 | import Tesseract from "node-tesseract-ocr"; |
| 7 | import type Discord from "discord.js"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 8 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 9 | interface NSFWSchema { |
| 10 | nsfw: boolean; |
| 11 | } |
| 12 | interface MalwareSchema { |
| 13 | safe: boolean; |
| 14 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 15 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 16 | export async function testNSFW(link: string): Promise<NSFWSchema> { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | const p = await saveAttachment(link); |
| 18 | const result = await us.nsfw.file(p); |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 19 | return { nsfw: result.nsfw ?? false }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 20 | } |
| 21 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 22 | export async function testMalware(link: string): Promise<MalwareSchema> { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | const p = await saveAttachment(link); |
| 24 | const result = await us.malware.file(p); |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 25 | return { safe: result.safe ?? true }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 26 | } |
| 27 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 28 | export async function saveAttachment(link: string): Promise<string> { |
| 29 | const image = (await (await fetch(link)).buffer()).toString("base64"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 30 | const fileName = generateFileName(link.split("/").pop()!.split(".").pop()!); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | writeFileSync(fileName, image, "base64"); |
| 32 | return fileName; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 33 | } |
| 34 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 35 | const defaultLinkTestResult: { safe: boolean; tags: string[] } = { |
| 36 | safe: true, |
| 37 | tags: [] |
| 38 | }; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 39 | export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> { |
| 40 | const scanned: { safe?: boolean; tags?: string[] } | undefined = await us.link.scan(link); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 41 | if (scanned === undefined) return defaultLinkTestResult; |
| 42 | return { |
| 43 | safe: scanned.safe ?? defaultLinkTestResult.safe, |
| 44 | tags: scanned.tags ?? defaultLinkTestResult.tags |
| 45 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 46 | } |
| 47 | |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 48 | const linkTypes = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 49 | PHISHING: "Links designed to trick users into clicking on them.", |
| 50 | DATING: "Dating sites.", |
| 51 | TRACKERS: "Websites that store or track personal information.", |
| 52 | ADVERTISEMENTS: "Websites only for ads.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 53 | FACEBOOK: "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 54 | AMP: "AMP pages. (AMP is a technology that allows websites to be served by Google. Read more on /privacy)", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 55 | "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 56 | "IP GRABBERS": "Websites that store your IP address, which shows your approximate location.", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 57 | PORN: "Websites that include pornography.", |
| 58 | GAMBLING: "Gambling sites, often scams.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 59 | MALWARE: "Websites which download files designed to break or slow down your device.", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | PIRACY: "Sites which include illegally downloaded material.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 61 | RANSOMWARE: "Websites which download a program that can steal your data and make you pay to get it back.", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 62 | REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.", |
| 63 | SCAMS: "Sites which are designed to trick you into doing something.", |
| 64 | TORRENT: "Websites that download torrent files.", |
| 65 | HATE: "Websites that spread hate towards groups or individuals.", |
| 66 | JUNK: "Websites that are designed to make you waste time." |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 67 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 68 | export { linkTypes }; |
| 69 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | export async function LinkCheck(message: Discord.Message): Promise<string[]> { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 71 | const links = |
| 72 | message.content.match( |
| 73 | /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi |
| 74 | ) ?? []; |
| 75 | const detections: { tags: string[]; safe: boolean }[] = []; |
| 76 | const promises: Promise<void>[] = links.map(async (element) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 77 | let returned; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 78 | try { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 79 | if (element.match(/https?:\/\/[a-zA-Z]+\.?discord(app)?\.(com|net)\/?/)) return; // Also matches discord.net, not enough of a bug |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 80 | returned = await testLink(element); |
| 81 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | detections.push({ tags: [], safe: true }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 83 | return; |
| 84 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 85 | detections.push({ tags: returned.tags, safe: returned.safe }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 86 | }); |
| 87 | await Promise.all(promises); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 88 | const detectionsTypes = detections |
| 89 | .map((element) => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 90 | const type = Object.keys(linkTypes).find((type) => element.tags.includes(type)); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 91 | if (type) return type; |
| 92 | // if (!element.safe) return "UNSAFE" |
| 93 | return undefined; |
| 94 | }) |
| 95 | .filter((element) => element !== undefined); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 96 | return detectionsTypes as string[]; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 97 | } |
| 98 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 99 | export async function NSFWCheck(element: string): Promise<boolean> { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 100 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 101 | const test = await testNSFW(element); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | return test.nsfw; |
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 false; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 108 | export async function SizeCheck(element: { height: number | null; width: number | null }): Promise<boolean> { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 109 | if (element.height === null || element.width === null) return true; |
| 110 | if (element.height < 20 || element.width < 20) return false; |
| 111 | return true; |
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 | export async function MalwareCheck(element: string): Promise<boolean> { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 115 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 116 | return (await testMalware(element)).safe; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 117 | } catch { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 118 | return true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 122 | export function TestString(string: string, soft: string[], strict: string[]): object | null { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 123 | for (const word of strict) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 124 | if (string.toLowerCase().includes(word)) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 125 | return { word: word, type: "strict" }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 128 | for (const word of soft) { |
| 129 | for (const word2 of string.match(/[a-z]+/gi) ?? []) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 130 | if (word2 === word) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 131 | return { word: word, type: "strict" }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 135 | return null; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 136 | } |
| 137 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 138 | export async function TestImage(url: string): Promise<string | null> { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 139 | const text = await Tesseract.recognize(url, { |
| 140 | lang: "eng", |
| 141 | oem: 1, |
| 142 | psm: 3 |
| 143 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 144 | return text; |
| 145 | } |