Permalink
Browse files

now supports chain, protocol, src, dst, dport, sport, sudo args

  • Loading branch information...
1 parent 65cdbc1 commit 9da63cc59f3e9037f740063523e23f17025805ed @pkrumins committed Apr 1, 2011
Showing with 34 additions and 0 deletions.
  1. +34 −0 index.js
View
34 index.js
@@ -0,0 +1,34 @@
+var spawn = require('process').spawn;
+
+exports.allow = function (rule) {
+ rule.target = 'ACCEPT';
+ iptables(rule);
+}
+
+exports.deny = function (rule) {
+ rule.target = 'DROP';
+ iptables(rule);
+}
+
+function iptables (rule) {
+ var cmd = 'iptables';
+ var args = [];
+
+ if (rule.chain) args = args.concat(["-I", rule.chain]);
+ if (rule.protocol) args = args.concat(["-p", rule.protocol]);
+ if (rule.src) args = args.concat(["--src", rule.src]);
+ if (rule.dst) args = args.concat(["--dst", rule.dst]);
+ if (rule.dport) args = args.concat(["--dport", rule.dport]);
+ if (rule.sport) args = args.concat(["--sport", rule.sport]);
+
+ if (rule.sudo) {
+ cmd = 'sudo';
+ args = ['iptables'].concat(args);
+ }
+
+ var proc = spawn(cmd, args);
+ proc.stderr.on('data', function (buf) {
+ console.error(buf.toString());
+ });
+}
+

0 comments on commit 9da63cc

Please sign in to comment.