Files
mfd-monitor-agent/internal/service/service_windows.go
codex de3fd88948 ✨ feat(agent): 添加 token 鉴权和流程文档
* 默认监听调整为 0.0.0.0:8080,并要求查询参数 token
* 支持首次 run 自动生成含随机 token 的配置文件
* 补充 HTTP 鉴权、配置校验和相关测试
* 完善代码注释,覆盖缓存、上报、服务和平台采集路径
* 新增架构与开发流程文档,补充一键构建脚本
2026-05-22 06:46:21 +00:00

47 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//go:build windows
package service
import (
"os/exec"
"strings"
"mfd-monitor-agent/internal/config"
)
// windowsManager 通过 Windows 服务控制管理器(SCM)管理 agent。
type windowsManager struct{}
// newManager 返回 Windows 服务管理器。
func newManager() Manager {
return windowsManager{}
}
// Install 将 agent 可执行文件注册为自动启动的 Windows 服务。
func (windowsManager) Install(executable string, cfg config.Config, opts InstallOptions) error {
args := []string{`"` + executable + `"`, "run"}
if opts.ConfigPath != "" {
// -config 被写入 binPath,确保 SCM 启动的 run 子命令读取指定配置。
args = append(args, "-config", `"`+opts.ConfigPath+`"`)
}
// sc.exe 的 binPath= 参数要求等号后单独一个参数,因此这里保持分段传参。
return exec.Command("sc.exe", "create", cfg.Service.Name, "binPath=", strings.Join(args, " "), "DisplayName=", cfg.Service.DisplayName, "start=", "auto").Run()
}
// Uninstall 停止并删除已配置的 Windows 服务。
func (windowsManager) Uninstall(cfg config.Config) error {
// 服务可能未运行,忽略 stop 错误并继续删除服务注册。
_ = exec.Command("sc.exe", "stop", cfg.Service.Name).Run()
return exec.Command("sc.exe", "delete", cfg.Service.Name).Run()
}
// Start 请求 SCM 启动服务。
func (windowsManager) Start(cfg config.Config) error {
return exec.Command("sc.exe", "start", cfg.Service.Name).Run()
}
// Stop 请求 SCM 停止服务。
func (windowsManager) Stop(cfg config.Config) error {
return exec.Command("sc.exe", "stop", cfg.Service.Name).Run()
}