Skip to content

一个强大的端口转发工具,支持HTTP/WebSocket/SSH隧道,用于将远程服务器端口映射到本地。

License

Notifications You must be signed in to change notification settings

ViaDroid/go-xtunnel

Repository files navigation

go-xtunnel

一个强大的端口转发工具,支持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

快速开始

方式1: 使用Docker (推荐)

# 克隆项目
git clone https://github.com/viadroid/go-xtunnel.git
cd go-xtunnel

# 一键启动完整环境 (包含隧道服务器、MySQL、Redis、RabbitMQ)
./docker-setup.sh

# 或使用Make命令
make docker-setup

方式2: 本地编译安装

# 克隆项目
git clone https://github.com/your-username/go-xtunnel.git
cd go-xtunnel

# 初始化依赖
go mod tidy

# 编译
go build -o xtunnel main.go

基本使用

  1. 在服务器B启动隧道服务
# 启动服务器(默认监听8080端口)
./xtunnel server --port 8080 --host 0.0.0.0

# 或使用配置文件
./xtunnel server --config config.yaml
  1. 在客户端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

🐳 Docker 使用

Docker 快速开始

# 1. 一键启动完整环境
./docker-setup.sh

# 2. 查看服务状态
docker-compose ps

# 3. 查看日志
docker-compose logs -f xtunnel-server

Docker 服务访问

启动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

Docker 客户端连接示例

# 编译客户端
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

Docker 常用命令

# 启动服务
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

2. 消息队列访问

# 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

3. 缓存服务访问

# 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")

API接口

服务器提供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加密传输
  • 连接认证和授权
  • 连接频率限制
  • 安全的错误处理

故障排除

常见问题

  1. 连接被拒绝

    • 检查服务器是否启动
    • 确认端口和防火墙设置
  2. 隧道断开

    • 检查网络连接
    • 查看服务器日志
  3. 性能问题

    • 调整缓冲区大小
    • 启用数据压缩

日志分析

# 启用调试日志
./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.go

许可证

MIT License - 详见 LICENSE 文件

贡献

欢迎提交Issue和Pull Request!

  1. Fork项目
  2. 创建功能分支
  3. 提交更改
  4. 发起Pull Request

联系方式

About

一个强大的端口转发工具,支持HTTP/WebSocket/SSH隧道,用于将远程服务器端口映射到本地。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published