From 43c1d63449631b2febf461847e551360beab9d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Fri, 10 Jul 2026 08:27:02 +0800 Subject: [PATCH] =?UTF-8?q?```=20feat:=20=E6=B7=BB=E5=8A=A0TCP=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增TCP代理服务器支持,允许在指定端口监听TCP请求并转发到目标服务器。 - 在README.md中添加TCP代理服务器特性说明 - 添加TCP代理配置文件说明(include/tcp_*.json) - 详细文档化TCP代理配置选项和使用示例 - 在changes.md中更新相关变更记录 - 实现从include目录加载TCP代理配置的功能(gohttp.go) - 支持tcp_*.json文件命名约定的配置加载 ``` --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ changes.md | 26 +++++++++++++++++++++++++- gohttp.go | 20 ++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aee5efd..8bd6061 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ - 静态文件服务 - 反向代理(Proxy) - 路径重写(Rewrite) +- TCP代理服务器 - 健康检查 - 访问控制(IP白名单/黑名单) - JWT认证 @@ -34,6 +35,7 @@ make build ### 配置 1. 主配置文件 (config.json) 2. 域名配置文件 (include/*.json) +3. TCP代理配置文件 (include/tcp_*.json) ### 配置示例 ```json @@ -131,6 +133,42 @@ make build - `timeout`: 检查超时时间 - `retries`: 失败重试次数 +### TCP代理配置 +TCP代理支持在指定端口监听TCP请求并转发到指定目标服务器。 + +#### 在主配置文件中配置 +```json +{ + "tcp_proxys": [ + { + "name": "mysql-proxy", + "listen": "0.0.0.0:3307", + "target": "192.168.1.100:3306", + "enabled": true + } + ] +} +``` + +#### 在include目录中配置 +TCP代理配置也可以从include目录加载,使用 `tcp_*.json` 文件命名约定: + +**include/tcp_mysql.json:** +```json +{ + "name": "mysql-proxy", + "listen": "0.0.0.0:3307", + "target": "192.168.1.100:3306", + "enabled": true +} +``` + +**TCP代理配置项:** +- `name`: 代理名称(唯一标识) +- `listen`: 监听地址,格式如 `0.0.0.0:3307` +- `target`: 目标服务器地址,格式如 `192.168.1.100:3306` +- `enabled`: 是否启用 + ## 指令系统 指令系统采用类似Nginx的指令系统,格式为: diff --git a/changes.md b/changes.md index 9f29724..e3e0c87 100644 --- a/changes.md +++ b/changes.md @@ -179,7 +179,31 @@ type TcpProxyConfig struct { } ``` -### 6.5 服务器管理 (server/manager.go) +### 6.5 从include目录加载TCP代理配置 + +TCP代理配置也可以从include目录中加载,使用 `tcp_*.json` 文件命名约定: + +**include目录结构:** +``` +./include/ +├── servers/ +│ ├── web.json +│ └── api.json +├── tcp_mysql.json # TCP代理配置 +└── tcp_redis.json # TCP代理配置 +``` + +**include/tcp_mysql.json 示例:** +```json +{ + "name": "mysql-proxy", + "listen": "0.0.0.0:3307", + "target": "192.168.1.100:3306", + "enabled": true +} +``` + +### 6.6 服务器管理 (server/manager.go) 新增TCP代理服务器管理功能: - `TcpProxyManager`:TCP代理处理器映射 diff --git a/gohttp.go b/gohttp.go index 3211e74..5ab387a 100644 --- a/gohttp.go +++ b/gohttp.go @@ -148,6 +148,26 @@ func LoadConfig() { } } } + + // 加载TCP代理配置 from include directory (tcp_*.json files) + tcpConfigs, err := filepath.Glob(model.Config.IncludDir + "/tcp_*.json") + if err == nil { + for _, config := range tcpConfigs { + content, err := os.ReadFile(config) + if err == nil { + decoder := json.NewDecoder(strings.NewReader(string(content))) + for decoder.More() { + tcpProxy := model.TcpProxyConfig{} + err = decoder.Decode(&tcpProxy) + if err == nil { + model.Config.TcpProxys = append(model.Config.TcpProxys, &tcpProxy) + } else { + break + } + } + } + } + } } // 遍历配置中的所有服务器,规范化每个服务器配置中的路径