Skip to content

Commit

Permalink
优化对windows平台的兼容性
Browse files Browse the repository at this point in the history
  • Loading branch information
kuanghy committed Feb 3, 2021
1 parent 560cfc8 commit 12f0345
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

服务操作:
Expand Down
16 changes: 15 additions & 1 deletion dnspx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

IS_UNIX = (IS_MACOSX or IS_LINUX)


# 用户主目录
USER_HOME = _os.getenv("HOME", "/home/server")

Expand Down Expand Up @@ -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/',
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions dnspx/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions dnspx/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright (c) Huoty, All rights reserved
# Author: Huoty <[email protected]>

import os
import sys
import time
import socket
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion dnspx/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (c) Huoty, All rights reserved
# Author: Huoty <[email protected]>

__version__ = '0.1.9'
__version__ = '0.1.10'


_VersionInfo = __import__("collections").namedtuple(
Expand Down
2 changes: 1 addition & 1 deletion scripts/regist-sys-service.bat
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 12f0345

Please sign in to comment.