Files
server/README.md
T

315 lines
11 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# VPS 流量转发配置
基于 **Xray + Nginx SNI 分流 + Mihomo TUN 透明代理 + WireGuard**,实现:
内网服务反向代理、多 VPS 链路中转、BT 透明代理、Minecraft UDP 转发。
---
## 设备
| 设备 | IP / 域名 | 角色 |
|---|---|---|
| **北京 VPS** | `salmonstill.cn` / `49.232.242.90` | 公网入口,Nginx + Xray + socat + WireGuard |
| **东京 VPS** | `tokyo.salmonstill.cn` / `43.165.178.10` | 代理出口,Xray Reality 出站 |
| **旁路由** | `192.168.1.199` / WG `10.0.0.2` | 内网核心,Xray bridge + Mihomo TUN |
| **NAS** | `192.168.1.188` | 绿联云 UGOS,思源笔记等服务 |
| **qBittorrent** | `192.168.1.200` (macvlan) | PT 下载,独立 IP |
| **Windows** | `192.168.1.177` | 内网办公,SSH |
| **Spark** | `192.168.1.166` | Ubuntu 主机,本地 TUN 代理 |
---
## Nginx SNI 分流 (`beijing-vps-stream.conf`)
监听 `:443`,按 SNI 将流量分发到不同后端:
| SNI | 后端 | 端口 | 用途 |
|---|---|---|---|
| `www.apple.com` | Xray interconn | :9443 | 反向代理隧道 |
| `www.microsoft.com` | Xray mihomo_in | :9444 | Mihomo 客户端 → 东京出口 |
| `news.apple.com` | Xray proxy_from_lan | :9445 | 北京直连代理 |
| `drive.salmonstill.cn` | NAS | :38653 | 绿联云直接转发 |
| 默认 | NPM | :8443 | Nginx Proxy Manager |
东京 VPS (`东京-vps-stream.conf`)`www.microsoft.com` → Xray :9443,默认 → NPM。
---
## Xray 核心
### 北京 VPS (`xray-北京vps-config.json`)
**Reality 入站** — 共享 privateKey `GGT9LfN_...`,由 Nginx SNI 转发:
| Tag | 端口 | SNI | Reality target |
|---|---|---|---|
| `interconn` | :9443 | `www.apple.com` | `www.apple.com:443` |
| `mihomo_in` | :9444 | `www.microsoft.com` | `www.microsoft.com:443` |
| `proxy_from_lan` | :9445 | `news.apple.com` | `www.apple.com:443` |
**dokodemo-door 入站** — 端口转发到旁路由反向隧道,共 11 个:
| Tag | 公网端口 | 路由目标 |
|---|---|---|
| `external` | 38653 | → portal → NAS :9443 |
| `external_siyuan` | 38654 | → portal → NAS :5005 |
| `external_minecraft` | 39132 | → portal → 旁路由 :39132 |
| `external_wsl` | 38655 | → portal → Windows :22 |
| `external_nas_ssh` | 38656 | → portal → NAS :22 |
| `external_router_ssh` | 38657 | → portal → 旁路由 :22 |
| `external_router_web` | 39766 | → portal → 旁路由 :80 |
| `external_tmp` | 8501 | → portal → Windows :8501 |
| `external_3000` | 3000 | → portal → NAS :3000 |
| `external_222` | 222 | → portal → NAS :222 |
| `external_qbit` | 51413 | → portal → qBittorrent :51413 |
**其他入站**
| Tag | 端口 | 类型 | 用途 |
|---|---|---|---|
| `proxy_in` | 10809 | mixed | SOCKS5/HTTP 代理 → 东京出口 |
| `socks-dynamic` | 38658 | SOCKS5 (password) | 动态 LAN 端口访问 |
**出站**`direct`freedom 直连)、`to_tokyo`VLESS+Reality → `us.salmonstill.cn:443`
**路由**:所有 `external_*` + `interconn` + `socks-dynamic``portal`(反向隧道);`mihomo_in` + `proxy_in``to_tokyo``proxy_from_lan``direct`
### 旁路由 (`xray-旁路由-config.json`)
**Bridge 入站**:与北京 VPS 的 Portal 建立永久反向隧道。
**出站**14 个):
| Tag | 目标 | 用途 |
|---|---|---|
| `to_nas` | `192.168.1.188:9443` | NAS 管理 |
| `to_siyuan` | `192.168.1.188:5005` | 思源笔记 |
| `to_wsl` | `192.168.1.177:22` | WSL SSH |
| `to_nas_ssh` | `192.168.1.188:22` | NAS SSH |
| `to_router_ssh` | `192.168.1.199:22` | 旁路由 SSH |
| `to_router_web` | `192.168.1.199:80` | 旁路由 Web |
| `to_minecraft` | `127.0.0.1:39132` | Minecraft 服务 |
| `to_tmp` | `192.168.1.177:8501` | 临时服务 |
| `to_3000` | `192.168.1.188:3000` | Web 服务 |
| `to_222` | `192.168.1.188:222` | 备用服务 |
| `to_qbit` | `192.168.1.200:51413` | qBittorrent 入站 |
| `interconn` | VLESS+Reality → `salmonstill.cn:443` (SNI=www.apple.com) | 隧道链接 |
| `to_beijing_direct` | VLESS+Reality → `salmonstill.cn:443` (SNI=news.apple.com) | 北京直连 |
| `direct` | freedom | 直连 |
**路由规则**bridge 入站按端口匹配):
- 38653 → `to_nas`NAS 管理)
- 38654 → `to_siyuan`
- 38655 → `to_wsl`
- 38656 → `to_nas_ssh`
- 38657 → `to_router_ssh`
- 8501 → `to_tmp`
- 39766 → `to_router_web`
- 3000 → `to_3000`
- 222 → `to_222`
- 39132 → `to_minecraft`
- 51413 → `to_qbit`
- 38658 → `direct`(SOCKS5 动态回家,直接连接目标地址)
- 默认(catch-all)→ **`direct`**(不匹配端口规则的动态请求直连目标)
- `socks-lan` 入站 → `to_beijing_direct`
> 所有显式端口规则优先于 catch-all,现有端口转发不受影响。
### 东京 VPS (`tokyo-vps-config.json`)
极简:VLESS+Reality 入站 `:9443`SNI=www.microsoft.com),freedom 出站。
---
## Mihomo
### 旁路由 (`旁路由的mihomo config.yaml`)
**TUN 透明代理**`tun` 模式,内核级劫持。创建 `Meta` 虚拟接口(`198.18.0.1/16`),所有经过旁路由网关的设备流量自动被 TUN 接管:
```yaml
tun:
enable: true
stack: system
dns-hijack:
- any:53
auto-route: true
auto-detect-interface: true
```
配合 **fake-ip DNS**`enhanced-mode: fake-ip`),DNS 请求返回 `198.18.x.x` 假 IP,强制流量进入代理路由。
**代理节点**
- `US-Direct` — VLESS+Reality → `us.salmonstill.cn:443`(默认国外出口)
- `Xray-Real` — VLESS+Reality → `49.232.242.90:443`(备用,经北京→东京)
**关键策略组**
- 国外 → `[US-Direct, 直连]`
**防死循环 IP**`49.232.242.90`(北京 VPS)、`43.165.178.10`(东京 VPS)强制直连。
**额外监听器**`:7891`(全局→US-Direct)、`:7892`(强制直连)
### macOS (`非tun模式的主机mihomo config.yaml`)
结构与旁路由一致,无 TUN,监听器 `:7890` + `:7891` + `:7892`
### Ubuntu / Spark (`tun模式的主机mihomo config.yaml`)
本地 TUN 模式(`stack: system`),不依赖旁路由网关。设备重启后 `auto-route` 自建路由表,稳定可靠。监听器 `:7890` + `:7891` + `:7892`,节点直连 US-Direct。
### subscribe.yaml
通用订阅模板,节点:US-Direct(默认)、Xray-Real(备选)。国外组 `[Xray-Real, US-Direct, 直连]`global-mixed listener 走 US-Direct。
---
## qBittorrent 透明代理
详见 [`qbittorrent流量转发.md`](./qbittorrent流量转发.md)。
```
出站: qBittorrent → SOCKS5 旁路由:1080 (socks-lan) → Xray to_beijing_direct
→ salmonstill.cn:443 (SNI=news.apple.com) → proxy_from_lan:9445 → direct → 互联网
入站: BT Peer → salmonstill.cn:51413 → portal → 隧道 → bridge → to_qbit → 1.200:51413
```
qBittorrent 设置:SOCKS5 代理 → 旁路由 `192.168.1.199:1080`,监听 `51413`,仅 TCP(关 μTP/UDP)。
---
## Minecraft UDP 转发
详见 [`我的世界udp转发.md`](./我的世界udp转发.md)。独立于 Xray 体系:
```
公网 :19132 → 北京VPS socat → WireGuard 隧道 → 旁路由 10.0.0.2
→ 端口转发+SNAT → NAS :19132 → Docker (itzg/minecraft-bedrock-server)
```
---
## SOCKS5 动态 LAN 端口访问
通过新增的 `socks-dynamic` 入站(`:38658`,密码认证),可从外部访问任意内网 IP:PORT,无需事先配置端口规则。
```
浏览器 SOCKS5 → salmonstill.cn:38658
→ 北京VPS socks-dynamic → portal → 反向隧道
→ 旁路由 bridge → catch-all direct → 任意 LAN IP:PORT
```
---
## Reality 密钥
| 位置 | Private Key | Public Key (客户端用) |
|---|---|---|
| **北京 VPS**3 个入站共用) | `GGT9LfN_2JdQG68cwrULgUK-adfT6wIokLzWjaB0fXs` | `62y5gDjPrdeuePGl-D2IW4Cw9Kb8_bSBBTmArvL7Nhs` |
| **东京 VPS**to_tokyo 出口) | `iBlu3eH1VLf1S5Qw87m_1w0TGYUktDwHAzgpQ2aKuGI` | `jr_zQjC4mvlQITuG5Ap5Mxqe5EBbGyyvwbVLDEi8OCA` |
`Beijing-Direct` 节点必须 `skip-cert-verify: true`,因 Reality 返回 target`www.apple.com`)的证书,而 SNI 是 `news.apple.com`TLS SAN 校验会失败。
---
## 流量路径
### 路径 1:内网服务反向代理
```
用户 → salmonstill.cn:端口 → dokodemo-door → portal → 隧道 → bridge
→ 端口匹配出站 → 内网目标 → 原路返回
```
### 路径 2:代理翻墙(东京出口)
```
Mihomo 客户端 → salmonstill.cn:443 (SNI=www.microsoft.com)
→ mihomo_in :9444 → to_tokyo → 东京 VPS → freedom → 互联网
```
### 路径 3qBittorrent 代理出站
```
qBittorrent → SOCKS5 旁路由:1080 → Xray to_beijing_direct
→ salmonstill.cn:443 (SNI=news.apple.com) → proxy_from_lan → direct → 互联网
```
### 路径 4BT 入站
```
BT Peer → salmonstill.cn:51413 → portal → 隧道 → bridge → to_qbit → 1.200:51413
```
### 路径 5Minecraft UDP
```
公网玩家 → salmonstill.cn:19132 → socat → WireGuard → 旁路由 → NAS :19132
```
### 路径 6SOCKS5 动态回家
```
浏览器 SOCKS5 → salmonstill.cn:38658 → socks-dynamic → portal → 隧道
→ bridge → catch-all direct → 任意 LAN IP:PORT
```
---
## 文件清单
| 文件 | 目标设备 | 部署路径 |
|---|---|---|
| `beijing-vps-stream.conf` | 北京 VPS | `/etc/nginx/stream.conf.d/` |
| `xray-北京vps-config.json` | 北京 VPS | `/usr/local/etc/xray/config.json` |
| `东京-vps-stream.conf` | 东京 VPS | `/etc/nginx/stream.conf.d/` |
| `tokyo-vps-config.json` | 东京 VPS | `/usr/local/etc/xray/config.json` |
| `xray-旁路由-config.json` | 旁路由 | `/etc/xray/config.json` |
| `旁路由的mihomo config.yaml` | 旁路由 | `/opt/mihomo/config.yaml` |
| `非tun模式的主机mihomo config.yaml` | macOS | `~/Library/LaunchAgents/` (launchctl 管理) |
| `tun模式的主机mihomo config.yaml` | Spark (Ubuntu) | `/opt/mihomo/config.yaml` (systemd) |
| `subscribe.yaml` | 通用 | 订阅模板 |
| `subscribe-7891-only.yaml` | 通用 | 精简版模板 |
| `qbittorrent流量转发.md` | — | 方案文档 |
| `我的世界udp转发.md` | — | 方案文档 |
---
## 部署流程
```
1. 腾讯云防火墙开放端口(443, 51413/tcp, 51820/udp, 19132/udp
2. 北京 VPS: 部署 Nginx stream → nginx -t && systemctl reload nginx
3. 北京 VPS: 部署 Xray → systemctl restart xray
4. 北京 VPS: 部署 WireGuard → wg-quick@wg0
5. 北京 VPS: 部署 socat → systemctl start mc-forward
6. 东京 VPS: 部署 Nginx + Xray
7. 旁路由: 部署 Xray → /etc/init.d/xray restart
8. 旁路由: 部署 Mihomo → /etc/init.d/mihomo restart
9. 旁路由: 配置 WireGuard + 端口转发 + SNATLuCI
10. NAS: 创建 macvlan 网络 → 启动 qBittorrent 容器
11. NAS: 启动 Minecraft 容器
```
---
## 验证命令
```bash
# 北京 VPS
ss -tlnp | grep -E '944[345]|3865[3-8]|51413|10809'
systemctl status nginx xray wg-quick@wg0 mc-forward
# 东京 VPS
ss -tlnp | grep 9443
systemctl status nginx xray
# 旁路由
ip link show Meta # TUN 接口存在且 UP
ss -tlnp | grep -E '789[0-2]|1080' # Mihomo 端口 + Xray socks-lan
mihomo -d /opt/mihomo -t # 配置文件校验
tail /opt/mihomo/logs/mihomo.log | grep TUN # 确认 TUN 无报错
wg show
# qBittorrent 连通性
docker exec qbittorrent curl -s https://ip.sb # 应返回北京 VPS IP
# Minecraft
systemctl status mc-forward
tcpdump -i wg0 udp port 19132 -n
```