基于 go-gost/x 库实现的配置同步和流量统计客户端。
- 远程配置同步: 通过 WebSocket 从服务端实时同步配置
- 流量统计: 收集并上传统计信息到服务端(默认 10 秒间隔)
- 自动重连: 连接断开时自动尝试重连
- 心跳机制: 保持与服务器的活跃连接
- 配置热更新: 无需重启即可应用新配置
- Prometheus 指标: 内置 Prometheus 指标支持
go build -o gost-node ./cmd/...创建 .env 文件或设置环境变量:
# .env 文件
server=ws://your-server:8080/ws
token=your-auth-token
stats_interval=30s
enable_prometheus=true./gost-node程序会自动从当前目录读取 config.json 配置文件。
| 变量 | 默认值 | 说明 |
|---|---|---|
server |
- | 配置服务器 WebSocket 地址(必需) |
token |
- | 认证令牌(通过 TYZ-NODE-TOKEN 头传递) |
stats_interval |
10s | 统计上报间隔 |
enable_prometheus |
true | 是否启用 Prometheus 指标 |
prometheus_addr |
- | Prometheus 绑定地址 |
package main
import (
"context"
"log"
"time"
"github.com/laoshan/gost-node/pkg/gostsync"
)
func main() {
client, err := gostsync.NewConfigSyncClient(gostsync.Options{
URL: "ws://your-server:8080/ws",
Token: "your-auth-token",
StatsInterval: 10 * time.Second,
ReconnectDelay: 5 * time.Second,
})
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
if err := client.Start(ctx); err != nil {
log.Fatalf("Failed to start client: %v", err)
}
<-ctx.Done()
client.Stop()
}{
"type": "config",
"payload": {
"version": "1234567890",
"timestamp": 1699987654,
"config": {
"services": [...]
}
}
}{
"type": "stats",
"payload": {
"timestamp": 1699987654,
"service_stats": {
"http": {
"total_conns": 100,
"current_conns": 5,
"input_bytes": 1024000,
"output_bytes": 2048000,
"total_errors": 2
}
},
"node_info": {
"hostname": "hostname",
"uptime": 86400.5,
"gost_version": "3.0"
}
}
}{
"type": "heartbeat",
"payload": {
"timestamp": 1699987654,
"status": "healthy"
}
}| 指标 | 类型 | 说明 |
|---|---|---|
gost_sync_stats_report_total |
Counter | 统计上报次数 |
gost_sync_stats_report_errors_total |
Counter | 统计上报错误次数 |
gost_sync_stats_bytes_sent_total |
Counter | 发送字节数 |
gost_sync_stats_bytes_received_total |
Counter | 接收字节数 |
gost_sync_stats_collection_total |
Counter | 统计收集次数 |
gost_sync_stats_collection_duration_seconds |
Histogram | 统计收集耗时 |
cmd/gost-node/
├── main.go # 程序入口和服务管理
└── register.go # GOST 组件注册
pkg/gostsync/
├── client.go # WebSocket 客户端和统计功能
├── config.go # 配置加载/保存
└── stats.go # 统计聚合和 Prometheus 指标
MIT License