-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from bmeck/queuedSpawn
* Added queued spawn support to prevent XTables concurrency fail lock. * Added an `iptables.dump` method to parse out `iptables-save`. * Added an `iptablables.check` method to check the existence one or more rules from the selected chain.
- Loading branch information
Showing
26 changed files
with
540 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var processQueue = require('process-queue'); | ||
|
||
var default_queue = processQueue.createQueue({concurrency:1}); | ||
|
||
exports.exec = function (line, opts, cb) { | ||
var stdout, stdout_length, stderr, stderr_length; | ||
|
||
// avoid allocation | ||
if (cb) { | ||
stdout = []; | ||
stdout_length = 0; | ||
stderr = []; | ||
stderr_length = 0; | ||
} | ||
|
||
var child; | ||
var queue = opts.queue || default_queue; | ||
if (cb) { | ||
queue = queue.wrap({ | ||
child: function (spawned_child, next) { | ||
child = spawned_child; | ||
if (cb) { | ||
child.stdout.on('data', function (data) { | ||
if (!Buffer.isBuffer(data)) data = new Buffer(data); | ||
stdout_length += data.length; | ||
stdout.push(data); | ||
}); | ||
child.stderr.on('data', function (data) { | ||
if (!Buffer.isBuffer(data)) data = new Buffer(data); | ||
stderr_length += data.length; | ||
stderr.push(data); | ||
}); | ||
} | ||
next(null, child); | ||
} | ||
}); | ||
} | ||
var cmd = { | ||
spawnOptions: ['sh', ['-c', 'exec ' + line]] | ||
}; | ||
function onFinish(err) { | ||
if (!cb) return; | ||
if (!child) { | ||
cb(new Error('there was a problem spawning the child process')); | ||
return; | ||
} | ||
if (err) { | ||
err.killed = child.killed; | ||
err.code = child.exitCode; | ||
err.signal = child.signalCode; | ||
} | ||
cb(err, | ||
stdout_length ? String(Buffer.concat(stdout, stdout_length)) : '', | ||
stderr_length ? String(Buffer.concat(stderr, stderr_length)) : '' | ||
); | ||
} | ||
queue.push(cmd, onFinish); | ||
|
||
// cleanup for gc | ||
cmd = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var exec = require('../child_utils').exec; | ||
|
||
var tables = require('./utils').tables; | ||
var processCommonRuleSpecs = require('./utils').processCommonRuleSpecs; | ||
|
||
|
||
/** | ||
* Checks the existence one or more rules from the selected chain. | ||
* | ||
* @param options | ||
* @param cb | ||
*/ | ||
module.exports = function (options, cb) { | ||
if (typeof arguments[0] != 'object') { | ||
throw new Error('Invalid arguments. Signature: (options, callback?)'); | ||
} | ||
|
||
var table = (typeof options.table != 'undefined') | ||
? options.table | ||
: tables.filter; | ||
|
||
var ipt_cmd = (options.sudo) | ||
? 'sudo ' | ||
: ''; | ||
|
||
ipt_cmd += (options.ipv6) | ||
? 'ip6tables' | ||
: 'iptables'; | ||
|
||
/* | ||
* Build cmd to execute. | ||
*/ | ||
var cmd = [ipt_cmd, '--table', table, '--check']; | ||
var args = []; | ||
|
||
/* | ||
* Process options. | ||
*/ | ||
if (typeof options.chain != 'undefined') { | ||
args = args.concat(options.chain); | ||
} | ||
|
||
if (typeof options.rulenum != 'undefined') { | ||
args = args.concat(options.rulenum); | ||
} | ||
else { | ||
var common_rule_specs = processCommonRuleSpecs(options); | ||
args = args.concat(common_rule_specs); | ||
} | ||
|
||
/* | ||
* Execute command. | ||
*/ | ||
exec(cmd.concat(args).join(' '), {queue: options.cmdQueue}, function (error, stdout, stderror) { | ||
if (error && cb) { | ||
var err = new Error(stderror.split('\n')[0]); | ||
err.cmd = cmd.concat(args).join(' '); | ||
err.code = error.code; | ||
|
||
cb(err); | ||
} | ||
else if (cb) { | ||
cb(null); | ||
} | ||
}); | ||
}; |
Oops, something went wrong.