-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwindivert.js
32 lines (32 loc) · 933 Bytes
/
windivert.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
var wd = require('bindings')('WinDivert');
wd.decoders = require('./decoders.js');
wd.listen = function(filter, cb, handleClose){
handleClose = handleClose || true;
var handle = new wd.WinDivert(filter, 0, 0, 0);
handle.open();
(function recvLoop(){
handle.recv(function(err, data){
var ret = cb(data.packet, data.addr);
if(Buffer.isBuffer(ret)){
handle.HelperCalcChecksums(data, 0);
handle.send(data);
}
else if(ret!==false){
handle.send(data);
}
recvLoop();
});
}());
if(handleClose){
function exitHandler(options, err) {
console.log("Exiting");
if (err) console.log(err.stack);
if (options.exit){handle.close(); process.exit();}
}
process.on('exit', exitHandler.bind(null,{cleanup:true}));
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
}
return handle;
};
module.exports = wd;