一个强大的端口转发工具,支持HTTP/WebSocket/SSH隧道,用于将远程服务器端口映射到本地。
- 🚀 支持多种协议:HTTP、WebSocket、SSH
- 🔧 可扩展的自定义协议支持
- 🗄️ 专为MySQL、MQ等服务优化
- ⚙️ 基于Cobra CLI框架,配置灵活
- 📊 实时状态监控和日志
- 🔒 支持TLS加密传输
- 🔄 自动重连和故障恢复
┌─────────────┐ HTTP/WS/SSH ┌─────────────┐ TCP ┌─────────────┐
│ Client │ ◄──────────────► │ Server │ ◄────────► │ Target │
│ (本地A) │ │ (远程B) │ │ Service │
└─────────────┘ └─────────────┘ └─────────────┘
│ │ │
监听本地端口 WebSocket服务 MySQL/MQ等
localhost:3306 0.0.0.0:8080 target:3306
# 克隆项目
git clone https://github.com/viadroid/go-xtunnel.git
cd go-xtunnel
# 一键启动完整环境 (包含隧道服务器、MySQL、Redis、RabbitMQ)
./docker-setup.sh
# 或使用Make命令
make docker-setup# 克隆项目
git clone https://github.com/your-username/go-xtunnel.git
cd go-xtunnel
# 初始化依赖
go mod tidy
# 编译
go build -o xtunnel main.go- 在服务器B启动隧道服务
# 启动服务器(默认监听8080端口)
./xtunnel server --port 8080 --host 0.0.0.0
# 或使用配置文件
./xtunnel server --config config.yaml- 在客户端A创建隧道
# 将远程MySQL映射到本地3306端口
./xtunnel client \
--server ws://your-server:8080/tunnel \
--local-port 3306 \
--remote-host localhost \
--remote-port 3306
# 现在可以通过localhost:3306访问远程MySQL
mysql -h 127.0.0.1 -P 3306 -u username -p创建 config.yaml:
# 服务器配置
server:
host: "0.0.0.0"
port: 8080
protocol: "http"
tls:
enabled: false
# 客户端配置
client:
server_url: "ws://your-server:8080/tunnel"
tunnels:
- name: "mysql"
local_port: 3306
remote_host: "db.internal.com"
remote_port: 3306
- name: "rabbitmq"
local_port: 5672
remote_host: "mq.internal.com"
remote_port: 5672
- name: "redis"
local_port: 6379
remote_host: "cache.internal.com"
remote_port: 6379
log-level: "info"# 使用配置文件
./xtunnel server --config config.yaml
./xtunnel client --config config.yaml# 1. 一键启动完整环境
./docker-setup.sh
# 2. 查看服务状态
docker-compose ps
# 3. 查看日志
docker-compose logs -f xtunnel-server启动Docker环境后,可以访问以下服务:
# 隧道服务器
curl http://localhost:8080/health
# MySQL数据库
mysql -h 127.0.0.1 -P 3306 -u testuser -p testpassword
# Redis缓存
redis-cli -h 127.0.0.1 -p 6379
# RabbitMQ管理界面
open http://localhost:15672 # admin/admin123# 编译客户端
make build
# 连接到Docker环境中的MySQL
./xtunnel client \
--server ws://localhost:8080/tunnel \
--local-port 3307 \
--remote-host mysql \
--remote-port 3306
# 现在可以通过localhost:3307访问Docker中的MySQL
mysql -h 127.0.0.1 -P 3307 -u testuser -p# 启动服务
make compose-up
docker-compose up -d
# 停止服务
make compose-down
docker-compose down
# 查看日志
make compose-logs
docker-compose logs -f
# 重启服务
make compose-restart
docker-compose restart
# 清理环境
make docker-clean
docker-compose down -v# 使用生产环境配置
docker-compose -f docker-compose.prod.yml up -d
# 查看生产环境状态
docker-compose -f docker-compose.prod.yml ps
## 使用场景
### 1. 数据库访问
```bash
# MySQL隧道
./xtunnel client -s ws://server:8080/tunnel -l 3306 -r 3306 -R db.internal
# PostgreSQL隧道
./xtunnel client -s ws://server:8080/tunnel -l 5432 -r 5432 -R pg.internal
# RabbitMQ AMQP
./xtunnel client -s ws://server:8080/tunnel -l 5672 -r 5672 -R mq.internal
# RabbitMQ Management UI
./xtunnel client -s ws://server:8080/tunnel -l 15672 -r 15672 -R mq.internal# Redis隧道
./xtunnel client -s ws://server:8080/tunnel -l 6379 -r 6379 -R cache.internal# 启动服务器
xtunnel server [flags]
Flags:
-H, --host string 服务器监听地址 (default "0.0.0.0")
-p, --port int 服务器监听端口 (default 8080)
-P, --protocol string 服务器协议 (default "http")
--config string 配置文件路径
-v, --log-level string 日志级别 (default "info")# 启动客户端
xtunnel client [flags]
Flags:
-s, --server string 服务器URL (default "ws://localhost:8080/tunnel")
-l, --local-port int 本地监听端口 (default 3306)
-r, --remote-port int 远程目标端口 (default 3306)
-R, --remote-host string 远程目标主机 (default "localhost")
--config string 配置文件路径
-v, --log-level string 日志级别 (default "info")服务器提供REST API用于监控和管理:
# 健康检查
curl http://server:8080/health
# 状态信息
curl http://server:8080/api/status响应示例:
{
"status": "running",
"clients": 2,
"uptime": "2023-12-01T10:00:00Z"
}- HTTP: 基础HTTP隧道
- WebSocket: 实时双向通信(推荐)
- TCP: 原生TCP转发
项目设计支持自定义协议扩展,可以通过实现 Protocol 接口添加新协议:
type Protocol interface {
Name() string
Connect(config *ProtocolConfig) (Connection, error)
Handle(conn Connection, data []byte) error
}- 使用连接池减少连接开销
- 支持数据压缩降低带宽使用
- 智能重连机制保证服务可用性
- 并发处理多个隧道连接
- 支持TLS加密传输
- 连接认证和授权
- 连接频率限制
- 安全的错误处理
-
连接被拒绝
- 检查服务器是否启动
- 确认端口和防火墙设置
-
隧道断开
- 检查网络连接
- 查看服务器日志
-
性能问题
- 调整缓冲区大小
- 启用数据压缩
# 启用调试日志
./xtunnel server --log-level debug
# 查看特定隧道日志
./xtunnel client --log-level debug 2>&1 | grep "tunnel_id"go-xtunnel/
├── cmd/ # CLI命令
│ ├── root.go # 根命令
│ ├── server.go # 服务器命令
│ └── client.go # 客户端命令
├── pkg/ # 核心包
│ ├── server/ # 服务器实现
│ ├── client/ # 客户端实现
│ ├── protocol/ # 协议定义
│ ├── config/ # 配置管理
│ └── logger/ # 日志管理
├── config.yaml # 示例配置
└── main.go # 程序入口
# 编译
go build -o xtunnel main.go
# 运行测试
go test ./...
# 交叉编译
GOOS=linux GOARCH=amd64 go build -o xtunnel-linux main.go
GOOS=windows GOARCH=amd64 go build -o xtunnel-windows.exe main.goMIT License - 详见 LICENSE 文件
欢迎提交Issue和Pull Request!
- Fork项目
- 创建功能分支
- 提交更改
- 发起Pull Request
- GitHub Issues: [项目Issue页面]
- Email: likai.qin75@gmail.com