-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
384 lines (344 loc) · 10.1 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
'use strict'
/**
* Module dependencies.
*/
const net = require('net')
const fs = require('fs')
const path = require('path')
const Readable = require('stream').Readable
const Transform = require('stream').Transform
/**
* Module exports.
*/
module.exports = {
createScanner,
ping,
version,
isCleanReply
}
/**
* Create a scanner
*
* @param {string} host clamav server's host
* @param {number} port clamav sever's port
* @return {object}
* @public
*/
function createScanner (host, port) {
if (!host || !port) throw new Error('must provide the host and port that clamav server listen to')
/**
* scan a read stream
* @param {object} readStream
* @param {number} [timeout = 5000] the socket's timeout option
* @return {Promise}
*/
function scanStream (readStream, timeout) {
if (typeof timeout !== 'number' || timeout < 0) timeout = 5000
return new Promise(function (resolve, reject) {
let readFinished = false
const socket = net.createConnection({
host,
port
}, function () {
socket.write('zINSTREAM\0')
// fotmat the chunk
readStream.pipe(chunkTransform()).pipe(socket)
readStream
.on('end', function () {
readFinished = true
readStream.destroy()
})
.on('error', reject)
})
let replys = []
socket.setTimeout(timeout)
socket
.on('data', function (chunk) {
clearTimeout(connectAttemptTimer)
if (!readStream.isPaused()) readStream.pause()
replys.push(chunk)
})
.on('end', function () {
clearTimeout(connectAttemptTimer)
let reply = Buffer.concat(replys)
if (!readFinished) reject(new Error('Scan aborted. Reply from server: ' + reply))
else resolve(reply.toString())
})
.on('error', reject)
const connectAttemptTimer = setTimeout(function () {
socket.destroy(new Error('Timeout connecting to server'));
}, timeout)
})
}
/**
* scan a Buffer
* @param {string} path
* @param {number} [timeout = 5000] the socket's timeout option
* @param {number} [chunkSize = 64kb] size of the chunk, which send to Clamav server
* @return {Promise}
*/
function scanBuffer (buffer, timeout, chunkSize) {
if (typeof timeout !== 'number' || timeout < 0) timeout = 5000
if (typeof chunkSize !== 'number') chunkSize = 64 * 1024
let start = 0
const bufReader = new Readable({
highWaterMark: chunkSize,
read (size) {
if (start < buffer.length) {
let block = buffer.slice(start, start + size)
this.push(block)
start += block.length
} else {
this.push(null)
}
}
})
return scanStream(bufReader, timeout)
}
/**
* scan a file
* @param {string} filePath
* @param {number} [timeout = 5000] the socket's timeout option
* @param {number} [chunkSize = 64kb] size of the chunk, which send to Clamav server
* @return {Promise}
*/
function scanFile (filePath, timeout, chunkSize) {
filePath = path.normalize(filePath)
if (typeof timeout !== 'number' || timeout < 0) timeout = 5000
if (typeof chunkSize !== 'number') chunkSize = 64 * 1024
try {
let stats = fs.statSync(filePath)
if (stats.isDirectory()) {
return Promise.reject(new Error(filePath + ' is a directory, please use scanDirectory instead!'))
} else if (!stats.isFile() || stats.isSymbolicLink()){
return Promise.reject(new Error(filePath + ' is Not a regular file'))
}
} catch (error) {
return Promise.reject(error)
}
let s = fs.createReadStream(filePath, {
highWaterMark: chunkSize
})
return scanStream(s, timeout)
}
/**
* scan a directory
* @param {string} rootPath
* @param {object} [options]
* @return {Promise}
*/
function scanDirectory (rootPath, options) {
// TODO add ignore option
rootPath = path.normalize(rootPath)
let opts = options || {}
let timeout = typeof opts.timeout !== 'number'
? 5000
: opts.timeout
let chunkSize = opts.chunkSize || 64 * 1024
let scanningFile = opts.scanningFile || 10
let detail = opts.detail !== false
let cont = opts.cont !== false
return new Promise(function (resolve, reject) {
// scanning result
let ScannedFiles = 0
let Infected = 0
let EncounterError = 0
let Result = []
// scaning queue's state
let scanning = 0
// keep track of the files and directories path, which upcoming to scan
let dirs = []
let files = []
function scanDir (pathName) {
let flist = null
try {
flist = fs.readdirSync(pathName)
} catch (error) {
if (cont) return done(pathName, null, error.message, 0)
else return reject(error)
}
flist.forEach(function (entry) {
let stats
try {
stats = fs.lstatSync(path.join(pathName, entry))
} catch (error) {
if (cont) return done(pathName, null, error.message, 0)
else return reject(error)
}
if (stats.isDirectory() && !stats.isSymbolicLink()) {
dirs.push(path.join(pathName, entry))
} else if (stats.isFile() && !stats.isSymbolicLink()) {
files.push(path.join(pathName, entry))
}
})
// schedul scaning queue after scanned a directory
schedulScan()
}
function scanFileWrap (path) {
scanning = scanning + 1
scanFile(path, timeout, chunkSize)
.then(function (res) {
done(path, res, null, 1)
})
.catch(function (e) {
done(path, null, e.message, 1)
})
}
function done (file, reply, errorMsg, finished) {
scanning = scanning - finished
ScannedFiles = ScannedFiles + 1
if (reply !== null && !isCleanReply(reply))Infected = Infected + 1
if (errorMsg !== null)EncounterError = EncounterError + 1
if (detail) {
Result.push({
file,
reply,
errorMsg
})
} else if (errorMsg !== null || (reply !== null && !isCleanReply(reply))) {
Result.push({
file,
reply,
errorMsg
})
}
if (files.length !== 0) scanFileWrap(files.shift())
else if (dirs.length !== 0) scanDir(dirs.shift())
else if (scanning === 0) {
resolve({
ScannedFiles,
Infected,
EncounterError,
Result
})
}
}
function schedulScan () {
if (scanning >= scanningFile) return
if (files.length !== 0) {
while (scanning < scanningFile && files.length !== 0) {
scanFileWrap(files.shift())
}
} else if (dirs.length !== 0) scanDir(dirs.shift())
else if (scanning === 0) {
resolve({
ScannedFiles,
Infected,
EncounterError,
Result
})
}
}
let stats = null
try {
stats = fs.lstatSync(rootPath)
} catch (error) {
if (cont) return done(rootPath, null, error.message, 0)
else return reject(error)
}
if (stats.isDirectory() && !stats.isSymbolicLink()) scanDir(rootPath)
else if (stats.isFile() && !stats.isSymbolicLink()) scanFileWrap(rootPath)
else reject(new Error(rootPath + ' is Not a regular file or directory'))
})
}
return {
scanStream,
scanBuffer,
scanFile,
scanDirectory
}
}
/**
* Check the daemon’s state
*
* @param {string} host clamav server's host
* @param {number} port clamav sever's port
* @param {number} [timeout = 5000] the socket's timeout option
* @return {boolean}
* @public
*/
function ping (host, port, timeout) {
if (!host || !port) throw new Error('must provide the host and port that clamav server listen to')
if (typeof timeout !== 'number' || timeout < 0) timeout = 5000
return _command(host, port, timeout, 'zPING\0')
.then(function (res) {
return res.equals(Buffer.from('PONG\0'))
})
}
/**
* Get clamav version detail.
*
* @param {string} host clamav server's host
* @param {number} port clamav sever's port
* @param {number} [timeout = 5000] pass to sets the socket's timeout optine
* @return {string}
* @public
*/
function version (host, port, timeout) {
if (!host || !port) throw new Error('must provide the host and port that clamav server listen to')
if (typeof timeout !== 'number' || timeout < 0) timeout = 5000
return _command(host, port, timeout, 'zVERSION\0')
.then(function (res) {
return res.toString()
})
}
/**
* Check the reply mean the file infect or not
*
* @param {*} reply get from the scanner
* @return {boolean}
* @public
*/
function isCleanReply (reply) {
return reply.includes('OK') && !reply.includes('FOUND')
}
/**
* transform the chunk from read stream to the fotmat that clamav server expect
*
* @return {object} stream.Transform
*/
function chunkTransform () {
return new Transform(
{
transform (chunk, encoding, callback) {
const length = Buffer.alloc(4)
length.writeUInt32BE(chunk.length, 0)
this.push(length)
this.push(chunk)
callback()
},
flush (callback) {
const zore = Buffer.alloc(4)
zore.writeUInt32BE(0, 0)
this.push(zore)
callback()
}
})
}
/**
* helper function for single command function like ping() and version()
* @param {string} host
* @param {number} port
* @param {number} timeout
* @param {string} command will send to clamav server, either 'zPING\0' or 'zVERSION\0'
*/
function _command (host, port, timeout, command) {
return new Promise(function (resolve, reject) {
const client = net.createConnection({
host,
port
}, function () {
client.write(command)
})
client.setTimeout(timeout)
let replys = []
client
.on('data', function (chunk) {
replys.push(chunk)
})
.on('end', function () {
resolve(Buffer.concat(replys))
})
.on('error', reject)
})
}