feat(core): add status check for masquerade

This commit is contained in:
ReturnFI
2025-11-30 21:00:12 +00:00
parent 0949afcb6c
commit f9172252f7
4 changed files with 43 additions and 28 deletions

View File

@ -401,20 +401,23 @@ def update_geo(country: str):
@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', is_flag=True, help="Enable 'masquerade' in 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.''' '''Manage 'masquerade' in Hysteria2 configuration.'''
try: try:
if not remove and not enable: if sum([remove, enable, status]) != 1:
raise click.UsageError('Error: You must use either --remove or --enable') raise click.UsageError('Error: You must use exactly one of --remove, --enable, or --status.')
if remove and enable:
raise click.UsageError('Error: You cannot use both --remove and --enable at the same time')
if enable: if enable:
cli_api.enable_hysteria2_masquerade() response = cli_api.enable_hysteria2_masquerade()
click.echo('Masquerade enabled successfully.') click.echo(response)
elif remove: elif remove:
cli_api.disable_hysteria2_masquerade() response = cli_api.disable_hysteria2_masquerade()
click.echo('Masquerade disabled successfully.') click.echo(response)
elif status:
response = cli_api.get_hysteria2_masquerade_status()
click.echo(response)
except Exception as e: except Exception as e:
click.echo(f'{e}', err=True) click.echo(f'{e}', err=True)

View File

@ -231,11 +231,15 @@ def check_hysteria2_obfs():
def enable_hysteria2_masquerade(): def enable_hysteria2_masquerade():
'''Enables masquerade for Hysteria2.''' '''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(): def disable_hysteria2_masquerade():
'''Disables masquerade for Hysteria2.''' '''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]: def get_hysteria2_config_file() -> dict[str, Any]:

View File

@ -10,10 +10,15 @@ def is_masquerade_enabled():
with open(CONFIG_FILE, 'r') as f: with open(CONFIG_FILE, 'r') as f:
config = json.load(f) config = json.load(f)
return "masquerade" in config return "masquerade" in config
except Exception as e: except Exception:
print(f"Error reading config: {e}")
return False return False
def get_status():
if is_masquerade_enabled():
print("Enabled")
else:
print("Disabled")
def enable_masquerade(): def enable_masquerade():
try: try:
with open(CONFIG_FILE, 'r') as f: with open(CONFIG_FILE, 'r') as f:
@ -72,9 +77,10 @@ def remove_masquerade():
def main(): def main():
if len(sys.argv) < 2: 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("1: Enable Masquerade")
print("2: Remove Masquerade") print("2: Remove Masquerade")
print("status: Get current status")
sys.exit(1) sys.exit(1)
action = sys.argv[1] action = sys.argv[1]
@ -85,8 +91,10 @@ def main():
elif action == "2": elif action == "2":
print("Removing 'masquerade' from config.json...") print("Removing 'masquerade' from config.json...")
remove_masquerade() remove_masquerade()
elif action == "status":
get_status()
else: else:
print("Invalid option. Use 1 to enable or 2 to disable masquerade.") print("Invalid option. Use 1, 2, or status.")
sys.exit(1) sys.exit(1)
if __name__ == "__main__": if __name__ == "__main__":

24
menu.sh
View File

@ -63,7 +63,6 @@ hysteria2_add_user_handler() {
done done
read -p "Enter the traffic limit (in GB): " traffic_limit_GB read -p "Enter the traffic limit (in GB): " traffic_limit_GB
read -p "Enter the expiration days: " expiration_days read -p "Enter the expiration days: " expiration_days
local unlimited_arg="" local unlimited_arg=""
@ -926,23 +925,24 @@ geo_update_handler() {
masquerade_handler() { masquerade_handler() {
while true; do 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 "${cyan}1.${NC} Enable Masquerade"
echo -e "${red}2.${NC} Remove Masquerade" echo -e "${cyan}2.${NC} Remove Masquerade"
echo "0. Back" echo "0. Back"
read -p "Choose an option: " option read -p "Choose an option: " option
case $option in case $option in
1) 1)
if systemctl is-active --quiet hysteria-webpanel.service; then python3 $CLI_PATH masquerade -e
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
;; ;;
2) 2)
python3 $CLI_PATH masquerade -r python3 $CLI_PATH masquerade -r