From 136e9dd1fc1d93f6a118989afe069b7151abf9f1 Mon Sep 17 00:00:00 2001 From: chuan Date: Wed, 27 May 2026 09:50:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90docker=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E5=92=8C=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 28 ++++++++++++++++++++++++++++ Dockerfile | 45 ++++++++++++++++++++++++++------------------- TODO.md | 15 --------------- compose.yaml | 19 +++++++++++++++++++ pyproject.toml | 2 +- scripts/build.sh | 18 ++++++++++++++++++ 6 files changed, 92 insertions(+), 35 deletions(-) create mode 100644 .dockerignore delete mode 100644 TODO.md create mode 100644 compose.yaml create mode 100644 scripts/build.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..46e63f7 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,28 @@ +.git +.gitignore +.dockerignore + +.venv +venv +env + +__pycache__ +*.py[cod] +*.pyo +*.pyd +.pytest_cache +.ruff_cache +.mypy_cache +.pyre +.coverage +htmlcov + +data +.v2rayA + +*.tmp +*.log + +Dockerfile +docs +tests diff --git a/Dockerfile b/Dockerfile index 3fb572f..ca76b2f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,12 +2,14 @@ FROM ghcr.io/astral-sh/uv:python3.14-bookworm-slim WORKDIR /app -ARG XRAY_VERSION="" -ARG TARGETARCH ARG APT_MIRROR="https://mirrors.ustc.edu.cn/debian" ARG UV_INDEX_URL="https://pypi.mirrors.ustc.edu.cn/simple/" -ENV UV_INDEX_URL="${UV_INDEX_URL}" +ENV UV_INDEX_URL="${UV_INDEX_URL}" \ + PYTHONUNBUFFERED=1 \ + PYXRAY_HOST=0.0.0.0 \ + PYXRAY_PORT=8080 \ + PYXRAY_XRAY_DIR=/config/xray RUN if [ -f /etc/apt/sources.list.d/debian.sources ]; then \ sed -i "s|http://deb.debian.org/debian|${APT_MIRROR}|g; s|http://deb.debian.org/debian-security|${APT_MIRROR}-security|g" /etc/apt/sources.list.d/debian.sources; \ @@ -16,29 +18,34 @@ RUN if [ -f /etc/apt/sources.list.d/debian.sources ]; then \ sed -i "s|http://deb.debian.org/debian|${APT_MIRROR}|g; s|http://security.debian.org/debian-security|${APT_MIRROR}-security|g" /etc/apt/sources.list; \ fi \ && apt-get update \ - && apt-get install -y --no-install-recommends iproute2 iptables ca-certificates curl unzip \ + && apt-get install -y --no-install-recommends \ + ca-certificates \ + curl \ + iproute2 \ + iptables \ + nftables \ + unzip \ && rm -rf /var/lib/apt/lists/* -RUN if [ -n "$XRAY_VERSION" ]; then \ - case "$TARGETARCH" in \ - amd64|"") XRAY_ARCH="64" ;; \ - arm64) XRAY_ARCH="arm64-v8a" ;; \ - arm) XRAY_ARCH="arm32-v7a" ;; \ - *) echo "unsupported TARGETARCH: $TARGETARCH" >&2; exit 1 ;; \ - esac; \ - curl -fsSL "https://github.com/XTLS/Xray-core/releases/download/${XRAY_VERSION}/Xray-linux-${XRAY_ARCH}.zip" -o /tmp/xray.zip; \ - unzip /tmp/xray.zip xray geoip.dat geosite.dat -d /usr/local/share/xray; \ - install -m 0755 /usr/local/share/xray/xray /usr/local/bin/xray; \ - rm -f /tmp/xray.zip; \ - fi - COPY pyproject.toml uv.lock README.md ./ COPY pyxray ./pyxray -RUN uv sync --frozen --no-dev +RUN uv sync --frozen --no-dev \ + && uv pip install --python /app/.venv/bin/python gunicorn==23.0.0 + +RUN printf '%s\n' \ + '#!/bin/sh' \ + 'set -eu' \ + 'mkdir -p "$PYXRAY_XRAY_DIR"' \ + 'exec /app/.venv/bin/gunicorn --bind "$PYXRAY_HOST:$PYXRAY_PORT" --workers 1 --threads 8 --timeout 120 --access-logfile - --error-logfile - "pyxray.web.server:create_app(\"$PYXRAY_XRAY_DIR\")"' \ + > /usr/local/bin/pyxray-entrypoint \ + && chmod 0755 /usr/local/bin/pyxray-entrypoint VOLUME ["/config"] EXPOSE 8080 -CMD ["/app/.venv/bin/pyxray", "--nodes-file", "/config/nodes.toml", "--state-file", "/config/state.toml", "--runtime-file", "/config/runtime.toml", "web", "--host", "0.0.0.0", "--port", "8080", "--config-file", "/config/config.json", "--log-file", "/config/xray.log"] +HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ + CMD curl -fsS "http://127.0.0.1:${PYXRAY_PORT}/" >/dev/null || exit 1 + +CMD ["/usr/local/bin/pyxray-entrypoint"] diff --git a/TODO.md b/TODO.md deleted file mode 100644 index 0242d72..0000000 --- a/TODO.md +++ /dev/null @@ -1,15 +0,0 @@ -1. Xray 自动下载 / 检查 -2. 节点链接导入与标准化 -3. 节点管理与选择 -4. Xray 配置生成 - - [x] 定义 `settings.toml` 结构:core / inbounds / routing / transparent / dns / outbound settings。 - - [x] 实现设置读写与默认值:使用 TOML 保存所有配置生成相关参数。 - - [x] 实现基础配置模型:log / inbounds / outbounds / routing / dns。 - - [x] 实现选中节点到 Xray outbound 的转换。 - - [x] 实现 inbound 生成:socks / http / rule socks / rule http / custom inbounds / sniffing。 - - [x] 实现 routing 生成:proxy / direct / whitelist / gfwlist / custom / RoutingA。 - - [x] 实现 DNS 生成:dns rules / dns hosts / dns outbound / dns routing。 - - [x] 实现透明代理配置生成:close / proxy / whitelist / gfwlist / pac;redirect / tproxy / system_proxy / tun。 - - [x] 实现 mux / tcpFastOpen / logLevel 等 outbound sockopt 和运行参数。 - - [x] 增加完整配置生成测试:最小配置、规则端口、DNS、透明代理、各协议节点。 -5. Xray 运行控制 diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..00f4321 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,19 @@ +services: + pyxray: + image: pyxray:latest + build: + context: . + dockerfile: Dockerfile + container_name: pyxray + restart: unless-stopped + privileged: true + network_mode: host + environment: + PYXRAY_HOST: 127.0.0.1 + PYXRAY_PORT: 13999 + PYXRAY_XRAY_DIR: /config/xray + volumes: + - ./data:/config + - /lib/modules:/lib/modules:ro + - /etc/resolv.conf:/etc/resolv.conf + stop_grace_period: 15s diff --git a/pyproject.toml b/pyproject.toml index 8624983..8df733d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "pyxray" -version = "0.1.0" +version = "1.0.0" description = "A lightweight Linux xray control plane." readme = "README.md" requires-python = ">=3.14" diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100644 index 0000000..d283dd7 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,18 @@ +#!/bin/sh +set -eu + +SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +PROJECT_DIR=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd) + +IMAGE_NAME=${IMAGE_NAME:-pyxray} +IMAGE_TAG=${IMAGE_TAG:-latest} +APT_MIRROR=${APT_MIRROR:-https://mirrors.ustc.edu.cn/debian} +UV_INDEX_URL=${UV_INDEX_URL:-https://pypi.mirrors.ustc.edu.cn/simple/} + +cd "$PROJECT_DIR" + +docker build \ + --build-arg "APT_MIRROR=$APT_MIRROR" \ + --build-arg "UV_INDEX_URL=$UV_INDEX_URL" \ + -t "$IMAGE_NAME:$IMAGE_TAG" \ + .