From d8ef1f3d13cf241630cab8051f32ea40b8065ae8 Mon Sep 17 00:00:00 2001 From: TheCodedProf Date: Mon, 6 Mar 2023 19:15:18 -0500 Subject: [PATCH] started fix on NSFW & Malware checking (using tfjs-node + NSFWJS & clamscan) --- package.json | 17 +++++++++++------ src/reflex/scanners.ts | 35 ++++++++++++----------------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 2f63ea6..6457877 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,16 @@ { "dependencies": { - "@discordjs/rest": "^0.2.0-canary.0", "@hokify/agenda": "^6.2.12", + "@tensorflow/tfjs": "^4.0.0", + "@tensorflow/tfjs-node": "^4.2.0", "@total-typescript/ts-reset": "^0.3.7", "@tsconfig/node18-strictest-esm": "^1.0.0", "@types/node": "^18.14.6", "@ungap/structured-clone": "^1.0.1", "agenda": "^4.3.0", "body-parser": "^1.20.0", + "canvas": "^2.11.0", + "clamscan": "^2.1.2", "discord.js": "^14.7.1", "eslint": "^8.21.0", "express": "^4.18.1", @@ -18,12 +21,11 @@ "mongodb": "^4.7.0", "node-fetch": "^3.3.0", "node-tesseract-ocr": "^2.2.1", + "nsfwjs": "2.4.2", + "seedrandom": "^3.0.5", "structured-clone": "^0.2.2", "systeminformation": "^5.17.3" }, - "resolutions": { - "discord-api-types": "0.37.23" - }, "name": "nucleus", "version": "0.0.1", "description": "Nucleus: The core of your server", @@ -38,7 +40,8 @@ "lint-list": "echo 'Style checking...'; prettier --check .; echo 'Linting...'; eslint src; echo 'To view errors in more detail, please run `yarn lint`'; true", "lint-ci": "echo 'Style checking...' && prettier --check . && echo 'Linting...' && eslint src", "setup": "node Installer.js", - "win-force-build": "clear | rm -r dist | tsc-suppress" + "win-force-build": "clear | rm -r dist | tsc-suppress", + "audit-fix": "yarn-audit-fix" }, "repository": { "type": "git", @@ -57,6 +60,7 @@ "private": false, "type": "module", "devDependencies": { + "@types/clamscan": "^2.0.4", "@types/lodash": "^4.14.191", "@typescript-eslint/eslint-plugin": "^5.32.0", "@typescript-eslint/parser": "^5.32.0", @@ -64,6 +68,7 @@ "prettier": "^2.7.1", "prettier-eslint": "^15.0.1", "tsc-suppress": "^1.0.7", - "typescript": "^4.9.4" + "typescript": "^4.9.4", + "yarn-audit-fix": "^9.3.9" } } diff --git a/src/reflex/scanners.ts b/src/reflex/scanners.ts index 71e63b4..450a4af 100644 --- a/src/reflex/scanners.ts +++ b/src/reflex/scanners.ts @@ -5,6 +5,9 @@ import Tesseract from "node-tesseract-ocr"; import type Discord from "discord.js"; import client from "../utils/client.js"; import { createHash } from "crypto"; +import * as nsfwjs from 'nsfwjs'; +import * as clamscan from 'clamscan' +import * as tf from "@tensorflow/tfjs-node"; interface NSFWSchema { nsfw: boolean; @@ -15,32 +18,18 @@ interface MalwareSchema { errored?: boolean; } +const model = await nsfwjs.load(); + export async function testNSFW(link: string): Promise { - const [p, hash] = await saveAttachment(link); + const [fileName, hash] = await saveAttachment(link); const alreadyHaveCheck = await client.database.scanCache.read(hash); if (alreadyHaveCheck) return { nsfw: alreadyHaveCheck.data }; - const data = new URLSearchParams(); - const r = createReadStream(p); - data.append("file", r.read(fs.statSync(p).size)); - const result = await fetch("https://unscan.p.rapidapi.com/", { - method: "POST", - headers: { - "X-RapidAPI-Key": client.config.rapidApiKey, - "X-RapidAPI-Host": "unscan.p.rapidapi.com" - }, - body: data - }) - .then((response) => - response.status === 200 ? (response.json() as Promise) : { nsfw: false, errored: true } - ) - .catch((err) => { - console.error(err); - return { nsfw: false, errored: true }; - }); - if (!result.errored) { - client.database.scanCache.write(hash, result.nsfw); - } - return { nsfw: result.nsfw }; + + // const image = tf.node.decodePng() + + // const result = await model.classify(image) + + return { nsfw: false }; } export async function testMalware(link: string): Promise {