Skip to content

Commit

Permalink
catching connection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hannah-macdonald1 committed Dec 6, 2023
1 parent 404d941 commit 1ce9e50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1ce9e50

Please sign in to comment.