-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·67 lines (58 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"use strict";
const cmd = require('node-cmd');
const http = require('http');
const fs = require('fs');
const uuid = require('uuid/v4');
const path = require('path');
const fileFolder = "files";
const hostname = 'localhost';
const port = 3000;
const scanner = "/opt/sophos-av/bin/savscan -ss -archive -mime -oe -tnef -pua -suspicious";
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
let data = [];
req.on('data', function (chunk) {
data.push(chunk);
}).on('end', function () {
const buffer = Buffer.concat(data);
const folder = path.join(__dirname, fileFolder);
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
const filename = path.join(folder, uuid());
fs.writeFile(filename, buffer, "binary", function (err) {
if (err) {
console.log(err);
} else {
console.log("The file was saved!");
}
scan(filename, (result) => {
fs.unlink(filename);
const json = JSON.stringify(result);
console.log(json);
res.end(json);
});
});
});
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
function scan(file, callback) {
const command = [`${scanner} ${file}`];
cmd.get(command, function (err, data, stderr) {
console.log("scan result:", data);
let viruses = [];
let okResponse = {
result: "OK",
};
const lines = data.split("\n");
lines.forEach((line) => {
if (line.startsWith(">>> Virus")) {
viruses.push(line.split("'")[1]);
}
});
callback(viruses.length == 0 ? okResponse : viruses);
});
}