Format cli.py

This commit is contained in:
Iam54r1n4
2025-01-26 11:18:52 +00:00
parent e711e5fc34
commit 101dc5a934

View File

@ -5,6 +5,7 @@ import click
import cli_api import cli_api
import json import json
def pretty_print(data: typing.Any): def pretty_print(data: typing.Any):
if isinstance(data, dict): if isinstance(data, dict):
print(json.dumps(data, indent=4)) print(json.dumps(data, indent=4))
@ -12,12 +13,14 @@ def pretty_print(data: typing.Any):
print(data) print(data)
@click.group() @click.group()
def cli(): def cli():
pass pass
# region Hysteria2 # region Hysteria2
@cli.command('install-hysteria2') @cli.command('install-hysteria2')
@click.option('--port', '-p', required=True, help='Port for Hysteria2', type=int) @click.option('--port', '-p', required=True, help='Port for Hysteria2', type=int)
@click.option('--sni', '-s', required=False, default='bts.com', help='SNI for Hysteria2 (default: bts.com)', type=str) @click.option('--sni', '-s', required=False, default='bts.com', help='SNI for Hysteria2 (default: bts.com)', type=str)
@ -28,6 +31,7 @@ def install_hysteria2(port: int, sni: str):
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('uninstall-hysteria2') @cli.command('uninstall-hysteria2')
def uninstall_hysteria2(): def uninstall_hysteria2():
try: try:
@ -36,6 +40,7 @@ def uninstall_hysteria2():
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('update-hysteria2') @cli.command('update-hysteria2')
def update_hysteria2(): def update_hysteria2():
try: try:
@ -44,6 +49,7 @@ def update_hysteria2():
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('restart-hysteria2') @cli.command('restart-hysteria2')
def restart_hysteria2(): def restart_hysteria2():
try: try:
@ -52,6 +58,7 @@ def restart_hysteria2():
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('change-hysteria2-port') @cli.command('change-hysteria2-port')
@click.option('--port', '-p', required=True, help='New port for Hysteria2', type=int) @click.option('--port', '-p', required=True, help='New port for Hysteria2', type=int)
def change_hysteria2_port(port: int): def change_hysteria2_port(port: int):
@ -61,6 +68,7 @@ def change_hysteria2_port(port: int):
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('change-hysteria2-sni') @cli.command('change-hysteria2-sni')
@click.option('--sni', '-s', required=True, help='New SNI for Hysteria2', type=str) @click.option('--sni', '-s', required=True, help='New SNI for Hysteria2', type=str)
def change_hysteria2_sni(sni: str): def change_hysteria2_sni(sni: str):
@ -70,6 +78,7 @@ def change_hysteria2_sni(sni: str):
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('backup-hysteria') @cli.command('backup-hysteria')
def backup_hysteria(): def backup_hysteria():
try: try:
@ -82,6 +91,7 @@ def backup_hysteria():
# region User # region User
@ cli.command('list-users') @ cli.command('list-users')
def list_users(): def list_users():
try: try:
@ -117,6 +127,7 @@ def add_user(username: str, traffic_limit: int, expiration_days: int, password:
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('edit-user') @cli.command('edit-user')
@click.option('--username', '-u', required=True, help='Username for the user to edit', type=str) @click.option('--username', '-u', required=True, help='Username for the user to edit', type=str)
@click.option('--new-username', '-nu', required=False, help='New username for the user', type=str) @click.option('--new-username', '-nu', required=False, help='New username for the user', type=str)
@ -133,6 +144,7 @@ def edit_user(username: str, new_username: str, new_traffic_limit: int, new_expi
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@ cli.command('reset-user') @ cli.command('reset-user')
@ click.option('--username', '-u', required=True, help='Username for the user to Reset', type=str) @ click.option('--username', '-u', required=True, help='Username for the user to Reset', type=str)
def reset_user(username: str): def reset_user(username: str):
@ -142,6 +154,7 @@ def reset_user(username: str):
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@ cli.command('remove-user') @ cli.command('remove-user')
@ click.option('--username', '-u', required=True, help='Username for the user to remove', type=str) @ click.option('--username', '-u', required=True, help='Username for the user to remove', type=str)
def remove_user(username: str): def remove_user(username: str):
@ -151,6 +164,7 @@ def remove_user(username: str):
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('show-user-uri') @cli.command('show-user-uri')
@click.option('--username', '-u', required=True, help='Username for the user to show the URI', type=str) @click.option('--username', '-u', required=True, help='Username for the user to show the URI', type=str)
@click.option('--qrcode', '-qr', is_flag=True, help='Generate QR code for the URI') @click.option('--qrcode', '-qr', is_flag=True, help='Generate QR code for the URI')
@ -170,6 +184,8 @@ def show_user_uri(username: str, qrcode: bool, ipv: int, all: bool, singbox: boo
# endregion # endregion
# region Server # region Server
@ cli.command('traffic-status') @ cli.command('traffic-status')
def traffic_status(): def traffic_status():
try: try:
@ -177,6 +193,7 @@ def traffic_status():
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('server-info') @cli.command('server-info')
def server_info(): def server_info():
try: try:
@ -188,6 +205,7 @@ def server_info():
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('manage_obfs') @cli.command('manage_obfs')
@click.option('--remove', '-r', is_flag=True, help="Remove 'obfs' from config.json.") @click.option('--remove', '-r', is_flag=True, help="Remove 'obfs' from config.json.")
@click.option('--generate', '-g', is_flag=True, help="Generate new 'obfs' in config.json.") @click.option('--generate', '-g', is_flag=True, help="Generate new 'obfs' in config.json.")
@ -198,6 +216,7 @@ def manage_obfs(remove: bool, generate: bool):
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('ip-address') @cli.command('ip-address')
@click.option('--edit', is_flag=True, help="Edit IP addresses manually.") @click.option('--edit', is_flag=True, help="Edit IP addresses manually.")
@click.option('-4', '--ipv4', type=str, help="Specify the new IPv4 address.") @click.option('-4', '--ipv4', type=str, help="Specify the new IPv4 address.")
@ -214,6 +233,7 @@ def ip_address(edit: bool, ipv4: str, ipv6: str):
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('update-geo') @cli.command('update-geo')
@click.option('--country', '-c', @click.option('--country', '-c',
type=click.Choice(['iran', 'china', 'russia'], case_sensitive=False), type=click.Choice(['iran', 'china', 'russia'], case_sensitive=False),
@ -226,6 +246,7 @@ def update_geo(country: str):
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('masquerade') @cli.command('masquerade')
@click.option('--remove', '-r', is_flag=True, help="Remove 'masquerade' from config.json.") @click.option('--remove', '-r', is_flag=True, help="Remove 'masquerade' from config.json.")
@click.option('--enable', '-e', metavar="<domain>", type=str, help="Enable 'masquerade' in config.json with the specified domain.") @click.option('--enable', '-e', metavar="<domain>", type=str, help="Enable 'masquerade' in config.json with the specified domain.")
@ -241,6 +262,7 @@ def masquerade(remove: bool, enable: str):
# region Advanced Menu # region Advanced Menu
@ cli.command('install-tcp-brutal') @ cli.command('install-tcp-brutal')
def install_tcp_brutal(): def install_tcp_brutal():
try: try:
@ -249,6 +271,7 @@ def install_tcp_brutal():
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@ cli.command('install-warp') @ cli.command('install-warp')
def install_warp(): def install_warp():
try: try:
@ -257,6 +280,7 @@ def install_warp():
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@ cli.command('uninstall-warp') @ cli.command('uninstall-warp')
def uninstall_warp(): def uninstall_warp():
try: try:
@ -265,6 +289,7 @@ def uninstall_warp():
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('configure-warp') @cli.command('configure-warp')
@click.option('--all', '-a', is_flag=True, help='Use WARP for all connections') @click.option('--all', '-a', is_flag=True, help='Use WARP for all connections')
@click.option('--popular-sites', '-p', is_flag=True, help='Use WARP for popular sites like Google, OpenAI, etc') @click.option('--popular-sites', '-p', is_flag=True, help='Use WARP for popular sites like Google, OpenAI, etc')
@ -279,6 +304,7 @@ def configure_warp(all: bool, popular_sites: bool, domestic_sites: bool, block_a
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('warp-status') @cli.command('warp-status')
def warp_status(): def warp_status():
try: try:
@ -290,6 +316,7 @@ def warp_status():
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('telegram') @cli.command('telegram')
@click.option('--action', '-a', required=True, help='Action to perform: start or stop', type=click.Choice(['start', 'stop'], case_sensitive=False)) @click.option('--action', '-a', required=True, help='Action to perform: start or stop', type=click.Choice(['start', 'stop'], case_sensitive=False))
@click.option('--token', '-t', required=False, help='Token for running the telegram bot', type=str) @click.option('--token', '-t', required=False, help='Token for running the telegram bot', type=str)
@ -301,6 +328,7 @@ def telegram(action: str, token: str, adminid: str):
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('singbox') @cli.command('singbox')
@click.option('--action', '-a', required=True, help='Action to perform: start or stop', type=click.Choice(['start', 'stop'], case_sensitive=False)) @click.option('--action', '-a', required=True, help='Action to perform: start or stop', type=click.Choice(['start', 'stop'], case_sensitive=False))
@click.option('--domain', '-d', required=False, help='Domain name for SSL', type=str) @click.option('--domain', '-d', required=False, help='Domain name for SSL', type=str)
@ -312,6 +340,7 @@ def singbox(action: str, domain: str, port: int):
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)
@cli.command('normal-sub') @cli.command('normal-sub')
@click.option('--action', '-a', required=True, help='Action to perform: start or stop', type=click.Choice(['start', 'stop'], case_sensitive=False)) @click.option('--action', '-a', required=True, help='Action to perform: start or stop', type=click.Choice(['start', 'stop'], case_sensitive=False))
@click.option('--domain', '-d', required=False, help='Domain name for SSL', type=str) @click.option('--domain', '-d', required=False, help='Domain name for SSL', type=str)
@ -325,5 +354,6 @@ def normalsub(action: str, domain: str, port: int):
# endregion # endregion
if __name__ == '__main__': if __name__ == '__main__':
cli() cli()