From 189ea95d9d1dbf3a8ca7d9a34014d555dba24d1c Mon Sep 17 00:00:00 2001 From: Diosney Date: Fri, 26 Sep 2014 15:00:23 -0400 Subject: [PATCH] Removed empty lines. Changed code style to match overall style. --- lib/iptables/append.js | 3 +- lib/iptables/check.js | 3 +- lib/iptables/delete.js | 3 +- lib/iptables/delete_chain.js | 2 +- lib/iptables/dump.js | 310 +++++++++++++++++++++-------------- lib/iptables/flush.js | 2 +- lib/iptables/insert.js | 3 +- lib/iptables/new.js | 2 +- lib/iptables/policy.js | 2 +- lib/iptables/rename.js | 2 +- lib/iptables/replace.js | 3 +- lib/iptables/zero.js | 2 +- 12 files changed, 198 insertions(+), 139 deletions(-) diff --git a/lib/iptables/append.js b/lib/iptables/append.js index a9a2872..1db9d96 100644 --- a/lib/iptables/append.js +++ b/lib/iptables/append.js @@ -3,7 +3,6 @@ var exec = require('../child_utils').exec; var tables = require('./utils').tables; var processCommonRuleSpecs = require('./utils').processCommonRuleSpecs; - /** * Append one or more rules to the end of the selected chain. * @@ -58,4 +57,4 @@ module.exports = function (options, cb) { cb(null); } }); -}; +}; \ No newline at end of file diff --git a/lib/iptables/check.js b/lib/iptables/check.js index 57c8019..a8ed0ff 100644 --- a/lib/iptables/check.js +++ b/lib/iptables/check.js @@ -3,7 +3,6 @@ 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. * @@ -63,4 +62,4 @@ module.exports = function (options, cb) { cb(null); } }); -}; +}; \ No newline at end of file diff --git a/lib/iptables/delete.js b/lib/iptables/delete.js index 2c51b9a..2baef4c 100644 --- a/lib/iptables/delete.js +++ b/lib/iptables/delete.js @@ -3,7 +3,6 @@ var exec = require('../child_utils').exec; var tables = require('./utils').tables; var processCommonRuleSpecs = require('./utils').processCommonRuleSpecs; - /** * Delete one or more rules from the selected chain. * @@ -63,4 +62,4 @@ module.exports = function (options, cb) { cb(null); } }); -}; +}; \ No newline at end of file diff --git a/lib/iptables/delete_chain.js b/lib/iptables/delete_chain.js index b80846b..64c37d8 100644 --- a/lib/iptables/delete_chain.js +++ b/lib/iptables/delete_chain.js @@ -66,4 +66,4 @@ module.exports = function (/* options?, cb */) { cb(null); } }); -}; +}; \ No newline at end of file diff --git a/lib/iptables/dump.js b/lib/iptables/dump.js index 3bf26e3..53e5740 100644 --- a/lib/iptables/dump.js +++ b/lib/iptables/dump.js @@ -3,41 +3,35 @@ var shell_quote = require('shell-quote'); var tables = require('./utils').tables; - -/** - * Print out all the rules in iptables-save - * - * @param options - * @param cb(err, {table:[rule,...]}) - */ -var MAPPED_ARG = { - '-A': '--append', - '-D': '--delete', - '-I': '--insert', - '-R': '--replace', - '-L': '--list', - '-S': '--list-rules', - '-F': '--flush', - '-Z': '--zero', - '-N': '--new-chain', - '-X': '--delete-chain', - '-P': '--policy', - '-E': '--rename-chain', - '-d': '--destination', - '--dport': '--destination-port', +var MAPPED_ARGS = { + '-A' : '--append', + '-D' : '--delete', + '-I' : '--insert', + '-R' : '--replace', + '-L' : '--list', + '-S' : '--list-rules', + '-F' : '--flush', + '-Z' : '--zero', + '-N' : '--new-chain', + '-X' : '--delete-chain', + '-P' : '--policy', + '-E' : '--rename-chain', + '-d' : '--destination', + '--dport' : '--destination-port', '--dports': '--destination-ports', - '-i': '--in-interface', - '-j': '--jump', - '-f': '--fragment', - '-g': '--goto', - '-m': '--match', - '-o': '--out-interface', - '-p': '--protocol', - '-s': '--source', - '-t': '--table', - '--sport': '--source-port', + '-i' : '--in-interface', + '-j' : '--jump', + '-f' : '--fragment', + '-g' : '--goto', + '-m' : '--match', + '-o' : '--out-interface', + '-p' : '--protocol', + '-s' : '--source', + '-t' : '--table', + '--sport' : '--source-port', '--sports': '--source-ports' }; + var MATCH_EXTENSIONS = [ 'addrtype', 'ah', @@ -78,8 +72,9 @@ var MATCH_EXTENSIONS = [ 'tos', 'ttl', 'u32', - 'unclean' + 'unclean' ]; + var COMMON_ARGS = [ 'protocol', 'jump', @@ -88,11 +83,31 @@ var COMMON_ARGS = [ 'destination', 'in-interface', 'out-interface', - 'fragment', -] -module.exports = function (options, cb) { - if (typeof arguments[0] != 'object') { - throw new Error('Invalid arguments. Signature: (options, callback?)'); + 'fragment' +]; + +/** + * Print out all the rules in `iptables-save`. + * + * @param options + * @param cb + */ +module.exports = function (/* options?, cb */) { + var options; + var cb; + + if (typeof arguments[0] == 'function') { + options = {}; + cb = arguments[0]; + } + else if (typeof arguments[0] == 'object' + && typeof arguments[1] == 'function') { + + options = arguments[0]; + cb = arguments[1]; + } + else { + throw new Error('Invalid arguments. Signature: [options,] callback'); } var table = options.table; @@ -109,6 +124,7 @@ module.exports = function (options, cb) { * Build cmd to execute. */ var cmd = [ipt_cmd]; + if (table) { cmd.push('--table', table); } @@ -124,8 +140,10 @@ module.exports = function (options, cb) { err.code = error.code; cb(err); + return; } - else if (cb) { + + if (cb) { var lines = String(stdout).split(/\n/g); var tables = Object.create(null); var chains = Object.create(null); @@ -133,135 +151,181 @@ module.exports = function (options, cb) { var chain = ''; var policy = ''; var rules = []; + + /** + * Adds the processed contents to the current chain. + * Procedure to save code lines. + */ function commit_chain() { - if (chain === '') return; + if (chain === '') { + return; + } + if (chains[chain]) { chains[chain].rules = chains[chain].rules.concat(rules); } else { chains[chain] = { policy: policy, - rules: rules + rules : rules } } + chain = ''; policy = ''; rules = []; } + lines.forEach(function (line) { line = line.trim(); + if (line === 'COMMIT') { commit_chain(); + tables[table] = { chains: chains }; + table = ''; chains = Object.create(null); return; } + var c = line.charAt(0); - if (c === '#') return; - else if (c === '*') { + + if (c === '#') { + return; + } + + if (c === '*') { table = line.slice(1).trim(); return; } - else if (c === ':') { + + if (c === ':') { commit_chain(); + var parts = /^:(\S+)\s+(\S+)/.exec(line); - chain = parts[1]; + chain = parts[1]; policy = parts[2]; return; } - else { - var args = shell_quote.parse(line); - var rule = Object.create(null); - var negated = false; - var option = ''; - var in_target = false; - var match = null; - var matches = Object.create(null); - var target_options = null; - var had_match = false; - args.forEach(function (arg) { - if (option === '') { - arg = Object.prototype.hasOwnProperty.call(MAPPED_ARG, arg) ? MAPPED_ARG[arg] : arg; - if (arg.charAt(0) === '-') { - option = arg.replace(/^[\-]+/,''); - if ('random' === option) { - rule.random = true; - option = ''; - } - else if ('ports' === option) { - rule['source-ports'] = rule['source-ports'] || []; - rule['destination-ports'] = rule['destination-ports'] || []; - } - else if ('--source-ports' === option) { - rule['source-ports'] = rule['source-ports'] || []; - } - else if ('--destination-ports' === option) { - rule['destination-ports'] = rule['destination-ports'] || []; - } + + var args = shell_quote.parse(line); + var rule = Object.create(null); + var negated = false; + var option = ''; + var in_target = false; + var match = null; + var matches = Object.create(null); + var target_options = null; + var had_match = false; + + args.forEach(function (arg) { + if (option === '') { + arg = Object.prototype.hasOwnProperty.call(MAPPED_ARGS, arg) + ? MAPPED_ARGS[arg] + : arg; + + if (arg.charAt(0) === '-') { + option = arg.replace(/^[\-]+/, ''); + + if (option === 'random') { + rule.random = true; + option = ''; } - else if (arg === '!') { - negated = true; - } - } - else { - if (option === 'jump') { - match = null; - in_target = true; + else if (option === 'ports') { + rule['source-ports'] = rule['source-ports'] || []; + rule['destination-ports'] = rule['destination-ports'] || []; } - var configure_target = rule; - if (MATCH_EXTENSIONS.indexOf(option) !== -1) { - had_match = true; - matches[option] = match = Object.create(null); - option = ''; - negated = false; - return; + else if (option === '--source-ports') { + rule['source-ports'] = rule['source-ports'] || []; } - else if (COMMON_ARGS.indexOf(option) === -1) { - // little bit wonky to detect if target_options was used - if (in_target) configure_target = target_options || (target_options = Object.create(null)); - else configure_target = match; + else if (option === '--destination-ports') { + rule['destination-ports'] = rule['destination-ports'] || []; } - if (option === 'match' || option === 'protocol') { - had_match = true; - matches[arg] = match = matches[arg] || Object.create(null); - option = ''; - negated = false; + } + else if (arg === '!') { + negated = true; + } + } + else { + if (option === 'jump') { + match = null; + in_target = true; + } + + var configure_target = rule; + + if (MATCH_EXTENSIONS.indexOf(option) !== -1) { + had_match = true; + matches[option] = match = Object.create(null); + option = ''; + negated = false; + return; + } + + if (COMMON_ARGS.indexOf(option) === -1) { + // Little bit wonky to detect if target_options was used. + if (in_target) { + configure_target = target_options || (target_options = Object.create(null)); } - else if (option === 'append') { - if (arg != chain) commit_chain(); - chain = arg; - option = ''; - negated = false; + else { + configure_target = match; } - else if (option === 'ports') { - configure_target['source-ports'].push(arg); - configure_target['destination-ports'].push(arg); - option = ''; - negated = false; + } + + if (option === 'match' || option === 'protocol') { + had_match = true; + matches[arg] = match = matches[arg] || Object.create(null); + option = ''; + negated = false; + } + else if (option === 'append') { + if (arg != chain) { + commit_chain(); + } + + chain = arg; + option = ''; + negated = false; + } + else if (option === 'ports') { + configure_target['source-ports'].push(arg); + configure_target['destination-ports'].push(arg); + option = ''; + negated = false; + } + else { + var val = negated + ? '!' + arg + : arg; + + if (Array.isArray(rule[option])) { + configure_target[option].push(val); } else { - var val = negated ? '!' + arg : arg; - if (Array.isArray(rule[option])) { - configure_target[option].push(val); - } - else { - configure_target[option] = val; - } - option = ''; - negated = false; + configure_target[option] = val; } + + option = ''; + negated = false; } - }); - if (had_match) rule.matches = matches; - if (target_options != null) rule.target_options = target_options; - rules.push(rule); + } + }); + + if (had_match) { + rule.matches = matches; + } + + if (target_options != null) { + rule.target_options = target_options; } + + rules.push(rule); }); + cb(null, tables); } }); -}; - +}; \ No newline at end of file diff --git a/lib/iptables/flush.js b/lib/iptables/flush.js index 1227d08..b57dc3f 100644 --- a/lib/iptables/flush.js +++ b/lib/iptables/flush.js @@ -66,4 +66,4 @@ module.exports = function (/* options?, cb */) { cb(null); } }); -}; +}; \ No newline at end of file diff --git a/lib/iptables/insert.js b/lib/iptables/insert.js index 93413c9..4355442 100644 --- a/lib/iptables/insert.js +++ b/lib/iptables/insert.js @@ -3,7 +3,6 @@ var exec = require('../child_utils').exec; var tables = require('./utils').tables; var processCommonRuleSpecs = require('./utils').processCommonRuleSpecs; - /** * Insert one or more rules in the selected chain as the given rule number. * @@ -63,4 +62,4 @@ module.exports = function (options, cb) { cb(null); } }); -}; +}; \ No newline at end of file diff --git a/lib/iptables/new.js b/lib/iptables/new.js index 345f090..206690d 100644 --- a/lib/iptables/new.js +++ b/lib/iptables/new.js @@ -53,4 +53,4 @@ module.exports = function (options, cb) { cb(null); } }); -}; +}; \ No newline at end of file diff --git a/lib/iptables/policy.js b/lib/iptables/policy.js index 5578377..0013849 100644 --- a/lib/iptables/policy.js +++ b/lib/iptables/policy.js @@ -57,4 +57,4 @@ module.exports = function (options, cb) { cb(null); } }); -}; +}; \ No newline at end of file diff --git a/lib/iptables/rename.js b/lib/iptables/rename.js index 3036b81..2a5d889 100644 --- a/lib/iptables/rename.js +++ b/lib/iptables/rename.js @@ -57,4 +57,4 @@ module.exports = function (options, cb) { cb(null); } }); -}; +}; \ No newline at end of file diff --git a/lib/iptables/replace.js b/lib/iptables/replace.js index d9200c3..048f5eb 100644 --- a/lib/iptables/replace.js +++ b/lib/iptables/replace.js @@ -3,7 +3,6 @@ var exec = require('../child_utils').exec; var tables = require('./utils').tables; var processCommonRuleSpecs = require('./utils').processCommonRuleSpecs; - /** * Replace a rule in the selected chain. * @@ -63,4 +62,4 @@ module.exports = function (options, cb) { cb(null); } }); -}; +}; \ No newline at end of file diff --git a/lib/iptables/zero.js b/lib/iptables/zero.js index 716dce0..cd23ad4 100644 --- a/lib/iptables/zero.js +++ b/lib/iptables/zero.js @@ -70,4 +70,4 @@ module.exports = function (/* options?, cb */) { cb(null); } }); -}; +}; \ No newline at end of file