Skip to content

Commit

Permalink
Improve code readability and restructure the project
Browse files Browse the repository at this point in the history
  • Loading branch information
denizariyan committed May 5, 2022
1 parent 401fb67 commit 3926ea3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
4 changes: 2 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ let md5Previous = md5(fs.readFileSync(env));
/**
* Watch the .env file for changes and restart the scanner with new options if there is any change
* @param {string} env - Path to the .env file
* @param {} event - A filesystem event
* @param {} filename - Name of the file for the filesystem event
* @param {event} event - A filesystem event
* @param {string} filename - Name of the file for the filesystem event
*/
fs.watch(env, (event, filename) => {
if (filename) {
Expand Down
18 changes: 16 additions & 2 deletions scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const quarantinePath = "/usr/VSterilizer/infected/";
* Find the line for the given key and splice it to replace with the new value
* @param {string} key - Key for the given env variable
* @param {string} newValue - New value for the given key
* @param {string} env - path to the environment file. Defaults to the project supplied one
*/
async function changeParam(key, newValue, env = "./.env") {
const ENV_VARS = fs.readFileSync(env, "utf8").split(os.EOL);
Expand Down Expand Up @@ -43,18 +44,31 @@ async function optionHandler(req, res) {
}
}

/**
* Parse incoming requests body as JSON
* @param {function} - JSON parser
*/
app.use(bodyParser.json());

/**
* @param {string} '/options' - API endpoint
* @param {request} req
* @param {response} res
* @param {request} req - Incoming post request
* @param {response} res - Response to be sent
*/
app.post('/options', (req, res) => {
optionHandler(req, res);
});

/**
* Launch the server instance
* @param {integer} port - Port to launch the server on. Default: 8080
*/
const server = app.listen(port, () => console.log(`Listening on port ${port}!`))

/**
* Close the server when an exit event is emited
* @param {string} "exit" - Event name
*/
app.on("exit", () => server.close())

module.exports.changeParam = changeParam;
Expand Down
File renamed without changes.
23 changes: 10 additions & 13 deletions scripts/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ options =

/**
* Send the results of a scan to the API endpoint in realtime
* @param {array<string>} badFileList=null
* @param {array<string>} badFileList - List of infected files, defaults to null
*/
async function sendResults(badFileList = null) {
if (badFileList === null) {
sendStatus("Scan completed, no infected files has been detected.")
} else {
for (element of badFileList) {
let payload = { badFile: element.filename, virus: element.virus };
let res = await axios.post('http://httpbin.org/post', payload);
let res = await axios.post('http://httpbin.org/post', payload); /* Mock API server for demo */
let data = res.data;
console.log(data);
};
Expand All @@ -57,11 +57,11 @@ async function sendResults(badFileList = null) {
/**
* Sends status information to the API endpoint
* This includes data with information level severity such as a new USB being detected, a new scan running etc.
* @param {string} status
* @param {string} status - Status message to send
*/
async function sendStatus(status) {
let payload = { status: status };
let res = await axios.post('http://httpbin.org/post', payload);
let res = await axios.post('http://httpbin.org/post', payload); /* Mock API server for demo */
let data = res.data;
console.log(data);
}
Expand All @@ -74,7 +74,6 @@ async function sendStatus(status) {
async function scanDirectory(path) {
const clamscan = await new NodeClam().init(options);
try {
// TODO: Replace hard-coded path with path var it is here for faster testing
clamscan.scanDir(path, async function (err, goodFiles, badFiles, viruses) {
if (badFiles.length > 0) {
let badFileList = await parseLog(path);
Expand All @@ -90,7 +89,7 @@ async function scanDirectory(path) {

/**
* Add the ability to wait in parts of the code without blocking rest of the program
* @param {integer} ms - Time to wait
* @param {integer} ms - Time to wait in miliseconds
*/
function sleep(ms) {
return new Promise((resolve) => {
Expand All @@ -101,7 +100,7 @@ function sleep(ms) {
/**
* Parses the scanner log file to get detailed information about infected files
* Takes a keyword parameter which has the path of a given infected file
* @param {string} keyword - Keyword that we are looking for
* @param {string} keyword - path of the file we are intersted in
*/
async function parseLog(keyword, logfile = options.scanLog) {
let badFiles = [];
Expand Down Expand Up @@ -129,14 +128,14 @@ async function parseLog(keyword, logfile = options.scanLog) {
async function getMountPoint(serialNumber) {
sendStatus("Accessing the USB Device...");
await sleep(5000); // Wait for device to be mounted by kernel
let out = child_process.spawnSync('/home/deari/projects/VSterilizer/getMountPoint.sh', [serialNumber]);
let out = child_process.spawnSync('./scripts/getMountPoint.sh', [serialNumber]);
mount(out.stdout.toString('utf8').split("\n")[0]);
}

/**
* Mount the given device to a auto-generated dir under the
* products own mounting directory
* @param {string} source - source for the device to be mounted
* @param {string} source - physical port of the device to be mounted. ex: /dev/sda0
*/
function mount(source) {
let uuid = uuidv4();
Expand All @@ -149,7 +148,7 @@ function mount(source) {
/**
* Enable watcher to detect newly plugged USB devices
* Calls the mounting point getter function when a new device is detected
* @param {string} 'add' - Const string
* @param {string} 'add' - Adds a new USB event listener
*/
USBWatch.on('add', function (device) {
getMountPoint(device.serialNumber);
Expand All @@ -163,12 +162,10 @@ function start() {
fs.writeFileSync(options.scanLog, ''); // Clear the logs
console.log("Started to monitor for USB inserts!");
USBWatch.startMonitoring();
//getMountPoint("C03FD5F2F334F15109A501FD"); // For testing
//scanDirectory("/home/deari/Downloads/"); // Enable for testing
}

/**
* Helper script to end the USB monitoring
* Helper to end the USB monitoring
* Utilized while stopping or restarting the service
*/
function endWatch() {
Expand Down

0 comments on commit 3926ea3

Please sign in to comment.