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:
@ -9,16 +9,14 @@ 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}")
|
||||
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)
|
||||
@ -27,9 +25,9 @@ def remove_obfs():
|
||||
del config['obfs']
|
||||
with open(CONFIG_FILE, 'w') as f:
|
||||
json.dump(config, f, indent=2)
|
||||
print("✅ Successfully removed 'obfs' from config.json.")
|
||||
print("Successfully removed 'obfs' from config.json.")
|
||||
else:
|
||||
print("ℹ️ 'obfs' section not found in config.json.")
|
||||
print("'obfs' section not found in config.json.")
|
||||
|
||||
restart_hysteria()
|
||||
|
||||
@ -39,13 +37,12 @@ def remove_obfs():
|
||||
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.")
|
||||
print("'obfs' section already exists. Replacing it.")
|
||||
del config['obfs']
|
||||
|
||||
password = ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(32))
|
||||
@ -60,29 +57,47 @@ def generate_obfs():
|
||||
with open(CONFIG_FILE, 'w') as f:
|
||||
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()
|
||||
|
||||
except FileNotFoundError:
|
||||
print(f"❌ Config file not found: {CONFIG_FILE}")
|
||||
print(f"Config file not found: {CONFIG_FILE}")
|
||||
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():
|
||||
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)
|
||||
|
||||
option = sys.argv[1]
|
||||
if option in ("--remove", "-r"):
|
||||
print("Removing 'obfs' from config.json...")
|
||||
# print("Removing 'obfs' from config.json...")
|
||||
remove_obfs()
|
||||
elif option in ("--generate", "-g"):
|
||||
print("Generating 'obfs' in config.json...")
|
||||
# print("Generating 'obfs' in config.json...")
|
||||
generate_obfs()
|
||||
elif option in ("--check", "-c"):
|
||||
# print("Checking 'obfs' status in config.json...")
|
||||
check_obfs()
|
||||
else:
|
||||
print("Invalid option. Use --remove|-r or --generate|-g")
|
||||
print("Invalid option. Use --remove|-r, --generate|-g, or --check|-c")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user