补充Windows服务安装和权限部署链路
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-01 11:20:46 +08:00
1 parent a8f88af887
commit fcf81cbcb9
6 files changed
+371 -3

No files matched your search

@@ -0,0 +1,61 @@
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestWindowsDeploymentScriptsKeepAdminChannelManual(t *testing.T) {
root := filepath.Clean(filepath.Join("..", "..", ".."))
installer, err := os.ReadFile(filepath.Join(root, "deploy", "install-windows.ps1"))
if err != nil {
t.Fatal(err)
}
content := string(installer)
for _, required := range []string{
"sc.exe",
"smail-admin.exe",
"Set-RestrictedAcl",
"NT SERVICE\\Smail:(F)",
".backup-",
"$mainBackup",
"$adminBackup",
"不会自动注册或启动服务",
} {
if !strings.Contains(content, required) {
t.Fatalf("Windows installer is missing %q", required)
}
}
for _, forbidden := range []string{
"create SmailAdmin",
"Start-Service -Name \"SmailAdmin\"",
"start= auto" + "\n" + "start= auto",
} {
if strings.Contains(content, forbidden) {
t.Fatalf("Windows installer contains forbidden admin-service action %q", forbidden)
}
}
}
func TestWindowsUninstallerPreservesData(t *testing.T) {
root := filepath.Clean(filepath.Join("..", "..", ".."))
uninstaller, err := os.ReadFile(filepath.Join(root, "deploy", "uninstall-windows.ps1"))
if err != nil {
t.Fatal(err)
}
content := string(uninstaller)
if strings.Contains(content, "Remove-Item -LiteralPath $resolvedInstallRoot -Recurse") {
t.Fatal("Windows uninstaller recursively removes the data root")
}
for _, required := range []string{
"smail.exe",
"smail-admin.exe",
"配置、数据库、证书和用户数据未删除",
} {
if !strings.Contains(content, required) {
t.Fatalf("Windows uninstaller is missing %q", required)
}
}
}
+1 -3
View File
@@ -3,12 +3,10 @@ package main
import (
"fmt"
"os"
"git.pcux.cn/xuans/Smail/internal/app"
)
func main() {
if err := app.RecoverPanic(app.Run); err != nil {
if err := runMain(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
+9
View File
@@ -0,0 +1,9 @@
//go:build !windows
package main
import "git.pcux.cn/xuans/Smail/internal/app"
func runMain() error {
return app.RecoverPanic(app.Run)
}
+71
View File
@@ -0,0 +1,71 @@
//go:build windows
package main
import (
"time"
"git.pcux.cn/xuans/Smail/internal/app"
"git.pcux.cn/xuans/Smail/signal"
"golang.org/x/sys/windows/svc"
)
func runMain() error {
if !svc.IsWindowsService() {
return app.RecoverPanic(app.Run)
}
return svc.Run("Smail", &windowsService{})
}
type windowsService struct{}
func (windowsService) Execute(_ []string, requests <-chan svc.ChangeRequest, status chan<- svc.Status) (bool, uint32) {
const accepts = svc.AcceptStop | svc.AcceptShutdown
status <- svc.Status{State: svc.StartPending}
done := make(chan error, 1)
go func() {
done <- app.RecoverPanic(app.Run)
}()
status <- svc.Status{State: svc.Running, Accepts: accepts}
for {
select {
case err := <-done:
if err != nil {
return false, 1
}
status <- svc.Status{State: svc.StopPending}
return false, 0
case request, ok := <-requests:
if !ok {
return false, 0
}
switch request.Cmd {
case svc.Interrogate:
status <- svc.Status{State: svc.Running, Accepts: accepts}
case svc.Stop, svc.Shutdown:
return windowsServiceStop(done, status)
}
}
}
}
func windowsServiceStop(done <-chan error, status chan<- svc.Status) (bool, uint32) {
status <- svc.Status{State: svc.StopPending, WaitHint: 30_000}
select {
case signal.StopChan <- true:
case <-time.After(2 * time.Second):
return false, 1
}
select {
case err := <-done:
if err != nil {
return false, 1
}
return false, 0
case <-time.After(30 * time.Second):
return false, 1
}
}