Guest User

Untitled

a guest
Oct 18th, 2016
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * This version support only compiled code and works with streams API
  3.  */
  4.  
  5. const debug = require('debug')('NetFlowV9');
  6. const dgram = require('dgram');
  7. const clone = require('clone');
  8. const util = require('util');
  9. const eventEmitter = require('events');
  10. const Dequeue = require('dequeue');
  11.  
  12. const nft = require('./js/nf9/nftypes');
  13. const nf1PktDecode = require('./js/nf1/nf1decode');
  14. const nf5PktDecode = require('./js/nf5/nf5decode');
  15. const nf7PktDecode = require('./js/nf7/nf7decode');
  16. const nf9PktDecode = require('./js/nf9/nf9decode');
  17.  
  18. class NetFlowV9 extends eventEmitter {
  19.  
  20.     constructor(options) {
  21.         super();
  22.         this.templates  = {};
  23.         this.nfTypes    = clone(nft.nfTypes);
  24.         this.nfScope    = clone(nft.nfScope);
  25.         this.cb         = null;
  26.         this.templateCb = null;
  27.         this.socketType = 'udp4';
  28.         this.port       = null;
  29.         this.proxy      = null;
  30.         this.fifo       = new Dequeue();
  31.  
  32.         if (typeof options == 'function') this.cb = options; else
  33.         if (typeof options.cb == 'function') this.cb = options.cb;
  34.         if (typeof options.templateCb == 'function') this.templateCb = options.templateCb;
  35.         if (typeof options == 'object') {
  36.             if (options.ipv4num) decIpv4Rule[4] = "o['$name']=buf.readUInt32BE($pos);";
  37.             if (options.nfTypes) this.nfTypes = util._extend(this.nfTypes,options.nfTypes); // Inherit nfTypes
  38.             if (options.nfScope) this.nfScope = util._extend(this.nfScope,options.nfScope); // Inherit nfTypes
  39.             if (options.socketType) this.socketType = options.socketType;
  40.             if (options.port) this.port = options.port;
  41.             if (options.templates) this.templates = options.templates;
  42.             if (options.fwd) this.fwd = options.fwd;
  43.             if (typeof options.proxy == 'object' || typeof options.proxy == 'string') {
  44.                 this.proxy = [];
  45.                 if (typeof options.proxy == 'string') {
  46.                     debug('Defining proxy destination %s',options.proxy);
  47.                     var m = options.proxy.match(/^(.*)(\:(\d+))$/);
  48.                     if (m) {
  49.                         this.proxy.push({host: m[1], port: m[3]||5555});
  50.                         debug('Proxy added %s:%s',m[1],m[3]||5555);
  51.                     }
  52.                 } else {
  53.                     for (var k in options.proxy) {
  54.                         var v = options.proxy[k];
  55.                         if (typeof v == 'string') {
  56.                             debug('Defining proxy destination %s = %s',k,v);
  57.                             var m = v.match(/^(.*)(\:(\d+))$/);
  58.                             if (m) {
  59.                                 this.proxy.push({host: m[1], port: m[3]||5555});
  60.                                 debug('Proxy added %s:%s',m[1],m[3]||5555);
  61.                             }
  62.                         }
  63.                     }
  64.                 }
  65.                
  66.                 if (this.proxy.length == 0) {
  67.                     this.proxy = null;
  68.                 }
  69.             }
  70.             eventEmitter.call(this,options);
  71.         }
  72.  
  73.         this.server = dgram.createSocket(this.socketType);
  74.         this.server.on('message',(msg,rinfo) => {
  75.             this.fifo.push([msg, rinfo]);
  76.             if (!this.closed && this.set) {
  77.                 this.set = false;
  78.                 setImmediate(this.fetch);
  79.             }
  80.             if (this.proxy) { // Resend the traffic
  81.                 this.proxy.forEach((p) => {
  82.                     this.server.send(msg,0,msg.length,p.port,p.host,function() {});
  83.                 });
  84.             }
  85.         });
  86.  
  87.         this.server.on('close', () => {
  88.             this.closed = true;
  89.         });
  90.  
  91.         if (this.port){
  92.             this.listen(options.port, options.host)
  93.         };
  94.     }
  95.  
  96.     fetch() {
  97.         while (this.fifo.length > 0 && !this.closed) {
  98.             var data = this.fifo.shift();
  99.             var msg = data[0];
  100.             var rinfo = data[1];
  101.             var startTime = new Date().getTime();
  102.             if (this.fwd) {
  103.                 var data = JSON.parse(msg.toString());
  104.                 msg = new Buffer(data.buffer);
  105.                 rinfo = data.rinfo;
  106.             }
  107.             if (rinfo.size<20) return;
  108.             var o = this.nfPktDecode(msg,rinfo);
  109.             var timeMs = (new Date().getTime()) - startTime;
  110.             debug('Flows length => '+o.flows.length);
  111.             if (o && o.flows.length > 0) { // If the packet does not contain flows, only templates we do not decode
  112.                 o.rinfo = rinfo;
  113.                 o.packet = msg;
  114.                 o.decodeMs = timeMs;
  115.                 if (this.cb)
  116.                     this.cb(o);
  117.                 else
  118.                     this.emit('data',o);
  119.             } else if (o && o.templates) {
  120.                 o.rinfo = rinfo;
  121.                 o.packet = msg;
  122.                 o.decodeMs = timeMs;
  123.                 if (this.templateCb)
  124.                     this.templateCb(o);
  125.                 else
  126.                     this.emit('template', o);
  127.             } else {
  128.                 debug('Undecoded flows',o);
  129.             }
  130.         }
  131.  
  132.         this.set = true;
  133.     };
  134.  
  135.     listen(port,host,cb) {
  136.         this.fetch();
  137.         setTimeout(() => {
  138.             if (host && typeof host === 'function')
  139.                 this.server.bind(port,host);
  140.             else if (host && typeof host === 'string' && cb)
  141.                 this.server.bind(port,host,cb);
  142.             else if (host && typeof host === 'string' && !cb)
  143.                 this.server.bind(port,host);
  144.             else if (!host && cb)
  145.                 this.server.bind(port, cb);
  146.             else
  147.                 this.server.bind(port);
  148.         },50);
  149.     };
  150.  
  151.     addTemplate (template,rinfo) {
  152.         const id = rinfo.address + ':' + rinfo.port;
  153.         const tId = Object.keys(template[id])[0];
  154.         this.templates[tId] = template[id][tId];
  155.         console.log(this.templates);
  156.     }
  157.  
  158.     nfPktDecode(msg,rinfo) {
  159.         const version = msg.readUInt16BE(0);
  160.         debug('entering into nfPktDecode with version =>'+version);
  161.         switch (version) {
  162.             case 1:
  163.                 return nf1PktDecode(msg,rinfo);
  164.             case 5:
  165.                 return nf5PktDecode(msg,rinfo);
  166.             case 7:
  167.                 return nf7PktDecode(msg,rinfo);
  168.             case 9:
  169.                 return nf9PktDecode(msg,rinfo,this.templates);
  170.             default:
  171.                 debug('bad header version %d', version);
  172.                 return;
  173.         }
  174.     }
  175.  
  176. }
  177.  
  178. module.exports = NetFlowV9;
Advertisement
Add Comment
Please, Sign In to add comment