内容纲要

https://nssm.cc/ 可以将普通 .exe 包装为 windows 服务程序,且支持守护进程特性,崩溃会自动重启。

pritunl-client-electron/resources_win/post_install/main.go

package main

import (
    "os"
    "os/exec"
    "path/filepath"
    "sync"
)

func main() {
    wait := &sync.WaitGroup{}

    rootDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
    if err != nil {
        panic(err)
    }

    cmd := exec.Command("taskkill.exe", "/F", "/IM", "pritunl.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command("taskkill.exe", "/F", "/IM", "pritunl-service.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command("taskkill.exe", "/F", "/IM", "openvpn.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "stop", "pritunl")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command("taskkill.exe", "/F", "/IM", "pritunl.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command("taskkill.exe", "/F", "/IM", "pritunl-service.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command("taskkill.exe", "/F", "/IM", "openvpn.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()

    wait.Add(1)
    go func() {
        defer wait.Done()

        cmd := exec.Command(filepath.Join(rootDir, "tuntap", "tuntap.exe"),
            "uninstall")
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
        cmd.Run()
        cmd = exec.Command(filepath.Join(rootDir, "tuntap", "tuntap.exe"),
            "install")
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
        cmd.Run()
        cmd = exec.Command(filepath.Join(rootDir, "tuntap", "tuntap.exe"),
            "install")
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
        cmd.Run()
        cmd = exec.Command(filepath.Join(rootDir, "tuntap", "tuntap.exe"),
            "install")
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
        cmd.Run()
        cmd = exec.Command(filepath.Join(rootDir, "tuntap", "tuntap.exe"),
            "install")
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
        cmd.Run()
        cmd = exec.Command(filepath.Join(rootDir, "tuntap", "tuntap.exe"),
            "install")
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
        cmd.Run()
        cmd = exec.Command(filepath.Join(rootDir, "tuntap", "tuntap.exe"),
            "install")
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
        cmd.Run()
        cmd = exec.Command(filepath.Join(rootDir, "tuntap", "tuntap.exe"),
            "install")
        cmd.Stdout = os.Stdout
        cmd.Stderr = os.Stderr
        cmd.Run()
    }()

    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "stop", "pritunl")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "remove", "pritunl", "confirm")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "stop", "pritunl")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command("taskkill.exe", "/F", "/IM", "pritunl.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command("taskkill.exe", "/F", "/IM", "pritunl-service.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"), "install",
        "pritunl", filepath.Join(rootDir, "pritunl-service.exe"))
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "set", "pritunl", "DisplayName", "Pritunl Helper Service")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "set", "pritunl", "Start", "SERVICE_AUTO_START")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "set", "pritunl", "AppStdout",
        "C:\\ProgramData\\Pritunl\\service.log")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "set", "pritunl", "AppStderr",
        "C:\\ProgramData\\Pritunl\\service.log")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "set", "pritunl", "Start", "SERVICE_AUTO_START")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "start", "pritunl")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()

    wait.Wait()
}

pritunl-client-electron/resources_win/pre_uninstall/main.go

package main

import (
    "os"
    "os/exec"
    "path/filepath"
)

func main() {
    rootDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
    if err != nil {
        panic(err)
    }

    cmd := exec.Command("taskkill.exe", "/F", "/IM", "pritunl.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command("taskkill.exe", "/F", "/IM", "pritunl-service.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "stop", "pritunl")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "remove", "pritunl", "confirm")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "nssm.exe"),
        "stop", "pritunl")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command("taskkill.exe", "/F", "/IM", "openvpn.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "tuntap", "tuntap.exe"),
        "uninstall")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command("taskkill.exe", "/F", "/IM", "pritunl.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command("taskkill.exe", "/F", "/IM", "pritunl-service.exe")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
    cmd = exec.Command(filepath.Join(rootDir, "tuntap", "tuntap.exe"),
        "uninstall")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Run()
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注