diff --git a/lib/iptables/check.js b/lib/iptables/check.js new file mode 100644 index 0000000..707266c --- /dev/null +++ b/lib/iptables/check.js @@ -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); + } + }); +};