Skip to content

Commit

Permalink
missed a file
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck committed Sep 4, 2014
1 parent 03840ff commit e8eae8b
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions lib/iptables/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var exec = require('child_process').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(' '), 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);
}
});
};

0 comments on commit e8eae8b

Please sign in to comment.