Fix cli validator and scripts path

This commit is contained in:
Sarina
2024-07-22 16:28:19 +03:30
parent 62ec44ee23
commit 65817ade14
2 changed files with 9 additions and 8 deletions

View File

@ -11,7 +11,7 @@ import traffic
import validator
SCRIPT_DIR = 'scripts'
SCRIPT_DIR = '/etc/hysteria/core/scripts'
DEBUG = True
class Command(Enum):
@ -39,7 +39,7 @@ def run_cmd(command:list[str]):
Runs a command and returns the output.
Could raise subprocess.CalledProcessError
'''
result = subprocess.check_output(command, shell=True)
result = subprocess.check_output(command, shell=False)
if DEBUG:
print(result.decode().strip())
@ -48,7 +48,7 @@ def generate_password() -> str:
Generates a random password using pwgen for user.
Could raise subprocess.CalledProcessError
'''
return subprocess.check_output(['pwgen', '-s', '32', '1'], shell=True).decode().strip()
return subprocess.check_output(['pwgen', '-s', '32', '1'], shell=False).decode().strip()
# endregion
@ -61,6 +61,7 @@ def cli():
@cli.command('install-hysteria2')
@click.option('--port','-p', required=True, help='New port for Hysteria2',type=int,callback=validator.validate_port)
def install_hysteria2(port:int):
print(f"bash {Command.INSTALL_HYSTERIA2.value} {str(port)}")
run_cmd(['bash', Command.INSTALL_HYSTERIA2.value, str(port)])

View File

@ -1,10 +1,10 @@
import os
import click
def validate_port(ctx,param,value)-> bool:
def validate_port(ctx,param,value:int) -> int:
if value < 1 or value > 65535:
return False
raise click.BadParameter('Port must be between 1 and 65535')
# check if port is in use
if os.system(f'lsof -i:{value}') == 0:
return False
return True
raise click.BadParameter(f'Port {value} is in use')
return value