From 12f03454a23ff23bb05a0033ccc1869d64901cf5 Mon Sep 17 00:00:00 2001 From: Huayong Kuang Date: Wed, 3 Feb 2021 19:56:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=B9windows=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E7=9A=84=E5=85=BC=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- dnspx/config.py | 16 +++++++++++++++- dnspx/core.py | 6 ++++++ dnspx/log.py | 4 ++++ dnspx/version.py | 2 +- scripts/regist-sys-service.bat | 2 +- 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1e0c2f3..e2eb0b8 100644 --- a/README.md +++ b/README.md @@ -51,12 +51,12 @@ dnspx --help ### Windows -从 [Releases](https://github.com/kuanghy/dnspx/releases) 页面下载最新的发布版进行安装。或者 clone 本项目到本地,参考脚本 [build-app.bat](./scripts/build-app.bat) 自动构建。 +从 [Releases](https://github.com/kuanghy/dnspx/releases) 页面下载最新的发布版进行安装。或者 clone 本项目到本地,参考脚本 [build-app.bat](./scripts/build-app.bat) 自行构建。 安装完成后,可注册系统服务,让程序随系统自动启动(假设安装到了 `D:\dnspx` 目录下): ``` -sc create dnspx binPath= "D:\dnspx\dnspx.exe --config D:\dnspx\config" start= delayed-auto displayname= dnspx +sc create dnspx binPath= D:\dnspx\dnspx.exe start= delayed-auto displayname= dnspx ``` 服务操作: diff --git a/dnspx/config.py b/dnspx/config.py index f0ca177..211c1a1 100644 --- a/dnspx/config.py +++ b/dnspx/config.py @@ -16,7 +16,6 @@ IS_UNIX = (IS_MACOSX or IS_LINUX) - # 用户主目录 USER_HOME = _os.getenv("HOME", "/home/server") @@ -154,6 +153,19 @@ EMAIL_TOADDRS = [] # 收件人列表 +# Windows 平台下打包的可执行程序路径 +APP_PATH = None +APP_LOG_PATH = None +APP_BUNDLE_PATH = None +if IS_WIN32 and getattr(_sys, 'frozen', False): + APP_PATH = _os.path.dirname(_sys.executable) + APP_LOG_PATH = _os.path.join(APP_PATH, "logs") + APP_BUNDLE_PATH = getattr(_sys, '_MEIPASS', None) + + if not ROTATE_LOG_FILE: + ROTATE_LOG_FILE = _os.path.join(APP_LOG_PATH, "dnspx.log") + + # 配置文件目录 _CONFIG_DIRS = [ '/etc/dnspx/', @@ -223,6 +235,8 @@ def load_config(path=None, reset=False): env_path = _os.getenv("DNSPX_CONFIG_PATH") if env_path and _os.path.isdir(env_path): _CONFIG_DIRS.append(env_path) + if APP_PATH: + _CONFIG_DIRS.append(_os.path.join(APP_PATH, "config")) config_paths = _get_default_config_paths() if path and _os.path.isfile(path): diff --git a/dnspx/core.py b/dnspx/core.py index 61d4fc4..e016e92 100644 --- a/dnspx/core.py +++ b/dnspx/core.py @@ -229,6 +229,12 @@ def run(self): self.set_priority() self.set_proctitle() + if config.APP_PATH: + log.debug("Change working directory to '%s'", config.APP_PATH) + os.chdir(config.APP_PATH) + if config.APP_BUNDLE_PATH: + log.debug("Application bundle path: %s", config.APP_BUNDLE_PATH) + self._udp_server = ThreadedUDPServer( self.server_address, UDPHandler, diff --git a/dnspx/log.py b/dnspx/log.py index 6f15969..7df6053 100644 --- a/dnspx/log.py +++ b/dnspx/log.py @@ -3,6 +3,7 @@ # Copyright (c) Huoty, All rights reserved # Author: Huoty +import os import sys import time import socket @@ -292,6 +293,9 @@ def setup_logging(reset=False, enable_rotate_log=False, logger.addHandler(stream_handler) def _add_rotating_file_handler(logfile, level): + if config.APP_LOG_PATH and logfile.startswith(config.APP_LOG_PATH): + if not os.path.exists(config.APP_LOG_PATH): + os.makedirs(config.APP_LOG_PATH, exist_ok=True) logsize = int(rotate_log_maxsize or (20 * 1024 * 1024)) backups = int(rotate_log_backups or 10) file_handler = RotatingFileHandler( diff --git a/dnspx/version.py b/dnspx/version.py index b69a91b..3c0890c 100644 --- a/dnspx/version.py +++ b/dnspx/version.py @@ -3,7 +3,7 @@ # Copyright (c) Huoty, All rights reserved # Author: Huoty -__version__ = '0.1.9' +__version__ = '0.1.10' _VersionInfo = __import__("collections").namedtuple( diff --git a/scripts/regist-sys-service.bat b/scripts/regist-sys-service.bat index e4203f0..ea49e01 100644 --- a/scripts/regist-sys-service.bat +++ b/scripts/regist-sys-service.bat @@ -1,6 +1,6 @@ @echo off -sc create dnspx binPath= "D:\dnspx\dnspx.exe --config D:\dnspx\config" start= delayed-auto displayname= dnspx +sc create dnspx binPath= D:\dnspx\dnspx.exe start= delayed-auto displayname= dnspx pause exit