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')
@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.")
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()

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)
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.'''

View File

@ -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
"type": "string",
"string": {
"content": "HTTP 502: Bad Gateway",
"headers": {
"Content-Type": "text/plain; charset=utf-8",
"Server": "Caddy"
},
"listenHTTP": ":80",
"listenHTTPS": ":443",
"forceHTTPS": True
"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()