Refactor: Implement port update functionality in Python

This commit is contained in:
Whispering Wind
2025-05-02 12:08:19 +03:30
committed by GitHub
parent 50bb7fe7ff
commit 19192bd02d
3 changed files with 56 additions and 25 deletions

View File

@ -0,0 +1,54 @@
#!/usr/bin/env python3
import json
import sys
import re
import subprocess
from init_paths import *
from paths import *
def update_port(port):
"""
Update the port in the configuration file and restart the service.
Args:
port (str): The port number to set
Returns:
bool: True if successful, False otherwise
"""
try:
if not re.match(r'^[0-9]+$', port) or int(port) < 1 or int(port) > 65535:
print("Invalid port number. Please enter a number between 1 and 65535.")
return False
try:
with open(CONFIG_FILE, 'r') as f:
config = json.load(f)
except FileNotFoundError:
print(f"Error: Config file {CONFIG_FILE} not found.")
return False
config['listen'] = f":{port}"
with open(CONFIG_FILE, 'w') as f:
json.dump(config, f, indent=2)
subprocess.run([CLI_PATH, "restart-hysteria2"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
print(f"Port changed successfully to {port}.")
return True
except Exception as e:
print(f"Error updating port: {str(e)}")
return False
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python update_port.py <port_number>")
sys.exit(1)
success = update_port(sys.argv[1])
sys.exit(0 if success else 1)