iptabler-smart
v1.0.1
Published
A wrapper for the iptabler module with some useful functionality
Downloads
3
Readme
iptabler-smart
A wrapper for the iptabler module with some useful functionality.
The main thing this module offers is the ability to only apply rules that aren't already applied. Currently iptabler will raise a signal and cause your program to terminate if you apply a rule that already exists. This module's applyRuleSafely() method will check against existing firewall rules and only create the rule if it doesn't already exist.
Usage
const fw = new iptabler();
const createMyChain = {
sudo: true,
table: 'nat',
new_chain: 'MYCHAIN'
};
fw.applyRuleSafely(createMyChain).then(() => {
// Apply the rule safely, fetching existing rules first
return fw.applyRuleSafely(createMyChain);
}).then(() => {
// Applying the same rule again. This time it won't be applied since it is already present.
return fw.applyRuleSafely(createMyChain);
}).then(() => {
// Apply the rule safely, without first fetching existing rules
return fw.applyRuleSafely({
sudo: true,
table: 'nat',
new_chain: 'MYCHAIN2'
}, false);
}).then(() => {
// For a 'templated' rule, use a string replacement map
return fw.applyRuleSafely({
sudo: true,
table: 'nat',
append: 'MYCHAIN2',
source: 'GATEWAY_IP',
in_interface: 'WAN',
jump: 'RETURN'
}, false, {
GATEWAY_IP: '192.168.1.1',
WAN: 'eth0
});
/*
* This rule gets transformed:
* GATEWAY_IP => 192.168.1.1
* WAN => eth0
*/
}).then(() => {
let newChainRules = [
{
sudo: true,
table: 'nat',
append: 'POSTROUTING',
jump: 'MYCHAIN'
},
{
sudo: true,
table: 'nat',
append: 'MYCHAIN',
out_interface: 'cni0',
jump: 'MASQUERADE'
}
];
/*
* Apply the array of rules. Fetches existing rules at the beginning,
* and then applies the rules after.
*/
return fw.applyRulesSafely(newChainRules);
}).then(() => {
// Fetch all the rules manually so they will be up-to-date for next time
return fw.getAllRules();
}).then(() => {
console.log('done');
});
Methods
|Method name|Parameters|Function| |-----------|----------|--------| |getAllRules|None|Fetches existing rules so we know what has been applied| |applyRuleSafely|rule - iptabler rule updateCurrent - call getAllRules before running this rule (default: true)replacements - optional key:value map of string replacements|Apply rule only if it hasn't been applied already| |applyRulesSafely|rules - array of iptabler rulesreplacements - optional key:value map of string replacements|Calls applyRuleSafely for each rule in the array, only calling getAllRules at the beginning| |applyRule|rule - iptabler rulereplacements - optional key:value map of string replacements|Simply execs iptabler rule without checking (unsafe)| |applyRules|rules - array of iptabler rulesreplacements - optional key:value map of string replacements|calls applyRule on all rules in the array (unsafe)|