122 lines
2.8 KiB
Go
Executable File
122 lines
2.8 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"image"
|
|
"image/png"
|
|
"log"
|
|
"time"
|
|
|
|
"lanrdp/internal/protocol"
|
|
)
|
|
|
|
// readLoop consumes frame/resize messages from the host and applies them to
|
|
// the shared framebuffer, then asks the window to repaint.
|
|
func readLoop(a *appState) {
|
|
defer close(a.done)
|
|
for {
|
|
t, payload, err := protocol.ReadMessage(a.conn)
|
|
if err != nil {
|
|
log.Printf("connection closed: %v", err)
|
|
return
|
|
}
|
|
switch t {
|
|
case protocol.MsgFrameFull, protocol.MsgFrameDelta:
|
|
fu, err := protocol.DecodeFrameUpdate(payload)
|
|
if err != nil {
|
|
log.Printf("bad frame update: %v", err)
|
|
continue
|
|
}
|
|
applyFrame(a, fu)
|
|
case protocol.MsgResize:
|
|
r, err := protocol.DecodeResize(payload)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
resizeFramebuffer(a, int(r.Width), int(r.Height))
|
|
case protocol.MsgPong:
|
|
// keepalive round trip; nothing to do
|
|
case protocol.MsgFileResult:
|
|
fr, err := protocol.DecodeFileResult(payload)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if fr.OK {
|
|
log.Printf("file transfer succeeded: %s", fr.Message)
|
|
} else {
|
|
log.Printf("file transfer failed: %s", fr.Message)
|
|
}
|
|
case protocol.MsgDisconnect:
|
|
log.Printf("host disconnected")
|
|
return
|
|
case protocol.MsgKeyLogEvent:
|
|
kl, err := protocol.DecodeKeyLogEvent(payload)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
select {
|
|
case a.keylogEvents <- kl:
|
|
default:
|
|
// Channel full; drop event rather than blocking the read loop.
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func resizeFramebuffer(a *appState, w, h int) {
|
|
a.fbMu.Lock()
|
|
a.fbW, a.fbH = w, h
|
|
a.fb = make([]byte, w*h*4)
|
|
a.fbMu.Unlock()
|
|
invalidateWindow(a)
|
|
}
|
|
|
|
func applyFrame(a *appState, fu protocol.FrameUpdate) {
|
|
img, err := png.Decode(bytes.NewReader(fu.PNG))
|
|
if err != nil {
|
|
log.Printf("decoding frame PNG: %v", err)
|
|
return
|
|
}
|
|
|
|
rect := image.Rect(int(fu.X), int(fu.Y), int(fu.X)+int(fu.W), int(fu.Y)+int(fu.H))
|
|
|
|
a.fbMu.Lock()
|
|
if a.fbW == 0 || a.fbH == 0 {
|
|
a.fbMu.Unlock()
|
|
return
|
|
}
|
|
// Draw into an RGBA view backed by the BGRA framebuffer bytes, swapping
|
|
// R/B as we go, so painting later is a direct GDI blit with no per-frame
|
|
// conversion pass.
|
|
for y := rect.Min.Y; y < rect.Max.Y && y < a.fbH; y++ {
|
|
for x := rect.Min.X; x < rect.Max.X && x < a.fbW; x++ {
|
|
r, g, b, aa := img.At(x-rect.Min.X, y-rect.Min.Y).RGBA()
|
|
off := (y*a.fbW + x) * 4
|
|
a.fb[off+0] = byte(b >> 8)
|
|
a.fb[off+1] = byte(g >> 8)
|
|
a.fb[off+2] = byte(r >> 8)
|
|
a.fb[off+3] = byte(aa >> 8)
|
|
}
|
|
}
|
|
a.fbMu.Unlock()
|
|
|
|
invalidateWindow(a)
|
|
}
|
|
|
|
// pingLoop keeps the TLS connection from being silently dropped by NAT/idle
|
|
// timers, and gives us an early signal if the host has gone away.
|
|
func pingLoop(a *appState) {
|
|
ticker := time.NewTicker(10 * time.Second)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-a.done:
|
|
return
|
|
case <-ticker.C:
|
|
if err := a.send(protocol.MsgPing, nil); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|