Explorar el Código

feat: docker build

hujiarui hace 1 mes
padre
commit
7f4efbb46d
Se han modificado 3 ficheros con 151 adiciones y 22 borrados
  1. 2 3
      Dockerfile
  2. 133 14
      download_deps.py
  3. 16 5
      start_proxy.py

+ 2 - 3
Dockerfile

@@ -1,4 +1,3 @@
-# syntax=docker/dockerfile:1
 FROM python:3.10-slim-bookworm
 FROM python:3.10-slim-bookworm
 
 
 ENV PYTHONDONTWRITEBYTECODE=1 \
 ENV PYTHONDONTWRITEBYTECODE=1 \
@@ -61,11 +60,11 @@ RUN mkdir -p /opt/ungoogled-chromium && \
 
 
 # 4. 复制并解压标准命名的 Mihomo 代理内核
 # 4. 复制并解压标准命名的 Mihomo 代理内核
 COPY downloads/mihomo-linux.gz /tmp/mihomo.gz
 COPY downloads/mihomo-linux.gz /tmp/mihomo.gz
-RUN gzip -dc /tmp/mihomo.gz > /bin/mihomo && chmod +x /bin/mihomo && rm /tmp/mihomo.gz
+RUN gzip -dc /tmp/mihomo.gz > /bin/mihomo && chmod +x /bin/mihomo && ln -sf /bin/mihomo /usr/local/bin/mihomo && rm /tmp/mihomo.gz
 
 
 # 5. 启动脚本
 # 5. 启动脚本
 COPY entrypoint.sh /entrypoint.sh
 COPY entrypoint.sh /entrypoint.sh
-RUN chmod +x /entrypoint.sh
+RUN sed -i 's/\r$//' /entrypoint.sh && chmod +x /entrypoint.sh
 
 
 # 6. 项目业务代码(变动频繁,置于末尾,保证修改代码后重建时 1 秒命中缓存)
 # 6. 项目业务代码(变动频繁,置于末尾,保证修改代码后重建时 1 秒命中缓存)
 COPY . .
 COPY . .

+ 133 - 14
download_deps.py

@@ -7,8 +7,21 @@ import os
 import sys
 import sys
 import platform
 import platform
 import requests
 import requests
+import time
 from deps_registry import registry, Dependency
 from deps_registry import registry, Dependency
 
 
+if sys.platform == "win32":
+    try:
+        sys.stdout.reconfigure(encoding="utf-8")
+        sys.stderr.reconfigure(encoding="utf-8")
+    except Exception:
+        pass
+
+try:
+    from tqdm import tqdm
+except ImportError:
+    tqdm = None
+
 SAVE_DIR = os.path.join(os.path.dirname(__file__), "downloads")
 SAVE_DIR = os.path.join(os.path.dirname(__file__), "downloads")
 os.makedirs(SAVE_DIR, exist_ok=True)
 os.makedirs(SAVE_DIR, exist_ok=True)
 
 
@@ -21,7 +34,51 @@ def get_current_platform() -> str:
         return "darwin"
         return "darwin"
     return "linux"
     return "linux"
 
 
-def download_dependency(dep: Dependency):
+def get_effective_proxies() -> dict | None:
+    """
+    自动检测并获取最适用的网络代理:
+    1. 检查命令行环境变量 (HTTP_PROXY / HTTPS_PROXY / ALL_PROXY)
+    2. 检查 Windows 注册表系统代理 (Internet Settings -> ProxyEnable / ProxyServer)
+    """
+    # 1. 优先读取环境变量
+    for key in ("https_proxy", "http_proxy", "all_proxy", "HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY"):
+        val = os.environ.get(key)
+        if val:
+            if not val.startswith(("http://", "https://", "socks5://")):
+                val = f"http://{val}"
+            return {"http": val, "https": val}
+
+    # 2. Windows 平台:主动读取系统全局代理设置 (WinINet)
+    if platform.system().lower() == "windows":
+        try:
+            import winreg
+            with winreg.OpenKey(
+                winreg.HKEY_CURRENT_USER,
+                r"Software\Microsoft\Windows\CurrentVersion\Internet Settings"
+            ) as key:
+                enabled, _ = winreg.QueryValueEx(key, "ProxyEnable")
+                if enabled:
+                    server, _ = winreg.QueryValueEx(key, "ProxyServer")
+                    if server:
+                        if "=" in server:
+                            proxies = {}
+                            for part in server.split(";"):
+                                if "=" in part:
+                                    p_proto, p_addr = part.split("=", 1)
+                                    if not p_addr.startswith(("http://", "https://", "socks5://")):
+                                        p_addr = f"http://{p_addr}"
+                                    proxies[p_proto.strip()] = p_addr.strip()
+                            return proxies
+                        else:
+                            if not server.startswith(("http://", "https://", "socks5://")):
+                                server = f"http://{server}"
+                            return {"http": server, "https": server}
+        except Exception:
+            pass
+
+    return None
+
+def download_dependency(dep: Dependency, proxies: dict | None = None):
     """下载单个依赖项"""
     """下载单个依赖项"""
     filename = dep.get_filename()
     filename = dep.get_filename()
     save_path = os.path.join(SAVE_DIR, filename)
     save_path = os.path.join(SAVE_DIR, filename)
@@ -35,21 +92,61 @@ def download_dependency(dep: Dependency):
     print(f"\n开始下载 [{dep.name}] (版本: {dep.version}, 平台: {dep.platform})", flush=True)
     print(f"\n开始下载 [{dep.name}] (版本: {dep.version}, 平台: {dep.platform})", flush=True)
     print(f"目标文件: {filename}", flush=True)
     print(f"目标文件: {filename}", flush=True)
     print(f"下载链接: {dep.url}", flush=True)
     print(f"下载链接: {dep.url}", flush=True)
+    if proxies:
+        proxy_display = proxies.get("https") or proxies.get("http")
+        print(f"代理通道: {proxy_display}", flush=True)
 
 
     temp_path = save_path + ".tmp"
     temp_path = save_path + ".tmp"
     try:
     try:
-        r = requests.get(dep.url, stream=True, timeout=60)
+        r = requests.get(dep.url, stream=True, timeout=60, proxies=proxies)
         r.raise_for_status()
         r.raise_for_status()
-        total = 0
-        with open(temp_path, "wb") as f:
-            for chunk in r.iter_content(chunk_size=16384):
-                if chunk:
-                    f.write(chunk)
-                    total += len(chunk)
+        total_size = int(r.headers.get("content-length", 0))
+        chunk_size = 64 * 1024  # 64 KB 提高传输吞吐量
+
+        if tqdm is not None:
+            with tqdm(
+                total=total_size,
+                unit="B",
+                unit_scale=True,
+                unit_divisor=1024,
+                desc=filename,
+                ncols=90,
+                leave=True
+            ) as pbar:
+                with open(temp_path, "wb") as f:
+                    for chunk in r.iter_content(chunk_size=chunk_size):
+                        if chunk:
+                            f.write(chunk)
+                            pbar.update(len(chunk))
+        else:
+            downloaded = 0
+            start_time = time.time()
+            with open(temp_path, "wb") as f:
+                for chunk in r.iter_content(chunk_size=chunk_size):
+                    if chunk:
+                        f.write(chunk)
+                        downloaded += len(chunk)
+                        elapsed = time.time() - start_time
+                        speed = (downloaded / 1024 / 1024) / max(elapsed, 0.001)
+                        if total_size > 0:
+                            percent = (downloaded / total_size) * 100
+                            bar_len = 28
+                            filled = int(bar_len * downloaded / total_size)
+                            bar = "█" * filled + "░" * (bar_len - filled)
+                            print(
+                                f"\r[{bar}] {percent:5.1f}% ({downloaded/1048576:.1f}/{total_size/1048576:.1f} MB) {speed:.2f} MB/s",
+                                end="",
+                                flush=True,
+                            )
+                        else:
+                            print(f"\r已下载: {downloaded/1048576:.1f} MB ({speed:.2f} MB/s)", end="", flush=True)
+            print()
+
         if os.path.exists(save_path):
         if os.path.exists(save_path):
             os.remove(save_path)
             os.remove(save_path)
         os.rename(temp_path, save_path)
         os.rename(temp_path, save_path)
-        print(f"下载完成: {save_path} ({round(total / 1024 / 1024, 2)} MB)", flush=True)
+        final_size = os.path.getsize(save_path)
+        print(f"✓ 下载完成: {filename} ({round(final_size / 1024 / 1024, 2)} MB)", flush=True)
     except Exception as e:
     except Exception as e:
         if os.path.exists(temp_path):
         if os.path.exists(temp_path):
             try:
             try:
@@ -59,9 +156,31 @@ def download_dependency(dep: Dependency):
         raise e
         raise e
 
 
 def main():
 def main():
+    # 代理判定:支持 --no-proxy 或 --proxy <addr>,默认自动读取系统代理
+    proxies = get_effective_proxies()
+    args = sys.argv[1:]
+
+    if "--no-proxy" in args:
+        args.remove("--no-proxy")
+        proxies = None
+        print("[代理状态] 用户已禁用代理,使用直连模式", flush=True)
+    elif any(a.startswith("--proxy=") for a in args):
+        p_arg = [a for a in args if a.startswith("--proxy=")][0]
+        args.remove(p_arg)
+        p_val = p_arg.split("=", 1)[1]
+        if not p_val.startswith(("http://", "https://", "socks5://")):
+            p_val = f"http://{p_val}"
+        proxies = {"http": p_val, "https": p_val}
+    
+    if proxies:
+        p_addr = proxies.get("https") or proxies.get("http")
+        print(f"[代理状态] 已自动启用系统/环境代理: {p_addr}", flush=True)
+    else:
+        print("[代理状态] 未检测到代理设置,使用直连模式", flush=True)
+
     # 目标平台判定:支持传参覆盖(如 python download_deps.py linux / windows / all),默认自动检测宿主机
     # 目标平台判定:支持传参覆盖(如 python download_deps.py linux / windows / all),默认自动检测宿主机
-    if len(sys.argv) > 1:
-        arg = sys.argv[1].lower().replace("--", "")
+    if args:
+        arg = args[0].lower().replace("--", "")
         if arg in ("docker", "linux"):
         if arg in ("docker", "linux"):
             target = "linux"
             target = "linux"
         elif arg in ("win", "windows"):
         elif arg in ("win", "windows"):
@@ -73,7 +192,7 @@ def main():
     else:
     else:
         target = get_current_platform()
         target = get_current_platform()
 
 
-    print(f"目标运行平台: [{target}] (可通过命令行指定: python download_deps.py linux / windows / darwin / all)", flush=True)
+    print(f"[目标平台] [{target}] (可通过命令行指定: python download_deps.py linux / windows / darwin / all)\n", flush=True)
 
 
     deps = registry.get_by_platform(target)
     deps = registry.get_by_platform(target)
     if not deps:
     if not deps:
@@ -81,10 +200,10 @@ def main():
         print(f"平台 [{target}] 未注册任何依赖。当前可用平台: {available_platforms}", flush=True)
         print(f"平台 [{target}] 未注册任何依赖。当前可用平台: {available_platforms}", flush=True)
         return
         return
 
 
-    print(f"已匹配到 {len(deps)} 个依赖项:\n", flush=True)
+    print(f"已匹配到 {len(deps)} 个依赖项:", flush=True)
     for dep in deps:
     for dep in deps:
         try:
         try:
-            download_dependency(dep)
+            download_dependency(dep, proxies=proxies)
         except Exception as e:
         except Exception as e:
             print(f"下载失败 [{dep.name} - {dep.version}]: {e}", flush=True)
             print(f"下载失败 [{dep.name} - {dep.version}]: {e}", flush=True)
 
 

+ 16 - 5
start_proxy.py

@@ -39,11 +39,15 @@ DEFAULT_LOG_FILE = os.path.join(LOG_DIR, "mihomo.log")
 
 
 def find_mihomo_bin(custom_path=None):
 def find_mihomo_bin(custom_path=None):
     """查找可用的 mihomo 二进制程序路径"""
     """查找可用的 mihomo 二进制程序路径"""
+    custom_path = custom_path or os.environ.get("MIHOMO_BIN")
     if custom_path:
     if custom_path:
         abs_p = os.path.abspath(custom_path)
         abs_p = os.path.abspath(custom_path)
         if os.path.isfile(abs_p):
         if os.path.isfile(abs_p):
             return abs_p
             return abs_p
-        raise FileNotFoundError(f"指定的 mihomo 路径不存在: {custom_path}")
+        import shutil
+        which_p = shutil.which(custom_path)
+        if which_p and os.path.isfile(which_p):
+            return which_p
 
 
     current_os = platform.system().lower()
     current_os = platform.system().lower()
     downloads_dir = os.path.join(PROJECT_ROOT, "downloads")
     downloads_dir = os.path.join(PROJECT_ROOT, "downloads")
@@ -55,23 +59,30 @@ def find_mihomo_bin(custom_path=None):
             os.path.join(downloads_dir, "mihomo-windows-amd64.exe"),
             os.path.join(downloads_dir, "mihomo-windows-amd64.exe"),
             os.path.join(downloads_dir, "mihomo.exe"),
             os.path.join(downloads_dir, "mihomo.exe"),
         ])
         ])
-        candidates.extend(glob.glob(os.path.join(downloads_dir, "*mihomo*.exe")))
+        for f in glob.glob(os.path.join(downloads_dir, "*mihomo*.exe")):
+            candidates.append(f)
     elif current_os == "darwin":
     elif current_os == "darwin":
         candidates.extend([
         candidates.extend([
             os.path.join(downloads_dir, "mihomo-darwin"),
             os.path.join(downloads_dir, "mihomo-darwin"),
             os.path.join(downloads_dir, "mihomo"),
             os.path.join(downloads_dir, "mihomo"),
         ])
         ])
-        candidates.extend(glob.glob(os.path.join(downloads_dir, "*mihomo*")))
+        for f in glob.glob(os.path.join(downloads_dir, "*mihomo*")):
+            if not f.endswith((".gz", ".zip", ".tar.gz", ".tmp")):
+                candidates.append(f)
     else:  # linux
     else:  # linux
         candidates.extend([
         candidates.extend([
+            "/bin/mihomo",
+            "/usr/bin/mihomo",
             "/usr/local/bin/mihomo",
             "/usr/local/bin/mihomo",
             os.path.join(downloads_dir, "mihomo-linux"),
             os.path.join(downloads_dir, "mihomo-linux"),
             os.path.join(downloads_dir, "mihomo"),
             os.path.join(downloads_dir, "mihomo"),
         ])
         ])
-        candidates.extend(glob.glob(os.path.join(downloads_dir, "*mihomo*")))
+        for f in glob.glob(os.path.join(downloads_dir, "*mihomo*")):
+            if not f.endswith((".gz", ".zip", ".tar.gz", ".tmp")):
+                candidates.append(f)
 
 
     for c in candidates:
     for c in candidates:
-        if os.path.isfile(c):
+        if os.path.isfile(c) and not c.endswith((".gz", ".zip", ".tar.gz", ".tmp")):
             # 确保 Linux/Mac 有可执行权限
             # 确保 Linux/Mac 有可执行权限
             if current_os != "windows" and not os.access(c, os.X_OK):
             if current_os != "windows" and not os.access(c, os.X_OK):
                 try:
                 try: