108 lines
3.0 KiB
Go
108 lines
3.0 KiB
Go
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
|
|
}
|