blob: 8ea22f72a58de70e9fbb3e7948960f01496d94bc [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
7
8export async function testNSFW(link: string): Promise<JSON> {
9 let p = await saveAttachment(link)
10 let result = await us.nsfw.file(p)
11 return result
12}
13
14export async function testMalware(link: string): Promise<JSON> {
15 let p = await saveAttachment(link)
16 let result = await us.malware.file(p)
17 return result
18}
19
20export async function saveAttachment(link): Promise<string> {
21 const image = (await (await fetch(link)).buffer()).toString('base64')
22 let fileName = generateFileName(link.split('/').pop().split('.').pop())
23 writeFileSync(fileName, image, 'base64')
24 return fileName
25}
26
27export async function testLink(link: string): Promise<JSON> {
28 return await us.link.scan(link)
29}
30
31
32const linkTypes = {
33 "PHISHING": "Links designed to trick users into clicking on them.",
34 "DATING": "Dating sites.",
35 "TRACKERS": "Websites that store or track personal information.",
36 "ADVERTISEMENTS": "Websites only for ads.",
37 "FACEBOOK": "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)",
38 "AMP": "AMP pages. (AMP is a technology that allows websites to be served by Google. Read more on /privacy)",
39 "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.",
40 "IP GRABBERS": "Websites that store your IP address, which shows your approximate location.",
41 "PORN": "Websites that include pornography.",
42 "GAMBLING": "Gambling sites, often scams.",
43 "MALWARE": "Websites which download files designed to break or slow down your device.",
44 "PIRACY": "Sites which include illegally downloaded material.",
45 "RANSOMWARE": "Websites which download a program that can steal your data and make you pay to get it back.",
46 "REDIRECTS": "Sites like bit.ly which could redirect to a malicious site.",
47 "SCAMS": "Sites which are designed to trick you into doing something.",
48 "TORRENT": "Websites that download torrent files.",
49 "HATE": "Websites that spread hate towards groups or individuals.",
50 "JUNK": "Websites that are designed to make you waste time.",
51}
52export { linkTypes };
53
54
55export async function LinkCheck(message): Promise<string[]> {
56 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) ?? []
57 let detections = []
58 const promises = links.map(async element => {
59 try {
60 if (element.match(/https?:\/\/[a-zA-Z]+\.?discord(app)?\.(com|net)\/?/)) return // Also matches discord.net, not enough of a bug
61 element = await testLink(element)
62 } catch {}
63 detections.push({tags: element.tags || [], safe: element.safe})
64 });
65 await Promise.all(promises);
66 let detectionsTypes = detections.map(element => {
67 let type = Object.keys(linkTypes).find(type => element.tags.includes(type))
68 if (type) return type
69 // if (!element.safe) return "UNSAFE"
70 return undefined
71 }).filter(element => element !== undefined)
72 return detectionsTypes
73}
74
75export async function NSFWCheck(element): Promise<boolean> {
76 try {
77 let test = (await testNSFW(element))
78 //@ts-ignore
79 return test.nsfw
80 } catch {
81 return false
82 }
83}
84
85export async function SizeCheck(element): Promise<boolean> {
86 if (element.height == undefined || element.width == undefined) return true
87 if (element.height < 20 || element.width < 20) return false
88 return true
89}
90
91export async function MalwareCheck(element): Promise<boolean> {
92 try {
93 //@ts-ignore
94 return (await scan.testMalware(element)).safe
95 } 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) || []) {
108 if (word2 == word) {
109 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}