Some checks failed
Upstream Sync / Sync latest commits from upstream repo (push) Has been cancelled
73 lines
4.0 KiB
Markdown
73 lines
4.0 KiB
Markdown
# 远程控制与 P2P 虚拟网(EasyTier)
|
||
|
||
## 方案对比(简要)
|
||
|
||
| 方案 | 特点 |
|
||
|------|------|
|
||
| [EasyTier](https://github.com/EasyTier/EasyTier) | 去中心化 mesh、NAT 穿透、Win/Android 有现成客户端;与「本机 HTTP API」组合简单。 |
|
||
| Tailscale / ZeroTier | 需协调服务或自建 Moon;上手快,依赖厂商或自建协调器。 |
|
||
| 纯 WebRTC | 真 P2P,但需信令(常要服务器)或复杂打洞流程,不适合直接替代「虚拟网 + REST」。 |
|
||
|
||
对本项目(`web2` FastAPI + Electron + 未来 React Native),**EasyTier + 固定虚拟 IP 访问 `http://<PC的虚拟IP>:8001`** 是务实组合。
|
||
|
||
## Windows 桌面端:内置 EasyTier
|
||
|
||
- 构建/开发前会执行 `pnpm run prepare-easytier`(或 `electron/scripts/download-easytier.mjs`),从 [EasyTier 官方 Release](https://github.com/EasyTier/EasyTier/releases) 下载 Windows x64 包并解压 `easytier-core.exe` 到 `electron/resources/easytier/`(LGPL-3.0,见同目录 `NOTICE-EasyTier.txt`)。
|
||
- 应用启动后自动拉起该进程,并在 `config/easytier_network.json` **首次自动生成**组网名与密钥(`peerUrls` 可留空或按需添加社区共享节点)。
|
||
- 若需跳过下载(如无网络 CI),可设置环境变量 `SKIP_EASYTIER=1`。
|
||
|
||
## 本项目已做的改造
|
||
|
||
1. **监听地址**:Electron 启动的 uvicorn 默认仍为 `127.0.0.1`。若存在 `config/web2_bind_host.txt`(单行,如 `0.0.0.0`)或环境变量 `WEB2_HOST`,则按该地址绑定。
|
||
2. **API 鉴权(可选)**:若存在 `config/web2_api_token.txt`(单行密钥)或环境变量 `WEB2_API_TOKEN`,则除 `/health`、OpenAPI 文档路径外,请求需带:
|
||
- `Authorization: Bearer <密钥>`,或
|
||
- `X-API-Token: <密钥>`
|
||
3. **Electron 前端**:自动从主进程读取 token 文件并注入请求头;浏览器 / React Native 需自行配置同一密钥。
|
||
4. **「网络」页**:提供「允许远程」开关(写入 `web2_bind_host.txt` 并重启后端)、本机候选虚拟网 IP 列表、**生成 API 密钥**,以及 **二维码**(JSON 文本,供 RN 扫码解析)。
|
||
|
||
### 扫码绑定 JSON 格式(`v` 为版本号)
|
||
|
||
```json
|
||
{
|
||
"v": 1,
|
||
"baseUrl": "http://10.126.126.1:8001",
|
||
"port": 8001,
|
||
"token": "与 web2_api_token.txt 一致",
|
||
"easytier": {
|
||
"networkName": "与 PC 上 config/easytier_network.json 一致",
|
||
"networkSecret": "同上",
|
||
"peerUrls": []
|
||
}
|
||
}
|
||
```
|
||
|
||
React Native:解析 JSON 后,**App 内**需使用与 PC 同版本的 EasyTier 组件并启动同一 `networkName` / `networkSecret`(可选 `peerUrls`),再请求 `baseUrl` 并带 `Authorization: Bearer ${token}`。
|
||
|
||
### 移动端 App 内置 EasyTier
|
||
|
||
- Android 可参考 [EasyTier 官方 APK](https://github.com/EasyTier/EasyTier/releases) 或同仓库原生库,将组网逻辑与扫码得到的 `easytier` 字段对齐;**同一套 JSON 即可实现 PC↔手机同网,无需用户再单独安装 EasyTier 客户端**(由你们 App 内置分发)。
|
||
|
||
## EasyTier 侧(用户操作)
|
||
|
||
1. PC 与手机分别安装 EasyTier,使用**相同** `--network-name` 与 `--network-secret`。
|
||
2. 按 [EasyTier 文档](https://github.com/EasyTier/EasyTier) 连接共享节点或自建入口,确认两端能 `ping` 通虚拟 IP。
|
||
3. 在 PC 上把 `config/web2_bind_host.txt` 设为 `0.0.0.0`,并创建 `config/web2_api_token.txt`(随机长串)。
|
||
4. 重启本应用;在手机浏览器或 RN 中请求:`http://<PC的EasyTier虚拟IP>:8001/process_monitor` 等,并带上 `Authorization` 头。
|
||
|
||
## React Native 示例
|
||
|
||
```ts
|
||
const BASE = 'http://10.126.126.1:8001' // 替换为 PC 在 EasyTier 中的 IP
|
||
const TOKEN = '你的 web2_api_token.txt 内容'
|
||
|
||
fetch(`${BASE}/health`).then((r) => r.json())
|
||
fetch(`${BASE}/process_monitor`, {
|
||
headers: { Authorization: `Bearer ${TOKEN}` },
|
||
})
|
||
```
|
||
|
||
## 安全说明
|
||
|
||
- `0.0.0.0` 且无 token 时,控制台会警告;**不要**将无鉴权 API 暴露到公网。
|
||
- 防火墙仅向 EasyTier 虚拟网段放行 `8001`(若需)。
|