diff --git a/expose_forwards.ps1 b/expose_forwards.ps1 index 857f2a1..72a38ec 100644 --- a/expose_forwards.ps1 +++ b/expose_forwards.ps1 @@ -1,6 +1,39 @@ #Requires -Modules powershell-yaml $ErrorActionPreference = 'Stop' +function Test-IptablesChain { + param ( + [string]$Chain, + [string]$Table = 'nat' + ) + + $output = iptables -t $Table -S $Chain + $reference = '-N {0}' -f $Chain + + #check $output in case $Chain has no rules + #otherwise check the first line with $output[0] + $output.Count -gt 0 ` + -and ($output -eq $reference ` + -or ` + $output[0] -eq $reference) +} +function Add-IptablesChain { + param ( + [string]$Chain, + [string]$Table = 'nat' + ) + + if(-not (Test-IptablesChain -Chain $Chain)) { + iptables -t $Table -N $Chain + } +} + +# setup SWARM-NAT chain +$chain = 'SWARM-NAT' +Write-Output ('Create chain {0}' -f $chain) +Add-IptablesChain -Chain $chain + + foreach($yaml in (Get-ChildItem -Filter '*.yml')) { Write-Output ('Processing {0}' -f $yaml) $definition = Get-Content -Path $yaml -Raw | ConvertFrom-Yaml