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

This reverts commit 3f374df724.
This commit is contained in:
sagitchu
2026-04-25 22:45:27 +08:00
parent 325baa8adc
commit a5c33c68d9
30 changed files with 123 additions and 744 deletions
-4
View File
@@ -116,10 +116,6 @@ 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,7 +16,6 @@ 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"
@@ -67,10 +66,6 @@ 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
}
+2 -9
View File
@@ -44,14 +44,9 @@ func Set(c *Config) {
func OnUpdate(f func(c *Config) error) error {
globalMux.Lock()
err := f(global)
globalMux.Unlock()
defer globalMux.Unlock()
if err == nil {
persist()
}
return err
return f(global)
}
type LogConfig struct {
@@ -578,7 +573,6 @@ func (c *Config) Load() error {
if err := v.ReadInConfig(); err != nil {
return err
}
SetPersistPath(v.ConfigFileUsed())
return v.Unmarshal(c)
}
@@ -596,7 +590,6 @@ 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
@@ -1,94 +0,0 @@
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
@@ -1,32 +0,0 @@
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,26 +1690,6 @@ 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)