diff --git a/config.js b/config.js index f034ffe..c68f0d7 100644 --- a/config.js +++ b/config.js @@ -14,7 +14,7 @@ const clamscanConfig = { active: process.env.CLAMD_ACTIVE ? /^true$/i.test(process.env.CLAMD_ACTIVE) : true // If true, this module will consider using the clamscan binary }, clamdscan: { - socket: process.env.CLAMD_SOCKET ? process.env.CLAMD_SOCKET : '/tmp/clamd.sock', // Socket file for connecting via TCP + socket: process.env.CLAMD_SOCKET && process.env.CLAMD_SOCKET != 'null'? process.env.CLAMD_SOCKET : null, // Socket file for connecting via TCP host: process.env.CLAMD_HOST ? process.env.CLAMD_HOST : '127.0.0.1', // IP of host to connect to TCP interface port: process.env.CLAMD_PORT ? parseInt(process.env.CLAMD_PORT) : 65615, // Port of host to use when connecting via TCP interface timeout: process.env.CLAMD_TIMEOUT ? parseInt(process.env.CLAMD_TIMEOUT) : 120000, // Timeout for scanning files diff --git a/index.js b/index.js index 9f1087c..84e1677 100644 --- a/index.js +++ b/index.js @@ -12,8 +12,13 @@ app.use(express.json()); app.use(cors()) // Middleware for attaching clamscan with the express request -app.use(async (req, _, next) => { - req.clamscan = await new NodeClam().init({ ...config.clamscanConfig }) +app.use(async (req, res, next) => { + try { + req.clamscan = await new NodeClam().init({ ...config.clamscanConfig }) + } catch (err) { + console.log(err); + return res.status(500).json({message: 'Could not connect to clamav for scanning' }); + } next() }) @@ -43,7 +48,13 @@ app.post('/virus-scan', async (req, res) => { }) } // await req.files.energuide.mv('./tmp/' + req.files.energuide.name); - const scanResult = await scanFile(req.files.energuide, req.clamscan); + let scanResult; + try { + scanResult = await scanFile(req.files.energuide, req.clamscan); + } catch (err) { + console.log(err); + return res.status(500).json({message: 'Clam av encountered an error while scanning'}); + } console.log(scanResult); if (scanResult.is_infected === true || scanFile.is_infected === null) {