From 9ce1a58bc3f43018ca82dce9162247c6e20b410a Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Thu, 12 Nov 2026 23:04:35 +0100 Subject: [PATCH] Feature(Go): read_file over the V2 stream receiver read_file reuses the list_files handshake: COMMAND_LIST_DRIVE opens the file-manager sub-conn, which then gets [CMD_DOWN_FILES_V2][targetDir\0][path\0][\0] and streams COMMAND_SEND_FILE_V2 chunks plus a COMMAND_FILE_COMPLETE_V2 carrying the whole-file SHA-256. Windows clients open a fresh authenticated sub-conn for the chunks; POSIX clients stream on the file-manager conn itself, so that conn is retired when the call ends rather than right after the request. TOKEN_DOWNLOAD_ERROR on the file-manager conn maps to -32009 / -32010 / -32000 like the Win32 server. Bytes land only in a per-call os.MkdirTemp dir; the client name is checked against the target dir (separator-normalised, path.Clean, so ..\.., absolute, UNC and mixed separators are refused) as defence-in-depth, never used as a path. First chunk enforces the size gate (ReadMax) and rejects multi-file batches, so an expanded directory fails with -32602 instead of returning its first file. Chunks from a sub-conn opened for an earlier call are dropped by connection ID, data sub-conns are closed when the call ends, and a closed sub-conn fails the call fast instead of waiting for the stall watchdog. Text windowing, encoding detection (BOM / UTF-8 / GBK / explicit / latin1) and EOL handling mirror McpServer.cpp. Verified with the Debug ghost on Windows 11 and the WSL Ubuntu ghost: .env text plus newline is byte-identical, sha256 equals SHA-256 over the file, windows/GBK/empty/ max_bytes/directory/size-gate/missing-path/POSIX-path cases all pass. Co-Authored-By: deepseek-flash --- server/go/cmd/main.go | 9 + server/go/mcp/filemgr.go | 136 +++-- server/go/mcp/filemgr_test.go | 19 +- server/go/mcp/filev2.go | 1033 ++++++++++++++++++++++++++++++++ server/go/mcp/filev2_test.go | 627 +++++++++++++++++++ server/go/mcp/server.go | 30 +- server/go/protocol/commands.go | 7 + 7 files changed, 1790 insertions(+), 71 deletions(-) create mode 100644 server/go/mcp/filev2.go create mode 100644 server/go/mcp/filev2_test.go diff --git a/server/go/cmd/main.go b/server/go/cmd/main.go index 52de078..1e27166 100644 --- a/server/go/cmd/main.go +++ b/server/go/cmd/main.go @@ -183,6 +183,15 @@ func (h *MyHandler) OnReceive(ctx *connection.Context, data []byte) { return } h.log.Info("Unhandled command %d from client %d", cmd, ctx.ID) + case protocol.CommandSendFileV2, protocol.CommandFileCompleteV2, protocol.TokenDownloadError: + // read_file's V2 transfer: chunks and the completion packet arrive on an + // authenticated sub-conn (a fresh one on Windows, the file-manager one on + // POSIX), the download error on the file-manager sub-conn. MCP routes + // them to the pending session for the device and writes the bytes itself. + if h.mcp != nil && h.mcp.OnTransferPacket(ctx, data) { + return + } + h.log.Info("Unhandled command %d from client %d", cmd, ctx.ID) case protocol.CmdSockDllLoader: // SOCKET_DLLLOADER (210): the shellcode / memory-DLL loader asks for the // full DLL payload before it can load the client. Serving it is the MFC diff --git a/server/go/mcp/filemgr.go b/server/go/mcp/filemgr.go index c63274d..bdf2d7c 100644 --- a/server/go/mcp/filemgr.go +++ b/server/go/mcp/filemgr.go @@ -4,6 +4,7 @@ import ( "context" "encoding/binary" "encoding/json" + "errors" "fmt" "strings" "sync" @@ -197,6 +198,7 @@ func (t *fileTable) deliver(deviceID string, ev fileEvent) bool { func (s *Server) registerFileTools() { s.handlers["list_files"] = s.callListFiles + s.handlers["read_file"] = s.callReadFile } // OnFilePacket feeds one file-manager packet from a sub-connection to the @@ -244,7 +246,9 @@ func (s *Server) OnFilePacket(ctx *connection.Context, data []byte) bool { // Nothing is waiting for this sub-conn: the call it belonged to already // timed out or finished. Retire it so the client does not leak its // file-manager thread, but only once the sub-conn has announced itself. - if ev.kind == fileEventDriveList { + // A live read_file keeps its file-manager sub-conn registered in the + // transfer table (POSIX clients stream on it), so never close that one. + if ev.kind == fileEventDriveList && !s.transfers.owns(ctx) { s.retireFileConn(ctx) } return true @@ -296,6 +300,67 @@ type fileEntry struct { Mtime int64 `json:"mtime"` } +// openFileManagerConn asks the device for its file-manager sub-connection +// (COMMAND_LIST_DRIVE on the main connection) and waits for the drive list. +// On success it returns the sub-connection, the still-pending request (later +// packets are matched against it) and the parsed drives; the caller owns both +// and must call closeFileManagerConn when done. Every failure path retires the +// sub-connection here, so a timed-out or rejected handshake leaves no orphan +// file-manager thread on the client. +func (s *Server) openFileManagerConn(ctx context.Context, deviceID string) (*connection.Context, *fileRequest, []driveEntry, *rpcError) { + req, ok := s.files.begin(deviceID) + if !ok { + return nil, nil, nil, errf(CodeBusy, "Device busy: another file request is already pending for host %s", deviceID) + } + keep := false + defer func() { + if !keep { + if sub := s.files.cancel(deviceID, req); sub != nil { + s.retireFileConn(sub) + } + } + }() + + if err := s.cfg.Hub.SendToDevice(deviceID, []byte{protocol.CommandListDrive}); err != nil { + if errors.Is(err, hub.ErrDeviceOffline) { + return nil, nil, nil, errf(CodeOffline, "Host not found or offline: %s", deviceID) + } + return nil, nil, nil, errf(CodeSendFailed, "Failed to ask the host for its file-manager channel: %v", err) + } + + timer := time.NewTimer(fileListTimeout) + defer timer.Stop() + select { + case ev := <-req.ch: + switch ev.kind { + case fileEventDriveList: + keep = true + return ev.ctx, req, parseDriveList(ev.payload), nil + case fileEventError: + return nil, nil, nil, errf(win32ErrorCode(ev.win32err), + "Failed to open the file-manager channel on host %s (win32=%d)", deviceID, ev.win32err) + default: + return nil, nil, nil, errf(CodeInternal, "Unexpected file-manager reply while listing drives") + } + case <-timer.C: + return nil, nil, nil, errf(CodeTimeout, + "Timeout waiting for the host to open a file-manager channel (%s). Older clients that do not implement file browsing stay silent; retrying will not help.", + fileListTimeout) + case <-ctx.Done(): + return nil, nil, nil, errf(CodeTimeout, "Request cancelled while waiting for the host's file-manager channel") + } +} + +// closeFileManagerConn ends the request and retires the sub-connection it +// learned about. It is the single retirement point for that sub-conn: the +// cancel below also covers a call that gave up while its drive-list packet was +// already queued (the select taking the timer branch with both ready). +func (s *Server) closeFileManagerConn(deviceID string, req *fileRequest) { + if sub := s.files.cancel(deviceID, req); sub != nil { + s.retireFileConn(sub) + } +} + // callListFiles lists drives (no path) or one directory level. func (s *Server) callListFiles(ctx context.Context, raw json.RawMessage) (any, *rpcError) { if s.cfg.Hub == nil { @@ -335,62 +400,17 @@ func (s *Server) callListFiles(ctx context.Context, raw json.RawMessage) (any, * "Path too long: %d bytes after conversion to the client's GBK encoding (limit 249)", len(pathGBK)) } - req, ok := s.files.begin(args.ID) - if !ok { - return nil, errf(CodeBusy, "Device busy: another list_files request is already pending for host %s", args.ID) - } - // One retirement point for every exit path, including the ones where the - // drive-list packet is already queued but the select picks the timer or a - // cancelled request: cancel hands back the sub-conn it saw so the client's - // file-manager thread is closed instead of leaked. - defer func() { - if sub := s.files.cancel(args.ID, req); sub != nil { - s.retireFileConn(sub) - } - }() - - if err := s.cfg.Hub.SendToDevice(args.ID, []byte{protocol.CommandListDrive}); err != nil { - if err == hub.ErrDeviceOffline { - return nil, errf(CodeOffline, "Host not found or offline: %s", args.ID) - } - return nil, errf(CodeSendFailed, "Failed to ask the host for its file-manager channel: %v", err) - } - - timer := time.NewTimer(fileListTimeout) - defer timer.Stop() - - // Phase 1: the client opens the sub-conn and sends its drive list. The - // sub-conn stays open for the directory request. - var drives []driveEntry - select { - case ev := <-req.ch: - switch ev.kind { - case fileEventDriveList: - drives = parseDriveList(ev.payload) - case fileEventError: - return nil, fileWin32Error(ev.win32err, path) - default: - return nil, errf(CodeInternal, "Unexpected file-manager reply while listing drives") - } - case <-timer.C: - return nil, errf(CodeTimeout, - "Timeout waiting for the host to open a file-manager channel (%s). Older clients that do not implement file browsing stay silent; retrying will not help.", - fileListTimeout) - case <-ctx.Done(): - return nil, errf(CodeTimeout, "Request cancelled while waiting for the host's file-manager channel") + sub, req, drives, rerr := s.openFileManagerConn(ctx, args.ID) + if rerr != nil { + return nil, rerr } + defer s.closeFileManagerConn(args.ID, req) if listDrives { return listFilesResult(args, drives, nil, false), nil } // Phase 2: reuse the same sub-conn for the directory itself. - sub := req.subConn() - if sub == nil { - // The list above succeeded, so a sub-conn was recorded; a nil here - // would mean the request was cancelled underneath us. - return nil, errf(CodeInternal, "File-manager channel closed before the directory request") - } pkt := make([]byte, 0, 2+len(pathGBK)) pkt = append(pkt, protocol.CommandListFiles) pkt = append(pkt, pathGBK...) @@ -399,6 +419,8 @@ func (s *Server) callListFiles(ctx context.Context, raw json.RawMessage) (any, * return nil, errf(CodeSendFailed, "Failed to send the directory request on the file-manager channel: %v", err) } + timer := time.NewTimer(fileListTimeout) + defer timer.Stop() select { case ev := <-req.ch: switch ev.kind { @@ -419,6 +441,20 @@ func (s *Server) callListFiles(ctx context.Context, raw json.RawMessage) (any, * } } +// win32ErrorCode maps a client-reported Win32 error onto an MCP code the same +// way the Win32 server does: 5 is a permission problem, 2/3 mean "no such +// target", everything else is an internal failure carrying the raw code. +func win32ErrorCode(win32err uint32) int { + switch win32err { + case errorAccessDenied: + return CodePermission + case errorFileNotFound, errorPathNotFound: + return CodeNotFound + default: + return CodeInternal + } +} + // fileWin32Error maps the client's win32 error onto MCP codes the same way the // Win32 server does: 5 is a permission problem, 2/3 mean "no such target", // everything else is an internal failure carrying the raw code. diff --git a/server/go/mcp/filemgr_test.go b/server/go/mcp/filemgr_test.go index 47c45ae..3394f39 100644 --- a/server/go/mcp/filemgr_test.go +++ b/server/go/mcp/filemgr_test.go @@ -23,11 +23,12 @@ type fileWire struct { } type fileRig struct { - t *testing.T - s *Server - h *hub.Hub - main *connection.Context - w chan fileWire + t *testing.T + s *Server + h *hub.Hub + main *connection.Context + w chan fileWire + connID uint64 // sub-connections get increasing IDs, like connection.Manager } func newFileRig(t *testing.T) *fileRig { @@ -50,7 +51,7 @@ func newFileRig(t *testing.T) *fileRig { } return nil }) - h.Register(&hub.Device{ID: "1001", PeerIP: "10.0.0.1"}, main) + h.Register(&hub.Device{ID: "1001", PeerIP: "10.0.0.1", Capability: "1"}, main) rig.s = New(Config{Token: testToken, Hub: h}) t.Cleanup(rig.s.Close) return rig @@ -62,7 +63,11 @@ func (r *fileRig) newSub() *connection.Context { r.t.Helper() c1, c2 := net.Pipe() r.t.Cleanup(func() { _ = c1.Close(); _ = c2.Close() }) - ctx := &connection.Context{Conn: c1, RemoteAddr: "10.0.0.1:4100"} + // Real contexts are numbered monotonically at accept time; read_file uses + // that to tell a data sub-conn opened for this call from one left over by an + // earlier call, so the fake ones have to be numbered too. + r.connID++ + ctx := &connection.Context{ID: r.connID, Conn: c1, RemoteAddr: "10.0.0.1:4100"} ctx.Info.ClientID = "1001" return ctx } diff --git a/server/go/mcp/filev2.go b/server/go/mcp/filev2.go new file mode 100644 index 0000000..e61de9a --- /dev/null +++ b/server/go/mcp/filev2.go @@ -0,0 +1,1033 @@ +package mcp + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/binary" + "encoding/hex" + "encoding/json" + "hash" + "os" + "path" + "path/filepath" + "strconv" + "strings" + "sync" + "time" + "unicode/utf16" + "unicode/utf8" + + "golang.org/x/text/encoding/simplifiedchinese" + + "github.com/yuanyuanxiang/SimpleRemoter/server/go/connection" + "github.com/yuanyuanxiang/SimpleRemoter/server/go/protocol" +) + +// read_file pulls one small file over the V2 stream receiver and returns it as +// paged text (design §5.4). The flow reuses the file-manager handshake M6 built: +// +// main conn : COMMAND_LIST_DRIVE(1) +// fm sub : [CMD_DOWN_FILES_V2(94)][targetDir\0][path\0][\0] +// stream : COMMAND_SEND_FILE_V2(85) chunks … +// → COMMAND_FILE_COMPLETE_V2(91) with the whole-file SHA-256 +// +// Windows clients open a fresh authenticated sub-connection for the chunks; +// POSIX clients send them on the file-manager sub-connection itself, so that +// connection must stay open until the call ends (MFC §13 F3 does the same — +// the plan's "close the file sub-conn first" only holds for Windows). +// +// Nothing the client sends is used as a filesystem path: every byte lands in +// /content, and the client-supplied name is only validated (it starts +// with the target directory we chose) as a defence-in-depth check against a +// compromised client steering writes elsewhere. + +const ( + // FileChunkPacketV2, packed (common/file_upload.h), 77 bytes: + // cmd(1) transferID(8) srcClientID(8) dstClientID(8) fileIndex(4) + // totalFiles(4) fileSize(8) offset(8) dataLength(8) nameLength(8) + // flags(2) checksum(2) reserved(8), then name, then data. + fileChunkHeaderLen = 77 + fileChunkOffTransferID = 1 + fileChunkOffDstClientID = 17 + fileChunkOffFileIndex = 25 + fileChunkOffTotalFiles = 29 + fileChunkOffFileSize = 33 + fileChunkOffOffset = 41 + fileChunkOffDataLength = 49 + fileChunkOffNameLength = 57 + fileChunkOffFlags = 65 + + // FileCompletePacketV2, packed, 69 bytes: + // cmd(1) transferID(8) srcClientID(8) dstClientID(8) fileIndex(4) + // fileSize(8) sha256(32). + fileCompleteHeaderLen = 69 + fileCompleteOffTransferID = 1 + fileCompleteOffDstClientID = 17 + fileCompleteOffFileIndex = 25 + fileCompleteOffFileSize = 29 + fileCompleteOffSHA256 = 37 + fileCompleteOffSHA256Length = 32 + + // FileFlagsV2 bits that matter here. + ffV2LastChunk uint16 = 0x0001 + ffV2Directory uint16 = 0x0010 + + // read_file argument bounds, matching the published schema and MFC. + readFileDefaultLimit = 2000 + readFileMaxLimit = 200000 + readFileDefaultMaxBytes = 262144 + readFileMinMaxBytes = 1024 + readFileMaxMaxBytes = 2 << 20 + readFileOffsetBound = 100000000 + + // readFilePathMaxGBK keeps the path inside the client's MAX_PATH buffer + // once it has been widened to the ANSI form it passes to the Win32 APIs. + readFilePathMaxGBK = 249 + + // transferContentName is the single file every transfer writes to. The + // client's name never reaches the filesystem. + transferContentName = "content" +) + +// Transfer watchdogs. Variables only so tests can shorten them. +var ( + fileTransferStallTimeout = 60 * time.Second + fileTransferTotalTimeout = 120 * time.Second + fileTransferPollInterval = 250 * time.Millisecond +) + +// fileTransferFailure is a terminal session error, already in MCP shape. +type fileTransferFailure = rpcError + +// transferSession is one in-flight V2 download for a device. Every packet is +// processed synchronously on the TCP read loop under mu; the waiting call only +// polls the small state below, which keeps packet ordering (and therefore the +// chunk-offset check) exact. +type transferSession struct { + mu sync.Mutex + + deviceID string + remotePath string // as the caller wrote it, for messages and the result + dir string // private temp directory + contentPath string + targetDir string // forward-slash form with a trailing '/', for name checks + maxFileBytes uint64 + + fm *connection.Context // file-manager sub-conn that answered the drive list + stream *connection.Context // sub-conn chunks arrive on (== fm for POSIX clients) + + transferID uint64 + haveTransfer bool + fileIndex uint32 + totalFiles uint32 + haveFile bool + fileSize uint64 + received uint64 + digest hash.Hash + sha256hex string // filled in by the complete packet once verified + + file *os.File + name string // last validated relative name, for logs only + lastAt time.Time + failure *fileTransferFailure + done bool + closed bool +} + +// transferTable holds the single pending download per device. The per-device +// call lock already serialises tools; the table also rejects a second session +// rather than letting two calls share one stream. +type transferTable struct { + mu sync.Mutex + byDevice map[string]*transferSession +} + +func newTransferTable() *transferTable { + return &transferTable{byDevice: make(map[string]*transferSession)} +} + +func (t *transferTable) begin(deviceID string, maxFileBytes uint64, remotePath, dir, targetDir string) (*transferSession, bool) { + t.mu.Lock() + defer t.mu.Unlock() + if _, ok := t.byDevice[deviceID]; ok { + return nil, false + } + s := &transferSession{ + deviceID: deviceID, + remotePath: remotePath, + dir: dir, + contentPath: filepath.Join(dir, transferContentName), + targetDir: targetDir, + maxFileBytes: maxFileBytes, + lastAt: time.Now(), + } + t.byDevice[deviceID] = s + return s, true +} + +// end removes the session and releases its file. Safe to call once per call; +// a packet that already grabbed the pointer sees closed and drops itself. +func (t *transferTable) end(deviceID string, s *transferSession) { + t.mu.Lock() + if t.byDevice[deviceID] == s { + delete(t.byDevice, deviceID) + } + t.mu.Unlock() + s.close() +} + +func (t *transferTable) find(deviceID string) *transferSession { + t.mu.Lock() + defer t.mu.Unlock() + return t.byDevice[deviceID] +} + +// owns reports whether ctx belongs to a live transfer. OnFilePacket consults it +// before retiring an orphan file-manager sub-conn: on POSIX that connection is +// also the data stream, and closing it would break a running read_file. +func (t *transferTable) owns(ctx *connection.Context) bool { + t.mu.Lock() + sessions := make([]*transferSession, 0, len(t.byDevice)) + for _, s := range t.byDevice { + sessions = append(sessions, s) + } + t.mu.Unlock() + for _, s := range sessions { + s.mu.Lock() + own := s.fm == ctx || s.stream == ctx + s.mu.Unlock() + if own { + return true + } + } + return false +} + +// attachFM records the file-manager sub-connection the handshake produced. +func (t *transferSession) attachFM(ctx *connection.Context) { + t.mu.Lock() + t.fm = ctx + t.mu.Unlock() +} + +// fail records a terminal error unless the transfer already finished. +func (t *transferSession) fail(code int, format string, args ...any) { + if t.failure == nil && !t.done { + t.failure = errf(code, format, args...) + } + t.done = true +} + +func (t *transferSession) close() { + t.mu.Lock() + defer t.mu.Unlock() + t.closed = true + if t.file != nil { + _ = t.file.Close() + t.file = nil + } + // The data sub-conn exists only for this transfer. Closing it tells the + // client to stop: without that, a batch the server rejected (size gate, + // directory) keeps streaming in the background until the client finishes + // reading the whole thing. POSIX streams on the file-manager conn instead, + // which retireFileConn already closes. + if t.stream != nil { + _ = t.stream.Close() + } +} + +// waitState is the waiter's view of the session. +type waitState struct { + done bool + failure *fileTransferFailure + fm *connection.Context + stream *connection.Context + sha256 string +} + +func (t *transferSession) state() waitState { + t.mu.Lock() + defer t.mu.Unlock() + return waitState{done: t.done, failure: t.failure, fm: t.fm, stream: t.stream, sha256: t.sha256hex} +} + +func (t *transferSession) stalled(limit time.Duration) bool { + t.mu.Lock() + defer t.mu.Unlock() + return !t.done && time.Since(t.lastAt) >= limit +} + +// admits reports whether ctx may carry bytes for this call, and adopts the +// client's data sub-connection the first time one shows up (Windows opens a +// fresh sub-conn per download; POSIX streams on the file-manager sub-conn). +// +// Connections are numbered monotonically when the server accepts them, and +// this call's file-manager conn was created after the request went out, so a +// connection with a lower ID was opened for an earlier call. That matters +// because a client keeps streaming the rest of a batch after the server +// rejects it (the size gate is the common case), and those late bytes must not +// be able to attach to — or fail — the next call. The caller holds t.mu. +func (t *transferSession) admits(ctx *connection.Context) bool { + if ctx == t.fm { + return true // POSIX: chunks travel on the file-manager sub-conn itself + } + if t.stream != nil { + return ctx == t.stream + } + if t.fm != nil && ctx.ID <= t.fm.ID { + return false // stale sub-conn from an earlier transfer + } + t.stream = ctx + return true +} + +// acceptChunk validates and stores one COMMAND_SEND_FILE_V2 packet. The caller +// has already checked the framing (name/data lengths fit the packet). +func (t *transferSession) acceptChunk(ctx *connection.Context, transferID uint64, + fileIndex, totalFiles uint32, fileSize, offset uint64, flags uint16, name []byte, payload []byte) { + t.mu.Lock() + defer t.mu.Unlock() + if t.done || t.closed { + return + } + if !t.admits(ctx) { + return // not this call's connection + } + + if !t.haveTransfer { + t.transferID = transferID + t.haveTransfer = true + } + if transferID != t.transferID { + return // a late packet from an exhausted transfer + } + t.lastAt = time.Now() + + nameStr := string(name) + if strings.ContainsAny(nameStr, "\x00\r\n") { + t.fail(CodeInternal, "Path traversal detected in received path (control byte): %s", quoteWireName(nameStr)) + return + } + if flags&ffV2Directory != 0 { + t.fail(CodeInvalidParams, "Not a single regular file (directory or multiple matches): %s — use list_files / search_file to pick a specific file", t.remotePath) + return + } + rel, ok := transferNameWithinDir(t.targetDir, nameStr) + if !ok { + t.fail(CodeInternal, "Path traversal detected in received path: %s (expected a path inside %s)", + quoteWireName(nameStr), t.targetDir) + return + } + + if !t.haveFile { + // The client expands a directory into one chunk stream per file and puts + // the batch size in every chunk. Checking it here rejects a directory + // before anything is written — waiting for a second fileIndex would let a + // two-file directory complete on its first file and be returned as if it + // were the answer. totalFiles == 0 means "not reported": older senders + // may leave it unset, and a single file is still the only valid read. + if totalFiles > 1 { + t.fail(CodeInvalidParams, "Not a single regular file (directory or multiple matches): %s — use list_files / search_file to pick a specific file", t.remotePath) + return + } + if fileSize > t.maxFileBytes { + t.fail(CodeInvalidParams, + "File is larger than %d bytes: use download_file to fetch it (%s)", t.maxFileBytes, t.remotePath) + return + } + if offset != 0 { + t.fail(CodeInternal, "Unexpected chunk offset %d at the start of the transfer", offset) + return + } + f, err := os.Create(t.contentPath) + if err != nil { + t.fail(CodeInternal, "Failed to store the downloaded file: %v", err) + return + } + t.file = f + t.digest = sha256.New() + t.fileIndex = fileIndex + t.totalFiles = totalFiles + t.fileSize = fileSize + t.haveFile = true + } else if fileIndex != t.fileIndex { + // A second file index means the remote path matched more than one file. + t.fail(CodeInvalidParams, "Not a single regular file (directory or multiple matches): %s — use list_files / search_file to pick a specific file", t.remotePath) + return + } + + t.name = rel + if offset != t.received { + t.fail(CodeInternal, "Unexpected chunk offset %d (expected %d)", offset, t.received) + return + } + if t.received+uint64(len(payload)) > t.fileSize { + t.fail(CodeInternal, "Chunk overruns the declared file size (%d > %d)", t.received+uint64(len(payload)), t.fileSize) + return + } + if len(payload) > 0 { + if _, err := t.file.Write(payload); err != nil { + t.fail(CodeInternal, "Failed to store the downloaded file: %v", err) + return + } + t.digest.Write(payload) + t.received += uint64(len(payload)) + } + if flags&ffV2LastChunk != 0 && t.received != t.fileSize { + t.fail(CodeInternal, "Incomplete transfer: received %d of %d bytes", t.received, t.fileSize) + } +} + +// acceptComplete verifies the whole-file SHA-256 and closes the transfer. An +// empty file arrives as a complete packet with no chunk at all (Windows +// clients never send a zero-length chunk). +func (t *transferSession) acceptComplete(ctx *connection.Context, transferID uint64, fileIndex uint32, fileSize uint64, sum []byte) { + t.mu.Lock() + defer t.mu.Unlock() + if t.done || t.closed { + return + } + if !t.admits(ctx) { + return // not this call's connection + } + + if !t.haveTransfer { + t.transferID = transferID + t.haveTransfer = true + } + if transferID != t.transferID { + return + } + t.lastAt = time.Now() + if t.haveFile { + if fileIndex != t.fileIndex { + return // another file's completion; ours cannot be done yet + } + if fileSize != t.fileSize { + t.fail(CodeInternal, "Completion packet reports %d bytes, chunks reported %d", fileSize, t.fileSize) + return + } + if t.received != t.fileSize { + t.fail(CodeInternal, "Incomplete transfer: received %d of %d bytes", t.received, t.fileSize) + return + } + got := t.digest.Sum(nil) + if len(sum) != sha256.Size || !bytes.Equal(got, sum) { + t.fail(CodeInternal, "File SHA-256 verification failed") + return + } + t.sha256hex = hex.EncodeToString(got) + } else { + // No chunk at all: only an empty file may complete this way. + if fileSize != 0 { + t.fail(CodeInternal, "Incomplete transfer: the host sent no data for a %d-byte file", fileSize) + return + } + f, err := os.Create(t.contentPath) + if err != nil { + t.fail(CodeInternal, "Failed to store the downloaded file: %v", err) + return + } + t.file = f + t.fileIndex = fileIndex + t.haveFile = true + t.fileSize = 0 + got := sha256.Sum256(nil) + if len(sum) == sha256.Size && !bytes.Equal(got[:], sum) { + t.fail(CodeInternal, "File SHA-256 verification failed") + return + } + t.sha256hex = hex.EncodeToString(got[:]) + } + if t.file != nil { + if err := t.file.Close(); err != nil { + t.file = nil + t.fail(CodeInternal, "Failed to store the downloaded file: %v", err) + return + } + t.file = nil + } + t.done = true +} + +// acceptDownloadError turns the file-manager sub-conn's TOKEN_DOWNLOAD_ERROR +// into the same codes MFC reports (2/3 → -32009, 5 → -32010, else -32000). +func (t *transferSession) acceptDownloadError(ctx *connection.Context, win32err uint32) { + t.mu.Lock() + defer t.mu.Unlock() + if t.done || t.closed || ctx != t.fm { + return + } + t.lastAt = time.Now() + switch win32err { + case errorFileNotFound, errorPathNotFound: + t.fail(CodeNotFound, "Remote path does not exist: %s (win32=%d)", t.remotePath, win32err) + case errorAccessDenied: + t.fail(CodePermission, "Access denied to remote path: %s (win32=%d)", t.remotePath, win32err) + default: + t.fail(CodeInternal, "Client could not queue the download of %s (win32=%d)", t.remotePath, win32err) + } +} + +// quoteWireName renders a client-supplied wire name for an error message. The +// name is GBK bytes on the wire, so it is quoted verbatim and bounded: never use +// it as a path, only show it. +func quoteWireName(name string) string { + const maxShown = 200 + if len(name) > maxShown { + name = name[:maxShown] + "…" + } + return strconv.Quote(name) +} + +// transferNameWithinDir returns name relative to targetDir, or ok=false when it +// resolves outside it. Separators are normalised first and path.Clean folds +// '..' before the prefix test, so `..\..\x`, `a/../../b`, absolute paths, UNC +// paths and mixed separators are all rejected by the same rule. +func transferNameWithinDir(targetDir, name string) (string, bool) { + if name == "" || strings.ContainsAny(name, "\x00\r\n") { + return "", false + } + base := path.Clean(strings.ReplaceAll(targetDir, "\\", "/")) + "/" + got := path.Clean(strings.ReplaceAll(name, "\\", "/")) + if !strings.HasPrefix(got, base) { + return "", false + } + rel := got[len(base):] + if rel == "" || rel == "." { + return "", false + } + return rel, true +} + +// ----- packet entry points --------------------------------------------------- + +// OnTransferPacket feeds one V2 transfer packet from a sub-connection to the +// pending read_file call. It reports whether the packet belongs to this +// protocol at all; cmd/main.go forwards 85/91/173. +func (s *Server) OnTransferPacket(ctx *connection.Context, data []byte) bool { + if s == nil || len(data) == 0 { + return false + } + switch data[0] { + case protocol.CommandSendFileV2, protocol.CommandFileCompleteV2, protocol.TokenDownloadError: + default: + return false + } + deviceID := ctx.GetInfo().ClientID + if deviceID == "" { + return true + } + if s.cfg.Hub != nil && s.cfg.Hub.MainConn(deviceID) == ctx { + return true // chunks never travel on the main connection + } + sess := s.transfers.find(deviceID) + if sess == nil { + return true // no call is waiting: a late packet from a finished transfer + } + + switch data[0] { + case protocol.CommandSendFileV2: + s.onFileChunkPacket(sess, ctx, data) + case protocol.CommandFileCompleteV2: + if len(data) < fileCompleteHeaderLen { + return true + } + if binary.LittleEndian.Uint64(data[fileCompleteOffDstClientID:]) != 0 { + return true // relayed C2C traffic, not ours + } + transferID := binary.LittleEndian.Uint64(data[fileCompleteOffTransferID:]) + fileIndex := binary.LittleEndian.Uint32(data[fileCompleteOffFileIndex:]) + fileSize := binary.LittleEndian.Uint64(data[fileCompleteOffFileSize:]) + sum := data[fileCompleteOffSHA256 : fileCompleteOffSHA256+fileCompleteOffSHA256Length] + sess.acceptComplete(ctx, transferID, fileIndex, fileSize, sum) + case protocol.TokenDownloadError: + var win32err uint32 + if len(data) >= 5 { + win32err = binary.LittleEndian.Uint32(data[1:5]) + } + sess.acceptDownloadError(ctx, win32err) + } + return true +} + +// onFileChunkPacket checks the packed header against the packet length before +// handing the payload to the session; a malformed chunk is dropped rather than +// failing the transfer (the client may still send a good one). +func (s *Server) onFileChunkPacket(sess *transferSession, ctx *connection.Context, data []byte) { + if len(data) < fileChunkHeaderLen { + return + } + nameLen := binary.LittleEndian.Uint64(data[fileChunkOffNameLength:]) + dataLen := binary.LittleEndian.Uint64(data[fileChunkOffDataLength:]) + avail := uint64(len(data) - fileChunkHeaderLen) + if nameLen > avail || dataLen > avail-nameLen { + return + } + if binary.LittleEndian.Uint64(data[fileChunkOffDstClientID:]) != 0 { + return // relayed C2C traffic, not ours + } + start := fileChunkHeaderLen + int(nameLen) + sess.acceptChunk(ctx, + binary.LittleEndian.Uint64(data[fileChunkOffTransferID:]), + binary.LittleEndian.Uint32(data[fileChunkOffFileIndex:]), + binary.LittleEndian.Uint32(data[fileChunkOffTotalFiles:]), + binary.LittleEndian.Uint64(data[fileChunkOffFileSize:]), + binary.LittleEndian.Uint64(data[fileChunkOffOffset:]), + binary.LittleEndian.Uint16(data[fileChunkOffFlags:]), + data[fileChunkHeaderLen:start], + data[start:start+int(dataLen)], + ) +} + +// ----- tool call ------------------------------------------------------------- + +// readFileArgs is the published inputSchema as Go. limit and max_bytes are +// pointers so an explicit out-of-range value is rejected instead of being +// mistaken for "absent". +type readFileArgs struct { + ID string `json:"id"` + Path string `json:"path"` + Offset int `json:"offset"` + Limit *int `json:"limit"` + Encoding string `json:"encoding"` + MaxBytes *int `json:"max_bytes"` +} + +// readFileOutput mirrors outputSchema exactly; the model relies on these names. +type readFileOutput struct { + Path string `json:"path"` + Size int `json:"size"` + Encoding string `json:"encoding"` + EOL string `json:"eol"` + LinesTotal int `json:"lines_total"` + Offset int64 `json:"offset"` + LinesReturned int `json:"lines_returned"` + Truncated bool `json:"truncated"` + Text string `json:"text"` + SHA256 string `json:"sha256"` +} + +// callReadFile streams one file down and returns the requested text window. +func (s *Server) callReadFile(ctx context.Context, raw json.RawMessage) (any, *rpcError) { + if s.cfg.Hub == nil { + return nil, errf(CodeInternal, "server device registry is not configured") + } + var args readFileArgs + if rerr := decodeArgs(raw, &args); rerr != nil { + return nil, rerr + } + if args.ID == "" { + return nil, errf(CodeInvalidParams, "Missing required parameter: id") + } + if !isDecimalID(args.ID) { + return nil, errf(CodeInvalidParams, "Invalid id: expected a decimal host id string") + } + if s.cfg.Hub.MainConn(args.ID) == nil { + return nil, errf(CodeOffline, "Host not found or offline: %s", args.ID) + } + if !protocol.SupportsCap(s.cfg.Hub.Capability(args.ID), protocol.ClientCapV2) { + return nil, errf(CodeUnsupported, + "Host does not support V2 file transfer: the client does not declare CLIENT_CAP_V2 (upgrade the client)") + } + + if args.Path == "" { + return nil, errf(CodeInvalidParams, "Missing required parameter: path") + } + if strings.ContainsAny(args.Path, "\x00\r\n") { + return nil, errf(CodeInvalidParams, "Invalid path: NUL and line breaks are not allowed") + } + pathGBK := protocol.UTF8ToGBK(args.Path) + if len(pathGBK) >= readFilePathMaxGBK { + return nil, errf(CodeInvalidParams, + "Path too long: %d bytes after conversion to the client's GBK encoding (limit %d)", + len(pathGBK), readFilePathMaxGBK-1) + } + + encoding, rerr := normalizeEncoding(args.Encoding) + if rerr != nil { + return nil, rerr + } + if args.Offset < -readFileOffsetBound || args.Offset > readFileOffsetBound { + return nil, errf(CodeInvalidParams, "offset out of range") + } + limit := readFileDefaultLimit + if args.Limit != nil { + if *args.Limit < 1 || *args.Limit > readFileMaxLimit { + return nil, errf(CodeInvalidParams, "limit must be in range 1..%d", readFileMaxLimit) + } + limit = *args.Limit + } + maxBytes := readFileDefaultMaxBytes + if args.MaxBytes != nil { + if *args.MaxBytes < readFileMinMaxBytes || *args.MaxBytes > readFileMaxMaxBytes { + return nil, errf(CodeInvalidParams, "max_bytes must be in range %d..%d", readFileMinMaxBytes, readFileMaxMaxBytes) + } + maxBytes = *args.MaxBytes + } + + dir, err := os.MkdirTemp("", "yama-mcp-read-*") + if err != nil { + return nil, errf(CodeInternal, "Failed to create the MCP temp directory: %v", err) + } + defer os.RemoveAll(dir) + // The client concatenates its relative name onto this string verbatim; the + // trailing separator is what makes that a directory prefix (GetRelativePath + // returns a bare basename when the root is the file itself). + targetDir := dir + string(os.PathSeparator) + + maxFileBytes := uint64(s.cfg.ReadMax) + sess, ok := s.transfers.begin(args.ID, maxFileBytes, args.Path, dir, targetDir) + if !ok { + return nil, errf(CodeBusy, "Device busy: another file request is already pending for host %s", args.ID) + } + defer s.transfers.end(args.ID, sess) + + sub, req, drives, rerr := s.openFileManagerConn(ctx, args.ID) + if rerr != nil { + return nil, rerr + } + defer s.closeFileManagerConn(args.ID, req) + sess.attachFM(sub) + + // POSIX hosts report their single root as the '/' drive. Their paths are + // checked before the request goes out so a Windows-style path fails with a + // clear message instead of a stall. + if driveListPosix(drives) { + if !strings.HasPrefix(args.Path, "/") { + return nil, errf(CodeInvalidParams, + "path must be an absolute POSIX path for Linux/macOS hosts, e.g. /etc/app.conf") + } + if strings.Contains(args.Path, "\\") { + return nil, errf(CodeInvalidParams, + "path must not contain backslashes on Linux/macOS hosts; use '/'") + } + if hasDotDotSegment(args.Path) { + return nil, errf(CodeInvalidParams, "path must not contain '..' path segments") + } + } + + pkt := make([]byte, 0, 3+len(targetDir)+len(pathGBK)) + pkt = append(pkt, protocol.CmdDownFilesV2) + pkt = append(pkt, protocol.UTF8ToGBK(targetDir)...) + pkt = append(pkt, 0) + pkt = append(pkt, pathGBK...) + pkt = append(pkt, 0, 0) + if err := s.cfg.Hub.SendToConn(sub, pkt); err != nil { + return nil, errf(CodeSendFailed, "Failed to send the download request on the file-manager channel: %v", err) + } + + if rerr := s.waitFileTransfer(ctx, sess); rerr != nil { + return nil, rerr + } + fileBytes, err := os.ReadFile(sess.contentPath) + if err != nil { + return nil, errf(CodeInternal, "Failed to read the downloaded temp file: %v", err) + } + return buildReadFileResult(args.Path, fileBytes, sess.state().sha256, encoding, args.Offset, limit, maxBytes), nil +} + +// waitFileTransfer blocks until the session finishes, fails, or a watchdog +// fires. Packets are processed on the read loop, so polling every 250 ms costs +// nothing and keeps the packet path lock-free of the waiter. +func (s *Server) waitFileTransfer(ctx context.Context, sess *transferSession) *rpcError { + deadline := time.Now().Add(fileTransferTotalTimeout) + for { + st := sess.state() + if st.failure != nil { + return st.failure + } + if st.done { + return nil + } + if ctx.Err() != nil { + return errf(CodeTimeout, "Request cancelled while waiting for the file transfer") + } + if st.stream != nil && st.stream.IsClosed() { + // The completion packet would have been processed before the close: + // a closed stream with no result is a lost transfer. + return errf(CodeTimeout, "Connection lost: the transfer sub-connection closed") + } + if st.fm != nil && st.fm.IsClosed() { + // POSIX clients stream on the file-manager sub-conn, so its close is + // the only early sign that the transfer died; Windows clients only + // close it when the call ends. + return errf(CodeTimeout, "Connection lost: the file-manager sub-connection closed") + } + if !time.Now().Before(deadline) { + return errf(CodeTimeout, "Timeout waiting for the file transfer") + } + if sess.stalled(fileTransferStallTimeout) { + return errf(CodeTimeout, + "File transfer stalled: no data received for %s (the remote path is probably missing or inaccessible; verify it with list_files first)", + fileTransferStallTimeout) + } + select { + case <-time.After(fileTransferPollInterval): + case <-ctx.Done(): + } + } +} + +// driveListPosix reports whether the handshake came from a POSIX host: it +// advertises its single root as the '/' drive. +func driveListPosix(drives []driveEntry) bool { + for _, d := range drives { + if d.Drive == "/" { + return true + } + } + return false +} + +// hasDotDotSegment reports whether any '/'-separated segment is exactly "..". +func hasDotDotSegment(p string) bool { + for _, seg := range strings.Split(p, "/") { + if seg == ".." { + return true + } + } + return false +} + +// normalizeEncoding validates the requested encoding before the download so a +// typo does not cost a full transfer, and canonicalises the name. +func normalizeEncoding(requested string) (string, *rpcError) { + switch strings.ToLower(strings.TrimSpace(requested)) { + case "", "auto": + return "auto", nil + case "utf-8", "utf8": + return "utf-8", nil + case "gbk": + return "gbk", nil + case "gb18030": + return "gb18030", nil + case "latin1", "iso-8859-1": + return "latin1", nil + default: + return "", errf(CodeInvalidParams, + "Unsupported encoding: %s (use auto / utf-8 / gbk / gb18030 / latin1)", requested) + } +} + +// ----- text windowing -------------------------------------------------------- + +// buildReadFileResult decodes, normalises and windows the downloaded bytes and +// assembles the MCP envelope the published outputSchema describes. +func buildReadFileResult(remotePath string, rawBytes []byte, sha256hex, requested string, + offset, limit, maxBytes int) toolResult { + text, encoding := decodeFileText(rawBytes, requested) + eol := detectEOL(text) + normalized := normalizeEOL(text) + lines := splitTextLines(normalized) + total := len(lines) + + start := offset + if start < 0 { + start = total + start + } + start = max(start, 0) + start = min(start, total) + + var textOut strings.Builder + count := 0 + capHit := false + for i := start; i < total && count < limit; i++ { + ln := lines[i] + add := len(ln) + if textOut.Len() > 0 { + add++ + } + if textOut.Len()+add > maxBytes { + if textOut.Len() == 0 { + // A single line longer than the budget: cut hard but back off to + // a UTF-8 character boundary so the JSON stays valid. + cut := maxBytes + for cut > 0 && cut < len(ln) && ln[cut]&0xC0 == 0x80 { + cut-- + } + textOut.WriteString(ln[:cut]) + count = 1 + } + capHit = true + break + } + if textOut.Len() > 0 { + textOut.WriteByte('\n') + } + textOut.WriteString(ln) + count++ + } + window := textOut.String() + linesReturned := len(splitTextLines(window)) + truncated := capHit || start+count < total + + head := "[read_file] " + remotePath + " | " + strconv.Itoa(len(rawBytes)) + " bytes | " + + encoding + " | " + eol + " | lines " + strconv.Itoa(start) + "-" + strconv.Itoa(start+count) + + " of " + strconv.Itoa(total) + " | truncated=" + boolString(truncated) + " | sha256=" + sha256hex + + return newToolResult(head+"\n"+window, readFileOutput{ + Path: remotePath, + Size: len(rawBytes), + Encoding: encoding, + EOL: eol, + LinesTotal: total, + Offset: int64(start), + LinesReturned: linesReturned, + Truncated: truncated, + Text: window, + SHA256: sha256hex, + }) +} + +// decodeFileText applies the same precedence as the Win32 server: BOM → valid +// UTF-8 → GBK for "auto"; explicit encodings bypass detection. +func decodeFileText(raw []byte, requested string) (string, string) { + switch requested { + case "utf-8": + return string(stripUTF8BOM(raw)), "utf-8" + case "gbk": + return decodeGBKText(raw), "gbk" + case "gb18030": + return decodeGB18030Text(raw), "gb18030" + case "latin1": + return latin1ToUTF8(raw), "latin1" + } + if len(raw) >= 3 && raw[0] == 0xEF && raw[1] == 0xBB && raw[2] == 0xBF { + return string(raw[3:]), "utf-8" + } + if len(raw) >= 2 && raw[0] == 0xFF && raw[1] == 0xFE { + return utf16ToUTF8(raw[2:], binary.LittleEndian), "utf-16le" + } + if len(raw) >= 2 && raw[0] == 0xFE && raw[1] == 0xFF { + return utf16ToUTF8(raw[2:], binary.BigEndian), "utf-16be" + } + if utf8.Valid(raw) { + return string(raw), "utf-8" + } + return decodeGBKText(raw), "gbk" +} + +func stripUTF8BOM(raw []byte) []byte { + if len(raw) >= 3 && raw[0] == 0xEF && raw[1] == 0xBB && raw[2] == 0xBF { + return raw[3:] + } + return raw +} + +func utf16ToUTF8(raw []byte, order binary.ByteOrder) string { + units := make([]uint16, len(raw)/2) + for i := range units { + units[i] = order.Uint16(raw[2*i:]) + } + return string(utf16.Decode(units)) +} + +// decodeGBKText decodes CP936 without the NUL/printable filtering +// GbkToUTF8 applies — file content must survive byte for byte. +func decodeGBKText(raw []byte) string { + out, err := simplifiedchinese.GBK.NewDecoder().Bytes(raw) + if err != nil { + return string(raw) // not GBK after all; the UTF-8 fallback matches GbkToUTF8 + } + return string(out) +} + +func decodeGB18030Text(raw []byte) string { + out, err := simplifiedchinese.GB18030.NewDecoder().Bytes(raw) + if err != nil { + return string(raw) + } + return string(out) +} + +func latin1ToUTF8(raw []byte) string { + var b strings.Builder + b.Grow(len(raw) * 2) + for _, c := range raw { + if c < 0x80 { + b.WriteByte(c) + } else { + b.WriteRune(rune(c)) + } + } + return b.String() +} + +// detectEOL classifies the line endings: crlf / lf / cr / mixed / none. +func detectEOL(t string) string { + var crlf, lf, cr int + for i := 0; i < len(t); i++ { + switch t[i] { + case '\r': + if i+1 < len(t) && t[i+1] == '\n' { + crlf++ + i++ + } else { + cr++ + } + case '\n': + lf++ + } + } + switch { + case crlf+lf+cr == 0: + return "none" + case crlf > 0 && lf == 0 && cr == 0: + return "crlf" + case lf > 0 && crlf == 0 && cr == 0: + return "lf" + case cr > 0 && crlf == 0 && lf == 0: + return "cr" + default: + return "mixed" + } +} + +// normalizeEOL converts CRLF and bare CR to '\n' so line numbers and the +// returned text agree. +func normalizeEOL(t string) string { + if !strings.ContainsRune(t, '\r') { + return t + } + var b strings.Builder + b.Grow(len(t)) + for i := 0; i < len(t); i++ { + if t[i] == '\r' { + if i+1 < len(t) && t[i+1] == '\n' { + i++ + } + b.WriteByte('\n') + continue + } + b.WriteByte(t[i]) + } + return b.String() +} + +// splitTextLines splits on '\n'; a trailing newline does not add an empty line +// (same as Get-Content / readlines). +func splitTextLines(t string) []string { + if t == "" { + return nil + } + var out []string + for start := 0; start < len(t); { + i := strings.IndexByte(t[start:], '\n') + if i < 0 { + out = append(out, t[start:]) + break + } + out = append(out, t[start:start+i]) + start += i + 1 + } + return out +} + +func boolString(b bool) string { + if b { + return "true" + } + return "false" +} diff --git a/server/go/mcp/filev2_test.go b/server/go/mcp/filev2_test.go new file mode 100644 index 0000000..21f0372 --- /dev/null +++ b/server/go/mcp/filev2_test.go @@ -0,0 +1,627 @@ +package mcp + +import ( + "context" + "crypto/sha256" + "encoding/binary" + "encoding/hex" + "os" + "strings" + "testing" + "time" + "unicode/utf8" + + "github.com/yuanyuanxiang/SimpleRemoter/server/go/connection" + "github.com/yuanyuanxiang/SimpleRemoter/server/go/hub" + "github.com/yuanyuanxiang/SimpleRemoter/server/go/protocol" +) + +// shortenTransferTimeouts makes the read_file watchdogs test-sized. The poll +// interval stays well under the stall window so the stalled check is reached +// deterministically. +func shortenTransferTimeouts(t *testing.T, stall, total, poll time.Duration) { + t.Helper() + oldStall, oldTotal, oldPoll := fileTransferStallTimeout, fileTransferTotalTimeout, fileTransferPollInterval + fileTransferStallTimeout, fileTransferTotalTimeout, fileTransferPollInterval = stall, total, poll + t.Cleanup(func() { + fileTransferStallTimeout, fileTransferTotalTimeout, fileTransferPollInterval = oldStall, oldTotal, oldPoll + }) +} + +// ----- wire builders --------------------------------------------------------- + +func chunkPacket(transferID uint64, fileIndex, totalFiles uint32, fileSize, offset uint64, + flags uint16, name string, payload []byte) []byte { + nameGBK := protocol.UTF8ToGBK(name) + buf := make([]byte, fileChunkHeaderLen+len(nameGBK)+len(payload)) + buf[0] = protocol.CommandSendFileV2 + binary.LittleEndian.PutUint64(buf[fileChunkOffTransferID:], transferID) + binary.LittleEndian.PutUint32(buf[fileChunkOffFileIndex:], fileIndex) + binary.LittleEndian.PutUint32(buf[fileChunkOffTotalFiles:], totalFiles) + binary.LittleEndian.PutUint64(buf[fileChunkOffFileSize:], fileSize) + binary.LittleEndian.PutUint64(buf[fileChunkOffOffset:], offset) + binary.LittleEndian.PutUint64(buf[fileChunkOffDataLength:], uint64(len(payload))) + binary.LittleEndian.PutUint64(buf[fileChunkOffNameLength:], uint64(len(nameGBK))) + binary.LittleEndian.PutUint16(buf[fileChunkOffFlags:], flags) + copy(buf[fileChunkHeaderLen:], nameGBK) + copy(buf[fileChunkHeaderLen+len(nameGBK):], payload) + return buf +} + +func completePacket(transferID uint64, fileIndex uint32, fileSize uint64, sum []byte) []byte { + buf := make([]byte, fileCompleteHeaderLen) + buf[0] = protocol.CommandFileCompleteV2 + binary.LittleEndian.PutUint64(buf[fileCompleteOffTransferID:], transferID) + binary.LittleEndian.PutUint32(buf[fileCompleteOffFileIndex:], fileIndex) + binary.LittleEndian.PutUint64(buf[fileCompleteOffFileSize:], fileSize) + copy(buf[fileCompleteOffSHA256:], sum) + return buf +} + +func downloadErrorPacket(win32err uint32) []byte { + buf := make([]byte, 5) + buf[0] = protocol.TokenDownloadError + binary.LittleEndian.PutUint32(buf[1:], win32err) + return buf +} + +// ----- harness --------------------------------------------------------------- + +func (r *fileRig) callRead(args string) <-chan fileCall { + ch := make(chan fileCall, 1) + go func() { + res, rerr := r.s.callReadFile(context.Background(), []byte(args)) + if rerr != nil { + ch <- fileCall{err: rerr} + return + } + tr, ok := res.(toolResult) + if !ok { + ch <- fileCall{err: errf(CodeInternal, "unexpected result type %T", res)} + return + } + ch <- fileCall{res: tr} + }() + return ch +} + +// startReadCall runs one read_file call through the drive-list handshake and +// returns the waiting call, the file-manager sub-conn, and the target directory +// the server asked the client to write under (which is what real clients echo +// back as the packet name prefix). +func (r *fileRig) startReadCall(args string, drives ...driveRec) (<-chan fileCall, *connection.Context, string) { + r.t.Helper() + ch := r.callRead(args) + if got := r.next(r.main); len(got) != 1 || got[0] != protocol.CommandListDrive { + r.t.Fatalf("drive request wire = %v, want [%d]", got, protocol.CommandListDrive) + } + sub := r.newSub() + if drives == nil { + drives = []driveRec{{'C', 3, 500000, 123456, "本地磁盘", "NTFS"}} + } + if !r.s.OnFilePacket(sub, driveListPacket(drives...)) { + r.t.Fatal("drive-list packet was not consumed") + } + got := r.next(sub) + if got[0] != protocol.CmdDownFilesV2 { + r.t.Fatalf("download request wire = %v, want token %d first", got, protocol.CmdDownFilesV2) + } + rest := got[1:] + i := strings.IndexByte(string(rest), 0) + if i < 0 { + r.t.Fatal("download request has no NUL after targetDir") + } + targetDir := string(rest[:i]) + if !strings.HasSuffix(targetDir, string(os.PathSeparator)) { + r.t.Fatalf("targetDir %q does not end with a path separator", targetDir) + } + return ch, sub, targetDir +} + +// readOutput pulls the structured output out of a successful call. +func readOutput(t *testing.T, c fileCall) readFileOutput { + t.Helper() + if c.err != nil { + t.Fatalf("read_file failed: %d %s", c.err.Code, c.err.Message) + } + out, ok := c.res.StructuredContent.(readFileOutput) + if !ok { + t.Fatalf("structured content type = %T", c.res.StructuredContent) + } + return out +} + +// ----- name validation ------------------------------------------------------- + +func TestTransferNameWithinDir(t *testing.T) { + const sep = `\` + target := "C:" + sep + "Users" + sep + "tmp" + sep + "yama-mcp-read-1" + sep + cases := []struct { + name string + want string // "" means rejected + }{ + {target + "file.txt", "file.txt"}, + {target + `sub\file.txt`, "sub/file.txt"}, + {target + "a//b.txt", "a/b.txt"}, + {"../x", ""}, + {`..\..\x`, ""}, + {`C:\Windows\x`, ""}, + {"/etc/passwd", ""}, + {`\\server\share\x`, ""}, + {`a/../../b`, ""}, + {target + `sub\..\..\..\x`, ""}, + {target, ""}, + {"", ""}, + {"x\x00y", ""}, + {"x\ny", ""}, + } + for _, c := range cases { + got, ok := transferNameWithinDir(target, c.name) + if c.want == "" { + if ok { + t.Errorf("transferNameWithinDir(%q) = %q, want rejection", c.name, got) + } + continue + } + if !ok || got != c.want { + t.Errorf("transferNameWithinDir(%q) = %q,%v, want %q", c.name, got, ok, c.want) + } + } +} + +// ----- end-to-end transfers -------------------------------------------------- + +func TestReadFileWindowsStreamSubConn(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, fm, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\cfg\\app.ini"}`) + + content := []byte("line1\r\nline2\r\nline3\r\n") + split := 8 + stream := rig.newSub() // Windows clients open a fresh authenticated sub-conn + name := targetDir + "app.ini" + rig.s.OnTransferPacket(stream, chunkPacket(42, 0, 1, uint64(len(content)), 0, 0, name, content[:split])) + rig.s.OnTransferPacket(stream, chunkPacket(42, 0, 1, uint64(len(content)), uint64(split), ffV2LastChunk, name, content[split:])) + sum := sha256.Sum256(content) + rig.s.OnTransferPacket(stream, completePacket(42, 0, uint64(len(content)), sum[:])) + + out := readOutput(t, rig.wait(ch)) + if out.Size != len(content) || out.Text != "line1\nline2\nline3" { + t.Fatalf("text = %q size=%d", out.Text, out.Size) + } + if out.Encoding != "utf-8" || out.EOL != "crlf" { + t.Fatalf("encoding=%q eol=%q", out.Encoding, out.EOL) + } + if out.LinesTotal != 3 || out.LinesReturned != 3 || out.Truncated { + t.Fatalf("lines_total=%d lines_returned=%d truncated=%v", out.LinesTotal, out.LinesReturned, out.Truncated) + } + if want := hex.EncodeToString(sum[:]); out.SHA256 != want { + t.Fatalf("sha256 = %q, want %q", out.SHA256, want) + } + if !strings.Contains(out.Path, "app.ini") { + t.Fatalf("path = %q", out.Path) + } + // The file-manager sub-conn is retired once the call returns. + waitClosed(t, fm) +} + +func TestReadFilePosixChunksOnFileManagerConn(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, fm, targetDir := rig.startReadCall(`{"id":"1001","path":"/etc/app.conf"}`, + driveRec{'/', 4, 100000, 1000, "Linux", "ext4"}) + + content := []byte("a=1\nb=2\n") + name := targetDir + "app.conf" + rig.s.OnTransferPacket(fm, chunkPacket(7, 0, 1, uint64(len(content)), 0, ffV2LastChunk, name, content)) + sum := sha256.Sum256(content) + rig.s.OnTransferPacket(fm, completePacket(7, 0, uint64(len(content)), sum[:])) + + out := readOutput(t, rig.wait(ch)) + if out.Text != "a=1\nb=2" || out.EOL != "lf" { + t.Fatalf("text=%q eol=%q", out.Text, out.EOL) + } +} + +func TestReadFilePosixRejectsWindowsPath(t *testing.T) { + rig := newFileRig(t) + ch := rig.callRead(`{"id":"1001","path":"C:\\cfg\\app.ini"}`) + if got := rig.next(rig.main); len(got) != 1 || got[0] != protocol.CommandListDrive { + t.Fatalf("drive request wire = %v", got) + } + fm := rig.newSub() + if !rig.s.OnFilePacket(fm, driveListPacket(driveRec{'/', 4, 100000, 1000, "Linux", "ext4"})) { + t.Fatal("drive-list packet was not consumed") + } + // The path is checked before anything goes out on the file-manager conn. + c := rig.wait(ch) + if c.err == nil || c.err.Code != CodeInvalidParams { + t.Fatalf("error = %+v, want -32602", c.err) + } + if !strings.Contains(c.err.Message, "absolute POSIX path") { + t.Fatalf("message = %q", c.err.Message) + } + waitClosed(t, fm) +} + +func TestReadFileSizeGate(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + rig.s.cfg.ReadMax = 4096 + ch, _, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\big.bin"}`) + + stream := rig.newSub() + rig.s.OnTransferPacket(stream, chunkPacket(1, 0, 1, 5000, 0, 0, targetDir+"big.bin", make([]byte, 100))) + c := rig.wait(ch) + if c.err == nil || c.err.Code != CodeInvalidParams { + t.Fatalf("error = %+v, want -32602", c.err) + } + if !strings.Contains(c.err.Message, "download_file") { + t.Fatalf("message = %q", c.err.Message) + } +} + +func TestReadFileDirectoryRejected(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, _, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\folder"}`) + + stream := rig.newSub() + rig.s.OnTransferPacket(stream, chunkPacket(1, 0, 1, 0, 0, ffV2Directory, targetDir+"folder\\", nil)) + c := rig.wait(ch) + if c.err == nil || c.err.Code != CodeInvalidParams { + t.Fatalf("error = %+v, want -32602", c.err) + } + if !strings.Contains(c.err.Message, "Not a single regular file") { + t.Fatalf("message = %q", c.err.Message) + } +} + +// TestReadFileMultiFileBatchRejected pins the expanded-directory case: the +// client turns a directory path into a multi-file batch and stamps totalFiles on +// every chunk, so the very first chunk must fail the call. Waiting for a second +// fileIndex is not enough — file 0's completion packet would end the call and its +// content would be returned as if it were the requested path. +func TestReadFileMultiFileBatchRejected(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, _, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\dir"}`) + + stream := rig.newSub() + rig.s.OnTransferPacket(stream, chunkPacket(5, 0, 2, 3, 0, ffV2LastChunk, targetDir+`dir\a.txt`, []byte("aaa"))) + sum := sha256.Sum256([]byte("aaa")) + // Even a matching completion must not rescue a multi-file batch. + rig.s.OnTransferPacket(stream, completePacket(5, 0, 3, sum[:])) + + c := rig.wait(ch) + if c.err == nil || c.err.Code != CodeInvalidParams { + t.Fatalf("error = %+v, want -32602", c.err) + } + if !strings.Contains(c.err.Message, "single regular file") { + t.Fatalf("message = %q", c.err.Message) + } +} + +func TestReadFileSecondFileIndexRejected(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, _, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\dir"}`) + + stream := rig.newSub() + rig.s.OnTransferPacket(stream, chunkPacket(9, 0, 2, 3, 0, 0, targetDir+"a.txt", []byte("abc"))) + rig.s.OnTransferPacket(stream, chunkPacket(9, 1, 2, 3, 0, 0, targetDir+"b.txt", []byte("xyz"))) + c := rig.wait(ch) + if c.err == nil || c.err.Code != CodeInvalidParams { + t.Fatalf("error = %+v, want -32602", c.err) + } +} + +func TestReadFileSHA256Mismatch(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, _, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\a.txt"}`) + + content := []byte("hello") + stream := rig.newSub() + rig.s.OnTransferPacket(stream, chunkPacket(3, 0, 1, 5, 0, ffV2LastChunk, targetDir+"a.txt", content)) + bad := make([]byte, sha256.Size) + rig.s.OnTransferPacket(stream, completePacket(3, 0, 5, bad)) + c := rig.wait(ch) + if c.err == nil || c.err.Code != CodeInternal { + t.Fatalf("error = %+v, want -32000", c.err) + } + if !strings.Contains(c.err.Message, "SHA-256") { + t.Fatalf("message = %q", c.err.Message) + } +} + +func TestReadFileEmptyFile(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, _, _ := rig.startReadCall(`{"id":"1001","path":"C:\\empty.txt"}`) + + // Windows clients send no chunk for an empty file, only the completion. + stream := rig.newSub() + sum := sha256.Sum256(nil) + rig.s.OnTransferPacket(stream, completePacket(5, 0, 0, sum[:])) + + out := readOutput(t, rig.wait(ch)) + if out.Size != 0 || out.Text != "" || out.LinesTotal != 0 || out.LinesReturned != 0 || out.Truncated { + t.Fatalf("empty file output = %+v", out) + } + if out.EOL != "none" { + t.Fatalf("eol = %q, want none", out.EOL) + } + if want := hex.EncodeToString(sum[:]); out.SHA256 != want { + t.Fatalf("sha256 = %q, want %q", out.SHA256, want) + } +} + +func TestReadFileDropsForeignTransferIDChunks(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, _, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\a.txt"}`) + + content := []byte("payload") + name := targetDir + "a.txt" + stream := rig.newSub() + rig.s.OnTransferPacket(stream, chunkPacket(100, 0, 1, uint64(len(content)), 0, 0, name, content[:3])) + // A late chunk from another (older) transfer must not disturb this one. + rig.s.OnTransferPacket(stream, chunkPacket(99, 0, 1, 999, 0, 0, `C:\stale`, []byte("zzz"))) + rig.s.OnTransferPacket(stream, chunkPacket(100, 0, 1, uint64(len(content)), 3, ffV2LastChunk, name, content[3:])) + sum := sha256.Sum256(content) + rig.s.OnTransferPacket(stream, completePacket(100, 0, uint64(len(content)), sum[:])) + + out := readOutput(t, rig.wait(ch)) + if out.Text != string(content) { + t.Fatalf("text = %q", out.Text) + } +} + +// TestReadFileIgnoresStaleStreamFromEarlierCall covers the live race that +// surfaced as "Path traversal detected": once the server rejects a transfer (the +// size gate is the usual reason) the client keeps streaming the rest of its +// batch on the sub-connection it opened for that earlier call. Those late bytes +// must be dropped, not adopted as the new call's stream — adopting them failed +// the fresh call with a traversal error, and the stale connection closing would +// have failed it as "connection lost" too. +func TestReadFileIgnoresStaleStreamFromEarlierCall(t *testing.T) { + shortenTransferTimeouts(t, 500*time.Millisecond, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + + stale := rig.newSub() // the rejected earlier call's data sub-conn + ch, _, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\b.txt"}`) + + content := []byte("good") + name := targetDir + "b.txt" + rig.s.OnTransferPacket(stale, chunkPacket(7, 0, 1, 3<<20, 0, 0, + `C:\Users\tmp\yama-mcp-read-old\big.bin`, []byte("zz"))) + empty := sha256.Sum256(nil) + rig.s.OnTransferPacket(stale, completePacket(7, 0, 0, empty[:])) + + stream := rig.newSub() // opened after the download request, as Windows does + rig.s.OnTransferPacket(stream, chunkPacket(42, 0, 1, uint64(len(content)), 0, ffV2LastChunk, name, content)) + sum := sha256.Sum256(content) + rig.s.OnTransferPacket(stream, completePacket(42, 0, uint64(len(content)), sum[:])) + + out := readOutput(t, rig.wait(ch)) + if out.Text != string(content) { + t.Fatalf("text = %q, want %q", out.Text, content) + } + if out.SHA256 != hex.EncodeToString(sum[:]) { + t.Fatalf("sha256 = %s, want %s", out.SHA256, hex.EncodeToString(sum[:])) + } +} + +// TestReadFileClosesStreamSubConn checks that the call hangs up the client's +// data sub-connection when it ends, so a batch the server stopped reading does +// not keep streaming in the background. +func TestReadFileClosesStreamSubConn(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, _, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\c.txt"}`) + + content := []byte("bye") + stream := rig.newSub() + rig.s.OnTransferPacket(stream, chunkPacket(1, 0, 1, uint64(len(content)), 0, ffV2LastChunk, + targetDir+"c.txt", content)) + sum := sha256.Sum256(content) + rig.s.OnTransferPacket(stream, completePacket(1, 0, uint64(len(content)), sum[:])) + readOutput(t, rig.wait(ch)) + + if !stream.IsClosed() { + t.Fatal("data sub-conn is still open after the call returned") + } +} + +func TestReadFileDownloadErrorMapping(t *testing.T) { + cases := []struct { + win32 int + code int + frag string + }{ + {2, CodeNotFound, "does not exist"}, + {3, CodeNotFound, "does not exist"}, + {5, CodePermission, "Access denied"}, + {87, CodeInternal, "could not queue"}, + } + for _, c := range cases { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, fm, _ := rig.startReadCall(`{"id":"1001","path":"C:\\missing.txt"}`) + rig.s.OnTransferPacket(fm, downloadErrorPacket(uint32(c.win32))) + got := rig.wait(ch) + if got.err == nil || got.err.Code != c.code { + t.Fatalf("win32=%d error = %+v, want code %d", c.win32, got.err, c.code) + } + if !strings.Contains(got.err.Message, c.frag) { + t.Fatalf("win32=%d message = %q, want %q", c.win32, got.err.Message, c.frag) + } + } +} + +func TestReadFileOutOfOrderChunkRejected(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, _, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\a.txt"}`) + + stream := rig.newSub() + rig.s.OnTransferPacket(stream, chunkPacket(1, 0, 1, 10, 4, 0, targetDir+"a.txt", []byte("4567"))) + c := rig.wait(ch) + if c.err == nil || c.err.Code != CodeInternal { + t.Fatalf("error = %+v, want -32000", c.err) + } + if !strings.Contains(c.err.Message, "offset") { + t.Fatalf("message = %q", c.err.Message) + } +} + +func TestReadFileTraversalChunkRejected(t *testing.T) { + shortenTransferTimeouts(t, time.Second, 5*time.Second, 5*time.Millisecond) + rig := newFileRig(t) + ch, _, _ := rig.startReadCall(`{"id":"1001","path":"C:\\a.txt"}`) + + stream := rig.newSub() + rig.s.OnTransferPacket(stream, chunkPacket(1, 0, 1, 5, 0, ffV2LastChunk, `..\..\evil`, []byte("hello"))) + c := rig.wait(ch) + if c.err == nil || c.err.Code != CodeInternal { + t.Fatalf("error = %+v, want -32000", c.err) + } + if !strings.Contains(c.err.Message, "Path traversal") { + t.Fatalf("message = %q", c.err.Message) + } +} + +func TestReadFileStallTimesOut(t *testing.T) { + shortenTransferTimeouts(t, 150*time.Millisecond, 5*time.Second, 10*time.Millisecond) + rig := newFileRig(t) + ch, _, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\a.txt"}`) + + stream := rig.newSub() + rig.s.OnTransferPacket(stream, chunkPacket(1, 0, 1, 10, 0, 0, targetDir+"a.txt", []byte("12345"))) + c := rig.wait(ch) + if c.err == nil || c.err.Code != CodeTimeout { + t.Fatalf("error = %+v, want -32001", c.err) + } + if !strings.Contains(c.err.Message, "stalled") { + t.Fatalf("message = %q", c.err.Message) + } +} + +func TestReadFileStreamClosedFailsFast(t *testing.T) { + shortenTransferTimeouts(t, 10*time.Second, 20*time.Second, 10*time.Millisecond) + rig := newFileRig(t) + ch, _, targetDir := rig.startReadCall(`{"id":"1001","path":"C:\\a.txt"}`) + + stream := rig.newSub() + rig.s.OnTransferPacket(stream, chunkPacket(1, 0, 1, 10, 0, 0, targetDir+"a.txt", []byte("12345"))) + _ = stream.Close() + c := rig.wait(ch) + if c.err == nil || c.err.Code != CodeTimeout { + t.Fatalf("error = %+v, want -32001", c.err) + } + if !strings.Contains(c.err.Message, "Connection lost") { + t.Fatalf("message = %q", c.err.Message) + } +} + +func TestReadFileRequiresV2Capability(t *testing.T) { + rig := newFileRig(t) + rig.h.Register(&hub.Device{ID: "1001", PeerIP: "10.0.0.1"}, rig.main) // no capability + ch := rig.callRead(`{"id":"1001","path":"C:\\a.txt"}`) + c := rig.wait(ch) + if c.err == nil || c.err.Code != CodeUnsupported { + t.Fatalf("error = %+v, want -32005", c.err) + } + rig.expectNothing() +} + +// ----- text decoding and windowing ------------------------------------------ + +func TestBuildReadFileResultGBK(t *testing.T) { + rawGBK := protocol.UTF8ToGBK("中文\r\nabc") + res := buildReadFileResult("/tmp/x.conf", rawGBK, "abc", "auto", 0, 2000, 262144) + out := res.StructuredContent.(readFileOutput) + if out.Encoding != "gbk" || out.EOL != "crlf" { + t.Fatalf("encoding=%q eol=%q", out.Encoding, out.EOL) + } + if out.Text != "中文\nabc" || out.LinesTotal != 2 { + t.Fatalf("text=%q lines_total=%d", out.Text, out.LinesTotal) + } + if !strings.Contains(res.Content[0].Text, "[read_file] /tmp/x.conf") { + t.Fatalf("head line = %q", res.Content[0].Text) + } +} + +func TestBuildReadFileResultNegativeOffset(t *testing.T) { + res := buildReadFileResult("x", []byte("a\nb\nc\n"), "s", "auto", -2, 2000, 262144) + out := res.StructuredContent.(readFileOutput) + if out.Offset != 1 || out.Text != "b\nc" || out.LinesReturned != 2 || out.Truncated { + t.Fatalf("output = %+v", out) + } +} + +func TestBuildReadFileResultLimit(t *testing.T) { + res := buildReadFileResult("x", []byte("a\nb\nc\n"), "s", "auto", 0, 1, 262144) + out := res.StructuredContent.(readFileOutput) + if out.Text != "a" || out.LinesReturned != 1 || !out.Truncated { + t.Fatalf("output = %+v", out) + } +} + +func TestBuildReadFileResultMaxBytesCutOnCharBoundary(t *testing.T) { + line := strings.Repeat("中", 400) // 1200 UTF-8 bytes, one line + res := buildReadFileResult("x", []byte(line), "s", "auto", 0, 2000, 1024) + out := res.StructuredContent.(readFileOutput) + if !out.Truncated || out.LinesReturned != 1 { + t.Fatalf("truncated=%v lines_returned=%d", out.Truncated, out.LinesReturned) + } + if len(out.Text) != 1023 || !utf8.ValidString(out.Text) { + t.Fatalf("cut text is %d bytes and valid=%v, want 1023 valid bytes", len(out.Text), utf8.ValidString(out.Text)) + } +} + +func TestBuildReadFileResultEncodings(t *testing.T) { + // UTF-16LE with BOM. + le := []byte{0xFF, 0xFE, 'h', 0, 'i', 0} + if out := buildReadFileResult("x", le, "s", "auto", 0, 10, 1024).StructuredContent.(readFileOutput); out.Encoding != "utf-16le" || out.Text != "hi" { + t.Fatalf("utf-16le output = %+v", out) + } + // UTF-8 BOM is stripped. + bom := append([]byte{0xEF, 0xBB, 0xBF}, []byte("hi")...) + if out := buildReadFileResult("x", bom, "s", "auto", 0, 10, 1024).StructuredContent.(readFileOutput); out.Encoding != "utf-8" || out.Text != "hi" { + t.Fatalf("utf-8 bom output = %+v", out) + } + // Latin-1 is explicit only (byte 0xE9 would otherwise be invalid UTF-8 → GBK). + if out := buildReadFileResult("x", []byte{0xE9}, "s", "latin1", 0, 10, 1024).StructuredContent.(readFileOutput); out.Encoding != "latin1" || out.Text != "é" { + t.Fatalf("latin1 output = %+v", out) + } +} + +func TestDetectEOLVariants(t *testing.T) { + cases := map[string]string{ + "": "none", + "abc": "none", + "a\r\nb": "crlf", + "a\nb": "lf", + "a\rb": "cr", + "a\r\nb\n": "mixed", + } + for in, want := range cases { + if got := detectEOL(in); got != want { + t.Errorf("detectEOL(%q) = %q, want %q", in, got, want) + } + } +} + +func TestSplitTextLines(t *testing.T) { + if got := splitTextLines(""); got != nil { + t.Fatalf("empty text = %v", got) + } + if got := splitTextLines("a\nb\n"); len(got) != 2 || got[1] != "b" { + t.Fatalf("a\\nb\\n = %v", got) + } + if got := splitTextLines("\n"); len(got) != 1 || got[0] != "" { + t.Fatalf("\\n = %v", got) + } +} diff --git a/server/go/mcp/server.go b/server/go/mcp/server.go index 3ed98be..b8079c6 100644 --- a/server/go/mcp/server.go +++ b/server/go/mcp/server.go @@ -73,11 +73,12 @@ type Server struct { // locks serialises device operations; sem bounds the global in-flight // tools/call count. Both are non-blocking: contention is -32003, not a // queue, so a slow call cannot pile up requests behind it (design §9). - locks *deviceLocks - sem chan struct{} - sessions *sessionTable - previews *previewTable // in-flight get_screenshot requests, keyed by device - files *fileTable // in-flight list_files requests, keyed by device + locks *deviceLocks + sem chan struct{} + sessions *sessionTable + previews *previewTable // in-flight get_screenshot requests, keyed by device + files *fileTable // in-flight list_files requests, keyed by device + transfers *transferTable // in-flight read_file V2 transfers, keyed by device // stop ends the idle reaper and reaperDone reports that it returned; // closeOnce makes Close idempotent (tests call it both explicitly and from @@ -110,15 +111,16 @@ func New(cfg Config) *Server { cfg.Log = logger.New(logger.Config{Level: logger.LevelError}) } s := &Server{ - cfg: cfg, - log: cfg.Log, - tools: buildToolSet(toolsJSON), - handlers: make(map[string]toolFunc), - locks: newDeviceLocks(), - sem: make(chan struct{}, cfg.MaxConcurrency), - previews: newPreviewTable(), - files: newFileTable(), - stop: make(chan struct{}), + cfg: cfg, + log: cfg.Log, + tools: buildToolSet(toolsJSON), + handlers: make(map[string]toolFunc), + locks: newDeviceLocks(), + sem: make(chan struct{}, cfg.MaxConcurrency), + previews: newPreviewTable(), + files: newFileTable(), + transfers: newTransferTable(), + stop: make(chan struct{}), } if cfg.Hub != nil { s.sessions = newSessionTable(cfg.Hub) diff --git a/server/go/protocol/commands.go b/server/go/protocol/commands.go index 88c5630..fcc25d0 100644 --- a/server/go/protocol/commands.go +++ b/server/go/protocol/commands.go @@ -148,6 +148,9 @@ const ( CommandUninstall byte = 63 // TOKEN_UNINSTALL - uninstall the host agent (single byte) CommandTerminalRsize byte = 81 // CMD_TERMINAL_RESIZE - [cmd:1][cols:2 LE][rows:2 LE] CmdRestoreConsole byte = 82 // CMD_RESTORE_CONSOLE - RDP session "归位": switch back to the console session and restart capture + CommandSendFileV2 byte = 85 // COMMAND_SEND_FILE_V2 - one V2 file chunk (77-byte packed header + name + data) + CommandFileCompleteV2 byte = 91 // COMMAND_FILE_COMPLETE_V2 - end of one V2 file, carries its SHA-256 + CmdDownFilesV2 byte = 94 // CMD_DOWN_FILES_V2 - [cmd][targetDir\0][path\0][\0] on the file-manager sub-conn CommandScreenKeyframeReq byte = 169 // COMMAND_SCREEN_KEYFRAME_REQ - force the client to emit an IDR immediately CommandBye byte = 204 // COMMAND_BYE - disconnect CommandHeartbeat byte = 216 // CMD_HEARTBEAT_ACK @@ -168,6 +171,10 @@ const ( // directory" reply: [token:1][win32err:4 LE]. Legacy clients send nothing, // which the server can only report as a timeout. TokenFileListError byte = 171 // TOKEN_FILE_LIST_ERROR - directory listing failed + // TOKEN_DOWNLOAD_ERROR is the file-manager sub-conn's "nothing to send" + // reply to CMD_DOWN_FILES_V2: [token:1][win32err:4 LE]. The transfer sub-conn + // is not created in that case, so this is the only failure signal. + TokenDownloadError byte = 173 // TOKEN_DOWNLOAD_ERROR - download request rejected TokenTerminalStart byte = 232 // TOKEN_TERMINAL_START - modern PTY shell sub-conn open TokenTerminalClose byte = 233 // TOKEN_TERMINAL_CLOSE - shell exited / close ack TokenConnAuth byte = 246 // TOKEN_CONN_AUTH - sub-connection identity handshake