Fix(server,client): keep web terminal alive across heartbeat reconnects

The Go server answered TOKEN_HEARTBEAT with a 33-byte CMD_HEARTBEAT_ACK,
while the Linux and Android clients gate the ACK on 1 + sizeof(HeartbeatACK)
(1025 bytes) and silently drop anything shorter. Their application-level ACK
watchdog therefore never got fed and disconnected the main connection every
60s. Each re-login replaced the whole hub.Device, wiping the screen/shell
sub-conn bindings that ride their own TCP connections, so a web terminal kept
receiving shell output while every keystroke was dropped — the "terminal goes
unresponsive after a while" symptom — and the shell could no longer be torn
down (orphan bash + leaked sockets).

Clients now accept the 32-byte old ACK (Windows already did), and the server
sends the full 1024-byte struct. Hub.Register carries the sub-conn session
state across a re-login, and the disconnect cleanup is pinned to the
connection it belongs to so a stale OnDisconnect cannot tear down a
registration that has already replaced it. A shell sub-conn that can no
longer be bound now tells the viewer instead of leaving the pane waiting.

Verified on aa: RTT restored, zero "Heartbeat ACK timeout" lines, main-conn
port stable; Go build/vet/test pass. Server deploy to the Go host is still
pending, so un-upgraded clients will keep reconnecting until then.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 11:03:57 +01:00
1 parent 4aedca8ee0
commit 81f0d6c031
8 files changed
+412 -46

No files matched your search

+42 -22
View File
@@ -71,9 +71,15 @@ func (h *MyHandler) OnDisconnect(ctx *connection.Context) {
// device-offline event, instead of frames/output continuing to
// stream from orphaned sub-conn ctxs until they time out on
// their own. Both calls no-op if there's no active session.
h.hub.CloseScreen(info.ClientID)
h.hub.CloseTerminalSession(info.ClientID)
h.hub.Unregister(info.ClientID)
//
// Use the *Pinned variants: this runs concurrently with a re-login
// on a fresh connection, and the plain versions would tear down the
// sessions Register just carried over (and Unregister by ID alone
// would delete the device that just re-registered, hiding a live
// client until its next reconnect).
h.hub.CloseScreenPinned(info.ClientID, ctx)
h.hub.CloseTerminalSessionPinned(info.ClientID, ctx)
h.hub.UnregisterPinned(info.ClientID, ctx)
}
}
@@ -322,6 +328,16 @@ func (h *MyHandler) handleTerminalStart(ctx *connection.Context, isPTY bool) {
} else {
h.log.Warn("orphan terminal sub-conn: device=%s conn=%d isPTY=%v",
devID, ctx.ID, isPTY)
// Genuine device, but no pending session: the term_open that
// spawned this shell was issued on a main conn that has since
// been replaced (Register deliberately does not carry
// terminalPending over, so the browser can retry). Tell the
// viewer instead of leaving the pane on "connecting" until it
// is closed by hand — the browser treats term_closed as the
// session being over and lets the user reopen it.
if mc != nil && mc.GetPeerIP() == ctx.GetPeerIP() {
h.hub.PublishTerminalClosed(devID, "reconnected")
}
}
ctx.Close()
return
@@ -696,10 +712,9 @@ func (h *MyHandler) handleAuth(ctx *connection.Context, data []byte) {
// - offset 548: Passcode (44 bytes)
// - offset 592: PwdHmac (8 bytes, uint64)
//
// HeartbeatACK structure:
// - offset 0: Time (8 bytes, uint64)
// - offset 8: Authorized (1 byte, char)
// - offset 9: Reserved (23 bytes)
// The reply layout is HeartbeatACK (common/commands.h, 1024 bytes), rendered
// by buildHeartbeatACK from the offsets in protocol/commands.go — see the
// HeartbeatACKSize comment there for why the full size is mandatory.
func (h *MyHandler) handleHeartbeat(ctx *connection.Context, data []byte) {
// Parse Time from heartbeat request (offset 1, 8 bytes)
@@ -768,27 +783,32 @@ func (h *MyHandler) handleHeartbeat(ctx *connection.Context, data []byte) {
}
}
// Build HeartbeatACK response: CMD_HEARTBEAT_ACK(1) + HeartbeatACK(32)
resp := make([]byte, 33)
resp[0] = protocol.CommandHeartbeat // CMD_HEARTBEAT_ACK = 216
// Time at offset 1 (8 bytes, little-endian)
resp[1] = byte(hbTime)
resp[2] = byte(hbTime >> 8)
resp[3] = byte(hbTime >> 16)
resp[4] = byte(hbTime >> 24)
resp[5] = byte(hbTime >> 32)
resp[6] = byte(hbTime >> 40)
resp[7] = byte(hbTime >> 48)
resp[8] = byte(hbTime >> 56)
// Authorized at offset 9 (1 byte)
resp[9] = authorized
// Reserved[23] at offset 10 is already zero
resp := buildHeartbeatACK(hbTime, authorized)
if err := h.srv.Send(ctx, resp); err != nil {
h.log.Error("Failed to send heartbeat ACK to client %d: %v", ctx.ID, err)
}
}
// buildHeartbeatACK renders the CMD_HEARTBEAT_ACK reply: CMD_HEARTBEAT_ACK(1)
// + HeartbeatACK(1024). The packet MUST be the full struct size: clients
// deployed before 2026-11 only accept `ulLength >= 1 + sizeof(HeartbeatACK)`
// and silently ignore a shorter ACK, so a 33-byte reply left their 60s ACK
// watchdog unfed and made every such client reconnect forever. Current
// clients accept both sizes. The C++ server sends the same 1+1024 bytes
// (server/2015Remote/2015RemoteDlg.cpp).
//
// IsTrail / Authorization / ProcessingMs stay zero: clients sync the first two
// from CMD_MASTERSETTING, and ProcessingMs == 0 means "unknown", so the client
// computes RTT as plain now-Time (its behaviour before that field existed).
func buildHeartbeatACK(hbTime uint64, authorized byte) []byte {
resp := make([]byte, 1+protocol.HeartbeatACKSize)
resp[0] = protocol.CommandHeartbeat // CMD_HEARTBEAT_ACK = 216
binary.LittleEndian.PutUint64(resp[1+protocol.HeartbeatACKOffTime:], hbTime)
resp[1+protocol.HeartbeatACKOffAuthorized] = authorized
return resp
}
// parsePorts parses a semicolon-separated port string and returns port numbers
func parsePorts(portStr string) ([]int, error) {
var ports []int