From 0949afcb6cc8bd4a49884dcb659254e7593bed19 Mon Sep 17 00:00:00 2001 From: ReturnFI <151555003+ReturnFI@users.noreply.github.com> Date: Sun, 30 Nov 2025 20:37:34 +0000 Subject: [PATCH] refactor(core): update masquerade to string mode Modified `masquerade.py` to use `string` type instead of `proxy`. --- core/cli.py | 6 ++-- core/cli_api.py | 5 ++- core/scripts/hysteria2/masquerade.py | 46 ++++++++++++++-------------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/core/cli.py b/core/cli.py index 4e983cd..bed77cd 100644 --- a/core/cli.py +++ b/core/cli.py @@ -400,8 +400,8 @@ 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', metavar='', type=str, help="Enable 'masquerade' in config.json with the specified domain.") -def masquerade(remove: bool, enable: str): +@click.option('--enable', '-e', is_flag=True, help="Enable 'masquerade' in config.json.") +def masquerade(remove: bool, enable: bool): '''Manage 'masquerade' in Hysteria2 configuration.''' try: if not remove and not enable: @@ -410,7 +410,7 @@ def masquerade(remove: bool, enable: str): raise click.UsageError('Error: You cannot use both --remove and --enable at the same time') if enable: - cli_api.enable_hysteria2_masquerade(enable) + cli_api.enable_hysteria2_masquerade() click.echo('Masquerade enabled successfully.') elif remove: cli_api.disable_hysteria2_masquerade() diff --git a/core/cli_api.py b/core/cli_api.py index 312db64..7f909ac 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -229,10 +229,9 @@ def check_hysteria2_obfs(): result = subprocess.run(["python3", Command.MANAGE_OBFS.value, "--check"], check=True, capture_output=True, text=True) return result.stdout.strip() -def enable_hysteria2_masquerade(domain: str): +def enable_hysteria2_masquerade(): '''Enables masquerade for Hysteria2.''' - run_cmd(['python3', Command.MASQUERADE_SCRIPT.value, '1', domain]) - + run_cmd(['python3', Command.MASQUERADE_SCRIPT.value, '1']) def disable_hysteria2_masquerade(): '''Disables masquerade for Hysteria2.''' diff --git a/core/scripts/hysteria2/masquerade.py b/core/scripts/hysteria2/masquerade.py index 21155c0..05ccaa0 100644 --- a/core/scripts/hysteria2/masquerade.py +++ b/core/scripts/hysteria2/masquerade.py @@ -14,31 +14,35 @@ def is_masquerade_enabled(): print(f"Error reading config: {e}") return False -def enable_masquerade(domain: str): - if is_masquerade_enabled(): - print("Masquerade is already enabled.") - sys.exit(0) - - url = f"https://{domain}" +def enable_masquerade(): try: with open(CONFIG_FILE, 'r') as f: config = json.load(f) + if "masquerade" in config: + print("Masquerade is already enabled.") + sys.exit(0) + + if "obfs" in config: + print("Error: Cannot enable masquerade when 'obfs' is configured.") + sys.exit(1) + config["masquerade"] = { - "type": "proxy", - "proxy": { - "url": url, - "rewriteHost": True - }, - "listenHTTP": ":80", - "listenHTTPS": ":443", - "forceHTTPS": True + "type": "string", + "string": { + "content": "HTTP 502: Bad Gateway", + "headers": { + "Content-Type": "text/plain; charset=utf-8", + "Server": "Caddy" + }, + "statusCode": 502 + } } with open(CONFIG_FILE, 'w') as f: json.dump(config, f, indent=2) - print(f"Masquerade enabled with URL: {url}") + print("Masquerade enabled with a Caddy-like 502 Bad Gateway response.") subprocess.run(["python3", CLI_PATH, "restart-hysteria2"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) except Exception as e: @@ -68,20 +72,16 @@ def remove_masquerade(): def main(): if len(sys.argv) < 2: - print("Usage: python3 masquerade.py {1|2} [domain]") - print("1: Enable Masquerade [domain]") + print("Usage: python3 masquerade.py {1|2}") + print("1: Enable Masquerade") print("2: Remove Masquerade") sys.exit(1) action = sys.argv[1] if action == "1": - if len(sys.argv) < 3: - print("Error: Missing domain argument for enabling masquerade.") - sys.exit(1) - domain = sys.argv[2] - print(f"Enabling 'masquerade' with URL: {domain}...") - enable_masquerade(domain) + print("Enabling 'masquerade' with type string...") + enable_masquerade() elif action == "2": print("Removing 'masquerade' from config.json...") remove_masquerade()