-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathreplace.js
More file actions
executable file
·64 lines (53 loc) · 1.36 KB
/
replace.js
File metadata and controls
executable file
·64 lines (53 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var exec = require('../child_utils').exec;
var tables = require('./utils').tables;
var processCommonRuleSpecs = require('./utils').processCommonRuleSpecs;
/**
* Replace a rule in 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, '--replace'];
var args = [];
/*
* Process options.
*/
if (typeof options.chain != 'undefined') {
args = args.concat(options.chain);
}
if (typeof options.rulenum != 'undefined') {
args = args.concat(options.rulenum);
}
var common_rule_specs = processCommonRuleSpecs(options);
args = args.concat(common_rule_specs);
/*
* Execute command.
*/
exec(cmd.concat(args).join(' '), {queue: options.queue}, 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);
}
});
};