Comparing version 0.1.0 to 0.2.0
@@ -5,24 +5,24 @@ #!/usr/bin/env node | ||
var hasbin = require('hasbin'), | ||
sltc = require('../lib/sltc').sltc, | ||
minimist = require('minimist'); | ||
const hasbin = require('hasbin'); | ||
const sltc = require('../lib/sltc').sltc; | ||
const minimist = require('minimist'); | ||
var argv = minimist(process.argv.slice(2)); | ||
const argv = minimist(process.argv.slice(2)); | ||
if (argv.help) { | ||
console.log(' Run Simple Linux Traffic Control'); | ||
console.log(' Usage: sltc [options] \n'); | ||
console.log(' Options:'); | ||
console.log(' --device The device to use [eth0]'); | ||
console.log(' --bandwidth.download Download bandwidth [5Mbps] '); | ||
console.log(' --bandwidth.upload Upload bandwidth [1.6Mbps] '); | ||
console.log(' --latency Latency [28ms]'); | ||
console.log(' --pl Packet loss in percent [0%] '); | ||
console.log(' --remove Remove tc current rules'); | ||
console.log(' Run Simple Linux Traffic Control'); | ||
console.log(' Usage: sltc [options] \n'); | ||
console.log(' Options:'); | ||
console.log(' --device The device to use [eth0]'); | ||
console.log(' --bandwidth.download Download bandwidth [5Mbps] '); | ||
console.log(' --bandwidth.upload Upload bandwidth [1.6Mbps] '); | ||
console.log(' --latency Latency [28ms]'); | ||
console.log(' --pl Packet loss in percent [0%] '); | ||
console.log(' --remove Remove tc current rules'); | ||
} else { | ||
if (!hasbin.all.sync(['tc', 'modprobe', 'ip'])) { | ||
console.error('You need to have tc in your path to do traffic control'); | ||
} else { | ||
sltc(argv); | ||
} | ||
if (!hasbin.all.sync(['tc', 'modprobe', 'ip'])) { | ||
console.error('You need to have tc in your path to do traffic control'); | ||
} else { | ||
sltc(argv); | ||
} | ||
} |
158
lib/sltc.js
'use strict'; | ||
var execSync = require('child_process').execSync, | ||
util = require('util'), | ||
log = require('intel'), | ||
merge = require('lodash.merge'); | ||
const execSync = require('child_process').execSync; | ||
const util = require('util'); | ||
const log = require('intel'); | ||
const merge = require('lodash.merge'); | ||
const Promise = require('bluebird'); | ||
const defaultConfig = { | ||
device: 'eth0', | ||
bandwidth: { | ||
download: '625kbps', | ||
upload: '200kbps' | ||
}, | ||
latency: '28ms', | ||
pl: '0%' | ||
device: 'eth0', | ||
bandwidth: { | ||
download: '625kbps', | ||
upload: '200kbps', | ||
}, | ||
latency: '28ms', | ||
pl: '0%', | ||
}; | ||
@@ -26,3 +27,4 @@ | ||
// Redirect all ingress IP traffic to the IFB. | ||
'sudo tc filter add dev %s parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev ifb0', | ||
'sudo tc filter add dev %s parent ffff: protocol ip u32 match u32 0 0 action' + | ||
' mirred egress redirect dev ifb0', | ||
@@ -39,72 +41,96 @@ ]; | ||
'sudo tc qdisc add dev ifb0 handle 1: root htb', | ||
'sudo tc filter add dev ifb0 protocol ip prio 1 u32 match ip sport 80 0xffff flowid 1:11' | ||
'sudo tc filter add dev ifb0 protocol ip prio 1 u32 match ip sport 80 0xffff' + | ||
' flowid 1:11', | ||
]; | ||
const egressRateScripts = [ | ||
'sudo tc class add dev %s parent 1: classid 1:11 htb rate %s' | ||
'sudo tc class add dev %s parent 1: classid 1:11 htb rate %s', | ||
]; | ||
const ingressRateScripts = [ | ||
'sudo tc class add dev ifb0 parent 1: classid 1:11 htb rate %s' | ||
] | ||
'sudo tc class add dev ifb0 parent 1: classid 1:11 htb rate %s', | ||
]; | ||
const delayLossScripts = [ | ||
'sudo tc qdisc add dev %s parent 1:11 handle 10: netem delay %s loss %s' | ||
'sudo tc qdisc add dev %s parent 1:11 handle 10: netem delay %s loss %s', | ||
]; | ||
let deleteScripts = [ | ||
const deleteScripts = [ | ||
'sudo tc qdisc del dev %s root', | ||
'sudo tc qdisc del dev %s ingress', | ||
'sudo tc qdisc del dev ifb0 root' | ||
'sudo tc qdisc del dev ifb0 root', | ||
]; | ||
module.exports = { | ||
sltc: function(config) { | ||
const options = merge({}, defaultConfig, config); | ||
if (options.remove) { | ||
deleteScripts.forEach((script) => { | ||
if(!(script.includes('%s'))) { | ||
log.info(script); | ||
execSync(script); | ||
} else { | ||
log.info(util.format(script, options.device)); | ||
try { | ||
execSync(util.format(script, options.device)); | ||
} catch (e) {} | ||
} | ||
}) | ||
} else { | ||
setupScriptsNoParam.forEach((script) => { | ||
log.info(script); | ||
try { | ||
execSync(script); | ||
} catch (e) {} | ||
}) | ||
setupScripts.forEach((script) => { | ||
log.info(util.format(script, options.device)); | ||
try { | ||
execSync(util.format(script, options.device)); | ||
} catch (e) {} | ||
}) | ||
egressRateScripts.forEach((script) => { | ||
log.info(util.format(script, options.device, options.bandwidth.upload)); | ||
try { | ||
execSync(util.format(script, options.device, options.bandwidth.upload)); | ||
} catch (e) {} | ||
}) | ||
ingressRateScripts.forEach((script) => { | ||
log.info(util.format(script, options.bandwidth.download)); | ||
try { | ||
execSync(util.format(script, options.bandwidth.download)); | ||
} catch (e) {} | ||
}) | ||
delayLossScripts.forEach((script) => { | ||
log.info(util.format(script, options.device, options.latency, options.pl)); | ||
try { | ||
execSync(util.format(script, options.device, options.latency, options.pl)); | ||
} catch (e) {} | ||
}) | ||
} | ||
sltc: config => { | ||
const options = merge({}, defaultConfig, config); | ||
if (options.remove) { | ||
return new Promise(function(resolve, reject) { | ||
deleteScripts.forEach((script) => { | ||
if (!(script.includes('%s'))) { | ||
log.info(script); | ||
execSync(script); | ||
} else { | ||
log.info(util.format(script, options.device)); | ||
try { | ||
execSync(util.format(script, options.device)); | ||
resolve(); | ||
} catch (e) { | ||
log.error(e); | ||
reject(Error("Couldn't delete sltc settings")); | ||
} | ||
} | ||
}); | ||
}); | ||
} else { | ||
return new Promise(function(resolve, reject) { | ||
setupScriptsNoParam.forEach((script) => { | ||
log.info(script); | ||
try { | ||
execSync(script); | ||
} catch (e) { | ||
log.error(e); | ||
reject(Error()); | ||
} | ||
}); | ||
setupScripts.forEach((script) => { | ||
log.info(util.format(script, options.device)); | ||
try { | ||
execSync(util.format(script, options.device)); | ||
} catch (e) { | ||
log.error(e); | ||
reject(Error()); | ||
} | ||
}); | ||
egressRateScripts.forEach((script) => { | ||
log.info(util.format(script, options.device, options.bandwidth.upload)); | ||
try { | ||
execSync(util.format(script, options.device, options.bandwidth.upload)); | ||
} catch (e) { | ||
log.error(e); | ||
reject(Error()); | ||
} | ||
}); | ||
ingressRateScripts.forEach((script) => { | ||
log.info(util.format(script, options.bandwidth.download)); | ||
try { | ||
execSync(util.format(script, options.bandwidth.download)); | ||
} catch (e) { | ||
log.error(e); | ||
reject(Error()); | ||
} | ||
}); | ||
delayLossScripts.forEach((script) => { | ||
log.info(util.format(script, options.device, options.latency, options.pl)); | ||
try { | ||
execSync(util.format(script, options.device, options.latency, options.pl)); | ||
} catch (e) { | ||
log.error(e); | ||
reject(Error()); | ||
} | ||
}); | ||
resolve(); | ||
}); | ||
} | ||
}, | ||
}; |
{ | ||
"description": "Simple Linux Traffic Control", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"bin": "./bin/sltc.js", | ||
@@ -17,2 +17,4 @@ "main": "./lib/sltc.js", | ||
"eslint": "^2.9.0", | ||
"eslint-config-airbnb-base": "3.0.1", | ||
"eslint-plugin-import": "1.8.1", | ||
"mocha": "^2.5.2" | ||
@@ -25,3 +27,4 @@ }, | ||
"test": "mocha", | ||
"lint": "eslint ." | ||
"lint": "eslint .", | ||
"travis": "npm run lint" | ||
}, | ||
@@ -28,0 +31,0 @@ "name": "sltc", |
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
6426
6
144
6