bandwidth-monitor
Advanced tools
Comparing version 1.0.4 to 1.0.5
const BandwidthMonitor = require('.'); | ||
const interface = 'en4'; | ||
const b = new BandwidthMonitor({disableIPv6:true,interfaces:[ | ||
'en0' | ||
]}); | ||
interface | ||
],ignoreLAN:true}); | ||
console.log(Object.keys(b.monitors)); | ||
try{ | ||
b.monitors.en0.capture(); | ||
b.monitors[interface].capture(); | ||
setInterval(() => { | ||
console.log(b.monitors.en0.rxPerSec); | ||
console.log(b.monitors[interface].rxPerSec); | ||
// b.monitors.en0.close(); | ||
@@ -12,0 +14,0 @@ }, 1000); |
37
index.js
@@ -1,4 +0,4 @@ | ||
const Cap = require('cap').Cap; | ||
const decoders = require('cap').decoders; | ||
const ipUtils = require('ip'); | ||
@@ -8,9 +8,5 @@ const PROTOCOL = decoders.PROTOCOL; | ||
function isIPv4(string) { | ||
return !!/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(string); | ||
} | ||
function checkAddresses(device) { | ||
for (let address of device.addresses){ | ||
if (isIPv4(address.addr)){ | ||
for (let address of device.addresses) { | ||
if (ipUtils.isV4Format(address.addr)) { | ||
device.ipv4 = address.addr; | ||
@@ -23,5 +19,8 @@ return true; | ||
class BandwidthMonitor{ | ||
constructor(options = {disableIPv6:true}){ | ||
class BandwidthMonitor { | ||
constructor(options = {disableIPv6: true, ignoreLAN: false}) { | ||
this.disableIPv6 = options && options.disableIPv6; | ||
this.ignoreLAN = options && options.ignoreLAN; | ||
this.devices = Cap.deviceList(); | ||
@@ -34,3 +33,3 @@ if (options.interfaces) { | ||
if (options.disableIPv6){ | ||
if (this.disableIPv6) { | ||
this.devices = this.devices.filter((d) => checkAddresses(d)); | ||
@@ -41,6 +40,4 @@ } | ||
this.devices.forEach((device) => { | ||
// console.log(device); | ||
this.monitors[device.name] = new DeviceMonitor(device); | ||
this.monitors[device.name] = new DeviceMonitor(device,this.ignoreLAN); | ||
}); | ||
@@ -50,6 +47,7 @@ } | ||
class DeviceMonitor{ | ||
constructor(device){ | ||
class DeviceMonitor { | ||
constructor(device,ignoreLAN) { | ||
this.cap = new Cap(); | ||
this.ignoreLAN = ignoreLAN; | ||
this.device = device; | ||
@@ -60,3 +58,3 @@ this.totalRx = 0; | ||
this.txPerSec = 0; | ||
this.buffer = new Buffer(bufSize); | ||
this.buffer = Buffer.alloc(bufSize); | ||
@@ -66,3 +64,3 @@ this.isCapturing = false; | ||
capture(){ | ||
capture() { | ||
//need root | ||
@@ -77,2 +75,5 @@ this.link = this.cap.open(this.device.name, '', bufSize, this.buffer); | ||
ret = decoders.IPV4(this.buffer, ret.offset); | ||
if(this.ignoreLAN && ipUtils.isPrivate(ret.info.dstaddr)){ | ||
return; | ||
} | ||
if (ret.info.srcaddr !== this.device.ipv4) { | ||
@@ -100,3 +101,3 @@ this.totalRx += size; | ||
close(){ | ||
close() { | ||
this.isCapturing = false; | ||
@@ -103,0 +104,0 @@ |
{ | ||
"name": "bandwidth-monitor", | ||
"version": "1.0.4", | ||
"description": "", | ||
"version": "1.0.5", | ||
"description": "bandwidth-monitor", | ||
"main": "index.js", | ||
@@ -9,7 +9,9 @@ "scripts": { | ||
}, | ||
"repository": {}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"cap": "^0.2.0" | ||
"cap": "^0.2.0", | ||
"ip": "^1.1.5" | ||
} | ||
} |
@@ -6,2 +6,12 @@ # bandwidth-monitor | ||
## dependencies | ||
**Ubuntu** | ||
`apt install libpcap-dev` | ||
**MAC** | ||
`brew install libpcap-dev` | ||
## install | ||
@@ -8,0 +18,0 @@ `npm install bandwidth-monitor` |
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
12
99
20
17465
2