feat(remote): 支持单行连接与同机实例继承

- 远端地址和注册令牌合并为单行填写\n- 同机实例通过机器级配置继承远端连接\n- 实例令牌仍按实例独立保存
This commit is contained in:
yml
2026-11-21 22:01:33 +08:00
parent 3cb7a6bad8
commit d37fd50233
8 changed files with 310 additions and 27 deletions
+13
View File
@@ -187,6 +187,14 @@ func (a *App) Startup(ctx context.Context) {
// 优先初始化调度器和账号库,避免前端首屏请求早于启动网络探测时读到 nil。
cfg := config.LoadConfig(a.paths.ConfigFile)
profileFound := false
if profile, ok, err := config.LoadMachineRemoteProfile(); err != nil {
logger.LogEvent("配置", "warn", "读取本机共享远端配置失败: %v", err)
} else if ok {
config.ApplyMachineRemoteProfile(&cfg, profile)
profileFound = true
logger.LogEvent("配置", "success", "已继承本机共享远端配置")
}
sharedPath := strings.TrimSpace(cfg.SharedProxyConfigFile)
if sharedPath == "" {
if inheritedPath, err := config.LoadSharedProxyConfigPointer(); err != nil {
@@ -205,6 +213,11 @@ func (a *App) Startup(ctx context.Context) {
logger.LogEvent("配置", "success", "已加载共享代理配置: %s", sharedPath)
}
}
if !profileFound && cfg.RemoteEnabled {
if err := config.SaveMachineRemoteProfile(cfg); err != nil {
logger.LogEvent("配置", "warn", "初始化本机共享远端配置失败: %v", err)
}
}
a.setCfg(cfg)
logger.SetMinLevel(cfg.LogLevel)
a.replaceTrace(cfg)
+3
View File
@@ -180,6 +180,9 @@ func (a *App) SaveSettings(cfg types.Config) types.ErrorResult {
cfg.RemoteToken != oldCfg.RemoteToken ||
cfg.RemoteMachineName != oldCfg.RemoteMachineName
if remoteChanged {
if err := config.SaveMachineRemoteProfile(cfg); err != nil {
logger.LogEvent("配置", "warn", "保存本机共享远端配置失败: %v", err)
}
a.configureRemote(cfg)
}
if clientChanged {
+107
View File
@@ -0,0 +1,107 @@
package config
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"douyu_go/internal/types"
)
const machineRemoteProfileSchemaVersion = 1
const machineRemoteProfileFile = "remote-profile.json"
type MachineRemoteProfile struct {
SchemaVersion int `json:"schemaVersion"`
UpdatedAt string `json:"updatedAt"`
Enabled bool `json:"enabled"`
URL string `json:"url"`
EnrollmentToken string `json:"enrollmentToken"`
MachineName string `json:"machineName"`
}
func machineRemoteProfilePath() string {
return filepath.Join(defaultDataDir(), "remote", machineRemoteProfileFile)
}
// LoadMachineRemoteProfile loads the remote connection shared by every local instance.
func LoadMachineRemoteProfile() (MachineRemoteProfile, bool, error) {
data, err := os.ReadFile(machineRemoteProfilePath())
if errors.Is(err, os.ErrNotExist) {
return MachineRemoteProfile{}, false, nil
}
if err != nil {
return MachineRemoteProfile{}, false, err
}
var profile MachineRemoteProfile
if err := json.Unmarshal(data, &profile); err != nil {
return MachineRemoteProfile{}, false, fmt.Errorf("解析本机远端配置失败: %w", err)
}
if profile.SchemaVersion != machineRemoteProfileSchemaVersion {
return MachineRemoteProfile{}, false, fmt.Errorf(
"不支持的本机远端配置版本 %d,当前仅支持 %d",
profile.SchemaVersion, machineRemoteProfileSchemaVersion,
)
}
profile.URL = strings.TrimSpace(profile.URL)
profile.EnrollmentToken = strings.TrimSpace(profile.EnrollmentToken)
profile.MachineName = strings.TrimSpace(profile.MachineName)
return profile, true, nil
}
// SaveMachineRemoteProfile persists connection metadata inherited by future local instances.
func SaveMachineRemoteProfile(cfg types.Config) error {
profile := MachineRemoteProfile{
SchemaVersion: machineRemoteProfileSchemaVersion,
UpdatedAt: time.Now().Format(time.RFC3339),
Enabled: cfg.RemoteEnabled,
URL: strings.TrimSpace(cfg.RemoteURL),
EnrollmentToken: strings.TrimSpace(cfg.RemoteEnrollmentToken),
MachineName: strings.TrimSpace(cfg.RemoteMachineName),
}
data, err := json.MarshalIndent(profile, "", " ")
if err != nil {
return err
}
path := machineRemoteProfilePath()
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return err
}
temp, err := os.CreateTemp(filepath.Dir(path), ".remote-profile-*.tmp")
if err != nil {
return err
}
tempPath := temp.Name()
defer func() {
_ = os.Remove(tempPath)
}()
if err := temp.Chmod(0600); err != nil {
_ = temp.Close()
return err
}
if _, err := temp.Write(data); err != nil {
_ = temp.Close()
return err
}
if err := temp.Close(); err != nil {
return err
}
if err := os.Rename(tempPath, path); err != nil {
return err
}
return nil
}
func ApplyMachineRemoteProfile(cfg *types.Config, profile MachineRemoteProfile) {
if cfg == nil {
return
}
cfg.RemoteEnabled = profile.Enabled
cfg.RemoteURL = profile.URL
cfg.RemoteEnrollmentToken = profile.EnrollmentToken
cfg.RemoteMachineName = profile.MachineName
}
@@ -0,0 +1,66 @@
package config
import (
"os"
"path/filepath"
"testing"
"douyu_go/internal/types"
)
func TestMachineRemoteProfileIsInheritedByOtherInstances(t *testing.T) {
root := t.TempDir()
t.Setenv("DOUYU_DATA_DIR", root)
source := types.Config{
RemoteEnabled: true,
RemoteURL: " https://control.khhao.com ",
RemoteEnrollmentToken: " enroll-secret ",
RemoteMachineName: " PC-01 ",
}
if err := SaveMachineRemoteProfile(source); err != nil {
t.Fatal(err)
}
info, err := os.Stat(filepath.Join(root, "remote", machineRemoteProfileFile))
if err != nil {
t.Fatal(err)
}
if info.Mode().Perm() != 0600 {
t.Fatalf("profile mode=%o, want 600", info.Mode().Perm())
}
profile, ok, err := LoadMachineRemoteProfile()
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("machine remote profile should exist")
}
var inherited types.Config
ApplyMachineRemoteProfile(&inherited, profile)
if !inherited.RemoteEnabled ||
inherited.RemoteURL != "https://control.khhao.com" ||
inherited.RemoteEnrollmentToken != "enroll-secret" ||
inherited.RemoteMachineName != "PC-01" {
t.Fatalf("unexpected inherited remote config: %+v", inherited)
}
}
func TestMachineRemoteProfileCanDisableRemoteForNewInstances(t *testing.T) {
t.Setenv("DOUYU_DATA_DIR", t.TempDir())
if err := SaveMachineRemoteProfile(types.Config{
RemoteEnabled: false,
RemoteURL: "https://control.khhao.com",
}); err != nil {
t.Fatal(err)
}
profile, ok, err := LoadMachineRemoteProfile()
if err != nil || !ok {
t.Fatalf("LoadMachineRemoteProfile() ok=%v err=%v", ok, err)
}
cfg := types.Config{RemoteEnabled: true}
ApplyMachineRemoteProfile(&cfg, profile)
if cfg.RemoteEnabled {
t.Fatal("disabled machine profile should disable remote for new instances")
}
}