Auto-sync: 2026-04-26 20:02

This commit is contained in:
2026-04-26 20:02:45 +08:00
parent d42bc16120
commit b7d9d0f5d1
54 changed files with 3196 additions and 3021 deletions

View File

@@ -1,112 +1,47 @@
---
title: "TCP 隧道"
type: concept
aliases: [TCP Tunnel, TCP端口转发, TCP代理]
tags: [network, tunneling, protocol]
---
# TCP 隧道
## Definition
**TCP 隧道**是通过在两个端点之间建立虚拟连接,将本地 TCP 端口的流量透明传输到远程端点的技术。TCP 隧道是构建 VPN、反向代理和内网穿透的基础机制之一。
## How It Works
```
┌─────────────┐ ┌─────────────┐
│ 本地机器 │ │ VPS │
│ │ │ │
│ 应用 → :22 │ ──── TCP 隧道 ──── → │ :60022 ← ──│──── 外部 SSH 客户端
│ │ (frp/nc/socat) │ │
└─────────────┘ └─────────────┘
```
## TCP vs HTTP/HTTPS 隧道
| 特性 | TCP 隧道 | HTTP/HTTPS 隧道 |
|------|----------|----------------|
| **协议** | 任意 TCP | HTTP/HTTPS |
| **应用层解析** | ❌ 不解析 | ✅ 可解析 |
| **WebSocket** | ❌ 不支持 | ✅ 支持 |
| **使用场景** | SSH、数据库、任意 TCP | Web 服务 |
| **Caddy 支持** | ❌ 不支持 | ✅ 支持 |
## frp TCP 映射
### 配置示例
```ini
# frpc.ini
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 60022
```
### 访问链路
```
外部 SSH 客户端
ssh -p 60022 user@vps-ip
VPS :60022 (frps 监听)
frp 隧道
内网机器 :22
本地 SSH 服务
```
## Common Tools for TCP Tunneling
| 工具 | 特点 | 使用场景 |
|------|------|---------|
| **frp** | 高性能、支持 Dashboard | 内网穿透 |
| **socat** | 简单单次转发 | 临时调试 |
| **netcat (nc)** | 基础端口转发 | 快速测试 |
| **ssh -L** | SSH 内置隧道 | 临时访问 |
| **stunnel** | SSL 隧道加密 | 安全转发 |
## SSH 原生隧道 (替代方案)
### 临时隧道
```bash
# 本地端口转发:访问远程内网服务
ssh -L 8080:internal:80 user@vps
# 远程端口转发:暴露本地服务到公网
ssh -R 60022:localhost:22 user@vps
```
### 持久化问题
SSH 隧道在网络中断后不会自动重连,生产环境推荐使用 frp。
## Security Considerations
### TCP 隧道注意事项
1. **不经过 HTTP 代理**TCP 流量不被 Caddy/Nginx 解析
2. **直接暴露端口**SSH 22 端口不宜直接暴露,使用非标准端口
3. **IP 白名单**:防火墙限制来源 IP
4. **公钥认证**:禁用密码登录
### 建议配置
```bash
# VPS 防火墙:只允许特定 IP
sudo ufw allow from <home_ip> to any port 60022 proto tcp
# SSH 配置:禁用密码
sudo nano /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes
```
## Related Concepts
- [[内网穿透]] — TCP 隧道的典型应用场景
- [[frp]] — 实现 TCP 隧道的工具
- [[反向代理]] — HTTP/HTTPS 层面的代理(与 TCP 层互补)
- [[VPS]] — TCP 隧道的公网端点
## References
- frp TCP: https://gofrp.org/docs/features/common-address-types/#tcp
- SSH Tunneling: https://www.ssh.com/academy/ssh/tunneling
---
title: "TCP隧道"
type: concept
tags: [networking, tunnel, tcp, ssh, frp]
last_updated: 2026-04-03
---
## Aliases
- TCP Tunnel
- TCP 隧道
## Definition
TCP 隧道TCP Tunnel是一种通过已建立的连接传输 TCP 数据包的技术,允许将本地 TCP 端口的流量通过隧道传输到远程位置。常见实现方式包括 SSH 隧道(`ssh -L` / `ssh -R`)和 frp 的 TCP 类型映射。TCP 隧道与 HTTP 代理不同,它传输的是原始 TCP 数据,不解析应用层协议。
## SSH 隧道
```bash
# 本地端口转发Local Port Forwarding将远程端口映射到本地
ssh -L local_port:remote_host:remote_port user@gateway
# 反向端口转发Reverse Port Forwarding将本地端口映射到远程
ssh -R remote_port:local_host:local_port user@gateway
```
### SSH 反向隧道限制
- 需要保持 SSH 连接持续活跃(可用 `autossh` 自动重连)
- 不适合多客户端并发访问
- 无 Web 管理界面
- 不支持 UDP
## frp TCP 隧道
详见 [[frp]]
## 在本 Wiki 中的应用
- [[通过VPS+内网反向代理实现域名访问内网穿透]]
- frp TCP 映射 NAS 端口5000→15000
- frp TCP 映射 Ubuntu 端口5678→15678, 9091→19091, 3000→13000
- frp TCP 映射 SSH22→60022
- SSH 穿透不走 Caddy纯 TCPCaddy 只处理 HTTP/HTTPS
## Related Concepts
- [[内网穿透]]TCP 隧道是内网穿透的底层实现机制
- [[反向代理]]HTTP/HTTPS 流量在 TCP 隧道之上还需要反向代理层Caddy
- [[frp]]frp 支持 TCP/UDP/HTTP/WebSocket 等多种隧道类型
## References
- SSH tunneling: `man ssh`(搜索 "SSH_PORT_FORWARDING" 或 "-L/-R"
- frp TCP 类型: https://github.com/fatedier/frp#configuration