Commit 1eecd565 authored by Vitaly Lipatov's avatar Vitaly Lipatov

gateway: clear bans through durable unban queue

parent ab82341f
...@@ -84,8 +84,7 @@ if [ "$command" = "clear" ] ; then ...@@ -84,8 +84,7 @@ if [ "$command" = "clear" ] ; then
echo "Usage: eterban clear --force" >&2 echo "Usage: eterban clear --force" >&2
exit 2 exit 2
fi fi
ipset flush $setname /usr/share/eterban/autoban_cli.py clear
ipset flush $setname_ipv6
exit exit
fi fi
......
...@@ -21,6 +21,7 @@ CONFIG_PATH = '/etc/eterban/settings.ini' ...@@ -21,6 +21,7 @@ CONFIG_PATH = '/etc/eterban/settings.ini'
META_PREFIX = 'eterban:meta:' META_PREFIX = 'eterban:meta:'
SCHEDULE_KEY = 'eterban:unban_schedule' SCHEDULE_KEY = 'eterban:unban_schedule'
PERMANENT_KEY = 'eterban:permanent' PERMANENT_KEY = 'eterban:permanent'
ACTIVE_BANS_KEY = 'eterban:active_bans'
def format_duration(seconds): def format_duration(seconds):
...@@ -173,6 +174,26 @@ def cmd_permanent(): ...@@ -173,6 +174,26 @@ def cmd_permanent():
print(f" {ip}") print(f" {ip}")
def cmd_clear():
"""Queue durable unban commands for every authoritative active ban."""
r = get_redis()
pipeline = r.pipeline(transaction=False)
queued = 0
for member in r.sscan_iter(ACTIVE_BANS_KEY):
ip = member.decode() if isinstance(member, bytes) else member
pipeline.xadd('eterban:commands', {
'command': 'unban',
'ip': ip,
'by': 'all bans cleared by administrator',
})
queued += 1
if queued % 1000 == 0:
pipeline.execute()
if queued % 1000:
pipeline.execute()
print(f"Queued {queued} unban commands")
def usage(): def usage():
print("""Usage: autoban_cli.py <command> [args] print("""Usage: autoban_cli.py <command> [args]
...@@ -181,6 +202,7 @@ Commands: ...@@ -181,6 +202,7 @@ Commands:
reset <ip> - reset offense counter for IP reset <ip> - reset offense counter for IP
pending - list pending auto-unbans pending - list pending auto-unbans
permanent - list permanent bans permanent - list permanent bans
clear - queue unban for every active ban
""") """)
sys.exit(1) sys.exit(1)
...@@ -209,6 +231,9 @@ def main(): ...@@ -209,6 +231,9 @@ def main():
elif cmd == 'permanent': elif cmd == 'permanent':
cmd_permanent() cmd_permanent()
elif cmd == 'clear':
cmd_clear()
else: else:
print(f"Unknown command: {cmd}") print(f"Unknown command: {cmd}")
usage() usage()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment