feat(service): add auto-start management for services

Implement setBootAtStart and removeBootAtStart functions to handle service auto-start configuration separately from service creation/removal. Update handlers to use these new functions for better control of boot behavior. The changes support Windows, systemd and init.d systems.
This commit is contained in:
2026-03-25 11:05:17 +08:00
parent 3a00f5d6a5
commit da729b44ff
2 changed files with 83 additions and 17 deletions
+62
View File
@@ -249,6 +249,68 @@ func createWindowsBootService(username, instanceName, configPath string) error {
return nil
}
func setBootAtStart(username, instanceName string) error {
initType := GetInitSystem()
serviceName := fmt.Sprintf("superfrpc_%s_%s", username, instanceName)
switch initType {
case "windows":
cmd := exec.Command("sc", "config", serviceName, "start=", "auto")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to set Windows service %s to auto-start: %s, output: %s", serviceName, err, output)
}
return nil
case "systemd":
cmd := exec.Command("systemctl", "enable", serviceName)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to enable systemd service %s: %s, output: %s", serviceName, err, output)
}
return nil
case "init.d":
cmd := exec.Command("update-rc.d", serviceName, "enable")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to enable init.d service %s: %s, output: %s", serviceName, err, output)
}
return nil
default:
return fmt.Errorf("unsupported init system: %s", initType)
}
}
func removeBootAtStart(username, instanceName string) error {
initType := GetInitSystem()
serviceName := fmt.Sprintf("superfrpc_%s_%s", username, instanceName)
switch initType{
case "windows":
cmd := exec.Command("sc", "config", serviceName, "start=", "disabled")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to set Windows service %s to disabled-start: %s, output: %s", serviceName, err, output)
}
return nil
case "systemd":
cmd := exec.Command("systemctl", "disable", serviceName)
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to disable systemd service %s: %s, output: %s", serviceName, err, output)
}
return nil
case "init.d":
cmd := exec.Command("update-rc.d", serviceName, "disable")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to disable init.d service %s: %s, output: %s", serviceName, err, output)
}
return nil
default:
return fmt.Errorf("unsupported init system: %s", initType)
}
}
func removeBootService(username, instanceName string) error {
initType := GetInitSystem()