|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | +from ipaddress import ip_address |
| 5 | +import sys |
| 6 | + |
| 7 | +import click |
| 8 | + |
| 9 | +from nyaa import create_app, models |
| 10 | +from nyaa.extensions import db |
| 11 | + |
| 12 | + |
| 13 | +def is_cidr_valid(c): |
| 14 | + '''Checks whether a CIDR range string is valid.''' |
| 15 | + try: |
| 16 | + subnet, mask = c.split('/') |
| 17 | + except ValueError: |
| 18 | + return False |
| 19 | + if int(mask) < 1 or int(mask) > 32: |
| 20 | + return False |
| 21 | + try: |
| 22 | + ip = ip_address(subnet) |
| 23 | + except ValueError: |
| 24 | + return False |
| 25 | + return True |
| 26 | + |
| 27 | + |
| 28 | +def check_str(b): |
| 29 | + '''Returns a checkmark or cross depending on the condition.''' |
| 30 | + return '\u2713' if b else '\u2717' |
| 31 | + |
| 32 | + |
| 33 | +@click.group() |
| 34 | +def rangeban(): |
| 35 | + global app |
| 36 | + app = create_app('config') |
| 37 | + |
| 38 | + |
| 39 | +@rangeban.command() |
| 40 | +@click.option('--temp/--no-temp', help='Mark this entry as one that may be ' |
| 41 | + 'cleaned out occasionally.', default=False) |
| 42 | +@click.argument('cidrrange') |
| 43 | +def ban(temp, cidrrange): |
| 44 | + if not is_cidr_valid(cidrrange): |
| 45 | + click.secho('{} is not of the format xxx.xxx.xxx.xxx/xx.' |
| 46 | + .format(cidrrange), err=True, fg='red') |
| 47 | + sys.exit(1) |
| 48 | + with app.app_context(): |
| 49 | + ban = models.RangeBan(cidr_string=cidrrange, temp=datetime.utcnow() if temp else None) |
| 50 | + db.session.add(ban) |
| 51 | + db.session.commit() |
| 52 | + click.echo('Added {} for {}.'.format('temp ban' if temp else 'ban', |
| 53 | + cidrrange)) |
| 54 | + |
| 55 | + |
| 56 | +@rangeban.command() |
| 57 | +@click.argument('cidrrange') |
| 58 | +def unban(cidrrange): |
| 59 | + if not is_cidr_valid(cidrrange): |
| 60 | + click.secho('{} is not of the format xxx.xxx.xxx.xxx/xx.' |
| 61 | + .format(cidrrange), err=True, fg='red') |
| 62 | + sys.exit(1) |
| 63 | + with app.app_context(): |
| 64 | + # Dunno why this wants _cidr_string and not cidr_string, probably |
| 65 | + # due to this all being a janky piece of shit. |
| 66 | + bans = models.RangeBan.query.filter( |
| 67 | + models.RangeBan._cidr_string == cidrrange).all() |
| 68 | + if len(bans) == 0: |
| 69 | + click.echo('Ban not found.') |
| 70 | + for b in bans: |
| 71 | + click.echo('Unbanned {}'.format(b.cidr_string)) |
| 72 | + db.session.delete(b) |
| 73 | + db.session.commit() |
| 74 | + |
| 75 | + |
| 76 | +@rangeban.command() |
| 77 | +def list(): |
| 78 | + with app.app_context(): |
| 79 | + bans = models.RangeBan.query.all() |
| 80 | + if len(bans) == 0: |
| 81 | + click.echo('No bans.') |
| 82 | + else: |
| 83 | + click.secho('ID CIDR Range Enabled Temp', bold=True) |
| 84 | + for b in bans: |
| 85 | + click.echo('{0: <6} {1: <18} {2: <7} {3: <4}' |
| 86 | + .format(b.id, b.cidr_string, |
| 87 | + check_str(b.enabled), |
| 88 | + check_str(b.temp is not None))) |
| 89 | + |
| 90 | +@rangeban.command() |
| 91 | +@click.argument('banid', type=int) |
| 92 | +@click.argument('status') |
| 93 | +def enabled(banid, status): |
| 94 | + yeses = ['true', '1', 'yes', '\u2713'] |
| 95 | + noses = ['false', '0', 'no', '\u2717'] |
| 96 | + if status.lower() in yeses: |
| 97 | + set_to = True |
| 98 | + elif status.lower() in noses: |
| 99 | + set_to = False |
| 100 | + else: |
| 101 | + click.secho('Please choose one of {} or {}.' |
| 102 | + .format(yeses, noses), err=True, fg='red') |
| 103 | + sys.exit(1) |
| 104 | + with app.app_context(): |
| 105 | + ban = models.RangeBan.query.get(banid) |
| 106 | + if not ban: |
| 107 | + click.secho('No ban with id {} found.' |
| 108 | + .format(banid), err=True, fg='red') |
| 109 | + sys.exit(1) |
| 110 | + ban.enabled = set_to |
| 111 | + db.session.add(ban) |
| 112 | + db.session.commit() |
| 113 | + click.echo('{} ban {} on {}.'.format('Enabled' if set_to else 'Disabled', |
| 114 | + banid, ban._cidr_string)) |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == '__main__': |
| 119 | + rangeban() |
0 commit comments