Skip to content

Commit

Permalink
Merge pull request #4 from internxt/feat/stop-sockets-if-needed
Browse files Browse the repository at this point in the history
[_]: feat/stop sockets if needed
  • Loading branch information
xabg2 authored Jan 31, 2025
2 parents ac5b4d5 + 05d5ae1 commit bbba057
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 66 deletions.
94 changes: 29 additions & 65 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const { execFile } = require('child_process');
const { Readable } = require('stream');
const { Socket } = require('dgram');
const fsPromises = require('fs').promises;
const { stat } = require('fs/promises');
const NodeClamError = require('./lib/NodeClamError');
const NodeClamTransform = require('./lib/NodeClamTransform');
const getFiles = require('./lib/getFiles');
Expand Down Expand Up @@ -54,8 +53,9 @@ class NodeClam {
*/
constructor() {
this.initialized = false;
this.debugLabel = 'node-clam';
this.debugLabel = 'inxt-clamScan';
this.defaultScanner = 'clamdscan';
this.activeSockets = [];

// Configuration Settings
this.defaults = Object.freeze({
Expand Down Expand Up @@ -124,35 +124,6 @@ class NodeClam {
* @param {Function} [cb = null] - Callback method. Prototype: `(err, <instance of NodeClam>)`
* @returns {Promise<object>} An initated instance of NodeClam
* @example
* const NodeClam = require('clamscan');
* const ClamScan = new NodeClam().init({
* removeInfected: false,
* quarantineInfected: false,
* scanLog: null,
* debugMode: false,
* fileList: null,
* scanRecursively: true,
* clamscan: {
* path: '/usr/bin/clamscan',
* db: null,
* scanArchives: true,
* active: true
* },
* clamdscan: {
* socket: false,
* host: false,
* port: false,
* timeout: 60000,
* localFallback: false,
* path: '/usr/bin/clamdscan',
* configFile: null,
* multiscan: true,
* reloadDb: false,
* active: true,
* bypassTest: false,
* },
* preference: 'clamdscan'
});
*/
async init(options = {}, cb = null) {
let hasCb = false;
Expand Down Expand Up @@ -578,6 +549,8 @@ class NodeClam {
// Set the socket timeout if specified
if (this.settings.clamdscan.timeout) client.setTimeout(this.settings.clamdscan.timeout);

this.activeSockets.push(client);

// Setup socket client listeners
client
.on('connect', () => {
Expand Down Expand Up @@ -618,6 +591,19 @@ class NodeClam {
});
}

closeAllSockets() {
return new Promise((resolve) => {
for (const socket of this.activeSockets) {
if (!socket.destroyed) {
console.log('DESTROYING SOCKETS');
socket.destroy();
}
}
this.activeSockets = [];
resolve('');
});
}

/**
* Checks to see if a particular binary is a clamav binary. The path for the
* binary must be specified in the NodeClam config at `init`. If you have a
Expand Down Expand Up @@ -1016,44 +1002,22 @@ class NodeClam {
}
}

async binPackFiles(filePaths, numBins = 10) {
const filesWithSize = [];
for (const path of filePaths) {
try {
const { size } = await stat(path);
filesWithSize.push({ path, size });
} catch (err) {
// Si da error stat, lo tratamos como size=0 o lo excluimos
filesWithSize.push({ path, size: 0 });
}
}

filesWithSize.sort((a, b) => b.size - a.size);

const bins = [];
for (let i = 0; i < numBins; i++) {
bins.push({
totalSize: 0,
files: [],
});
}

for (const fileObj of filesWithSize) {
let bestBinIndex = 0;
let minSize = Infinity;
async scanFile(filePath) {
try {
const scannedFile = await this.isInfected(filePath);

for (let i = 0; i < numBins; i++) {
if (bins[i].totalSize < minSize) {
minSize = bins[i].totalSize;
bestBinIndex = i;
}
return scannedFile;
} catch (err) {
let error = err;
if (err instanceof NodeClamError && err.data?.err instanceof Error) {
error = err.data.err;
}

bins[bestBinIndex].files.push(fileObj);
bins[bestBinIndex].totalSize += fileObj.size;
if (!isPermissionError(error)) {
console.error(`Error scanning file ${filePath}:`, error);
throw error;
}
}

return bins.map((bin) => bin.files);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@internxt/scan",
"version": "1.0.1",
"version": "1.0.2",
"author": "Kyle Farris <[email protected]> (https://infotechinc.com)",
"description": "Use Node JS to scan files on your server with ClamAV's clamscan/clamdscan binary or via TCP to a remote server or local UNIX Domain socket. This is especially useful for scanning uploaded files provided by un-trusted sources.",
"main": "index.js",
Expand Down

0 comments on commit bbba057

Please sign in to comment.