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