Update server.py

This commit is contained in:
Vicente 2023-03-09 23:14:12 +01:00
parent 37a173eb65
commit 88dfdd1dfb
1 changed files with 22 additions and 22 deletions

View File

@ -877,29 +877,29 @@ def snat4(snat_target):
table.autocommit = False table.autocommit = False
new_rule = get_snat4_rule() new_rule = get_snat4_rule()
if not chain.rules: if not chain.rules:
# if there are no rules in the chain, insert the new rule directly # if there are no rules in the chain, insert the new rule directly
logInfo(f'Added POSTROUTING rule for source network {new_rule.src} to SNAT target {snat_target}') logInfo(f'Added POSTROUTING rule for source network {new_rule.src} to SNAT target {snat_target}')
chain.insert_rule(new_rule) chain.insert_rule(new_rule)
else: else:
for position, rule in enumerate(chain.rules): for position, rule in enumerate(chain.rules):
match = all(( match = all((
new_rule.get_src() == rule.get_src(), new_rule.get_src() == rule.get_src(),
new_rule.get_dst() == rule.get_dst(), new_rule.get_dst() == rule.get_dst(),
new_rule.target.parameters == rule.target.parameters, new_rule.target.parameters == rule.target.parameters,
new_rule.target.name == rule.target.name new_rule.target.name == rule.target.name
)) ))
if position == 0: if position == 0:
if not match: if not match:
logInfo(f'Added POSTROUTING rule for source network {new_rule.src} to SNAT target {snat_target}') logInfo(f'Added POSTROUTING rule for source network {new_rule.src} to SNAT target {snat_target}')
chain.insert_rule(new_rule) chain.insert_rule(new_rule)
else: else:
if match: if match:
logInfo(f'Remove rule for source network {new_rule.src} to SNAT target {snat_target} from POSTROUTING chain at position {position}') logInfo(f'Remove rule for source network {new_rule.src} to SNAT target {snat_target} from POSTROUTING chain at position {position}')
chain.delete_rule(rule) chain.delete_rule(rule)
table.commit() table.commit()
table.autocommit = True table.autocommit = True
else: else:
snat_rule("ip", snat_target) snat_rule("ip", snat_target)
except: except: