From 6b8ecd875c3ce93045ef2913fbc5c587a5c4e68e Mon Sep 17 00:00:00 2001 From: Ben Song Date: Thu, 16 May 2024 15:40:37 +0800 Subject: [PATCH] fix init issue --- openvpn_ui/apis/client_api.py | 6 +++--- openvpn_ui/apis/openvpn_api.py | 2 +- openvpn_ui/bin/openvpn-install.sh | 0 openvpn_ui/readme.md | 34 +++++++++++++++++++++++-------- openvpn_ui/vpn/vpn_command.py | 15 +++++++++----- 5 files changed, 40 insertions(+), 17 deletions(-) mode change 100644 => 100755 openvpn_ui/bin/openvpn-install.sh diff --git a/openvpn_ui/apis/client_api.py b/openvpn_ui/apis/client_api.py index 56d0863..4df5125 100644 --- a/openvpn_ui/apis/client_api.py +++ b/openvpn_ui/apis/client_api.py @@ -31,7 +31,7 @@ router = APIRouter() 200: {"description": "OK"}, }, tags=["client"], - summary="List all clients", + summary="List all clients and download vpn file with specific name", response_model_by_alias=True, ) async def get_client( @@ -40,8 +40,8 @@ async def get_client( try: file_path = command.install_dir if client_name: - client_name = f"{client_name.strip('.ovpn')}.ovpn" - file_location = file_path + client_name + client_name = client_name.strip('.ovpn') + ".ovpn" + file_location = file_path + "/" + client_name return FileResponse(file_location, filename=client_name, media_type="application/octet-stream") else: diff --git a/openvpn_ui/apis/openvpn_api.py b/openvpn_ui/apis/openvpn_api.py index 4f1c181..107e674 100644 --- a/openvpn_ui/apis/openvpn_api.py +++ b/openvpn_ui/apis/openvpn_api.py @@ -28,7 +28,7 @@ router = APIRouter() 200: {"description": "OK"}, }, tags=["openvpn"], - summary="Get OpenVPN configuration", + summary="Get OpenVPN status", response_model_by_alias=True, ) async def get_openvpn( diff --git a/openvpn_ui/bin/openvpn-install.sh b/openvpn_ui/bin/openvpn-install.sh old mode 100644 new mode 100755 diff --git a/openvpn_ui/readme.md b/openvpn_ui/readme.md index 792027a..f68688b 100644 --- a/openvpn_ui/readme.md +++ b/openvpn_ui/readme.md @@ -2,17 +2,35 @@ Simple web UI to manage OpenVPN users. -swagger ui <> python <> [openvpn-install.sh](https://github.com/Nyr/openvpn-install/blob/master/README.md) +swagger ui <> python <> [openvpn-install.sh](../openvpn-install.sh) # usage +## prepare python +``` +# parepare libs +apt update +apt install wget curl unzip build-essential libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev -y -- run server +# install python +wget -c https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tar.xz +tar -xvf Python-3.10.0.tar.xz +cd Python-3.10.0 +sudo ./configure --enable-optimizations +make altinstall # install Python 3.10 into /usr/local/bin +python3.10 --version + +# install pip +curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py +python get-pip.py +``` + +## run server ```commandline -Install python3.10 - -cd project-path -pip install -r -i https://mirrors.aliyun.com/pypi/simple/ requirements.txt +wget -c https://github.com/Song2017/openvpn-install-ui/archive/refs/heads/master.zip +unzip master.zip +cd openvpn-install-ui-master +pip install -r openvpn_ui/requirements.txt openvpn_ui/bin/run_http_server.sh ``` @@ -22,7 +40,7 @@ openvpn_ui/bin/run_http_server.sh http://0.0.0.0:8080/api/docs ``` -# develop +## develop ```commandline docker run -it -v /Users/songgs/_git/openvpn-install:/app -p 8080:8080 --cap-add=NET_ADMIN python:3.10.14-bullseye bash @@ -32,7 +50,7 @@ sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list ``` ```commandline -!! comment this line in openvpn-install.sh +!! comment this line in ./bin/openvpn-install.sh # Discard stdin. Needed when running from an one-liner which includes a newline read -N 999999 -t 0.001 ``` \ No newline at end of file diff --git a/openvpn_ui/vpn/vpn_command.py b/openvpn_ui/vpn/vpn_command.py index cb25f6f..0feaf3e 100644 --- a/openvpn_ui/vpn/vpn_command.py +++ b/openvpn_ui/vpn/vpn_command.py @@ -8,8 +8,7 @@ from typing import Union class VpnCommand: @cached_property def install_dir(self) -> str: - return os.path.dirname(__file__).replace("openvpn-ui/http_server/vpn", "").replace( - r"openvpn-ui\\http_server\\vpn", "") + return os.path.dirname(__file__).rstrip("/vpn") @cached_property def install_file(self) -> str: @@ -21,14 +20,17 @@ class VpnCommand: return f"Please install OpenVPN: {self.install_file}" def install_vpn(self) -> str: - return self.create_client("default") + return self.create_client("init") def create_client(self, name) -> str: result = self.install_cmd(name, "1") if isinstance(result, str): return result if result: - new_client = f"~/{name.strip('.ovpn')}.ovpn" + if not name.startswith('/'): + new_client = f"/root/{name.strip('.ovpn')}.ovpn" + else: + new_client = name # move vpn file if os.path.exists(new_client): shutil.copy(new_client, self.install_dir) @@ -49,7 +51,8 @@ class VpnCommand: def install_cmd(self, name: str, option: str, file_path="/etc/openvpn/server/server.conf") -> Union[bool, str]: - if self.get_vpn_status(file_path) != "OpenVPN has been installed": + if (self.get_vpn_status(file_path) != "OpenVPN has been installed" + and name != "init"): return "Please install OpenVPN" name = name.strip('.ovpn') @@ -76,4 +79,6 @@ class VpnCommand: # 输出脚本执行结果和状态 print("shell output: ", new_client, stdout_data, stderr_data, process.returncode, process.stderr) + if self.get_vpn_status(file_path) != "OpenVPN has been installed": + return stdout_data return True