这是一个用Golang实现的WebSocket代理系统,支持客户端ID代理转发和反向HTTP请求功能。
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐ │ HTTP客户端 │───▶│ WebSocket服务端 │◀───│ WebSocket客户端 │ │ │ │ │ │ │ │ 发起HTTP请求 │ │ 管理客户端连接 │ │ 执行实际HTTP请求 │ │ /proxy/{id}/... │ │ 转发请求/响应 │ │ 返回HTTP响应 │ └─────────────────┘ └──────────────────┘ └─────────────────┘
go mod tidy
使用PowerShell:
.\scripts\start_server.ps1
或使用CMD:
.\scripts\start_server.bat
或直接运行:
go run cmd/server/main.go -port 8080
使用PowerShell:
.\scripts\start_client.ps1
或使用CMD:
.\scripts\start_client.bat
或直接运行:
go run cmd/client/main.go -server localhost:8080 -id my-client-001
通过WebSocket客户端代理HTTP请求:
# 格式: /proxy/{client_id}/{target_url}
curl -X GET http://localhost:8080/proxy/my-client-001/httpbin.org/get
curl -X POST http://localhost:8080/proxy/my-client-001/httpbin.org/post -d '{"key":"value"}'
服务端主动发起HTTP请求:
curl -X POST http://localhost:8080/api/reverse-http \
-H "Content-Type: application/json" \
-d '{
"client_id": "my-client-001",
"method": "GET",
"url": "http://httpbin.org/get"
}'
curl http://localhost:8080/api/clients
curl http://localhost:8080/health
{
"server": {
"port": 8080,
"host": "localhost"
},
"websocket": {
"max_clients": 100,
"read_timeout": 60,
"write_timeout": 10
},
"http": {
"proxy_timeout": 10,
"max_body_size": 10485760
}
}
{
"client": {
"server_url": "localhost:8080",
"client_id": "my-client-001",
"reconnect_interval": 5
},
"http": {
"timeout": 30,
"max_redirects": 10
}
}
register - 客户端注册http_request - HTTP请求http_response - HTTP响应reverse_http - 反向HTTP请求{
"type": "http_request",
"client_id": "my-client-001",
"data": {
"method": "GET",
"url": "http://httpbin.org/get",
"headers": {"Content-Type": "application/json"},
"body": "{}"
},
"id": "1234567890"
}
websocket-proxy/ ├── cmd/ │ ├── server/ # 服务端入口 │ └── client/ # 客户端入口 ├── internal/ │ ├── server/ # 服务端核心逻辑 │ ├── client/ # 客户端核心逻辑 │ └── common/ # 通用类型定义 ├── configs/ # 配置文件 ├── scripts/ # 启动脚本 └── README.md
# 构建服务端
go build -o bin/server cmd/server/main.go
# 构建客户端
go build -o bin/client cmd/client/main.go
# 启动服务端
./bin/server -port 8080
# 启动客户端
./bin/client -server localhost:8080 -id test-client
客户端连接失败
代理请求超时
WebSocket连接断开
服务端和客户端都会输出详细的日志信息,包括连接状态、请求处理情况和错误信息。
MIT License