blob: 3b808162578e9ead6d026941a889f6c54bfb6809 [file] [log] [blame]
pineafan813bdf42022-07-24 10:39:10 +01001import * as us from 'unscan'
2import fetch from 'node-fetch'
3import { writeFileSync } from 'fs'
4import generateFileName from '../utils/temp/generateFileName.js'
5import Tesseract from 'node-tesseract-ocr';
6
pineafan02ba0232022-07-24 22:16:15 +01007interface NSFWSchema { nsfw: boolean }
8interface MalwareSchema { safe: boolean }
pineafan813bdf42022-07-24 10:39:10 +01009
pineafan02ba0232022-07-24 22:16:15 +010010export async function testNSFW(link: string): Promise<NSFWSchema> {
pineafan813bdf42022-07-24 10:39:10 +010011 let p = await saveAttachment(link)
12 let result = await us.nsfw.file(p)
13 return result
14}
15
pineafan02ba0232022-07-24 22:16:15 +010016export async function testMalware(link: string): Promise<MalwareSchema> {
pineafan813bdf42022-07-24 10:39:10 +010017 let p = await saveAttachment(link)
18 let result = await us.malware.file(p)
19 return result
20}
21
22export 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
pineafan02ba0232022-07-24 22:16:15 +010029export async function testLink(link: string): Promise<unknown> {
pineafan813bdf42022-07-24 10:39:10 +010030 return await us.link.scan(link)
31}
32
33
34const 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}
54export { linkTypes };
55
56
57export 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
77export async function NSFWCheck(element): Promise<boolean> {
78 try {
79 let test = (await testNSFW(element))
pineafan813bdf42022-07-24 10:39:10 +010080 return test.nsfw
81 } catch {
82 return false
83 }
84}
85
86export async function SizeCheck(element): Promise<boolean> {
pineafane23c4ec2022-07-27 21:56:27 +010087 if (element.height === undefined || element.width === undefined) return true
pineafan813bdf42022-07-24 10:39:10 +010088 if (element.height < 20 || element.width < 20) return false
89 return true
90}
91
92export async function MalwareCheck(element): Promise<boolean> {
93 try {
pineafan02ba0232022-07-24 22:16:15 +010094 return (await testMalware(element)).safe
pineafan813bdf42022-07-24 10:39:10 +010095 } catch {
96 return true
97 }
98}
99
100export 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) || []) {
pineafane23c4ec2022-07-27 21:56:27 +0100108 if (word2 === word) {
pineafan813bdf42022-07-24 10:39:10 +0100109 return {word: word, type: "strict"}
110 }
111 }
112 }
113 return null
114}
115
116export async function TestImage(url): Promise<string | null> {
117 let text = await Tesseract.recognize(url, {lang: "eng", oem: 1, psm: 3})
118 return text;
119}