Skip to content

Commit

Permalink
Fix duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
badrogger committed Nov 29, 2024
1 parent d420f58 commit 7a7ffe1
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions node_cli/core/nftables.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ def rule_exists(self, chain: str, new_rule_expr: list[dict]) -> bool:
existing_rules = self.get_rules(chain)

for rule in existing_rules:
if rule.get('expr') == new_rule_expr:
expr = rule.get('expr')
for i, statement in enumerate(expr):
if 'counter' in statement:
expr[i] = {'counter': None}
rule['counter'] = None
if expr == new_rule_expr:
return True
return False

Expand All @@ -155,24 +160,21 @@ def add_drop_rule_if_node_exists(self, protocol: str) -> None:
{"drop": None}
]
if not self.rule_exists(self.chain, expr):
# cmd = {
# 'nftables': [
# {
# 'add': {
# 'rule': {
# 'family': self.family,
# 'table': self.table,
# 'chain': self.chain,
# 'expr': expr,
# }
# }
# }
# ]
# }
# self.execute_cmd(cmd)
cmd = f'add rule {self.family} {self.table} {self.chain} ip protocol {protocol} counter drop'
logger.info('CMD %s', cmd)
self.nft.cmd(cmd)
cmd = {
'nftables': [
{
'add': {
'rule': {
'family': self.family,
'table': self.table,
'chain': self.chain,
'expr': expr,
}
}
}
]
}
self.execute_cmd(cmd)
logger.info('Added drop rule for %s', protocol)

def add_rule_if_not_exists(self, rule: Rule) -> None:
Expand Down

0 comments on commit 7a7ffe1

Please sign in to comment.