bandwidth-monitor
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -6,5 +6,12 @@ const BandwidthMonitor = require('.'); | ||
console.log(Object.keys(b.monitors)); | ||
setInterval(() => { | ||
console.log(b.monitors.en0.rxPerSec); | ||
// b.monitors.en0.close(); | ||
}, 1000); | ||
try{ | ||
b.monitors.en0.capture(); | ||
setInterval(() => { | ||
console.log(b.monitors.en0.rxPerSec); | ||
// b.monitors.en0.close(); | ||
}, 1000); | ||
}catch (e) { | ||
console.log('need root ! add sudo command.'); | ||
} | ||
23
index.js
@@ -7,3 +7,2 @@ | ||
const bufSize = 10 * 1024 * 1024; | ||
const buffer = new Buffer(bufSize); | ||
@@ -51,4 +50,4 @@ function isIPv4(string) { | ||
this.cap = new Cap(); | ||
//todo(cc): catch permission denied warning | ||
const link = this.cap.open(device.name, '', bufSize, buffer); | ||
this.device = device; | ||
this.totalRx = 0; | ||
@@ -58,10 +57,18 @@ this.totalTx = 0; | ||
this.txPerSec = 0; | ||
this.buffer = new Buffer(bufSize); | ||
} | ||
capture(){ | ||
//need root | ||
this.link = this.cap.open(this.device.name, '', bufSize, this.buffer); | ||
this.cap.on('packet', (size) => { | ||
if (link === 'ETHERNET') { | ||
let ret = decoders.Ethernet(buffer); | ||
if (this.link === 'ETHERNET') { | ||
let ret = decoders.Ethernet(this.buffer); | ||
//todo(cc): support ipv6 | ||
if (ret.info.type === PROTOCOL.ETHERNET.IPV4) { | ||
ret = decoders.IPV4(buffer, ret.offset); | ||
if (ret.info.srcaddr !== device.ipv4) { | ||
ret = decoders.IPV4(this.buffer, ret.offset); | ||
if (ret.info.srcaddr !== this.device.ipv4) { | ||
this.totalRx += size; | ||
@@ -89,2 +96,4 @@ } else { | ||
this.cap.close(); | ||
this.buffer = null; | ||
} | ||
@@ -91,0 +100,0 @@ } |
{ | ||
"name": "bandwidth-monitor", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23465
92