pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import fetch from "node-fetch"; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 2 | import FormData from "form-data"; |
| 3 | import { writeFileSync, createReadStream } from "fs"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 4 | import generateFileName from "../utils/temp/generateFileName.js"; |
| 5 | import Tesseract from "node-tesseract-ocr"; |
| 6 | import type Discord from "discord.js"; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 7 | import client from "../utils/client.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); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 18 | const data = new FormData(); |
| 19 | console.log(link); |
| 20 | data.append("file", createReadStream(p)); |
| 21 | const result = await fetch("https://unscan.p.rapidapi.com/", { |
| 22 | method: "POST", |
| 23 | headers: { |
| 24 | "X-RapidAPI-Key": client.config.rapidApiKey, |
| 25 | "X-RapidAPI-Host": "unscan.p.rapidapi.com" |
| 26 | }, |
| 27 | body: data |
| 28 | }) |
| 29 | .then((response) => response.json() as Promise<NSFWSchema>) |
| 30 | .catch((err) => { |
| 31 | console.error(err); |
| 32 | return { nsfw: false }; |
| 33 | }); |
| 34 | console.log(result); |
| 35 | return { nsfw: result.nsfw }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 36 | } |
| 37 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 38 | export async function testMalware(link: string): Promise<MalwareSchema> { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | const p = await saveAttachment(link); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 40 | const data = new FormData(); |
| 41 | data.append("file", createReadStream(p)); |
| 42 | console.log(link); |
| 43 | const result = await fetch("https://unscan.p.rapidapi.com/malware", { |
| 44 | method: "POST", |
| 45 | headers: { |
| 46 | "X-RapidAPI-Key": client.config.rapidApiKey, |
| 47 | "X-RapidAPI-Host": "unscan.p.rapidapi.com" |
| 48 | }, |
| 49 | body: data |
| 50 | }) |
| 51 | .then((response) => response.json() as Promise<MalwareSchema>) |
| 52 | .catch((err) => { |
| 53 | console.error(err); |
| 54 | return { safe: true }; |
| 55 | }); |
| 56 | console.log(result); |
| 57 | return { safe: result.safe }; |
| 58 | } |
| 59 | |
| 60 | export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> { |
| 61 | console.log(link); |
| 62 | const scanned: { safe?: boolean; tags?: string[] } = await fetch("https://unscan.p.rapidapi.com/malware", { |
| 63 | method: "POST", |
| 64 | headers: { |
| 65 | "X-RapidAPI-Key": client.config.rapidApiKey, |
| 66 | "X-RapidAPI-Host": "unscan.p.rapidapi.com" |
| 67 | }, |
| 68 | body: `{"link":"${link}"}` |
| 69 | }) |
| 70 | .then((response) => response.json() as Promise<MalwareSchema>) |
| 71 | .catch((err) => { |
| 72 | console.error(err); |
| 73 | return { safe: true, tags: [] }; |
| 74 | }); |
| 75 | console.log(scanned); |
| 76 | return { |
| 77 | safe: scanned.safe ?? true, |
| 78 | tags: scanned.tags ?? [] |
| 79 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 80 | } |
| 81 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 82 | export async function saveAttachment(link: string): Promise<string> { |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame^] | 83 | const image = (await fetch(link)).arrayBuffer().toString(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 84 | const fileName = generateFileName(link.split("/").pop()!.split(".").pop()!); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 85 | writeFileSync(fileName, image, "base64"); |
| 86 | return fileName; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 87 | } |
| 88 | |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 89 | const linkTypes = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 90 | PHISHING: "Links designed to trick users into clicking on them.", |
| 91 | DATING: "Dating sites.", |
| 92 | TRACKERS: "Websites that store or track personal information.", |
| 93 | ADVERTISEMENTS: "Websites only for ads.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 94 | 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] | 95 | 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] | 96 | "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 97 | "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] | 98 | PORN: "Websites that include pornography.", |
| 99 | GAMBLING: "Gambling sites, often scams.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 100 | 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] | 101 | PIRACY: "Sites which include illegally downloaded material.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 102 | 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] | 103 | REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.", |
| 104 | SCAMS: "Sites which are designed to trick you into doing something.", |
| 105 | TORRENT: "Websites that download torrent files.", |
| 106 | HATE: "Websites that spread hate towards groups or individuals.", |
| 107 | JUNK: "Websites that are designed to make you waste time." |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 108 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 109 | export { linkTypes }; |
| 110 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 111 | export async function LinkCheck(message: Discord.Message): Promise<string[]> { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 112 | const links = |
| 113 | message.content.match( |
| 114 | /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi |
| 115 | ) ?? []; |
| 116 | const detections: { tags: string[]; safe: boolean }[] = []; |
| 117 | const promises: Promise<void>[] = links.map(async (element) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 118 | let returned; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 119 | try { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 120 | 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] | 121 | returned = await testLink(element); |
| 122 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 123 | detections.push({ tags: [], safe: true }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 124 | return; |
| 125 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 126 | detections.push({ tags: returned.tags, safe: returned.safe }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 127 | }); |
| 128 | await Promise.all(promises); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 129 | const detectionsTypes = detections |
| 130 | .map((element) => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 131 | const type = Object.keys(linkTypes).find((type) => element.tags.includes(type)); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 132 | if (type) return type; |
| 133 | // if (!element.safe) return "UNSAFE" |
| 134 | return undefined; |
| 135 | }) |
| 136 | .filter((element) => element !== undefined); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 137 | return detectionsTypes as string[]; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 138 | } |
| 139 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 140 | export async function NSFWCheck(element: string): Promise<boolean> { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 141 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 142 | const test = await testNSFW(element); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 143 | return test.nsfw; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 144 | } catch { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 145 | return false; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 149 | export async function SizeCheck(element: { height: number | null; width: number | null }): Promise<boolean> { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 150 | if (element.height === null || element.width === null) return true; |
| 151 | if (element.height < 20 || element.width < 20) return false; |
| 152 | return true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 153 | } |
| 154 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 155 | export async function MalwareCheck(element: string): Promise<boolean> { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 156 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 157 | return (await testMalware(element)).safe; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 158 | } catch { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 159 | return true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 163 | export function TestString(string: string, soft: string[], strict: string[]): object | null { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 164 | for (const word of strict) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 165 | if (string.toLowerCase().includes(word)) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 166 | return { word: word, type: "strict" }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 167 | } |
| 168 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 169 | for (const word of soft) { |
| 170 | for (const word2 of string.match(/[a-z]+/gi) ?? []) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 171 | if (word2 === word) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 172 | return { word: word, type: "strict" }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 176 | return null; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 177 | } |
| 178 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 179 | export async function TestImage(url: string): Promise<string | null> { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 180 | const text = await Tesseract.recognize(url, { |
| 181 | lang: "eng", |
| 182 | oem: 1, |
| 183 | psm: 3 |
| 184 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 185 | return text; |
| 186 | } |