Check if rules exist before adding them

This commit is contained in:
Pablo Carranza Velez 2016-08-16 09:30:38 -03:00
parent c4ea2c77a0
commit 145d124410

View File

@ -281,9 +281,14 @@ exports.validateKeys = (options, validSet) ->
invalidKeys = _.keys(_.omit(options, validSet))
throw new Error("Using #{invalidKeys.join(', ')} is not allowed.") if !_.isEmpty(invalidKeys)
checkAndAddIptablesRule = (rule) ->
execAsync("iptables -C #{rule}")
.catch ->
execAsync("iptables -A #{rule}")
exports.createIpTablesRules = ->
allowedInterfaces = ['tun0', 'docker0', 'lo']
Promise.each allowedInterfaces, (iface) ->
execAsync("iptables -A INPUT -p tcp --dport #{config.listenPort} -i #{iface} -j ACCEPT")
checkAndAddIptablesRule("INPUT -p tcp --dport #{config.listenPort} -i #{iface} -j ACCEPT")
.then ->
execAsync("iptables -A INPUT -p tcp --dport #{config.listenPort} -j REJECT")
checkAndAddIptablesRule("INPUT -p tcp --dport #{config.listenPort} -j REJECT")