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