package service import ( "fmt" "os" "os/user" "path/filepath" ) // ServiceStatus 服务状态 type ServiceStatus int const ( StatusUnknown ServiceStatus = iota StatusRunning StatusStopped StatusNotInstalled ) // ServiceManager 服务管理器接口 type ServiceManager interface { Install(execPath string) error Uninstall() error Start() error Stop() error Restart() error Status() (ServiceStatus, error) } // GenericServiceManager 通用服务管理器(不支持系统服务) type GenericServiceManager struct{} func (m *GenericServiceManager) Install(execPath string) error { return fmt.Errorf("当前操作系统不支持系统服务") } func (m *GenericServiceManager) Uninstall() error { return fmt.Errorf("当前操作系统不支持系统服务") } func (m *GenericServiceManager) Start() error { return fmt.Errorf("当前操作系统不支持系统服务") } func (m *GenericServiceManager) Stop() error { return fmt.Errorf("当前操作系统不支持系统服务") } func (m *GenericServiceManager) Restart() error { return fmt.Errorf("当前操作系统不支持系统服务") } func (m *GenericServiceManager) Status() (ServiceStatus, error) { return StatusNotInstalled, fmt.Errorf("当前操作系统不支持系统服务") } // getServiceName 获取服务名称 func getServiceName() string { return "HostSync" } // getServiceDisplayName 获取服务显示名称 func getServiceDisplayName() string { return "HostSync - Hosts File Manager" } // getServiceDescription 获取服务描述 func getServiceDescription() string { return "HostSync hosts file management service with automatic updates and cron jobs" } // getLogPath 获取日志文件路径 func getLogPath() string { execPath, _ := os.Executable() execDir := filepath.Dir(execPath) return filepath.Join(execDir, "logs", "service.log") } // ensureLogDir 确保日志目录存在 func ensureLogDir() error { logPath := getLogPath() logDir := filepath.Dir(logPath) return os.MkdirAll(logDir, 0755) } // getUserConfigDir 获取用户配置目录 func getUserConfigDir() (string, error) { currentUser, err := user.Current() if err != nil { return "", err } return filepath.Join(currentUser.HomeDir, ".hostsync"), nil }