ae-send/main.go

95 lines
2.1 KiB
Go

package main
import (
"log"
"os"
"os/exec"
"strings"
"golang.org/x/sys/windows/registry"
)
func main() {
if len(os.Args) > 1 {
switch os.Args[1] {
case "install":
installContextMenuItem()
case "uninstall":
ensureAdminPrivileges()
uninstallContextMenuItem()
default:
processFile(os.Args[1])
}
} else {
log.Println("No file provided")
}
}
func processFile(filePath string) {
log.Printf("Processing file: %s", filePath)
// Add your existing file processing logic here
}
func installContextMenuItem() {
exePath, _ := os.Executable()
keyPath := `Software\Classes\*\shell\AE Send`
command := "\"" + exePath + "\" \"%1\""
k, _, err := registry.CreateKey(registry.CURRENT_USER, keyPath, registry.ALL_ACCESS)
if err != nil {
log.Fatalf("Failed to create registry key: %v", err)
return
}
defer k.Close()
err = k.SetStringValue("", "Send with AE Send")
if err != nil {
log.Fatalf("Failed to set registry value: %v", err)
return
}
commandKeyPath := keyPath + `\command`
commandKey, _, err := registry.CreateKey(registry.CURRENT_USER, commandKeyPath, registry.ALL_ACCESS)
if err != nil {
log.Fatalf("Failed to create command key: %v", err)
return
}
defer commandKey.Close()
err = commandKey.SetStringValue("", command)
if err != nil {
log.Fatalf("Failed to set command: %v", err)
}
log.Println("Context menu item installed successfully.")
}
func uninstallContextMenuItem() {
keyPath := `Software\Classes\*\shell\AE Send`
err := registry.DeleteKey(registry.CURRENT_USER, keyPath)
if err != nil {
log.Fatalf("Failed to remove context menu item: %v", err)
} else {
log.Println("Context menu item removed successfully.")
}
}
func ensureAdminPrivileges() {
if !isAdmin() {
// Relaunch the application with elevated privileges
exePath, _ := os.Executable()
args := strings.Join(os.Args, " ")
cmd := exec.Command("runas", "/user:Administrator", exePath+" "+args)
err := cmd.Run()
if err != nil {
log.Fatalf("Failed to relaunch as admin: %v", err)
}
os.Exit(0)
}
}
func isAdmin() bool {
_, err := os.Open("\\\\.\\PHYSICALDRIVE0")
return err == nil
}