feat: Add OBFS status check functionality

Adds a new `--check` or `-c` option to `manage_obfs.py` to determine if OBFS is currently configured in `config.json`.
This commit is contained in:
Whispering Wind
2025-06-02 00:14:55 +03:30
committed by GitHub
parent 026500f20f
commit 3ddb981077

View File

@ -9,16 +9,14 @@ from init_paths import *
from paths import * from paths import *
def restart_hysteria(): def restart_hysteria():
"""Restart the Hysteria2 service using the CLI script."""
try: try:
subprocess.run(["python3", CLI_PATH, "restart-hysteria2"], subprocess.run(["python3", CLI_PATH, "restart-hysteria2"],
stdout=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL) stderr=subprocess.DEVNULL)
except Exception as e: except Exception as e:
print(f"⚠️ Failed to restart Hysteria2: {e}") print(f"Failed to restart Hysteria2: {e}")
def remove_obfs(): def remove_obfs():
"""Remove the 'obfs' section from the config."""
try: try:
with open(CONFIG_FILE, 'r') as f: with open(CONFIG_FILE, 'r') as f:
config = json.load(f) config = json.load(f)
@ -27,9 +25,9 @@ def remove_obfs():
del config['obfs'] del config['obfs']
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("Successfully removed 'obfs' from config.json.") print("Successfully removed 'obfs' from config.json.")
else: else:
print(" 'obfs' section not found in config.json.") print("'obfs' section not found in config.json.")
restart_hysteria() restart_hysteria()
@ -39,13 +37,12 @@ def remove_obfs():
print(f"❌ Error removing 'obfs': {e}") print(f"❌ Error removing 'obfs': {e}")
def generate_obfs(): def generate_obfs():
"""Generate and add an 'obfs' section with a random password."""
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 'obfs' in config: if 'obfs' in config:
print(" 'obfs' section already exists. Replacing it.") print("'obfs' section already exists. Replacing it.")
del config['obfs'] del config['obfs']
password = ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(32)) password = ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(32))
@ -60,30 +57,48 @@ def generate_obfs():
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"Successfully added 'obfs' to config.json with password: {password}") print(f"Successfully added 'obfs' to config.json with password: {password}")
restart_hysteria() restart_hysteria()
except FileNotFoundError: except FileNotFoundError:
print(f"Config file not found: {CONFIG_FILE}") print(f"Config file not found: {CONFIG_FILE}")
except Exception as e: except Exception as e:
print(f"Error generating 'obfs': {e}") print(f"Error generating 'obfs': {e}")
def check_obfs():
try:
with open(CONFIG_FILE, 'r') as f:
config = json.load(f)
if 'obfs' in config:
print("OBFS is active.")
else:
print("OBFS is not active.")
except FileNotFoundError:
print(f"Config file not found: {CONFIG_FILE}")
except Exception as e:
print(f"Error checking 'obfs' status: {e}")
def main(): def main():
if len(sys.argv) != 2: if len(sys.argv) != 2:
print("Usage: python3 obfs_manager.py --remove|-r | --generate|-g") print("Usage: python3 obfs_manager.py --remove|-r | --generate|-g | --check|-c")
sys.exit(1) sys.exit(1)
option = sys.argv[1] option = sys.argv[1]
if option in ("--remove", "-r"): if option in ("--remove", "-r"):
print("Removing 'obfs' from config.json...") # print("Removing 'obfs' from config.json...")
remove_obfs() remove_obfs()
elif option in ("--generate", "-g"): elif option in ("--generate", "-g"):
print("Generating 'obfs' in config.json...") # print("Generating 'obfs' in config.json...")
generate_obfs() generate_obfs()
elif option in ("--check", "-c"):
# print("Checking 'obfs' status in config.json...")
check_obfs()
else: else:
print("Invalid option. Use --remove|-r or --generate|-g") print("Invalid option. Use --remove|-r, --generate|-g, or --check|-c")
sys.exit(1) sys.exit(1)
if __name__ == "__main__": if __name__ == "__main__":
main() main()