refactor(core): update masquerade to string mode

Modified `masquerade.py` to use `string` type instead of `proxy`.
This commit is contained in:
ReturnFI
2025-11-30 20:37:34 +00:00
parent 76472dfde5
commit 0949afcb6c
3 changed files with 28 additions and 29 deletions

View File

@ -400,8 +400,8 @@ 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', metavar='<domain>', type=str, help="Enable 'masquerade' in config.json with the specified domain.") @click.option('--enable', '-e', is_flag=True, help="Enable 'masquerade' in config.json.")
def masquerade(remove: bool, enable: str): def masquerade(remove: bool, enable: bool):
'''Manage 'masquerade' in Hysteria2 configuration.''' '''Manage 'masquerade' in Hysteria2 configuration.'''
try: try:
if not remove and not enable: 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') raise click.UsageError('Error: You cannot use both --remove and --enable at the same time')
if enable: if enable:
cli_api.enable_hysteria2_masquerade(enable) cli_api.enable_hysteria2_masquerade()
click.echo('Masquerade enabled successfully.') click.echo('Masquerade enabled successfully.')
elif remove: elif remove:
cli_api.disable_hysteria2_masquerade() cli_api.disable_hysteria2_masquerade()

View File

@ -229,10 +229,9 @@ def check_hysteria2_obfs():
result = subprocess.run(["python3", Command.MANAGE_OBFS.value, "--check"], check=True, capture_output=True, text=True) result = subprocess.run(["python3", Command.MANAGE_OBFS.value, "--check"], check=True, capture_output=True, text=True)
return result.stdout.strip() return result.stdout.strip()
def enable_hysteria2_masquerade(domain: str): def enable_hysteria2_masquerade():
'''Enables masquerade for Hysteria2.''' '''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(): def disable_hysteria2_masquerade():
'''Disables masquerade for Hysteria2.''' '''Disables masquerade for Hysteria2.'''

View File

@ -14,31 +14,35 @@ def is_masquerade_enabled():
print(f"Error reading config: {e}") print(f"Error reading config: {e}")
return False return False
def enable_masquerade(domain: str): def enable_masquerade():
if is_masquerade_enabled():
print("Masquerade is already enabled.")
sys.exit(0)
url = f"https://{domain}"
try: try:
with open(CONFIG_FILE, 'r') as f: with open(CONFIG_FILE, 'r') as f:
config = json.load(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"] = { config["masquerade"] = {
"type": "proxy", "type": "string",
"proxy": { "string": {
"url": url, "content": "HTTP 502: Bad Gateway",
"rewriteHost": True "headers": {
"Content-Type": "text/plain; charset=utf-8",
"Server": "Caddy"
}, },
"listenHTTP": ":80", "statusCode": 502
"listenHTTPS": ":443", }
"forceHTTPS": True
} }
with open(CONFIG_FILE, 'w') as f: with open(CONFIG_FILE, 'w') as f:
json.dump(config, f, indent=2) 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) subprocess.run(["python3", CLI_PATH, "restart-hysteria2"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except Exception as e: except Exception as e:
@ -68,20 +72,16 @@ def remove_masquerade():
def main(): def main():
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage: python3 masquerade.py {1|2} [domain]") print("Usage: python3 masquerade.py {1|2}")
print("1: Enable Masquerade [domain]") print("1: Enable Masquerade")
print("2: Remove Masquerade") print("2: Remove Masquerade")
sys.exit(1) sys.exit(1)
action = sys.argv[1] action = sys.argv[1]
if action == "1": if action == "1":
if len(sys.argv) < 3: print("Enabling 'masquerade' with type string...")
print("Error: Missing domain argument for enabling masquerade.") enable_masquerade()
sys.exit(1)
domain = sys.argv[2]
print(f"Enabling 'masquerade' with URL: {domain}...")
enable_masquerade(domain)
elif action == "2": elif action == "2":
print("Removing 'masquerade' from config.json...") print("Removing 'masquerade' from config.json...")
remove_masquerade() remove_masquerade()