Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added .list() that returns all rules in the given chain

  • Loading branch information...
commit 8d0fc5e7b2d01be932eb3f16bba5b2828dd644a0 1 parent ffa97ad
@pkrumins authored
Showing with 41 additions and 1 deletion.
  1. +41 −1 index.js
View
42 index.js
@@ -1,20 +1,59 @@
var spawn = require('child_process').spawn;
+var lazy = require('lazy');
exports.allow = function (rule) {
rule.target = 'ACCEPT';
+ if (!rule.action) rule.action = '-A';
newRule(rule);
}
exports.drop = function (rule) {
rule.target = 'DROP';
+ if (!rule.action) rule.action = '-A';
newRule(rule);
}
exports.reject = function (rule) {
rule.target = 'REJECT';
+ if (!rule.action) rule.action = '-A';
newRule(rule);
}
+exports.list = function(chain, cb) {
+ var rule = {
+ list : true,
+ chain : chain,
+ action : '-L',
+ sudo : true
+ };
+
+ lazy(iptables(rule).stdout)
+ .lines
+ .map(String)
+ .skip(2)
+ .map(function (line) {
+ // packets, bytes, target, pro, opt, in, out, src, dst, opts
+ var fields = line.trim().split(/\s+/, 9);
+ return {
+ parsed : {
+ packets : fields[0],
+ bytes : fields[1],
+ target : fields[2],
+ protocol : fields[3],
+ opt : fields[4],
+ in : fields[5],
+ out : fields[6],
+ src : fields[7],
+ dst : fields[8]
+ },
+ raw : line.trim()
+ };
+ })
+ .join(function (rules) {
+ cb(rules);
+ })
+}
+
exports.newRule = newRule;
exports.deleteRule = deleteRule;
@@ -31,6 +70,7 @@ function iptables (rule) {
proc.stderr.on('data', function (buf) {
console.error(buf.toString());
});
+ return proc;
}
function iptablesArgs (rule) {
@@ -38,7 +78,6 @@ function iptablesArgs (rule) {
if (!rule.chain) rule.chain = 'INPUT';
- if (!rule.action) rule.action = '-A';
if (rule.chain) args = args.concat([rule.action, rule.chain]);
if (rule.protocol) args = args.concat(["-p", rule.protocol]);
if (rule.src) args = args.concat(["--src", rule.src]);
@@ -48,6 +87,7 @@ function iptablesArgs (rule) {
if (rule.in) args = args.concat(["-i", rule.in]);
if (rule.out) args = args.concat(["-o", rule.out]);
if (rule.target) args = args.concat(["-j", rule.target]);
+ if (rule.list) args = args.concat(["-n", "-v"]);
return args;
}
Please sign in to comment.
Something went wrong with that request. Please try again.