feat(remote): 增加远端控制台与日志汇总导出
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"douyu_go/internal/constants"
|
||||
imgpkg "douyu_go/internal/image"
|
||||
"douyu_go/internal/logger"
|
||||
"douyu_go/internal/remote"
|
||||
"douyu_go/internal/seckill"
|
||||
"douyu_go/internal/trace"
|
||||
"douyu_go/internal/types"
|
||||
@@ -53,6 +54,9 @@ type App struct {
|
||||
|
||||
closeLog func()
|
||||
closeTrace func()
|
||||
|
||||
remoteMu sync.RWMutex
|
||||
remoteAgent *remote.Agent
|
||||
}
|
||||
|
||||
// getClient 返回当前 client 指针快照(读锁保护)。
|
||||
@@ -228,8 +232,10 @@ func (a *App) Startup(ctx context.Context) {
|
||||
scheduler.UpdateProxySettings(cfg)
|
||||
scheduler.SetStateChangeCallback(func(state string) {
|
||||
runtime.EventsEmit(a.ctx, seckillStateEvent, state)
|
||||
a.remoteSetState(state)
|
||||
})
|
||||
scheduler.LoadAccounts()
|
||||
a.configureRemote(cfg)
|
||||
|
||||
startupRefreshTried := false
|
||||
tryRefreshStartupProxy := func(reason string) bool {
|
||||
@@ -304,6 +310,7 @@ func (a *App) Startup(ctx context.Context) {
|
||||
|
||||
// Shutdown Wails 应用关闭时调用。
|
||||
func (a *App) Shutdown(ctx context.Context) {
|
||||
a.closeRemote()
|
||||
if scheduler := a.getScheduler(); scheduler != nil {
|
||||
scheduler.Close()
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"douyu_go/internal/config"
|
||||
"douyu_go/internal/constants"
|
||||
"douyu_go/internal/logger"
|
||||
"douyu_go/internal/remote"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
@@ -117,6 +118,9 @@ func (a *App) SaveSettings(cfg types.Config) types.ErrorResult {
|
||||
if cfg.LogLevel == "" {
|
||||
cfg.LogLevel = "info"
|
||||
}
|
||||
cfg.RemoteURL = strings.TrimSpace(cfg.RemoteURL)
|
||||
cfg.RemoteToken = strings.TrimSpace(cfg.RemoteToken)
|
||||
cfg.RemoteMachineName = strings.TrimSpace(cfg.RemoteMachineName)
|
||||
if proxyURL := a.effectiveProxyURL(cfg); proxyURL != "" {
|
||||
if _, err := client.ParseProxyURL(proxyURL); err != nil {
|
||||
logger.LogEvent("代理", "warn", "设置保存失败,代理地址无效: %v", err)
|
||||
@@ -137,6 +141,14 @@ func (a *App) SaveSettings(cfg types.Config) types.ErrorResult {
|
||||
return types.ErrorResult{Error: "AES 签名密钥长度应为 16、24 或 32 字节"}
|
||||
}
|
||||
}
|
||||
if cfg.RemoteEnabled {
|
||||
if strings.TrimSpace(cfg.RemoteURL) == "" {
|
||||
return types.ErrorResult{Error: "远端控制需要填写控制中心地址"}
|
||||
}
|
||||
if err := remote.ValidateServerURL(cfg.RemoteURL); err != nil {
|
||||
return types.ErrorResult{Error: "远端控制地址无效: " + err.Error()}
|
||||
}
|
||||
}
|
||||
|
||||
// 写入文件
|
||||
if err := config.SaveConfig(a.paths.ConfigFile, cfg); err != nil {
|
||||
@@ -161,6 +173,13 @@ func (a *App) SaveSettings(cfg types.Config) types.ErrorResult {
|
||||
logger.SetMinLevel(cfg.LogLevel)
|
||||
a.scheduler.UpdateSettings(cfg.Concurrency, cfg.Attempts, cfg.RetryStages, cfg.SeckillMode)
|
||||
a.scheduler.UpdateProxySettings(cfg)
|
||||
remoteChanged := cfg.RemoteEnabled != oldCfg.RemoteEnabled ||
|
||||
cfg.RemoteURL != oldCfg.RemoteURL ||
|
||||
cfg.RemoteToken != oldCfg.RemoteToken ||
|
||||
cfg.RemoteMachineName != oldCfg.RemoteMachineName
|
||||
if remoteChanged {
|
||||
a.configureRemote(cfg)
|
||||
}
|
||||
if clientChanged {
|
||||
proxyURL := a.effectiveProxyURL(cfg)
|
||||
newClient, clientErr := client.NewWithProxy(act, cfg.TimeAPIURL, a.getTrace(), proxyURL)
|
||||
@@ -186,7 +205,7 @@ func (a *App) SaveSettings(cfg types.Config) types.ErrorResult {
|
||||
}
|
||||
}
|
||||
|
||||
logger.LogEvent("配置", "success", "配置已保存并生效: 日志等级=%s 并发=%d 重试=%d 重试阶段=%d 模式=%d trace=%v 代理=%v",
|
||||
cfg.LogLevel, cfg.Concurrency, cfg.Attempts, len(cfg.RetryStages), cfg.SeckillMode, cfg.TraceEnabled, cfg.ProxyEnabled)
|
||||
logger.LogEvent("配置", "success", "配置已保存并生效: 日志等级=%s 并发=%d 重试=%d 重试阶段=%d 模式=%d trace=%v 代理=%v 远端=%v",
|
||||
cfg.LogLevel, cfg.Concurrency, cfg.Attempts, len(cfg.RetryStages), cfg.SeckillMode, cfg.TraceEnabled, cfg.ProxyEnabled, cfg.RemoteEnabled)
|
||||
return types.ErrorResult{Success: true}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"douyu_go/internal/logger"
|
||||
"douyu_go/internal/remote"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
func (a *App) configureRemote(cfg types.Config) {
|
||||
a.closeRemote()
|
||||
if !cfg.RemoteEnabled {
|
||||
return
|
||||
}
|
||||
serverURL := strings.TrimSpace(cfg.RemoteURL)
|
||||
token := strings.TrimSpace(cfg.RemoteToken)
|
||||
if serverURL == "" {
|
||||
logger.LogEvent("远端", "warn", "远端控制已启用,但控制中心地址为空")
|
||||
return
|
||||
}
|
||||
machineID, err := remote.LoadOrCreateMachineID(a.paths.DataDir)
|
||||
if err != nil {
|
||||
logger.LogEvent("远端", "warn", "远端机器标识初始化失败: %v", err)
|
||||
return
|
||||
}
|
||||
machineName := strings.TrimSpace(cfg.RemoteMachineName)
|
||||
if machineName == "" {
|
||||
machineName, _ = os.Hostname()
|
||||
}
|
||||
agent, err := remote.NewAgent(remote.AgentOptions{
|
||||
DataDir: a.paths.DataDir,
|
||||
ServerURL: serverURL,
|
||||
Token: token,
|
||||
MachineID: machineID,
|
||||
MachineName: machineName,
|
||||
InstanceID: a.instanceName,
|
||||
Instance: a.instanceName,
|
||||
Version: a.GetVersion(),
|
||||
Snapshot: func() remote.Snapshot {
|
||||
scheduler := a.getScheduler()
|
||||
if scheduler == nil {
|
||||
return remote.Snapshot{}
|
||||
}
|
||||
return remote.Snapshot{
|
||||
ActivityID: a.getCfg().Activity,
|
||||
Running: scheduler.IsRunning(),
|
||||
Results: scheduler.Results(),
|
||||
AccountCount: scheduler.AccountCount(),
|
||||
}
|
||||
},
|
||||
Stop: func() error {
|
||||
result := a.StopSeckill()
|
||||
if result.Error != "" {
|
||||
return fmt.Errorf("%s", result.Error)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Cancel: func() error {
|
||||
a.CancelSchedule()
|
||||
return nil
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.LogEvent("远端", "warn", "远端控制初始化失败: %v", err)
|
||||
return
|
||||
}
|
||||
agent.SetState("idle")
|
||||
logger.SetObserver(agent.Observe)
|
||||
agent.Start(a.ctx)
|
||||
a.remoteMu.Lock()
|
||||
a.remoteAgent = agent
|
||||
a.remoteMu.Unlock()
|
||||
logger.LogEvent("远端", "info", "远端控制已启用: %s,机器=%s,实例=%s", serverURL, machineName, a.instanceName)
|
||||
}
|
||||
|
||||
func (a *App) closeRemote() {
|
||||
logger.SetObserver(nil)
|
||||
a.remoteMu.Lock()
|
||||
agent := a.remoteAgent
|
||||
a.remoteAgent = nil
|
||||
a.remoteMu.Unlock()
|
||||
if agent != nil {
|
||||
_ = agent.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) remoteSetState(state string) {
|
||||
a.remoteMu.RLock()
|
||||
agent := a.remoteAgent
|
||||
a.remoteMu.RUnlock()
|
||||
if agent != nil {
|
||||
agent.SetState(state)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) GetRemoteStatus() types.RemoteStatusResult {
|
||||
a.remoteMu.RLock()
|
||||
agent := a.remoteAgent
|
||||
a.remoteMu.RUnlock()
|
||||
if agent == nil {
|
||||
return types.RemoteStatusResult{Enabled: false, Message: "远端控制未启用"}
|
||||
}
|
||||
connected, lastError, lastConnect := agent.Status()
|
||||
message := "未连接"
|
||||
if connected {
|
||||
message = "已连接"
|
||||
} else if lastError != "" {
|
||||
message = lastError
|
||||
}
|
||||
result := types.RemoteStatusResult{
|
||||
Enabled: true,
|
||||
Connected: connected,
|
||||
Message: message,
|
||||
}
|
||||
if !lastConnect.IsZero() {
|
||||
result.LastConnect = lastConnect.Format(time.RFC3339)
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user