From f9172252f75e0d933fbcb0db25d826b2f14b57e0 Mon Sep 17 00:00:00 2001 From: ReturnFI <151555003+ReturnFI@users.noreply.github.com> Date: Sun, 30 Nov 2025 21:00:12 +0000 Subject: [PATCH] feat(core): add status check for masquerade --- core/cli.py | 21 ++++++++++++--------- core/cli_api.py | 8 ++++++-- core/scripts/hysteria2/masquerade.py | 18 +++++++++++++----- menu.sh | 24 ++++++++++++------------ 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/core/cli.py b/core/cli.py index bed77cd..0c419ee 100644 --- a/core/cli.py +++ b/core/cli.py @@ -401,20 +401,23 @@ def update_geo(country: str): @cli.command('masquerade') @click.option('--remove', '-r', is_flag=True, help="Remove 'masquerade' from config.json.") @click.option('--enable', '-e', is_flag=True, help="Enable 'masquerade' in config.json.") -def masquerade(remove: bool, enable: bool): +@click.option('--status', '-s', is_flag=True, help="Get 'masquerade' status.") +def masquerade(remove: bool, enable: bool, status: bool): '''Manage 'masquerade' in Hysteria2 configuration.''' try: - if not remove and not enable: - raise click.UsageError('Error: You must use either --remove or --enable') - if remove and enable: - raise click.UsageError('Error: You cannot use both --remove and --enable at the same time') + if sum([remove, enable, status]) != 1: + raise click.UsageError('Error: You must use exactly one of --remove, --enable, or --status.') if enable: - cli_api.enable_hysteria2_masquerade() - click.echo('Masquerade enabled successfully.') + response = cli_api.enable_hysteria2_masquerade() + click.echo(response) elif remove: - cli_api.disable_hysteria2_masquerade() - click.echo('Masquerade disabled successfully.') + response = cli_api.disable_hysteria2_masquerade() + click.echo(response) + elif status: + response = cli_api.get_hysteria2_masquerade_status() + click.echo(response) + except Exception as e: click.echo(f'{e}', err=True) diff --git a/core/cli_api.py b/core/cli_api.py index 7f909ac..301fce7 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -231,11 +231,15 @@ def check_hysteria2_obfs(): def enable_hysteria2_masquerade(): '''Enables masquerade for Hysteria2.''' - run_cmd(['python3', Command.MASQUERADE_SCRIPT.value, '1']) + return run_cmd(['python3', Command.MASQUERADE_SCRIPT.value, '1']) def disable_hysteria2_masquerade(): '''Disables masquerade for Hysteria2.''' - run_cmd(['python3', Command.MASQUERADE_SCRIPT.value, '2']) + return run_cmd(['python3', Command.MASQUERADE_SCRIPT.value, '2']) + +def get_hysteria2_masquerade_status(): + '''Gets the current masquerade status for Hysteria2.''' + return run_cmd(['python3', Command.MASQUERADE_SCRIPT.value, 'status']) def get_hysteria2_config_file() -> dict[str, Any]: diff --git a/core/scripts/hysteria2/masquerade.py b/core/scripts/hysteria2/masquerade.py index 05ccaa0..4e71a55 100644 --- a/core/scripts/hysteria2/masquerade.py +++ b/core/scripts/hysteria2/masquerade.py @@ -10,10 +10,15 @@ def is_masquerade_enabled(): with open(CONFIG_FILE, 'r') as f: config = json.load(f) return "masquerade" in config - except Exception as e: - print(f"Error reading config: {e}") + except Exception: return False +def get_status(): + if is_masquerade_enabled(): + print("Enabled") + else: + print("Disabled") + def enable_masquerade(): try: with open(CONFIG_FILE, 'r') as f: @@ -72,9 +77,10 @@ def remove_masquerade(): def main(): if len(sys.argv) < 2: - print("Usage: python3 masquerade.py {1|2}") + print("Usage: python3 masquerade.py {1|2|status}") print("1: Enable Masquerade") print("2: Remove Masquerade") + print("status: Get current status") sys.exit(1) action = sys.argv[1] @@ -85,9 +91,11 @@ def main(): elif action == "2": print("Removing 'masquerade' from config.json...") remove_masquerade() + elif action == "status": + get_status() else: - print("Invalid option. Use 1 to enable or 2 to disable masquerade.") + print("Invalid option. Use 1, 2, or status.") sys.exit(1) if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/menu.sh b/menu.sh index f677eed..23d0c0c 100644 --- a/menu.sh +++ b/menu.sh @@ -63,7 +63,6 @@ hysteria2_add_user_handler() { done read -p "Enter the traffic limit (in GB): " traffic_limit_GB - read -p "Enter the expiration days: " expiration_days local unlimited_arg="" @@ -926,23 +925,24 @@ geo_update_handler() { masquerade_handler() { while true; do + status=$(python3 $CLI_PATH masquerade -s) + + echo "--------------------------" + if [ "$status" == "Enabled" ]; then + echo -e "Masquerade Status: ${green}${status}${NC}" + else + echo -e "Masquerade Status: ${red}${status}${NC}" + fi + echo "--------------------------" + echo -e "${cyan}1.${NC} Enable Masquerade" - echo -e "${red}2.${NC} Remove Masquerade" + echo -e "${cyan}2.${NC} Remove Masquerade" echo "0. Back" read -p "Choose an option: " option case $option in 1) - if systemctl is-active --quiet hysteria-webpanel.service; then - echo -e "${red}Error:${NC} Masquerade cannot be enabled because hysteria-webpanel.service is running." - else - read -p "Enter the URL for rewriteHost: " url - if [ -z "$url" ]; then - echo "Error: URL cannot be empty. Please try again." - else - python3 $CLI_PATH masquerade -e "$url" - fi - fi + python3 $CLI_PATH masquerade -e ;; 2) python3 $CLI_PATH masquerade -r