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