From 11e206e16f45f20c0d8c0460052859ec22d097a5 Mon Sep 17 00:00:00 2001 From: Sarina Date: Sun, 21 Jul 2024 20:54:19 +0330 Subject: [PATCH] Fix cli.py install-hysteria2 --- core/cli.py | 9 +++++---- core/validator.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 core/validator.py diff --git a/core/cli.py b/core/cli.py index 4c4f025..26ab005 100644 --- a/core/cli.py +++ b/core/cli.py @@ -6,9 +6,9 @@ import io import click import subprocess from enum import StrEnum -from contextlib import redirect_stdout import traffic +import validator SCRIPT_DIR = 'scripts' DEBUG = True @@ -58,8 +58,9 @@ def cli(): # region hysteria2 menu options @cli.command('install-hysteria2') -def install_hysteria2(): - run_cmd(['bash', Command.INSTALL_HYSTERIA2]) +@click.option('--port','-p', required=True, help='New port for Hysteria2',type=int,validate=validator.validate_port) +def install_hysteria2(port:int): + run_cmd(['bash', Command.INSTALL_HYSTERIA2, str(port)]) @cli.command('uninstall-hysteria2') @@ -76,7 +77,7 @@ def restart_hysteria2(): @cli.command('change-hysteria2-port') -@click.option('--port','-p', required=True, help='New port for Hysteria2',type=int) +@click.option('--port','-p', required=True, help='New port for Hysteria2',type=int,validate=validator.validate_port) def change_hysteria2_port(port:int): run_cmd(['bash', Command.CHANGE_PORT_HYSTERIA2, str(port)]) diff --git a/core/validator.py b/core/validator.py new file mode 100644 index 0000000..1ede3d4 --- /dev/null +++ b/core/validator.py @@ -0,0 +1,10 @@ +import os + + +def validate_port(p:int)-> bool: + if p < 1 or p > 65535: + return False + # check if port is in use + if os.system(f'lsof -i:{p}') == 0: + return False + return True \ No newline at end of file