From 7a48544932dffa3baccbe5aa7eb1fe32bb94802d Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Wed, 17 Jul 2024 18:54:29 -0400 Subject: [PATCH] Fix ANSI escapes (on Windows) not being interpreted after using GPG pinentry --- src/backend/2globalsUNIX.go | 6 ++++++ src/backend/2globalsWIN.go | 17 +++++++++++++++++ src/backend/gpg.go | 5 +++++ wiki/libmutton/bugs.md | 3 +++ 4 files changed, 31 insertions(+) create mode 100644 wiki/libmutton/bugs.md diff --git a/src/backend/2globalsUNIX.go b/src/backend/2globalsUNIX.go index caafb97..6e3bacc 100644 --- a/src/backend/2globalsUNIX.go +++ b/src/backend/2globalsUNIX.go @@ -12,3 +12,9 @@ const ( PathSeparator = "/" IsWindows = false ) + +// enableVirtualTerminalProcessing is a dummy function on UNIX-like systems (only needed on Windows) +// TODO remove after migration off of GPG, as pinentry is responsible for disabling ANSI escape sequence interpretation +func enableVirtualTerminalProcessing() { + return +} diff --git a/src/backend/2globalsWIN.go b/src/backend/2globalsWIN.go index 5b92a53..1da260f 100644 --- a/src/backend/2globalsWIN.go +++ b/src/backend/2globalsWIN.go @@ -2,6 +2,11 @@ package backend +import ( + "os" + "syscall" +) + // EntryRoot path to libmutton entry directory var EntryRoot = Home + "\\AppData\\Local\\libmutton\\entries" var ConfigDir = Home + "\\AppData\\Local\\libmutton\\config" @@ -12,3 +17,15 @@ const ( PathSeparator = "\\" IsWindows = true ) + +// enableVirtualTerminalProcessing ensures ANSI escape sequences are interpreted properly on Windows +// TODO remove after migration off of GPG, as pinentry is responsible for disabling ANSI escape sequence interpretation +func enableVirtualTerminalProcessing() { + stdout := syscall.Handle(os.Stdout.Fd()) + + var originalMode uint32 + syscall.GetConsoleMode(stdout, &originalMode) + originalMode |= 0x0004 + + syscall.MustLoadDLL("kernel32").MustFindProc("SetConsoleMode").Call(uintptr(stdout), uintptr(originalMode)) +} diff --git a/src/backend/gpg.go b/src/backend/gpg.go index 2d22aeb..11351ef 100644 --- a/src/backend/gpg.go +++ b/src/backend/gpg.go @@ -13,11 +13,16 @@ import ( func DecryptGPG(targetLocation string) []string { cmd := exec.Command("gpg", "--pinentry-mode", "loopback", "-q", "-d", targetLocation) output, err := cmd.Output() + + // ensure ANSI escape sequences are interpreted properly on Windows + enableVirtualTerminalProcessing() + if err != nil { fmt.Println(AnsiError + "Failed to decrypt \"" + targetLocation + "\" - ensure it is a valid GPG-encrypted file and that you entered your passphrase correctly" + AnsiReset) os.Exit(1) } outputSlice := strings.Split(string(output), "\n") + return outputSlice } diff --git a/wiki/libmutton/bugs.md b/wiki/libmutton/bugs.md new file mode 100644 index 0000000..709cac9 --- /dev/null +++ b/wiki/libmutton/bugs.md @@ -0,0 +1,3 @@ +# Known Bugs - libmutton +- On Windows, GPG is sometimes (seems unpredictable) incredibly slow to start (often after a reboot), leading to many operations seemingly hanging + - **This will be addressed** in the migration off of GPG that will take place before v1.0.0