Skip to content

Commit

Permalink
iptables.utils:
Browse files Browse the repository at this point in the history
* Removed specific `comments` match in favor of the `matches` object.
* Fixed issue function don't processing correctly negated argument (with leading '!').
  • Loading branch information
diosney committed Sep 26, 2014
1 parent 3066954 commit 25e5cd2
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions lib/iptables/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,62 @@ exports.tables = {
};

/**
* Process common rule specs for `append`, `check`, `delete`, `insert` and `replace`.
* Processes an iptables possible argument to build the actual query args.
*
* @param options
* @returns {Array}
* @param {String} property The property value to be processed (f.e: the content of the `protocol` property).
* @param {String} flag The iptables related argument name (f.e: '--destination').
* @param {Array} args The actual arguments construction where the cmd line is being saved to.
* @param {Boolean} is_boolean If true then the property is boolean and don't use a value. False otherwise. F.e: `fragment`.
*/
function manage_arg(property, flag, args) {
function manage_arg(property, flag, args, is_boolean) {
if (property != null) {
var val = String(property);
var index = val.indexOf('!');
var index = val.indexOf('!');
var is_boolean = is_boolean || false; // Defaults to false.

if (index !== -1) {
args.push('!', flag, val.slice(index));
args.push('!', flag);

if (!is_boolean) {
args.push(val.slice(index + 1)); // To not include the ! character.
}
}
else {
args.push(flag, val);
args.push(flag);

if (!is_boolean) {
args.push(val);
}
}
}
}
function hasOwnProperty(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);

/**
* TODO: Ask @bmeck the reasoning behind overriding the native method.
*
* @param {Object} object Object to be tested for property inclusion.
* @param {String} key Property to be tested against the object.
* @return {Boolean}
*/
function hasOwnProperty(object, key) {
return Object.prototype.hasOwnProperty.call(object, key);
}

/**
* Process common rule specs for `append`, `check`, `delete`, `insert` and `replace`.
*
* @param {Object} options
* @returns {Array}
*/
exports.processCommonRuleSpecs = function (options) {
var args = [];

if (options.comment != null) {
args.push('-m', 'comment', '--comment', String(options.comment).slice(0,255).replace(/[^A-Za-z_]/g,'\\$&'));
}

manage_arg(options.protocol, '--protocol', args);
manage_arg(options.source, '--source', args);
manage_arg(options.destination, '--destination', args);
manage_arg(options['in-interface'], '--in-interface', args);
manage_arg(options['out-interface'], '--out-interface', args);

if (options.fragment != null) {
var val = String(options.fragment);
var index = val.indexOf('!');
if (index !== -1) {
args.push('!', '--fragment');
}
else {
args.push('--fragment');
}
}
manage_arg(options.fragment, '--fragment', args, true);

/*
* Matches processing.
Expand All @@ -66,7 +80,7 @@ exports.processCommonRuleSpecs = function (options) {
if (this_match != null && typeof this_match === 'object') {
for (var match_option in this_match) {
if (hasOwnProperty(this_match, match_option)) {
manage_arg(this_match[match_option], '--' + match_option, args);
manage_arg(this_match[match_option], '--' + match_option, args);
}
}
}
Expand All @@ -92,4 +106,4 @@ exports.processCommonRuleSpecs = function (options) {
}

return args;
};
};

0 comments on commit 25e5cd2

Please sign in to comment.