Add no-gui flag and improve traffic status output control
- Added --no-gui flag to traffic-status command - Modified traffic.py to accept no_gui parameter - Updated cli_api.traffic_status() to control output display - Ensured silent operation when called from remove-user command - Improved function return values for programmatic use - Added proper parameter handling throughout the call chain
This commit is contained in:
@ -170,7 +170,7 @@ def reset_user(username: str):
|
||||
def remove_user(username: str):
|
||||
try:
|
||||
cli_api.kick_user_by_name(username)
|
||||
cli_api.traffic_status()
|
||||
cli_api.traffic_status(display_output=False)
|
||||
cli_api.remove_user(username)
|
||||
click.echo(f"User '{username}' removed successfully.")
|
||||
except Exception as e:
|
||||
@ -182,7 +182,7 @@ def kick_user(username: str):
|
||||
"""Kicks a specific user by username."""
|
||||
try:
|
||||
cli_api.kick_user_by_name(username)
|
||||
click.echo(f"User '{username}' kicked successfully.")
|
||||
# click.echo(f"User '{username}' kicked successfully.")
|
||||
except Exception as e:
|
||||
click.echo(f'{e}', err=True)
|
||||
|
||||
@ -209,9 +209,10 @@ def show_user_uri(username: str, qrcode: bool, ipv: int, all: bool, singbox: boo
|
||||
|
||||
|
||||
@cli.command('traffic-status')
|
||||
def traffic_status():
|
||||
@click.option('--no-gui', is_flag=True, help='Retrieve traffic data without displaying output')
|
||||
def traffic_status(no_gui):
|
||||
try:
|
||||
cli_api.traffic_status()
|
||||
cli_api.traffic_status(no_gui=no_gui)
|
||||
except Exception as e:
|
||||
click.echo(f'{e}', err=True)
|
||||
|
||||
|
||||
@ -49,7 +49,7 @@ class Command(Enum):
|
||||
SERVICES_STATUS = os.path.join(SCRIPT_DIR, 'services_status.sh')
|
||||
VERSION = os.path.join(SCRIPT_DIR, 'hysteria2', 'version.py')
|
||||
LIMIT_SCRIPT = os.path.join(SCRIPT_DIR, 'hysteria2', 'limit.sh')
|
||||
KICK_USER_SCRIPT = os.path.join(SCRIPT_DIR, 'hysteria2', 'kickuser.sh')
|
||||
KICK_USER_SCRIPT = os.path.join(SCRIPT_DIR, 'hysteria2', 'kickuser.py')
|
||||
|
||||
|
||||
# region Custom Exceptions
|
||||
@ -312,7 +312,7 @@ def kick_user_by_name(username: str):
|
||||
if not os.path.exists(script_path):
|
||||
raise ScriptNotFoundError(f"Kick user script not found at: {script_path}")
|
||||
try:
|
||||
subprocess.run(['bash', script_path, username], check=True)
|
||||
subprocess.run(['python3', script_path, username], check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
raise CommandExecutionError(f"Failed to execute kick user script: {e}")
|
||||
|
||||
@ -339,9 +339,10 @@ def show_user_uri(username: str, qrcode: bool, ipv: int, all: bool, singbox: boo
|
||||
# region Server
|
||||
|
||||
|
||||
def traffic_status():
|
||||
def traffic_status(no_gui=False, display_output=False):
|
||||
'''Fetches traffic status.'''
|
||||
traffic.traffic_status()
|
||||
data = traffic.traffic_status(no_gui=True if not display_output else no_gui)
|
||||
return data
|
||||
|
||||
|
||||
# TODO: it's better to return json
|
||||
|
||||
@ -9,7 +9,7 @@ CONFIG_FILE = '/etc/hysteria/config.json'
|
||||
USERS_FILE = '/etc/hysteria/users.json'
|
||||
API_BASE_URL = 'http://127.0.0.1:25413'
|
||||
|
||||
def traffic_status():
|
||||
def traffic_status(no_gui=False):
|
||||
green = '\033[0;32m'
|
||||
cyan = '\033[0;36m'
|
||||
NC = '\033[0m'
|
||||
@ -73,8 +73,11 @@ def traffic_status():
|
||||
with open(USERS_FILE, 'w') as users_file:
|
||||
json.dump(users_data, users_file, indent=4)
|
||||
|
||||
if not no_gui:
|
||||
display_traffic_data(users_data, green, cyan, NC)
|
||||
|
||||
return users_data
|
||||
|
||||
def display_traffic_data(data, green, cyan, NC):
|
||||
if not data:
|
||||
print("No traffic data to display.")
|
||||
|
||||
Reference in New Issue
Block a user