78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
//go:build windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"archive/zip"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
)
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
fmt.Fprintln(os.Stderr, "LanUps installation failed:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
self, err := os.Executable()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
archive, err := zip.OpenReader(self)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer archive.Close()
|
|
stage, err := os.MkdirTemp("", "LanUps-Setup-")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.RemoveAll(stage)
|
|
for _, entry := range archive.File {
|
|
path := filepath.FromSlash(entry.Name)
|
|
if !fs.ValidPath(entry.Name) || !filepath.IsLocal(path) || !entry.Mode().IsRegular() {
|
|
return fmt.Errorf("invalid installer entry: %q", entry.Name)
|
|
}
|
|
if err := extract(filepath.Join(stage, path), entry); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if len(os.Args) == 2 && os.Args[1] == "--verify" {
|
|
fmt.Printf("Verified %d installer files\n", len(archive.File))
|
|
return nil
|
|
}
|
|
setup := filepath.Join(stage, "Setup.ps1")
|
|
args := append([]string{"-NoProfile", "-ExecutionPolicy", "Bypass", "-File", setup}, os.Args[1:]...)
|
|
powershell := filepath.Join(os.Getenv("SystemRoot"), "System32", "WindowsPowerShell", "v1.0", "powershell.exe")
|
|
command := exec.Command(powershell, args...)
|
|
command.Stdout, command.Stderr = os.Stdout, os.Stderr
|
|
return command.Run()
|
|
}
|
|
|
|
func extract(path string, entry *zip.File) error {
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
|
return err
|
|
}
|
|
input, err := entry.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer input.Close()
|
|
output, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = io.Copy(output, input)
|
|
closeErr := output.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return closeErr
|
|
}
|