feat: trust peer
This commit is contained in:
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"mesh-drop/internal/security"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
@@ -19,18 +20,21 @@ type WindowState struct {
|
||||
Maximised bool `mapstructure:"maximised"`
|
||||
}
|
||||
|
||||
var Version = "0.0.2"
|
||||
var Version = "next"
|
||||
|
||||
type Config struct {
|
||||
v *viper.Viper
|
||||
mu sync.RWMutex
|
||||
|
||||
WindowState WindowState `mapstructure:"window_state"`
|
||||
ID string `mapstructure:"id"`
|
||||
SavePath string `mapstructure:"save_path"`
|
||||
HostName string `mapstructure:"host_name"`
|
||||
AutoAccept bool `mapstructure:"auto_accept"`
|
||||
SaveHistory bool `mapstructure:"save_history"`
|
||||
WindowState WindowState `mapstructure:"window_state"`
|
||||
ID string `mapstructure:"id"`
|
||||
PrivateKey string `mapstructure:"private_key"`
|
||||
PublicKey string `mapstructure:"public_key"`
|
||||
SavePath string `mapstructure:"save_path"`
|
||||
HostName string `mapstructure:"host_name"`
|
||||
AutoAccept bool `mapstructure:"auto_accept"`
|
||||
SaveHistory bool `mapstructure:"save_history"`
|
||||
TrustedPeer map[string]string `mapstructure:"trusted_peer"` // ID -> PublicKey
|
||||
}
|
||||
|
||||
// 默认窗口配置
|
||||
@@ -104,6 +108,28 @@ func Load() *Config {
|
||||
|
||||
config.v = v
|
||||
|
||||
// 如果没有密钥对,生成新的
|
||||
if config.PrivateKey == "" || config.PublicKey == "" {
|
||||
priv, pub, err := security.GenerateKey()
|
||||
if err != nil {
|
||||
slog.Error("Failed to generate identity keys", "error", err)
|
||||
} else {
|
||||
config.PrivateKey = priv
|
||||
config.PublicKey = pub
|
||||
v.Set("private_key", priv)
|
||||
v.Set("public_key", pub)
|
||||
// 保存新生成的密钥
|
||||
if err := config.Save(); err != nil {
|
||||
slog.Error("Failed to save generated keys", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化 TrustedPeer map if nil
|
||||
if config.TrustedPeer == nil {
|
||||
config.TrustedPeer = make(map[string]string)
|
||||
}
|
||||
|
||||
return &config
|
||||
}
|
||||
|
||||
@@ -111,7 +137,10 @@ func Load() *Config {
|
||||
func (c *Config) Save() error {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.save()
|
||||
}
|
||||
|
||||
func (c *Config) save() error {
|
||||
configDir := GetConfigDir()
|
||||
if err := os.MkdirAll(configDir, 0755); err != nil {
|
||||
return err
|
||||
@@ -122,6 +151,14 @@ func (c *Config) Save() error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 设置配置文件权限为 0600 (仅所有者读写)
|
||||
configFile := c.v.ConfigFileUsed()
|
||||
if configFile != "" {
|
||||
if err := os.Chmod(configFile, 0600); err != nil {
|
||||
slog.Warn("Failed to set config file permissions", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -133,6 +170,7 @@ func (c *Config) SetSavePath(savePath string) {
|
||||
c.SavePath = savePath
|
||||
c.v.Set("save_path", savePath)
|
||||
_ = os.MkdirAll(savePath, 0755)
|
||||
_ = c.save()
|
||||
}
|
||||
|
||||
func (c *Config) GetSavePath() string {
|
||||
@@ -146,6 +184,7 @@ func (c *Config) SetHostName(hostName string) {
|
||||
defer c.mu.Unlock()
|
||||
c.HostName = hostName
|
||||
c.v.Set("host_name", hostName)
|
||||
_ = c.save()
|
||||
}
|
||||
|
||||
func (c *Config) GetHostName() string {
|
||||
@@ -165,6 +204,7 @@ func (c *Config) SetAutoAccept(autoAccept bool) {
|
||||
defer c.mu.Unlock()
|
||||
c.AutoAccept = autoAccept
|
||||
c.v.Set("auto_accept", autoAccept)
|
||||
_ = c.save()
|
||||
}
|
||||
|
||||
func (c *Config) GetAutoAccept() bool {
|
||||
@@ -178,6 +218,7 @@ func (c *Config) SetSaveHistory(saveHistory bool) {
|
||||
defer c.mu.Unlock()
|
||||
c.SaveHistory = saveHistory
|
||||
c.v.Set("save_history", saveHistory)
|
||||
_ = c.save()
|
||||
}
|
||||
|
||||
func (c *Config) GetSaveHistory() bool {
|
||||
@@ -195,6 +236,7 @@ func (c *Config) SetWindowState(state WindowState) {
|
||||
defer c.mu.Unlock()
|
||||
c.WindowState = state
|
||||
c.v.Set("window_state", state)
|
||||
_ = c.save()
|
||||
}
|
||||
|
||||
func (c *Config) GetWindowState() WindowState {
|
||||
@@ -202,3 +244,35 @@ func (c *Config) GetWindowState() WindowState {
|
||||
defer c.mu.RUnlock()
|
||||
return c.WindowState
|
||||
}
|
||||
|
||||
func (c *Config) AddTrustedPeer(peerID string, publicKey string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.TrustedPeer == nil {
|
||||
c.TrustedPeer = make(map[string]string)
|
||||
}
|
||||
c.TrustedPeer[peerID] = publicKey
|
||||
c.v.Set("trusted_peer", c.TrustedPeer)
|
||||
_ = c.save()
|
||||
}
|
||||
|
||||
func (c *Config) GetTrustedPeer() map[string]string {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
return c.TrustedPeer
|
||||
}
|
||||
|
||||
func (c *Config) RemoveTrustedPeer(peerID string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
delete(c.TrustedPeer, peerID)
|
||||
c.v.Set("trusted_peer", c.TrustedPeer)
|
||||
_ = c.save()
|
||||
}
|
||||
|
||||
func (c *Config) IsTrustedPeer(peerID string) bool {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
_, exists := c.TrustedPeer[peerID]
|
||||
return exists
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user