You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.2 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# TCP 转发服务
## 功能
1. 监听 **TCP 端口**(默认 9600),接收外部命令
2. 根据命令向远端 TCP 服务发送对应消息
3. 启动后自动延时发送 `<1,1,post>`
## 命令流程
| TCP 收到(服务端) | 远端 TCP 发送 | 后续动作 |
|-------------------|--------------|----------|
| `<1,0,post>` | `<1,1,post>` | - |
| `<1,1,post>` | `<1,0,post>` | 延时后自动再发 `<1,1,post>` |
| 启动延时 | `<1,1,post>` | 仅一次 |
## 配置说明 (`config.json`)
| 字段 | 默认值 | 说明 |
|------|--------|------|
| `server.port` | `9600` | 本机 TCP 监听端口 |
| `remote.host` | `127.0.0.1` | 远端 TCP 服务地址 |
| `remote.port` | `9000` | 远端 TCP 服务端口 |
| `remote.dial_timeout_seconds` | `5` | 连接远端超时秒数 |
| `timing.startup_delay_seconds` | `180` | 启动后延时发送秒数(默认 3 分钟) |
| `timing.resend_delay_seconds` | `180` | 收到 `<1,1,post>` 后重发延时秒数(默认 3 分钟) |
| `log.dir` | `logs` | 日志目录 |
| `log.level` | `info` | 日志级别: debug/info/warn/error |
| `log.keep_days` | `60` | 日志保留天数,超期自动删除 |
| `reverse` | `false` | `true`=正(反转发送逻辑), `false`=反(默认不变) |
## 正反参数说明
| `reverse` | 收到 `<1,0,post>` 时发送 | 收到 `<1,1,post>` 时发送 | 启动/退出/定时发送 |
|-----------|-------------------------|-------------------------|-------------------|
| `false`(反,默认) | `<1,1,post>` | `<1,0,post>` | `<1,1,post>` |
| `true`(正) | `<1,0,post>` | `<1,1,post>` | `<1,0,post>` |
## 日志
- 按天滚动:`logs/tcp_service_2026-07-01.log`
- 每天零点自动切换到新文件
- 保留最近 60 天,超期自动清理
- 同时输出到控制台和文件
## 使用方式
### 编译
```bash
cd tcp
set GOOS=windows&& set GOARCH=386&& go build -o tcp_service.exe main.go
```
### 运行
```bash
# 使用默认 config.json
./tcp_service.exe
# 指定配置文件
./tcp_service.exe /path/to/config.json
```
### 测试(用 telnet / nc 发命令)
```bash
# 发送 <1,0,post>
echo "<1,0,post>" | nc 127.0.0.1 9600
# 发送 <1,1,post>
echo "<1,1,post>" | nc 127.0.0.1 9600
```