Skip to content

Commit

Permalink
Prioritize allow rules on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
jjxtra committed Aug 17, 2020
1 parent 223448f commit 2a83e40
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions IPBanCore/Linux/IPBanLinuxBaseFirewall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,15 @@ protected bool CreateOrUpdateRule(string ruleName, string action, string hashTyp
RunProcess(IpTablesProcess, true, out IReadOnlyList<string> lines, "-L --line-numbers");
string portString = " ";
bool replaced = false;
bool block = (action == "DROP");

if (allowedPortsArray != null && allowedPortsArray.Length != 0)
{
string portList = (action == "DROP" ? IPBanFirewallUtility.GetBlockPortRangeString(allowedPorts) :
string portList = (block ? IPBanFirewallUtility.GetBlockPortRangeString(allowedPorts) :
IPBanFirewallUtility.GetPortRangeStringAllow(allowedPorts));
portString = " -m multiport -p tcp --dports " + portList.Replace('-', ':') + " "; // iptables uses ':' instead of '-' for range
}

string ruleNameWithSpaces = " " + ruleName + " ";
foreach (string line in lines)
{
Expand All @@ -221,8 +224,9 @@ protected bool CreateOrUpdateRule(string ruleName, string action, string hashTyp
}
if (!replaced)
{
// add a new rule
RunProcess(IpTablesProcess, true, $"-A INPUT -m set{portString}--match-set \"{ruleName}\" src -j {action}");
// add a new rule, for block add to end of list (lower priority) for allow add to begin of list (higher priority)
string addCommand = (block ? "-A" : "-I");
RunProcess(IpTablesProcess, true, $"{addCommand} INPUT -m set{portString}--match-set \"{ruleName}\" src -j {action}");
}

if (cancelToken.IsCancellationRequested)
Expand Down

0 comments on commit 2a83e40

Please sign in to comment.