From 1f081efd395d4a5e2c0902b2becc1b0901a22085 Mon Sep 17 00:00:00 2001 From: Aetherinox Date: Sun, 21 Apr 2024 04:42:31 -0700 Subject: [PATCH] chore: adjustments to tasks.grunt.virustotal --- build/tasks/grunt-virustotal.js | 42 ++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/build/tasks/grunt-virustotal.js b/build/tasks/grunt-virustotal.js index 94b5af97..7f2657fb 100644 --- a/build/tasks/grunt-virustotal.js +++ b/build/tasks/grunt-virustotal.js @@ -5,7 +5,7 @@ module.exports = function (grunt) { const path = require('path'); const fs = require('fs'); - const fetch = require('node-fetch'); + const got = require('got'); const FormData = require('form-data'); Promise.all( @@ -16,6 +16,16 @@ module.exports = function (grunt) { ) ).then(done); + /* + convert to a more suitable time + */ + + function formatMilliseconds(ms) { + const m = Math.floor(ms / 60000); + const s = ((ms % 60000) / 1000).toFixed(0); + return m + 'm ' + (s < 10 ? '0' : '') + s + 's'; + } + async function checkFile(opt, file) { grunt.log.writeln(`Uploading to VirusTotal: ${file}...`); @@ -31,63 +41,67 @@ module.exports = function (grunt) { const form = new FormData(); form.append('file', fileData, fileName); - const fileUploadResp = await fetch('https://www.virustotal.com/api/v3/files', { + + const fileUploadResp = await got('https://www.virustotal.com/api/v3/files', { method: 'POST', headers, body: form - }); - const fileUploadRespData = await fileUploadResp.json(); - if (fileUploadRespData.error) { - const errStr = JSON.stringify(fileUploadRespData.error); + }).json(); + + if (fileUploadResp.error) { + const errStr = JSON.stringify(fileUploadResp.error); throw new Error(`File upload error: ${errStr}`); } - const id = fileUploadRespData.data.id; + const id = fileUploadResp.data.id; if (!id) { throw new Error('File upload error: empty id'); } - grunt.log.writeln(`Uploaded ${file} to VirusTotal, id: ${id}`); + grunt.log.writeln(`\nUploaded ${file} to VirusTotal, id: ${id}`.grey.bold); let elapsed; do { const checkResp = await fetch(`https://www.virustotal.com/api/v3/analyses/${id}`, { headers }); + const checkRespData = await checkResp.json(); if (checkRespData.error) { const errStr = JSON.stringify(checkRespData.error); - throw new Error(`File check error: ${errStr}`); + throw new Error(`File check error: ${errStr}`.red.bold); } + const { attributes } = checkRespData.data; if (attributes.status === 'completed') { const { stats } = attributes; if (stats.malicious > 0) { throw new Error( - `File ${file} reported as malicious ${stats.malicious} time(s)` + `File ${file} reported as malicious ${stats.malicious} time(s)`.yellow.bold ); } if (stats.suspicious > 0) { throw new Error( - `File ${file} reported as malicious ${stats.suspicious} time(s)` + `File ${file} reported as malicious ${stats.suspicious} time(s)`.red.bold ); } const statsStr = Object.entries(stats) .map(([k, v]) => `${k}=${v}`) .join(', '); - grunt.log.writeln(`VirusTotal check OK: ${file}, stats:`, statsStr); + grunt.log.writeln(`VirusTotal Check OK: ${file}, stats:`.green.bold, statsStr); return; } elapsed = Date.now() - timeStarted; + const elapsedHuman = formatMilliseconds(elapsed); grunt.log.writeln( - `VirusTotal check status: ${attributes.status}, elapsed ${elapsed}ms` + `VirusTotal Status: ${attributes.status}, ${elapsedHuman}`.grey.bold ); await wait(interval); } while (elapsed < timeout); - throw new Error(`Timed out after ${timeout}ms`); + throw new Error(`VirusTotal timed out after ${timeout}ms`.red.bold); } function wait(ms) {