diff --git a/core/cli.py b/core/cli.py index ef9a075..223ba0e 100644 --- a/core/cli.py +++ b/core/cli.py @@ -138,6 +138,20 @@ def add_user(username: str, traffic_limit: int, expiration_days: int, password: except Exception as e: click.echo(f'{e}', err=True) +@cli.command('bulk-user-add') +@click.option('--traffic-gb', '-t', required=True, help='Traffic limit for each user in GB.', type=float) +@click.option('--expiration-days', '-e', required=True, help='Expiration duration for each user in days.', type=int) +@click.option('--count', '-c', required=True, help='Number of users to create.', type=int) +@click.option('--prefix', '-p', required=True, help='Prefix for usernames.', type=str) +@click.option('--start-number', '-s', default=1, help='Starting number for username suffix.', type=int) +@click.option('--unlimited', is_flag=True, default=False, help='Flag to mark users as unlimited (exempt from IP limits).') +def bulk_user_add(traffic_gb: float, expiration_days: int, count: int, prefix: str, start_number: int, unlimited: bool): + """Adds multiple users in bulk.""" + try: + cli_api.bulk_user_add(traffic_gb, expiration_days, count, prefix, start_number, unlimited) + click.echo(f"Successfully initiated the creation of {count} users with prefix '{prefix}'.") + except Exception as e: + click.echo(f'Error during bulk user addition: {e}', err=True) @cli.command('edit-user') @click.option('--username', '-u', required=True, help='Username for the user to edit', type=str) diff --git a/core/cli_api.py b/core/cli_api.py index c235dce..c5d987e 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -27,6 +27,7 @@ class Command(Enum): CHANGE_SNI_HYSTERIA2 = os.path.join(SCRIPT_DIR, 'hysteria2', 'change_sni.py') GET_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'get_user.py') ADD_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'add_user.py') + BULK_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'bulk_users.py') EDIT_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'edit_user.sh') RESET_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'reset_user.py') REMOVE_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'remove_user.py') @@ -285,6 +286,24 @@ def add_user(username: str, traffic_limit: int, expiration_days: int, password: run_cmd(command) +def bulk_user_add(traffic_gb: float, expiration_days: int, count: int, prefix: str, start_number: int, unlimited: bool): + """ + Executes the bulk user creation script with specified parameters. + """ + command = [ + 'python3', + Command.BULK_USER.value, + '--traffic-gb', str(traffic_gb), + '--expiration-days', str(expiration_days), + '--count', str(count), + '--prefix', prefix, + '--start-number', str(start_number) + ] + + if unlimited: + command.append('--unlimited') + + run_cmd(command) def edit_user(username: str, new_username: str | None, new_traffic_limit: int | None, new_expiration_days: int | None, renew_password: bool, renew_creation_date: bool, blocked: bool | None, unlimited_ip: bool | None): '''