优化探针卸载&服务端删除探针自动卸载自身
This commit is contained in:
1 parent
7944fb09dc
commit
502de9d816
6 files changed
+98
-20
No files matched your search
+3
-3
@@ -18,12 +18,12 @@ type Manager struct {
|
||||
// NewManager 创建 ID 管理器
|
||||
func NewManager() *Manager {
|
||||
return &Manager{
|
||||
idFilePath: getIDFilePath(),
|
||||
idFilePath: GetIDFilePath(),
|
||||
}
|
||||
}
|
||||
|
||||
// getIDFilePath 获取 ID 文件路径
|
||||
func getIDFilePath() string {
|
||||
// GetIDFilePath 获取 ID 文件路径
|
||||
func GetIDFilePath() string {
|
||||
// 获取用户主目录
|
||||
var homeDir = utils.GetSafeHomeDir()
|
||||
// 统一使用 ~/.pika/agent.id
|
||||
|
||||
@@ -296,6 +296,8 @@ func (a *Agent) readLoop(conn *websocket.Conn, done chan struct{}) error {
|
||||
go a.handleTamperProtect(msg.Data)
|
||||
case protocol.MessageTypeDDNSConfig:
|
||||
go a.handleDDNSConfig(msg.Data)
|
||||
case protocol.MessageTypeUninstall:
|
||||
go a.handleUninstall()
|
||||
default:
|
||||
// 忽略其他类型
|
||||
}
|
||||
@@ -829,3 +831,27 @@ func (a *Agent) getIPAddress(manager *collector.Manager, method, value string, i
|
||||
return "", fmt.Errorf("不支持的 IP 获取方式: %s", method)
|
||||
}
|
||||
}
|
||||
|
||||
// handleUninstall 处理服务端发送的卸载指令
|
||||
func (a *Agent) handleUninstall() {
|
||||
log.Println("📥 收到服务端卸载指令,开始执行卸载...")
|
||||
|
||||
// 获取配置文件路径
|
||||
cfgPath := a.cfg.Path
|
||||
if cfgPath == "" {
|
||||
cfgPath = config.GetDefaultConfigPath()
|
||||
}
|
||||
|
||||
// 执行卸载操作
|
||||
if err := UninstallAgent(cfgPath); err != nil {
|
||||
log.Printf("❌ 卸载失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("✅ 探针卸载成功,即将退出...")
|
||||
|
||||
// 卸载成功后,触发停止信号
|
||||
if a.cancel != nil {
|
||||
a.cancel()
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"syscall"
|
||||
|
||||
"github.com/dushixiang/pika/pkg/agent/config"
|
||||
"github.com/dushixiang/pika/pkg/agent/id"
|
||||
"github.com/dushixiang/pika/pkg/agent/sysutil"
|
||||
"github.com/dushixiang/pika/pkg/agent/updater"
|
||||
"github.com/kardianos/service"
|
||||
@@ -235,3 +236,46 @@ func (m *ServiceManager) Run() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UninstallAgent 执行探针卸载操作(可被复用)
|
||||
func UninstallAgent(cfgPath string) error {
|
||||
// 加载配置
|
||||
cfg, err := config.Load(cfgPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("加载配置失败: %w", err)
|
||||
}
|
||||
|
||||
// 创建服务管理器
|
||||
mgr, err := NewServiceManager(cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建服务管理器失败: %w", err)
|
||||
}
|
||||
|
||||
// 检查服务状态,如果在运行则停止
|
||||
status, err := mgr.Status()
|
||||
if err != nil {
|
||||
log.Printf("⚠️ 获取服务状态失败: %v", err)
|
||||
} else if status != "已停止 (Stopped)" {
|
||||
if err := mgr.Stop(); err != nil {
|
||||
return fmt.Errorf("停止服务失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 卸载服务
|
||||
if err := mgr.Uninstall(); err != nil {
|
||||
return fmt.Errorf("卸载服务失败: %w", err)
|
||||
}
|
||||
|
||||
// 删除配置文件
|
||||
if err := os.Remove(cfgPath); err != nil {
|
||||
log.Printf("⚠️ 删除配置文件失败: %v", err)
|
||||
}
|
||||
|
||||
// 删除探针 ID 文件
|
||||
idPath := id.GetIDFilePath()
|
||||
if err := os.Remove(idPath); err != nil {
|
||||
log.Printf("⚠️ 删除探针 ID 文件失败: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user