/* eslint-disable no-console */ import type NodeClamClass from "clamscan"; import FileInfectedError from "../../FileInfectedError.js"; import sleep from "../../../../common/sleep.js"; import LockFile from "../../LockFile.js"; import { LocalFile } from "@entity-access/server-pages/dist/core/LocalFile.js"; import { globalEnv } from "../../../../common/globalEnv.js"; import TempFolder from "@entity-access/server-pages/dist/core/TempFolder.js"; import SevenZip from "../../../services/SevenZip.js"; import FailedReporter from "../../../../common/FailedReporter.js"; const debugMode = (globalEnv.av.debug ?? "false") === "true"; const socket = (/^(false|none|0)$/i.test(globalEnv.av.port ?? "-1")) ? "" : (globalEnv.av.port || "/run/clamav/clamd.sock"); const host = socket ? globalEnv.av.host : void 0; let scanner: Promise; const loadScanner = async () => { const now = Date.now(); const after3Min = now + 3*60*1000; while(true) { // while(!existsSync(socket)) { // await sleep(10000); // if (Date.now() > after3Min) { // throw new Error(`clamd not found at ${socket}.`); // } // console.log(`Waiting for clamd to start...`); // } // socket exists... try to connect... // console.log(`Waiting for clamd to start... at port ${socket}`); try { const { default: NodeClam } = await import("clamscan"); const nc = new NodeClam(); return await nc.init({ debugMode, clamdscan: { socket, host } }); } catch (error) { console.error(error); await sleep(2000); } if (Date.now() > after3Min) { throw new Error(`could not connect to socket at ${socket}.`); } } }; export default class AVService { static scanner() { if (!socket) { return null; } scanner ??= loadScanner(); return scanner; } static async scan(file: LocalFile, hash = void 0) { // we need to unarchive zip/7z and scan individual files... if (/\.(gz|zip|7z|tar|rar)$/i.test(file.fileName)) { using t = new TempFolder(); const r = await SevenZip.unzip({ input: file, output: t.folder }); if (r.status !== 0) { await FailedReporter.copy(file, new Error(`Unzip failed for ${hash}\n${r.all}`)); } for await(const fileItem of t.read()) { await this.scan(fileItem); } return; } // dedicated unrar not available in alpine yet // if (/\.(rar)$/i.test(file.fileName)) { // using t = new TempFolder(); // const r = await SevenZip.unrar({ input: file, output: t.folder }); // if (r.status !== 0) { // await FailedReporter.copy(file, new Error(`Unzip failed for ${hash}\n${r.all}`)); // } // for await(const fileItem of t.read()) { // await this.scan(fileItem); // } // return; // } if (file.contentSize > 200*1024*1024) { console.warn(`File size above 200MB not scanned`); return; } const s = await this.scanner(); if (!s) { if (socket) { console.warn(`AV not enabled ${socket} ${globalEnv.av.port}`); } return; } // lets try file lock... using _fl = await LockFile.lock("av-lock"); const results = await s.scanStream(file.openReadStream()); if (results.isInfected) { throw new FileInfectedError(`File ${file.path} seem to be infected`); } // console.log(`File scan successful for ${file.path}`); } }