fix: 保留用户/规则高级设置并增强节点运行时恢复 (#468)

* fix: 用户编辑时 maxConn 字段未正确回填

- User 接口添加 maxConn 类型定义
- handleEdit 中回填 maxConn 值
- normalizeUserItem 添加 maxConn 字段处理

解决编辑用户时最大连接数显示为 0 的问题

* fix: 保留转发设置并增强节点运行时恢复
This commit is contained in:
sagit
2026-04-25 18:12:13 +08:00
committed by GitHub
parent 9b923a2d0b
commit 3f374df724
30 changed files with 744 additions and 123 deletions
+4
View File
@@ -116,6 +116,10 @@ func main() {
fmt.Printf("✅ 配置加载成功 - addr: %s\n", config.Addr)
// 设置运行时配置持久化路径
socket.SetConfigPersistPath("gost.json")
// 启用持久化将在 program.Start() 后开启,避免启动加载阶段触发冗余写入
log := xlogger.NewLogger()
logger.SetDefault(log)
+5
View File
@@ -16,6 +16,7 @@ import (
metrics "github.com/go-gost/x/metrics/service"
"github.com/go-gost/x/registry"
xservice "github.com/go-gost/x/service"
"github.com/go-gost/x/socket"
"github.com/judwhite/go-svc"
"net/http"
"os"
@@ -66,6 +67,10 @@ func (p *program) Start() error {
return err
}
// Enable config persistence after initial load so runtime mutations
// (AddService, UpdateService, DeleteService, etc.) are saved to disk.
socket.EnableConfigPersist()
if err := p.run(cfg); err != nil {
return err
}
+9 -2
View File
@@ -44,9 +44,14 @@ func Set(c *Config) {
func OnUpdate(f func(c *Config) error) error {
globalMux.Lock()
defer globalMux.Unlock()
err := f(global)
globalMux.Unlock()
return f(global)
if err == nil {
persist()
}
return err
}
type LogConfig struct {
@@ -573,6 +578,7 @@ func (c *Config) Load() error {
if err := v.ReadInConfig(); err != nil {
return err
}
SetPersistPath(v.ConfigFileUsed())
return v.Unmarshal(c)
}
@@ -590,6 +596,7 @@ func (c *Config) ReadFile(file string) error {
if err := v.ReadInConfig(); err != nil {
return err
}
SetPersistPath(v.ConfigFileUsed())
return v.Unmarshal(c)
}
+94
View File
@@ -0,0 +1,94 @@
package config
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
)
var (
persistPath string
persistMu sync.Mutex
persistEnable bool
)
// SetPersistPath sets the file path where runtime config changes will be
// automatically persisted. Call this once during agent startup before any
// OnUpdate mutations occur.
func SetPersistPath(path string) {
persistMu.Lock()
defer persistMu.Unlock()
persistPath = path
}
func PersistPath() string {
persistMu.Lock()
defer persistMu.Unlock()
return persistPath
}
// EnablePersist turns on automatic persistence. Call this after the initial
// config has been loaded (e.g. after program.Start) so that startup loading
// does not trigger redundant disk writes.
func EnablePersist() {
persistMu.Lock()
defer persistMu.Unlock()
persistEnable = true
}
// persist writes the current global config to the configured file atomically.
func persist() {
persistMu.Lock()
path := persistPath
enabled := persistEnable
persistMu.Unlock()
if !enabled || path == "" {
return
}
cfg := Global()
if cfg == nil {
return
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
if err := enc.Encode(cfg); err != nil {
fmt.Printf("⚠️ config persist: marshal failed: %v\n", err)
return
}
// Atomic write: write to temp file then rename
dir := filepath.Dir(path)
tmp, err := os.CreateTemp(dir, ".gost-*.tmp")
if err != nil {
fmt.Printf("⚠️ config persist: create temp file failed: %v\n", err)
return
}
tmpName := tmp.Name()
if _, err := tmp.Write(buf.Bytes()); err != nil {
tmp.Close()
os.Remove(tmpName)
fmt.Printf("⚠️ config persist: write failed: %v\n", err)
return
}
if err := tmp.Close(); err != nil {
os.Remove(tmpName)
fmt.Printf("⚠️ config persist: close temp file failed: %v\n", err)
return
}
if err := os.Rename(tmpName, path); err != nil {
os.Remove(tmpName)
fmt.Printf("⚠️ config persist: rename failed: %v\n", err)
return
}
fmt.Printf("💾 节点配置已持久化到 %s\n", path)
}
+32
View File
@@ -0,0 +1,32 @@
package config
import (
"os"
"path/filepath"
"testing"
)
func TestReadFileSetsPersistPath(t *testing.T) {
originalPath := persistPath
originalEnabled := persistEnable
persistPath = ""
persistEnable = false
t.Cleanup(func() {
persistPath = originalPath
persistEnable = originalEnabled
})
dir := t.TempDir()
configFile := filepath.Join(dir, "custom-gost.yaml")
if err := os.WriteFile(configFile, []byte("services: []\n"), 0o644); err != nil {
t.Fatalf("write config file: %v", err)
}
var cfg Config
if err := cfg.ReadFile(configFile); err != nil {
t.Fatalf("ReadFile: %v", err)
}
if persistPath != configFile {
t.Fatalf("expected persistPath %q, got %q", configFile, persistPath)
}
}
+20
View File
@@ -1690,6 +1690,26 @@ func StartWebSocketReporterWithConfig(addr string, secret string, http int, tls
return reporter
}
var configPersistPath string
// SetConfigPersistPath sets the path where runtime config changes will be
// persisted to disk (gost.json). Called by main during agent startup.
func SetConfigPersistPath(path string) {
configPersistPath = path
config.SetPersistPath(path)
}
// EnableConfigPersist turns on automatic disk persistence after the initial
// config has been loaded and applied.
func EnableConfigPersist() {
config.EnablePersist()
path := config.PersistPath()
if path == "" {
path = configPersistPath
}
fmt.Printf("🔒 节点配置持久化已启用,运行时变更将自动保存到 %s\n", path)
}
// handleTcpPing 处理TCP ping诊断命令
func (w *WebSocketReporter) handleTcpPing(data interface{}) (TcpPingResponse, error) {
jsonData, err := json.Marshal(data)