Skip to content

Commit bbba057

Browse files
authored
Merge pull request #4 from internxt/feat/stop-sockets-if-needed
[_]: feat/stop sockets if needed
2 parents ac5b4d5 + 05d5ae1 commit bbba057

2 files changed

Lines changed: 30 additions & 66 deletions

File tree

index.js

Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const { execFile } = require('child_process');
2525
const { Readable } = require('stream');
2626
const { Socket } = require('dgram');
2727
const fsPromises = require('fs').promises;
28-
const { stat } = require('fs/promises');
2928
const NodeClamError = require('./lib/NodeClamError');
3029
const NodeClamTransform = require('./lib/NodeClamTransform');
3130
const getFiles = require('./lib/getFiles');
@@ -54,8 +53,9 @@ class NodeClam {
5453
*/
5554
constructor() {
5655
this.initialized = false;
57-
this.debugLabel = 'node-clam';
56+
this.debugLabel = 'inxt-clamScan';
5857
this.defaultScanner = 'clamdscan';
58+
this.activeSockets = [];
5959

6060
// Configuration Settings
6161
this.defaults = Object.freeze({
@@ -124,35 +124,6 @@ class NodeClam {
124124
* @param {Function} [cb = null] - Callback method. Prototype: `(err, <instance of NodeClam>)`
125125
* @returns {Promise<object>} An initated instance of NodeClam
126126
* @example
127-
* const NodeClam = require('clamscan');
128-
* const ClamScan = new NodeClam().init({
129-
* removeInfected: false,
130-
* quarantineInfected: false,
131-
* scanLog: null,
132-
* debugMode: false,
133-
* fileList: null,
134-
* scanRecursively: true,
135-
* clamscan: {
136-
* path: '/usr/bin/clamscan',
137-
* db: null,
138-
* scanArchives: true,
139-
* active: true
140-
* },
141-
* clamdscan: {
142-
* socket: false,
143-
* host: false,
144-
* port: false,
145-
* timeout: 60000,
146-
* localFallback: false,
147-
* path: '/usr/bin/clamdscan',
148-
* configFile: null,
149-
* multiscan: true,
150-
* reloadDb: false,
151-
* active: true,
152-
* bypassTest: false,
153-
* },
154-
* preference: 'clamdscan'
155-
});
156127
*/
157128
async init(options = {}, cb = null) {
158129
let hasCb = false;
@@ -578,6 +549,8 @@ class NodeClam {
578549
// Set the socket timeout if specified
579550
if (this.settings.clamdscan.timeout) client.setTimeout(this.settings.clamdscan.timeout);
580551

552+
this.activeSockets.push(client);
553+
581554
// Setup socket client listeners
582555
client
583556
.on('connect', () => {
@@ -618,6 +591,19 @@ class NodeClam {
618591
});
619592
}
620593

594+
closeAllSockets() {
595+
return new Promise((resolve) => {
596+
for (const socket of this.activeSockets) {
597+
if (!socket.destroyed) {
598+
console.log('DESTROYING SOCKETS');
599+
socket.destroy();
600+
}
601+
}
602+
this.activeSockets = [];
603+
resolve('');
604+
});
605+
}
606+
621607
/**
622608
* Checks to see if a particular binary is a clamav binary. The path for the
623609
* binary must be specified in the NodeClam config at `init`. If you have a
@@ -1016,44 +1002,22 @@ class NodeClam {
10161002
}
10171003
}
10181004

1019-
async binPackFiles(filePaths, numBins = 10) {
1020-
const filesWithSize = [];
1021-
for (const path of filePaths) {
1022-
try {
1023-
const { size } = await stat(path);
1024-
filesWithSize.push({ path, size });
1025-
} catch (err) {
1026-
// Si da error stat, lo tratamos como size=0 o lo excluimos
1027-
filesWithSize.push({ path, size: 0 });
1028-
}
1029-
}
1030-
1031-
filesWithSize.sort((a, b) => b.size - a.size);
1032-
1033-
const bins = [];
1034-
for (let i = 0; i < numBins; i++) {
1035-
bins.push({
1036-
totalSize: 0,
1037-
files: [],
1038-
});
1039-
}
1040-
1041-
for (const fileObj of filesWithSize) {
1042-
let bestBinIndex = 0;
1043-
let minSize = Infinity;
1005+
async scanFile(filePath) {
1006+
try {
1007+
const scannedFile = await this.isInfected(filePath);
10441008

1045-
for (let i = 0; i < numBins; i++) {
1046-
if (bins[i].totalSize < minSize) {
1047-
minSize = bins[i].totalSize;
1048-
bestBinIndex = i;
1049-
}
1009+
return scannedFile;
1010+
} catch (err) {
1011+
let error = err;
1012+
if (err instanceof NodeClamError && err.data?.err instanceof Error) {
1013+
error = err.data.err;
10501014
}
10511015

1052-
bins[bestBinIndex].files.push(fileObj);
1053-
bins[bestBinIndex].totalSize += fileObj.size;
1016+
if (!isPermissionError(error)) {
1017+
console.error(`Error scanning file ${filePath}:`, error);
1018+
throw error;
1019+
}
10541020
}
1055-
1056-
return bins.map((bin) => bin.files);
10571021
}
10581022

10591023
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@internxt/scan",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"author": "Kyle Farris <kyle.farris@infotechinc.com> (https://infotechinc.com)",
55
"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.",
66
"main": "index.js",

0 commit comments

Comments
 (0)