refactor: migrate obfs manager script from Bash to Python
This commit is contained in:
@ -30,7 +30,7 @@ class Command(Enum):
|
|||||||
SHOW_USER_URI = os.path.join(SCRIPT_DIR, 'hysteria2', 'show_user_uri.py')
|
SHOW_USER_URI = os.path.join(SCRIPT_DIR, 'hysteria2', 'show_user_uri.py')
|
||||||
WRAPPER_URI = os.path.join(SCRIPT_DIR, 'hysteria2', 'wrapper_uri.py')
|
WRAPPER_URI = os.path.join(SCRIPT_DIR, 'hysteria2', 'wrapper_uri.py')
|
||||||
IP_ADD = os.path.join(SCRIPT_DIR, 'hysteria2', 'ip.sh')
|
IP_ADD = os.path.join(SCRIPT_DIR, 'hysteria2', 'ip.sh')
|
||||||
MANAGE_OBFS = os.path.join(SCRIPT_DIR, 'hysteria2', 'manage_obfs.sh')
|
MANAGE_OBFS = os.path.join(SCRIPT_DIR, 'hysteria2', 'manage_obfs.py')
|
||||||
MASQUERADE_SCRIPT = os.path.join(SCRIPT_DIR, 'hysteria2', 'masquerade.sh')
|
MASQUERADE_SCRIPT = os.path.join(SCRIPT_DIR, 'hysteria2', 'masquerade.sh')
|
||||||
TRAFFIC_STATUS = 'traffic.py' # won't be called directly (it's a python module)
|
TRAFFIC_STATUS = 'traffic.py' # won't be called directly (it's a python module)
|
||||||
UPDATE_GEO = os.path.join(SCRIPT_DIR, 'hysteria2', 'update_geo.py')
|
UPDATE_GEO = os.path.join(SCRIPT_DIR, 'hysteria2', 'update_geo.py')
|
||||||
@ -201,12 +201,12 @@ def restore_hysteria2(backup_file_path: str):
|
|||||||
|
|
||||||
def enable_hysteria2_obfs():
|
def enable_hysteria2_obfs():
|
||||||
'''Generates 'obfs' in Hysteria2 configuration.'''
|
'''Generates 'obfs' in Hysteria2 configuration.'''
|
||||||
run_cmd(['bash', Command.MANAGE_OBFS.value, '--generate'])
|
run_cmd(['python3', Command.MANAGE_OBFS.value, '--generate'])
|
||||||
|
|
||||||
|
|
||||||
def disable_hysteria2_obfs():
|
def disable_hysteria2_obfs():
|
||||||
'''Removes 'obfs' from Hysteria2 configuration.'''
|
'''Removes 'obfs' from Hysteria2 configuration.'''
|
||||||
run_cmd(['bash', Command.MANAGE_OBFS.value, '--remove'])
|
run_cmd(['python3', Command.MANAGE_OBFS.value, '--remove'])
|
||||||
|
|
||||||
|
|
||||||
def enable_hysteria2_masquerade(domain: str):
|
def enable_hysteria2_masquerade(domain: str):
|
||||||
|
|||||||
89
core/scripts/hysteria2/manage_obfs.py
Normal file
89
core/scripts/hysteria2/manage_obfs.py
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
import subprocess
|
||||||
|
import string
|
||||||
|
import secrets
|
||||||
|
from init_paths import *
|
||||||
|
from paths import *
|
||||||
|
|
||||||
|
def restart_hysteria():
|
||||||
|
"""Restart the Hysteria2 service using the CLI script."""
|
||||||
|
try:
|
||||||
|
subprocess.run(["python3", CLI_PATH, "restart-hysteria2"],
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
|
stderr=subprocess.DEVNULL)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ Failed to restart Hysteria2: {e}")
|
||||||
|
|
||||||
|
def remove_obfs():
|
||||||
|
"""Remove the 'obfs' section from the config."""
|
||||||
|
try:
|
||||||
|
with open(CONFIG_FILE, 'r') as f:
|
||||||
|
config = json.load(f)
|
||||||
|
|
||||||
|
if 'obfs' in config:
|
||||||
|
del config['obfs']
|
||||||
|
with open(CONFIG_FILE, 'w') as f:
|
||||||
|
json.dump(config, f, indent=2)
|
||||||
|
print("✅ Successfully removed 'obfs' from config.json.")
|
||||||
|
else:
|
||||||
|
print("ℹ️ 'obfs' section not found in config.json.")
|
||||||
|
|
||||||
|
restart_hysteria()
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"❌ Config file not found: {CONFIG_FILE}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Error removing 'obfs': {e}")
|
||||||
|
|
||||||
|
def generate_obfs():
|
||||||
|
"""Generate and add an 'obfs' section with a random password."""
|
||||||
|
try:
|
||||||
|
with open(CONFIG_FILE, 'r') as f:
|
||||||
|
config = json.load(f)
|
||||||
|
|
||||||
|
if 'obfs' in config:
|
||||||
|
print("ℹ️ 'obfs' section already exists. Replacing it.")
|
||||||
|
del config['obfs']
|
||||||
|
|
||||||
|
password = ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(32))
|
||||||
|
|
||||||
|
config['obfs'] = {
|
||||||
|
"type": "salamander",
|
||||||
|
"salamander": {
|
||||||
|
"password": password
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
with open(CONFIG_FILE, 'w') as f:
|
||||||
|
json.dump(config, f, indent=2)
|
||||||
|
|
||||||
|
print(f"✅ Successfully added 'obfs' to config.json with password: {password}")
|
||||||
|
|
||||||
|
restart_hysteria()
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
print(f"❌ Config file not found: {CONFIG_FILE}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Error generating 'obfs': {e}")
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
print("Usage: python3 obfs_manager.py --remove|-r | --generate|-g")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
option = sys.argv[1]
|
||||||
|
if option in ("--remove", "-r"):
|
||||||
|
print("Removing 'obfs' from config.json...")
|
||||||
|
remove_obfs()
|
||||||
|
elif option in ("--generate", "-g"):
|
||||||
|
print("Generating 'obfs' in config.json...")
|
||||||
|
generate_obfs()
|
||||||
|
else:
|
||||||
|
print("Invalid option. Use --remove|-r or --generate|-g")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@ -1,44 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
source /etc/hysteria/core/scripts/path.sh
|
|
||||||
|
|
||||||
remove_obfs() {
|
|
||||||
if jq 'has("obfs")' "$CONFIG_FILE" | grep -q true; then
|
|
||||||
jq 'del(.obfs)' "$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
|
|
||||||
echo "Successfully removed 'obfs' from config.json."
|
|
||||||
else
|
|
||||||
echo "'obfs' section not found in config.json."
|
|
||||||
fi
|
|
||||||
|
|
||||||
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
|
|
||||||
}
|
|
||||||
|
|
||||||
generate_obfs() {
|
|
||||||
obfspassword=$(pwgen -s 32 1)
|
|
||||||
|
|
||||||
if jq 'has("obfs")' "$CONFIG_FILE" | grep -q true; then
|
|
||||||
echo "'obfs' section already exists. Replacing it with a new one."
|
|
||||||
jq 'del(.obfs)' "$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
jq '. + {obfs: {type: "salamander", salamander: {password: "'"$obfspassword"'"}}}' "$CONFIG_FILE" > temp_config.json && mv temp_config.json "$CONFIG_FILE"
|
|
||||||
|
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
echo "Successfully added 'obfs' to config.json with password: $obfspassword"
|
|
||||||
else
|
|
||||||
echo "Error: Failed to add 'obfs' to config.json."
|
|
||||||
fi
|
|
||||||
|
|
||||||
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
|
|
||||||
}
|
|
||||||
|
|
||||||
if [[ $1 == "--remove" || $1 == "-r" ]]; then
|
|
||||||
echo "Removing 'obfs' from config.json..."
|
|
||||||
remove_obfs
|
|
||||||
elif [[ $1 == "--generate" || $1 == "-g" ]]; then
|
|
||||||
echo "Generating 'obfs' in config.json..."
|
|
||||||
generate_obfs
|
|
||||||
else
|
|
||||||
echo "Usage: $0 --remove|-r | --generate|-g"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
Reference in New Issue
Block a user