> ## Documentation Index
> Fetch the complete documentation index at: https://yumebox.gal.tf/llms.txt
> Use this file to discover all available pages before exploring further.

# 工作流程

YumeBox 负责管理配置、覆写和运行会话；mihomo 负责解析节点、建立连接并提供运行数据。

## 总体架构

这张图展示 YumeBox 从配置入口到运行数据的完整架构：配置、覆写、会话、核心、系统接入与观测数据彼此如何连接。

<Frame>
  <img src="https://mintcdn.com/yumebox/37Z_Xsb-cHWca0U0/images/diagrams/yumebox-architecture.svg?fit=max&auto=format&n=37Z_Xsb-cHWca0U0&q=85&s=aebe254c54d5865ad88b1effab363497" alt="YumeBox 全部功能的架构关系图" noZoom={true} width="1668" height="867" data-path="images/diagrams/yumebox-architecture.svg" />
</Frame>

## 配置启用与节点解析

配置更新只负责取得并提交新的配置；真正启动时，运行会话会再次编译当前配置和覆写链，然后等待 mihomo 控制器提供策略组。

```mermaid theme={null}
---
config:
  theme: base
  themeVariables:
    fontFamily: "Ubuntu Sans, sans-serif"
    fontSize: "14px"
    primaryColor: "#eef2ff"
    primaryTextColor: "#172554"
    primaryBorderColor: "#818cf8"
    lineColor: "#64748b"
    actorBkg: "#eef2ff"
    actorBorder: "#818cf8"
    actorTextColor: "#172554"
    signalColor: "#64748b"
    signalTextColor: "#172554"
    activationBkgColor: "#dbeafe"
    activationBorderColor: "#3b82f6"
---
sequenceDiagram
    autonumber
    participant U as 用户
    participant P as 配置管理
    participant C as 编译管线
    participant O as 覆写引擎
    participant R as 运行会话
    participant M as mihomo

    U->>P: 添加或更新订阅
    P->>P: 写入原始配置与 Provider
    U->>R: 启动当前配置
    R->>C: 读取 profile 与绑定覆写
    C->>O: 按顺序应用 YAML / JavaScript
    O-->>C: 返回覆写后的配置
    C->>C: 应用 DNS、Tun、监听器等运行补丁
    C->>C: 校验并生成最终 YAML
    C-->>R: 返回编译结果
    R->>M: 传入配置并启动核心
    M-->>R: 控制器就绪
    R->>M: 查询策略组与节点状态
    M-->>U: 显示节点与代理组
```

```yaml 运行模式对配置的影响 diff.yaml icon="file-code" lines theme={null}
tun:
  enable: true # [!code --]
  enable: false # [!code ++]
  auto-route: true # [!code --]
  auto-route: false # [!code ++]
  auto-detect-interface: true # [!code --]
  auto-detect-interface: false # [!code ++]
```

在 **Vpn Service** 和 **eBPF** 模式中，运行时补丁会关闭上述 Tun 入口；**Tun** 模式会保留 Tun 配置。

## 自定义覆写与热重载

自定义覆写保存后，YumeBox 会重新应用当前配置正在使用的覆写链。运行中的配置不会修改订阅原文；重新加载失败时，运行会话会尝试恢复上一份有效配置。

```mermaid theme={null}
---
config:
  theme: base
  themeVariables:
    fontFamily: "Ubuntu Sans, sans-serif"
    fontSize: "14px"
    primaryColor: "#eef2ff"
    primaryTextColor: "#172554"
    primaryBorderColor: "#818cf8"
    lineColor: "#64748b"
    actorBkg: "#eef2ff"
    actorBorder: "#818cf8"
    actorTextColor: "#172554"
    signalColor: "#64748b"
    signalTextColor: "#172554"
    activationBkgColor: "#dbeafe"
    activationBorderColor: "#3b82f6"
---
sequenceDiagram
    autonumber
    participant U as 用户
    participant S as 覆写配置
    participant R as 运行会话
    participant C as 编译管线
    participant M as mihomo

    U->>S: 保存 YAML / JavaScript
    S->>R: 通知当前配置使用了该覆写
    R->>C: 重新编译完整覆写链
    C-->>R: 新配置或编译错误
    alt 编译成功
        R->>M: 停止旧传输并加载新配置
        M-->>R: 策略组就绪
        R-->>U: 热重载完成
    else 编译或启动失败
        R->>M: 停止失败的新会话
        R->>M: 恢复上一份有效配置
        M-->>R: 旧配置恢复
        R-->>U: 显示错误并保留旧会话
    end
```

```yaml 旧配置.yaml icon="file-code" lines theme={null}
mixed-port: 7890
```

```yaml 新覆写.yaml icon="file-code" lines theme={null}
mixed-port: 10801
```

```yaml 热重载结果 diff.yaml icon="file-code" lines theme={null}
mixed-port: 7890 # [!code --]
mixed-port: 10801 # [!code ++]
```

Tun 包含应用包名列表的配置在运行中重新加载时不会重新建立 VPN 设备；这类变化会记录为下一次建立 Tun 时生效。

## 启动模式与服务进程

三种模式共享配置编译流程，但接管流量的宿主不同：**Vpn Service** 使用 Android VPN 服务，**Tun** 使用 Root mihomo 进程，**eBPF** 在 Root mihomo 进程之外再启动 eBPF bridge。

```mermaid theme={null}
---
config:
  theme: base
  themeVariables:
    fontFamily: "Ubuntu Sans, sans-serif"
    fontSize: "14px"
    primaryColor: "#eef2ff"
    primaryTextColor: "#172554"
    primaryBorderColor: "#818cf8"
    lineColor: "#64748b"
    secondaryColor: "#ecfeff"
    tertiaryColor: "#f8fafc"
    clusterBkg: "#f8fafc"
    clusterBorder: "#cbd5e1"
    edgeLabelBackground: "#ffffff"
---
flowchart LR
    A[启动请求] --> B{运行模式}
    B -->|Vpn Service| C[Android TunService]
    B -->|Tun| D[Root mihomo]
    B -->|eBPF| E[Root mihomo]
    C --> F[VPN 虚拟网卡]
    D --> G[Root Tun 接口]
    E --> H[eBPF bridge]
    H --> I[Socket 地址重定向]
    F --> J[mihomo 控制器与代理链]
    G --> J
    I --> J
    J --> K[连接与流量数据]

    classDef request fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:1.5px;
    classDef mode fill:#ecfeff,stroke:#0891b2,color:#164e63,stroke-width:1.5px;
    classDef host fill:#eef2ff,stroke:#818cf8,color:#172554,stroke-width:1.5px;
    classDef data fill:#dcfce7,stroke:#16a34a,color:#14532d,stroke-width:1.5px;
    class A request;
    class B mode;
    class C,D,E,F,G,H,I,J host;
    class K data;
```

启动请求会先检查远程控制器、运行状态、重复启动和内置 Geo 数据，再进入对应宿主。运行中的会话会持续刷新状态、策略组、日志和流量数据。

## Wi‑Fi 自动化

Wi‑Fi 自动化只在 **Vpn Service** 模式运行。它由独立的前台服务监测 SSID；网络状态稳定后才应用规则，避免瞬时变化造成重复启停。

```mermaid theme={null}
---
config:
  theme: base
  themeVariables:
    fontFamily: "Ubuntu Sans, sans-serif"
    fontSize: "14px"
    primaryColor: "#eef2ff"
    primaryTextColor: "#172554"
    primaryBorderColor: "#818cf8"
    lineColor: "#64748b"
    secondaryColor: "#ecfeff"
    tertiaryColor: "#f8fafc"
    clusterBkg: "#f8fafc"
    clusterBorder: "#cbd5e1"
    edgeLabelBackground: "#ffffff"
---
flowchart TB
    A[启动 Wi‑Fi 自动化服务] --> B{启用且模式为 Vpn Service？}
    B -->|否| C[停止服务]
    B -->|是| D[监听 SSID]
    D --> E[等待 1.5 秒稳定窗口]
    E --> F{SSID 状态}
    F -->|匹配规则| G{动作}
    F -->|其他 Wi‑Fi| H[执行其他 Wi‑Fi 动作]
    F -->|无 Wi‑Fi| I[执行无 Wi‑Fi 动作]
    F -->|不可用| J[忽略本次变化]
    G -->|启动| K[检查 VPN 权限并启动]
    G -->|停止| L[停止 VpnService]
    H --> M{Keep / Start / Stop}
    I --> M
    M -->|Keep| N[保持当前状态]
    M -->|Start| K
    M -->|Stop| L
    K --> O[运行状态更新]
    L --> O

    classDef input fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:1.5px;
    classDef decision fill:#ecfeff,stroke:#0891b2,color:#164e63,stroke-width:1.5px;
    classDef action fill:#eef2ff,stroke:#818cf8,color:#172554,stroke-width:1.5px;
    classDef result fill:#dcfce7,stroke:#16a34a,color:#14532d,stroke-width:1.5px;
    class A input;
    class B,F,G,M decision;
    class C,D,E,H,I,J,K,L,N action;
    class O result;
```

## 连接、日志与流量统计

运行会话建立控制器后，同时开启日志订阅和连接跟踪。界面查询到的连接、策略组、实时速度和历史统计都来自运行数据，而不是覆写文件。

```mermaid theme={null}
---
config:
  theme: base
  themeVariables:
    fontFamily: "Ubuntu Sans, sans-serif"
    fontSize: "14px"
    primaryColor: "#eef2ff"
    primaryTextColor: "#172554"
    primaryBorderColor: "#818cf8"
    lineColor: "#64748b"
    actorBkg: "#eef2ff"
    actorBorder: "#818cf8"
    actorTextColor: "#172554"
    signalColor: "#64748b"
    signalTextColor: "#172554"
    activationBkgColor: "#dbeafe"
    activationBorderColor: "#3b82f6"
---
sequenceDiagram
    autonumber
    participant M as mihomo
    participant S as SessionRuntime
    participant L as 日志订阅
    participant H as 连接跟踪
    participant T as 流量统计
    participant U as 用户界面

    S->>M: 控制器就绪
    S->>L: subscribeLogs
    S->>H: 周期查询 connections
    L-->>U: 推送日志
    H->>M: queryConnections
    M-->>H: 连接快照
    H->>U: 更新连接历史
    loop 周期采样
        T->>M: queryTrafficTotal
        T->>M: queryConnections
        M-->>T: 总流量与连接计数
        T->>T: 与上次基线计算增量
        T->>T: 按应用和路由归档
        T-->>U: 更新统计图表
    end
    S->>M: 停止运行
    S->>L: 关闭日志订阅
    S->>H: 停止连接跟踪
    T->>T: flushNow
```

流量统计会按连接增量、应用身份和最后一跳路由归档；无法归属的部分会记录到未归属桶。切换当前配置、总流量回退或连接计数重置时，统计器会重新建立基线，避免把旧会话误算到新会话。

## 运行状态回路

```mermaid theme={null}
---
config:
  theme: base
  themeVariables:
    fontFamily: "Ubuntu Sans, sans-serif"
    fontSize: "14px"
    primaryColor: "#eef2ff"
    primaryTextColor: "#172554"
    primaryBorderColor: "#818cf8"
    lineColor: "#64748b"
    secondaryColor: "#ecfeff"
    tertiaryColor: "#f8fafc"
    clusterBkg: "#f8fafc"
    clusterBorder: "#cbd5e1"
    edgeLabelBackground: "#ffffff"
---
stateDiagram-v2
    [*] --> Idle
    Idle --> Starting: 启动请求
    Starting --> Running: 核心与策略组就绪
    Starting --> Failed: 编译、传输或核心失败
    Running --> Reloading: 配置或覆写变化
    Reloading --> Running: 新配置加载成功
    Reloading --> Running: 失败后恢复旧配置
    Reloading --> Failed: 恢复旧配置也失败
    Running --> Stopping: 停止请求
    Starting --> Stopping: 中断启动
    Failed --> Starting: 再次启动
    Stopping --> Idle: 资源清理完成
    Failed --> Idle: 会话退出
```

该状态回路解释了为什么**热重载失败**不一定等于代理停止：如果旧配置恢复成功，会话仍然回到 **Running**，同时记录恢复原因。
