From d7accf1d30a5e93ebd809a2a96c3ee6d740b1e45 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Thu, 12 Nov 2026 22:34:17 +0100 Subject: [PATCH] Feature(Go): list_files over the file-manager sub-connection The MCP server could not browse a device's file system. It now sends COMMAND_LIST_DRIVE on the main connection, waits for the client to open its file-manager sub-connection and report drives, then reuses that same sub-connection for COMMAND_LIST_FILES with the directory path. Replies are TOKEN_DRIVE_LIST, TOKEN_FILE_LIST or TOKEN_FILE_LIST_ERROR, decoded from the same GBK the C++ client writes on every platform. An empty or missing path returns the drive list, matching the published tool description. Any other path, including /, lists one level of that directory; the Win32 server folds / into the drive list, but doing so would make a POSIX root listing unreachable. Entries carry name, isDir, size and an mtime converted from FILETIME, and the 500-entry cap matches the served contract and the Win32 parser; a truncated listing says so in the summary. The reply is paged with the usual limit/offset window. Errors are translated like the Win32 server: win32 5 is a permission error, 2 and 3 are not-found, anything else is internal. Legacy clients stay silent instead of reporting an error, which surfaces as a timeout with an explanation. The sub-connection is a resource the client allocates per call, so one deferred cancel is the single retirement point: it reports the sub-conn it saw and that connection is closed on every exit path, including a call that gives up while the drive list is already queued. Packets that arrive after the call returned are treated as orphans and retired, and only the connection that answered the drive-list phase may answer the directory request. Also add a uniqueness test over the command and token byte constants, and SendToConn so sub-connections are written through the hub's framing path instead of a raw conn write. Co-Authored-By: deepseek-flash --- server/go/cmd/main.go | 9 + server/go/hub/hub.go | 14 + server/go/mcp/filemgr.go | 578 ++++++++++++++++++++++ server/go/mcp/filemgr_test.go | 723 ++++++++++++++++++++++++++++ server/go/mcp/server.go | 3 + server/go/protocol/commands.go | 30 ++ server/go/protocol/commands_test.go | 60 ++- 7 files changed, 1416 insertions(+), 1 deletion(-) create mode 100644 server/go/mcp/filemgr.go create mode 100644 server/go/mcp/filemgr_test.go diff --git a/server/go/cmd/main.go b/server/go/cmd/main.go index da15598..52de078 100644 --- a/server/go/cmd/main.go +++ b/server/go/cmd/main.go @@ -174,6 +174,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.TokenDriveList, protocol.TokenFileList, protocol.TokenFileListError: + // list_files replies arrive on the file-manager sub-connection, whose + // ClientID is pinned by its ConnAuth handshake. MCP matches them against + // the pending request for that device and retires the sub-conn when the + // request finishes or an orphan announces itself. + if h.mcp != nil && h.mcp.OnFilePacket(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/hub/hub.go b/server/go/hub/hub.go index 76c45fa..5089b13 100644 --- a/server/go/hub/hub.go +++ b/server/go/hub/hub.go @@ -279,6 +279,20 @@ func (h *Hub) SendToDevice(id string, data []byte) error { return h.sender(d.conn, data) } +// SendToConn forwards a payload to an arbitrary connection (typically a +// sub-conn the caller learned about from an inbound packet) through the same +// framing/compression path as SendToDevice. Raw conn.Write would bypass the +// per-connection encoder and the client could not parse it. +func (h *Hub) SendToConn(ctx *connection.Context, data []byte) error { + if ctx == nil || ctx.IsClosed() { + return ErrDeviceOffline + } + if h.sender == nil { + return ErrNoSender + } + return h.sender(ctx, data) +} + // SendToScreen routes a payload to the device's currently-bound screen // sub-connection. Input events (COMMAND_SCREEN_CONTROL) MUST go through the // screen sub-conn rather than the main conn — the C++ client only dispatches diff --git a/server/go/mcp/filemgr.go b/server/go/mcp/filemgr.go new file mode 100644 index 0000000..c63274d --- /dev/null +++ b/server/go/mcp/filemgr.go @@ -0,0 +1,578 @@ +package mcp + +import ( + "context" + "encoding/binary" + "encoding/json" + "fmt" + "strings" + "sync" + "time" + + "github.com/yuanyuanxiang/SimpleRemoter/server/go/connection" + "github.com/yuanyuanxiang/SimpleRemoter/server/go/hub" + "github.com/yuanyuanxiang/SimpleRemoter/server/go/protocol" +) + +// list_files drives the client's file-manager sub-connection (design §5.3): +// +// main conn : COMMAND_LIST_DRIVE(1) +// └─ client opens a sub-conn, sends TOKEN_CONN_AUTH then +// TOKEN_DRIVE_LIST(103) — the sub-conn stays open +// that sub : COMMAND_LIST_FILES(2) [path\0] +// └─ TOKEN_FILE_LIST(104) or TOKEN_FILE_LIST_ERROR(171) +// then the server closes the sub-conn (COMMAND_BYE + 500 ms) +// +// The sub-conn carries ConnAuth, so handleConnAuth has already pinned the +// device's ClientID on its ctx and no pending-connection bookkeeping is needed +// (design §5.3); the pending request is keyed by device and remembers which +// ctx answered, which is also how late packets from a previous sub-conn are +// told apart from the current one. +const ( + // fileListMaxEntries matches the published contract: tools.json advertises + // "最多 500 条" and the Win32 server parses 500 entries, so a larger + // directory is cut here too (the summary tells the model it was cut). + fileListMaxEntries = 500 + + // fileRecordFixedLen is attr(1) + size(8) + FILETIME(8). + fileRecordFixedLen = 17 + // driveRecordFixedLen is letter(1) + type(1) + totalMB(4) + freeMB(4). + driveRecordFixedLen = 10 + + // filetimeEpochDiff converts FILETIME (100 ns ticks since 1601-01-01) to + // Unix seconds once divided by filetimeTicksPerSec. + filetimeTicksPerSec = 10000000 + filetimeEpochDiffSec = 11644473600 + errorAccessDenied = 5 + errorFileNotFound = 2 + errorPathNotFound = 3 + batchUnsupportedHint = "batch (ids) is not implemented by this server build; call list_files once per id" +) + +// fileListTimeout bounds one list_files round trip, including the sub-conn +// handshake. A variable only so tests can shorten it. +var fileListTimeout = 10 * time.Second + +// fileRetireDelay is how long the sub-conn stays open after COMMAND_BYE, so the +// client's read loop sees the goodbye before FIN (the hub uses the same 500 ms +// for screen sub-conns). +var fileRetireDelay = 500 * time.Millisecond + +// fileEventKind distinguishes the three packets a list_files call can receive. +type fileEventKind byte + +const ( + fileEventDriveList fileEventKind = iota + 1 // TOKEN_DRIVE_LIST + fileEventFileList // TOKEN_FILE_LIST + fileEventError // TOKEN_FILE_LIST_ERROR +) + +// fileEvent is one packet handed from the TCP read loop to the waiting call. +// ctx identifies the sub-conn it arrived on; payload bytes are copied because +// the read buffer is not ours to keep. +type fileEvent struct { + kind fileEventKind + ctx *connection.Context + payload []byte // drive records or file records (token byte removed) + win32err uint32 // fileEventError only +} + +// fileRequest is one in-flight list_files call. ch is buffered so the read loop +// never blocks; sub records which sub-conn answered the drive-list phase and +// rejects packets from any other connection for the same device. abandoned is +// set by cancel so a packet that loses the race with the call's own return is +// not queued into a channel nobody reads. +type fileRequest struct { + ch chan fileEvent + + mu sync.Mutex + sub *connection.Context + abandoned bool +} + +func (r *fileRequest) subConn() *connection.Context { + r.mu.Lock() + defer r.mu.Unlock() + return r.sub +} + +// fileTable holds the single pending list_files per device. The per-device +// call lock already serialises tools, so more than one entry cannot normally +// exist; the table rejects it anyway rather than mis-routing replies. +type fileTable struct { + mu sync.Mutex + byDevice map[string]*fileRequest +} + +func newFileTable() *fileTable { + return &fileTable{byDevice: make(map[string]*fileRequest)} +} + +func (t *fileTable) begin(deviceID string) (*fileRequest, bool) { + t.mu.Lock() + defer t.mu.Unlock() + if _, ok := t.byDevice[deviceID]; ok { + return nil, false + } + r := &fileRequest{ch: make(chan fileEvent, 2)} + t.byDevice[deviceID] = r + return r, true +} + +// cancel ends the request and returns the sub-conn it learned about, if any. +// The caller retires that sub-conn: this is the single retirement point, so a +// call that gives up while its drive-list packet is already queued (the timer +// and the channel being ready at the same time, or the MCP request cancelled) +// still closes the client's file-manager thread instead of leaking it. +func (t *fileTable) cancel(deviceID string, r *fileRequest) *connection.Context { + t.mu.Lock() + if t.byDevice[deviceID] == r { + delete(t.byDevice, deviceID) + } + t.mu.Unlock() + + r.mu.Lock() + r.abandoned = true + sub := r.sub + r.mu.Unlock() + return sub +} + +// deliver routes one event to the pending call. It reports whether the event +// was accepted; a packet from a connection other than the one that answered +// the drive-list phase is a late packet from an earlier sub-conn and must be +// ignored (the client sends a legacy one-byte TOKEN_FILE_LIST right after +// TOKEN_FILE_LIST_ERROR, and back-to-back calls can overlap with it). +func (t *fileTable) deliver(deviceID string, ev fileEvent) bool { + t.mu.Lock() + r := t.byDevice[deviceID] + t.mu.Unlock() + if r == nil { + return false + } + + r.mu.Lock() + if r.abandoned { + // The call already returned; let OnFilePacket treat the packet as an + // orphan (a drive list) instead of queueing it for nobody. + r.mu.Unlock() + return false + } + switch ev.kind { + case fileEventDriveList: + if r.sub != nil && r.sub != ev.ctx { + r.mu.Unlock() + return false + } + if r.sub == nil { + r.sub = ev.ctx + } + case fileEventError: + // No client answers COMMAND_LIST_DRIVE with an error, but if one ever + // does, adopt the connection and let the call translate the code + // instead of dropping the packet and timing out. + if r.sub == nil { + r.sub = ev.ctx + } + if r.sub != ev.ctx { + r.mu.Unlock() + return false + } + default: + if r.sub != ev.ctx { + r.mu.Unlock() + return false + } + } + r.mu.Unlock() + + select { + case r.ch <- ev: + default: + // The current phase already has an event queued; a duplicate is only + // noise (the legacy empty TOKEN_FILE_LIST after an error packet). + } + return true +} + +func (s *Server) registerFileTools() { + s.handlers["list_files"] = s.callListFiles +} + +// OnFilePacket feeds one file-manager packet from a sub-connection to the +// pending list_files call. It reports whether the packet belongs to this +// protocol at all; cmd/main.go forwards only the three file-manager tokens. +func (s *Server) OnFilePacket(ctx *connection.Context, data []byte) bool { + if s == nil || len(data) == 0 { + return false + } + switch data[0] { + case protocol.TokenDriveList, protocol.TokenFileList, protocol.TokenFileListError: + default: + return false + } + deviceID := ctx.GetInfo().ClientID + if deviceID == "" { + return true + } + if s.cfg.Hub != nil { + if main := s.cfg.Hub.MainConn(deviceID); main == ctx { + // File-manager packets only ever arrive on the dedicated sub-conn; + // a token that looks like one on the main connection is not ours. + return true + } + } + + ev := fileEvent{ctx: ctx} + switch data[0] { + case protocol.TokenDriveList: + ev.kind = fileEventDriveList + ev.payload = append([]byte(nil), data[1:]...) + case protocol.TokenFileList: + ev.kind = fileEventFileList + ev.payload = append([]byte(nil), data[1:]...) + case protocol.TokenFileListError: + ev.kind = fileEventError + if len(data) >= 5 { + ev.win32err = binary.LittleEndian.Uint32(data[1:5]) + } + } + + if s.files.deliver(deviceID, ev) { + return true + } + // 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 { + s.retireFileConn(ctx) + } + return true +} + +// retireFileConn closes a file-manager sub-conn the way the hub retires screen +// sub-conns: COMMAND_BYE so the client's IOCP loop exits through its clean +// path, then a delayed Close so the goodbye is not overtaken by FIN. +func (s *Server) retireFileConn(ctx *connection.Context) { + if ctx == nil { + return + } + if s.cfg.Hub != nil { + _ = s.cfg.Hub.SendToConn(ctx, []byte{protocol.CommandBye}) + } + // Read the delay before spawning: tests shorten it between calls. + delay := fileRetireDelay + go func(c *connection.Context) { + time.Sleep(delay) + _ = c.Close() + }(ctx) +} + +// ----- get_screenshot-style argument / result shapes ------------------------- + +// listFilesArgs is the published inputSchema as Go. ids is advertised by the +// Win32 server for batch reads; this server rejects it explicitly. +type listFilesArgs struct { + ID string `json:"id"` + IDs []string `json:"ids"` + Path string `json:"path"` + Limit int `json:"limit"` + Offset int `json:"offset"` +} + +type driveEntry struct { + Drive string `json:"drive"` + Type uint64 `json:"type"` + TypeName string `json:"typeName"` + FileSystem string `json:"fileSystem"` + TotalMB uint64 `json:"totalMB"` + FreeMB uint64 `json:"freeMB"` +} + +type fileEntry struct { + Name string `json:"name"` + IsDir bool `json:"isDir"` + Size uint64 `json:"size"` + Mtime int64 `json:"mtime"` +} + +// 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 { + return nil, errf(CodeInternal, "server device registry is not configured") + } + var args listFilesArgs + if rerr := decodeArgs(raw, &args); rerr != nil { + return nil, rerr + } + if len(args.IDs) > 0 { + return nil, errf(CodeUnsupported, "%s", batchUnsupportedHint) + } + 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") + } + // NUL and newlines would truncate or split the client's C string field. + if strings.ContainsAny(args.Path, "\x00\r\n") { + return nil, errf(CodeInvalidParams, "Invalid path: NUL and line breaks are not allowed") + } + if s.cfg.Hub.MainConn(args.ID) == nil { + return nil, errf(CodeOffline, "Host not found or offline: %s", args.ID) + } + + path := args.Path + // The published contract (tools.json) is: no path lists drives, any path + // lists that directory. MFC also folds "." and "/" into the drive list, + // but that makes a POSIX root listing unreachable, so this server follows + // the documented rule instead. + listDrives := path == "" + pathGBK := protocol.UTF8ToGBK(path) + if len(pathGBK) >= 250 { + // The client copies the path into a MAX_PATH buffer and appends "\\*.*". + return nil, errf(CodeInvalidParams, + "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") + } + + 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...) + pkt = append(pkt, 0) + if err := s.cfg.Hub.SendToConn(sub, pkt); err != nil { + return nil, errf(CodeSendFailed, "Failed to send the directory request on the file-manager channel: %v", err) + } + + select { + case ev := <-req.ch: + switch ev.kind { + case fileEventFileList: + files, truncated := parseFileList(ev.payload) + return listFilesResult(args, nil, files, truncated), nil + case fileEventError: + return nil, fileWin32Error(ev.win32err, path) + default: + return nil, errf(CodeInternal, "Unexpected file-manager reply while listing a directory") + } + case <-timer.C: + return nil, errf(CodeTimeout, + "Timeout waiting for the directory listing of %q (%s). If the path does not exist, an older client stays silent instead of reporting an error — list the parent directory first.", + path, fileListTimeout) + case <-ctx.Done(): + return nil, errf(CodeTimeout, "Request cancelled while waiting for the directory listing") + } +} + +// 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. +func fileWin32Error(win32err uint32, path string) *rpcError { + subject := fmt.Sprintf("Failed to list %q", path) + switch win32err { + case errorAccessDenied: + return errf(CodePermission, "%s: access denied (win32=%d)", subject, win32err) + case errorFileNotFound, errorPathNotFound: + return errf(CodeNotFound, "%s: target not found (win32=%d)", subject, win32err) + default: + return errf(CodeInternal, "%s: the host reported a failure (win32=%d)", subject, win32err) + } +} + +// parseDriveList decodes [letter][type][totalMB:4 LE][freeMB:4 LE][typeName\0][fs\0] +// records until a zero letter (the client's end marker) or a truncated record. +// Names are GBK on the wire for every platform. +func parseDriveList(p []byte) []driveEntry { + out := make([]driveEntry, 0, 8) + off := 0 + for off+driveRecordFixedLen <= len(p) && p[off] != 0 { + letter := p[off] + dtype := uint64(p[off+1]) + total := uint64(binary.LittleEndian.Uint32(p[off+2:])) + free := uint64(binary.LittleEndian.Uint32(p[off+6:])) + off += driveRecordFixedLen + + typeName, n, ok := cString(p[off:]) + if !ok { + break + } + off += n + 1 + fs, n, ok := cString(p[off:]) + if !ok { + break + } + off += n + 1 + + // Windows letters become "C:\". The POSIX clients report '/' as their + // single root "drive"; return it as "/" so it can be passed straight + // back as a path (MFC renders "/:\", which also works after the + // client's path conversion, but "/" is the friendlier form). + drive := string(letter) + ":\\" + if letter == '/' { + drive = "/" + } + out = append(out, driveEntry{ + Drive: drive, + Type: dtype, + TypeName: protocol.GbkToUTF8(typeName), + FileSystem: protocol.GbkToUTF8(fs), + TotalMB: total, + FreeMB: free, + }) + } + return out +} + +// parseFileList decodes [attr][name\0][sizeHi:4][sizeLo:4][ftLo:4][ftHi:4] +// records. An empty name is the client's trailing zero padding; reaching the +// entry cap stops the parse and reports truncation. +func parseFileList(p []byte) ([]fileEntry, bool) { + out := make([]fileEntry, 0, 64) + off := 0 + for off < len(p) { + if len(out) >= fileListMaxEntries { + return out, true + } + attr := p[off] + off++ + name, n, ok := cString(p[off:]) + if !ok || n == 0 { + break + } + off += n + 1 + if off+fileRecordFixedLen-1 > len(p) { + break + } + sizeHigh := uint64(binary.LittleEndian.Uint32(p[off:])) + sizeLow := uint64(binary.LittleEndian.Uint32(p[off+4:])) + ftLow := uint64(binary.LittleEndian.Uint32(p[off+8:])) + ftHigh := uint64(binary.LittleEndian.Uint32(p[off+12:])) + off += 16 + + ft := ftHigh<<32 | ftLow + var mtime int64 + if ft != 0 { + mtime = int64(ft/filetimeTicksPerSec) - filetimeEpochDiffSec + if mtime < 0 { + mtime = 0 + } + } + out = append(out, fileEntry{ + Name: protocol.GbkToUTF8(name), + IsDir: attr&0x10 != 0, + Size: sizeHigh<<32 | sizeLow, + Mtime: mtime, + }) + } + return out, false +} + +// cString returns the NUL-terminated field at the start of p. ok is false when +// p runs out before the terminator (a truncated packet). +func cString(p []byte) (name []byte, n int, ok bool) { + for i, b := range p { + if b == 0 { + return p[:i], i, true + } + } + return nil, 0, false +} + +// listFilesResult applies the limit/offset window and builds the MCP envelope. +// Only one of drives/files is non-nil; total/totalAll/hasMore describe the +// full listing, not the returned page. truncated reports that the directory +// held more entries than the parse cap, so the model is not told "that is all +// there is". +func listFilesResult(args listFilesArgs, drives []driveEntry, files []fileEntry, truncated bool) toolResult { + limit := clampPageLimit(args.Limit) + offset := max(args.Offset, 0) + + var structured map[string]any + var text string + if drives != nil { + total := len(drives) + begin := min(offset, total) + end := min(begin+limit, total) + structured = map[string]any{ + "drives": drives[begin:end], + "total": total, + "totalAll": total, + "limit": limit, + "offset": offset, + "hasMore": end < total, + } + text = pageSummary(total, limit, offset, "个驱动器") + } else { + total := len(files) + begin := min(offset, total) + end := min(begin+limit, total) + structured = map[string]any{ + "files": files[begin:end], + "total": total, + "totalAll": total, + "limit": limit, + "offset": offset, + "hasMore": end < total, + } + text = pageSummary(total, limit, offset, "个条目") + } + if truncated { + text += fmt.Sprintf("目录条目超过 %d 条,只解析了前 %d 条。", fileListMaxEntries, fileListMaxEntries) + } + return newToolResult(text, structured) +} diff --git a/server/go/mcp/filemgr_test.go b/server/go/mcp/filemgr_test.go new file mode 100644 index 0000000..47c45ae --- /dev/null +++ b/server/go/mcp/filemgr_test.go @@ -0,0 +1,723 @@ +package mcp + +import ( + "bytes" + "context" + "encoding/binary" + "net" + "strings" + "testing" + "time" + + "github.com/yuanyuanxiang/SimpleRemoter/server/go/connection" + "github.com/yuanyuanxiang/SimpleRemoter/server/go/hub" + "github.com/yuanyuanxiang/SimpleRemoter/server/go/protocol" +) + +// fileRig is a live MCP server plus a hub whose sender records every outbound +// write together with the connection it went to. Tests answer requests through +// OnFilePacket, exactly what cmd/main.go does from the TCP read loop. +type fileWire struct { + ctx *connection.Context + data []byte +} + +type fileRig struct { + t *testing.T + s *Server + h *hub.Hub + main *connection.Context + w chan fileWire +} + +func newFileRig(t *testing.T) *fileRig { + t.Helper() + oldTimeout, oldDelay := fileListTimeout, fileRetireDelay + fileListTimeout = 5 * time.Second + fileRetireDelay = 10 * time.Millisecond + t.Cleanup(func() { fileListTimeout, fileRetireDelay = oldTimeout, oldDelay }) + + h := hub.New() + main := &connection.Context{RemoteAddr: "10.0.0.1:4000"} + main.Info.ClientID = "1001" + main.Info.ClientType = "EXE" + rig := &fileRig{t: t, h: h, main: main, w: make(chan fileWire, 32)} + h.SetSender(func(ctx *connection.Context, data []byte) error { + select { + case rig.w <- fileWire{ctx: ctx, data: append([]byte(nil), data...)}: + default: + rig.t.Errorf("sender buffer full, dropping %v", data) + } + return nil + }) + h.Register(&hub.Device{ID: "1001", PeerIP: "10.0.0.1"}, main) + rig.s = New(Config{Token: testToken, Hub: h}) + t.Cleanup(rig.s.Close) + return rig +} + +// newSub builds a sub-connection context with the ClientID ConnAuth would pin. +// The pipe gives Close something real to close; nothing writes to it. +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"} + ctx.Info.ClientID = "1001" + return ctx +} + +func (r *fileRig) next(want *connection.Context) []byte { + r.t.Helper() + select { + case w := <-r.w: + if w.ctx != want { + r.t.Fatalf("wire went to an unexpected connection (%p, want %p): %v", w.ctx, want, w.data) + } + return w.data + case <-time.After(5 * time.Second): + r.t.Fatal("no wire was sent") + return nil + } +} + +func (r *fileRig) expectNothing() { + r.t.Helper() + select { + case w := <-r.w: + r.t.Fatalf("unexpected wire to %p: %v", w.ctx, w.data) + case <-time.After(50 * time.Millisecond): + } +} + +type fileCall struct { + res toolResult + err *rpcError +} + +func (r *fileRig) call(args string) <-chan fileCall { + ch := make(chan fileCall, 1) + go func() { + res, rerr := r.s.callListFiles(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 +} + +func (r *fileRig) wait(ch <-chan fileCall) fileCall { + r.t.Helper() + select { + case c := <-ch: + return c + case <-time.After(5 * time.Second): + r.t.Fatal("timed out waiting for the tool call") + return fileCall{} + } +} + +// startDirectoryCall runs one list_files call up to and including the +// COMMAND_LIST_FILES send, and returns the waiting call plus its sub-conn. +func (r *fileRig) startDirectoryCall(args string) (<-chan fileCall, *connection.Context) { + r.t.Helper() + ch := r.call(args) + if got := r.next(r.main); !bytes.Equal(got, []byte{protocol.CommandListDrive}) { + r.t.Fatalf("drive request wire = %v, want [%d]", got, protocol.CommandListDrive) + } + sub := r.newSub() + if !r.s.OnFilePacket(sub, driveListPacket(driveRec{'C', 3, 500000, 123456, "本地磁盘", "NTFS"})) { + r.t.Fatal("drive-list packet was not consumed") + } + if got := r.next(sub); got[0] != protocol.CommandListFiles { + r.t.Fatalf("directory request wire = %v, want token %d first", got, protocol.CommandListFiles) + } + return ch, sub +} + +func waitClosed(t *testing.T, ctx *connection.Context) { + t.Helper() + deadline := time.Now().Add(2 * time.Second) + for time.Now().Before(deadline) { + if ctx.IsClosed() { + return + } + time.Sleep(5 * time.Millisecond) + } + t.Fatal("sub-connection was not closed") +} + +// ----- wire fixtures -------------------------------------------------------- + +type driveRec struct { + letter byte + dtype byte + total uint32 + free uint32 + typeName string // UTF-8, encoded as GBK on the wire + fs string +} + +func appendU32(buf []byte, v uint32) []byte { + var b [4]byte + binary.LittleEndian.PutUint32(b[:], v) + return append(buf, b[:]...) +} + +func driveListPacket(recs ...driveRec) []byte { + buf := []byte{protocol.TokenDriveList} + for _, r := range recs { + buf = append(buf, r.letter, r.dtype) + buf = appendU32(buf, r.total) + buf = appendU32(buf, r.free) + buf = append(buf, protocol.UTF8ToGBK(r.typeName)...) + buf = append(buf, 0) + buf = append(buf, protocol.UTF8ToGBK(r.fs)...) + buf = append(buf, 0) + } + return buf +} + +type fileRec struct { + attr byte + name string // UTF-8, encoded as GBK on the wire + size uint64 + mtime int64 // Unix seconds; 0 leaves FILETIME zero +} + +func fileListPacket(recs ...fileRec) []byte { + buf := []byte{protocol.TokenFileList} + for _, r := range recs { + buf = append(buf, r.attr) + buf = append(buf, protocol.UTF8ToGBK(r.name)...) + buf = append(buf, 0) + buf = appendU32(buf, uint32(r.size>>32)) + buf = appendU32(buf, uint32(r.size)) + var ft uint64 + if r.mtime != 0 { + ft = uint64(r.mtime+filetimeEpochDiffSec) * filetimeTicksPerSec + } + buf = appendU32(buf, uint32(ft)) + buf = appendU32(buf, uint32(ft>>32)) + } + return buf +} + +func fileListErrorPacket(win32err uint32) []byte { + buf := []byte{protocol.TokenFileListError} + return appendU32(buf, win32err) +} + +// checkInt asserts an exact int field. structuredContent here is a Go map, not +// JSON that went through a float64 round trip, so intField does not apply. +func checkInt(t *testing.T, m map[string]any, key string, want int) { + t.Helper() + got, ok := m[key].(int) + if !ok { + t.Fatalf("%q = %v (%T), want int", key, m[key], m[key]) + } + if got != want { + t.Fatalf("%q = %d, want %d", key, got, want) + } +} + +// ----- argument validation -------------------------------------------------- + +func TestListFilesRejectsBadArguments(t *testing.T) { + r := newFileRig(t) + cases := []struct { + name string + args string + code int + }{ + {"batch ids", `{"ids":["1001","1002"]}`, CodeUnsupported}, + {"missing id", `{}`, CodeInvalidParams}, + {"non-decimal id", `{"id":"abc"}`, CodeInvalidParams}, + {"numeric id", `{"id":1001}`, CodeInvalidParams}, + {"NUL in path", `{"id":"1001","path":"C:\\a\u0000b"}`, CodeInvalidParams}, + {"newline in path", `{"id":"1001","path":"C:\\a\nb"}`, CodeInvalidParams}, + {"path too long", `{"id":"1001","path":"` + strings.Repeat("a", 260) + `"}`, CodeInvalidParams}, + {"offline", `{"id":"2002"}`, CodeOffline}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + _, rerr := r.s.callListFiles(context.Background(), []byte(c.args)) + if rerr == nil { + t.Fatalf("call succeeded, want error %d", c.code) + } + if rerr.Code != c.code { + t.Fatalf("error code = %d (%s), want %d", rerr.Code, rerr.Message, c.code) + } + }) + } + r.expectNothing() +} + +// ----- drive listing -------------------------------------------------------- + +func TestListFilesDrives(t *testing.T) { + r := newFileRig(t) + ch := r.call(`{"id":"1001"}`) + if got := r.next(r.main); !bytes.Equal(got, []byte{protocol.CommandListDrive}) { + t.Fatalf("drive request wire = %v", got) + } + + sub := r.newSub() + pkt := driveListPacket( + driveRec{'C', 3, 500000, 123456, "本地磁盘", "NTFS"}, + driveRec{'/', 3, 100000, 50000, "Linux", "ext4"}, + ) + if !r.s.OnFilePacket(sub, pkt) { + t.Fatal("drive-list packet was not consumed") + } + + c := r.wait(ch) + if c.err != nil { + t.Fatalf("call failed: %d %s", c.err.Code, c.err.Message) + } + sc := c.res.StructuredContent.(map[string]any) + drives := sc["drives"].([]driveEntry) + if len(drives) != 2 { + t.Fatalf("drives = %#v, want 2 entries", drives) + } + win, posix := drives[0], drives[1] + if win.Drive != `C:\` || win.TypeName != "本地磁盘" || win.FileSystem != "NTFS" || + win.TotalMB != 500000 || win.FreeMB != 123456 || win.Type != 3 { + t.Fatalf("windows drive = %#v", win) + } + if posix.Drive != "/" || posix.FileSystem != "ext4" || posix.TotalMB != 100000 { + t.Fatalf("posix drive = %#v", posix) + } + checkInt(t, sc, "total", 2) + if !strings.Contains(c.res.Content[0].Text, "共 2 个驱动器") { + t.Fatalf("summary = %q", c.res.Content[0].Text) + } + + // A drive-only listing still leaves the sub-conn to be retired. + if got := r.next(sub); !bytes.Equal(got, []byte{protocol.CommandBye}) { + t.Fatalf("retire wire = %v, want COMMAND_BYE", got) + } + waitClosed(t, sub) +} + +func TestListFilesEmptyDriveList(t *testing.T) { + r := newFileRig(t) + ch := r.call(`{"id":"1001","path":""}`) + r.next(r.main) + sub := r.newSub() + r.s.OnFilePacket(sub, []byte{protocol.TokenDriveList}) + c := r.wait(ch) + if c.err != nil { + t.Fatalf("call failed: %d %s", c.err.Code, c.err.Message) + } + sc := c.res.StructuredContent.(map[string]any) + if got := sc["drives"].([]driveEntry); len(got) != 0 { + t.Fatalf("drives = %#v, want none", got) + } + if !strings.Contains(c.res.Content[0].Text, "共 0 个驱动器") { + t.Fatalf("summary = %q", c.res.Content[0].Text) + } +} + +// ----- directory listing ---------------------------------------------------- + +func TestListFilesDirectory(t *testing.T) { + r := newFileRig(t) + ch, sub := r.startDirectoryCall(`{"id":"1001","path":"C:\\工作","limit":2,"offset":1}`) + + pkt := fileListPacket( + fileRec{attr: 0x10, name: "子目录", mtime: 1700000000}, + fileRec{attr: 0x00, name: "a.txt", size: 12345, mtime: 1700000000}, + fileRec{attr: 0x00, name: "中文文档.docx", size: 5 << 30, mtime: 1600000000}, + ) + if !r.s.OnFilePacket(sub, pkt) { + t.Fatal("file-list packet was not consumed") + } + + c := r.wait(ch) + if c.err != nil { + t.Fatalf("call failed: %d %s", c.err.Code, c.err.Message) + } + sc := c.res.StructuredContent.(map[string]any) + files := sc["files"].([]fileEntry) + if len(files) != 2 { + t.Fatalf("page = %#v, want 2 entries (offset 1, limit 2)", files) + } + // Offset 1 skips 子目录. + if files[0].Name != "a.txt" || files[0].IsDir || files[0].Size != 12345 { + t.Fatalf("first entry = %#v", files[0]) + } + if files[0].Mtime != 1700000000 { + t.Fatalf("mtime = %d, want 1700000000", files[0].Mtime) + } + if files[1].Name != "中文文档.docx" || files[1].Size != 5<<30 { + t.Fatalf("second entry = %#v", files[1]) + } + checkInt(t, sc, "total", 3) + checkInt(t, sc, "totalAll", 3) + checkInt(t, sc, "limit", 2) + checkInt(t, sc, "offset", 1) + if sc["hasMore"] != false { + t.Fatalf("hasMore = %v, want false", sc["hasMore"]) + } + if !strings.Contains(c.res.Content[0].Text, "共 3 个条目") || + !strings.Contains(c.res.Content[0].Text, "第 2-3 条") { + t.Fatalf("summary = %q", c.res.Content[0].Text) + } + + if got := r.next(sub); !bytes.Equal(got, []byte{protocol.CommandBye}) { + t.Fatalf("retire wire = %v, want COMMAND_BYE", got) + } + waitClosed(t, sub) +} + +func TestListFilesDirectoryRequestWire(t *testing.T) { + r := newFileRig(t) + ch := r.call(`{"id":"1001","path":"C:\\工作"}`) + r.next(r.main) + sub := r.newSub() + r.s.OnFilePacket(sub, driveListPacket(driveRec{'C', 3, 1, 1, "本地磁盘", "NTFS"})) + + got := r.next(sub) + want := append([]byte{protocol.CommandListFiles}, protocol.UTF8ToGBK(`C:\工作`)...) + want = append(want, 0) + if !bytes.Equal(got, want) { + t.Fatalf("directory request wire = %v, want %v", got, want) + } + r.s.OnFilePacket(sub, fileListPacket()) + c := r.wait(ch) + if c.err != nil { + t.Fatalf("call failed: %d %s", c.err.Code, c.err.Message) + } +} + +func TestListFilesRootSlashIsAPath(t *testing.T) { + // tools.json says a non-empty path lists that directory; "/" must not be + // folded into the drive list or a POSIX root listing is unreachable. + r := newFileRig(t) + ch := r.call(`{"id":"1001","path":"/"}`) + r.next(r.main) + sub := r.newSub() + r.s.OnFilePacket(sub, driveListPacket(driveRec{'/', 3, 1, 1, "Linux", "ext4"})) + if got := r.next(sub); !bytes.Equal(got, []byte{protocol.CommandListFiles, '/', 0}) { + t.Fatalf("wire = %v, want COMMAND_LIST_FILES plus \"/\" and a NUL", got) + } + r.s.OnFilePacket(sub, fileListPacket(fileRec{name: "etc"}, fileRec{name: "tmp"})) + c := r.wait(ch) + if c.err != nil { + t.Fatalf("call failed: %d %s", c.err.Code, c.err.Message) + } + sc := c.res.StructuredContent.(map[string]any) + if files := sc["files"].([]fileEntry); len(files) != 2 || files[0].Name != "etc" { + t.Fatalf("files = %#v", files) + } +} + +func TestListFilesEmptyDirectory(t *testing.T) { + r := newFileRig(t) + ch, sub := r.startDirectoryCall(`{"id":"1001","path":"C:\\empty"}`) + // The Windows client sends just the token when a directory is empty. + r.s.OnFilePacket(sub, []byte{protocol.TokenFileList}) + c := r.wait(ch) + if c.err != nil { + t.Fatalf("call failed: %d %s", c.err.Code, c.err.Message) + } + sc := c.res.StructuredContent.(map[string]any) + if got := sc["files"].([]fileEntry); len(got) != 0 { + t.Fatalf("files = %#v, want none", got) + } + if sc["hasMore"] != false { + t.Fatalf("hasMore = %v", sc["hasMore"]) + } +} + +func TestListFilesPagingHasMore(t *testing.T) { + r := newFileRig(t) + ch, sub := r.startDirectoryCall(`{"id":"1001","path":"C:\\big","limit":2}`) + var recs []fileRec + for _, name := range []string{"a", "b", "c", "d", "e"} { + recs = append(recs, fileRec{name: name, size: 1, mtime: 1700000000}) + } + r.s.OnFilePacket(sub, fileListPacket(recs...)) + c := r.wait(ch) + if c.err != nil { + t.Fatalf("call failed: %d %s", c.err.Code, c.err.Message) + } + sc := c.res.StructuredContent.(map[string]any) + if got := sc["files"].([]fileEntry); len(got) != 2 || got[1].Name != "b" { + t.Fatalf("page = %#v", got) + } + if sc["hasMore"] != true { + t.Fatalf("hasMore = %v, want true", sc["hasMore"]) + } + checkInt(t, sc, "totalAll", 5) +} + +func TestListFilesErrorMapping(t *testing.T) { + cases := []struct { + win32 uint32 + code int + }{ + {5, CodePermission}, + {2, CodeNotFound}, + {3, CodeNotFound}, + {32, CodeInternal}, + {0, CodeInternal}, + } + r := newFileRig(t) + for _, c := range cases { + ch, sub := r.startDirectoryCall(`{"id":"1001","path":"C:\\missing"}`) + r.s.OnFilePacket(sub, fileListErrorPacket(c.win32)) + got := r.wait(ch) + if got.err == nil { + t.Fatalf("win32=%d: call succeeded, want %d", c.win32, c.code) + } + if got.err.Code != c.code { + t.Fatalf("win32=%d: code = %d (%s), want %d", c.win32, got.err.Code, got.err.Message, c.code) + } + if wire := r.next(sub); !bytes.Equal(wire, []byte{protocol.CommandBye}) { + t.Fatalf("win32=%d: retire wire = %v", c.win32, wire) + } + } +} + +// An error reply can only arrive on the sub-conn, but a client that fails the +// drive enumeration itself answers with TOKEN_FILE_LIST_ERROR before any +// TOKEN_DRIVE_LIST. The first packet for a waiting call is adopted, so the +// raw win32 code reaches the caller instead of a timeout. +func TestListFilesErrorBeforeDriveList(t *testing.T) { + r := newFileRig(t) + ch := r.call(`{"id":"1001","path":"C:\\missing"}`) + r.next(r.main) + + sub := r.newSub() + if !r.s.OnFilePacket(sub, fileListErrorPacket(2)) { + t.Fatal("early error packet was not consumed") + } + got := r.wait(ch) + if got.err == nil || got.err.Code != CodeNotFound { + t.Fatalf("err = %#v, want not-found", got.err) + } + if wire := r.next(sub); !bytes.Equal(wire, []byte{protocol.CommandBye}) { + t.Fatalf("retire wire = %v, want COMMAND_BYE", wire) + } + waitClosed(t, sub) +} + +// cancel is the only place that both clears the pending slot and learns the +// sub-conn, so it must hand the sub back for retirement. +func TestFileTableCancelReturnsAnsweredSub(t *testing.T) { + tbl := newFileTable() + req, ok := tbl.begin("1001") + if !ok { + t.Fatal("begin rejected the first request") + } + if sub := tbl.cancel("1001", req); sub != nil { + t.Fatalf("cancel before any packet returned %p", sub) + } + + req, _ = tbl.begin("1001") + ctx := &connection.Context{} + if !tbl.deliver("1001", fileEvent{kind: fileEventDriveList, ctx: ctx}) { + t.Fatal("drive list was not delivered") + } + if sub := tbl.cancel("1001", req); sub != ctx { + t.Fatalf("cancel returned %p, want the answering sub-conn %p", sub, ctx) + } + if _, ok := tbl.begin("1001"); !ok { + t.Fatal("the slot was not freed") + } +} + +// A packet that loses the race with the call's return must not be queued for a +// reader that is gone; it is reported as unclaimed so the orphan sub-conn gets +// retired. The second half pins the window where deliver already looked the +// request up when cancel marked it: the abandoned flag decides. +func TestFileTableDeliverAfterCancelIsUnclaimed(t *testing.T) { + tbl := newFileTable() + req, _ := tbl.begin("1001") + tbl.cancel("1001", req) + if tbl.deliver("1001", fileEvent{kind: fileEventDriveList, ctx: &connection.Context{}}) { + t.Fatal("deliver accepted a packet for an abandoned request") + } + select { + case ev := <-req.ch: + t.Fatalf("abandoned request still received %#v", ev) + default: + } + + req, _ = tbl.begin("1001") + req.mu.Lock() + req.abandoned = true + req.mu.Unlock() + if tbl.deliver("1001", fileEvent{kind: fileEventDriveList, ctx: &connection.Context{}}) { + t.Fatal("deliver accepted a packet after the request was abandoned") + } +} + +// ----- lifecycle ------------------------------------------------------------ + +func TestListFilesTimeoutRetiresLateSubConn(t *testing.T) { + r := newFileRig(t) + fileListTimeout = 40 * time.Millisecond + ch := r.call(`{"id":"1001"}`) + r.next(r.main) + c := r.wait(ch) + if c.err == nil || c.err.Code != CodeTimeout { + t.Fatalf("err = %#v, want timeout", c.err) + } + + // The client opens its sub-conn after we gave up: it is an orphan and must + // be retired, or the client leaks a file-manager thread. + sub := r.newSub() + if !r.s.OnFilePacket(sub, driveListPacket(driveRec{'C', 3, 1, 1, "本地磁盘", "NTFS"})) { + t.Fatal("orphan drive list was not consumed") + } + if got := r.next(sub); !bytes.Equal(got, []byte{protocol.CommandBye}) { + t.Fatalf("orphan retire wire = %v, want COMMAND_BYE", got) + } + waitClosed(t, sub) +} + +func TestListFilesTimeoutClearsPending(t *testing.T) { + r := newFileRig(t) + fileListTimeout = 40 * time.Millisecond + ch, sub := r.startDirectoryCall(`{"id":"1001","path":"C:\\slow"}`) + _ = sub.Close() // the client drops the sub-conn mid-request + c := r.wait(ch) + if c.err == nil || c.err.Code != CodeTimeout { + t.Fatalf("err = %#v, want timeout", c.err) + } + r.s.files.mu.Lock() + defer r.s.files.mu.Unlock() + if len(r.s.files.byDevice) != 0 { + t.Fatalf("pending table still holds %d requests", len(r.s.files.byDevice)) + } +} + +func TestListFilesDropsForeignSubConnPackets(t *testing.T) { + r := newFileRig(t) + ch := r.call(`{"id":"1001","path":"C:\\"}`) + r.next(r.main) + + real, foreign := r.newSub(), r.newSub() + + // A file list before the drive-list phase belongs to no known sub-conn. + if !r.s.OnFilePacket(foreign, fileListPacket(fileRec{name: "stale"})) { + t.Fatal("foreign file list was not claimed by the protocol") + } + // The real sub-conn announces itself and gets the directory request. + r.s.OnFilePacket(real, driveListPacket(driveRec{'C', 3, 1, 1, "本地磁盘", "NTFS"})) + r.next(real) + // A second drive list from another connection is a duplicate: it is + // retired rather than allowed to steal the pending call. + r.s.OnFilePacket(foreign, driveListPacket(driveRec{'D', 3, 1, 1, "本地磁盘", "NTFS"})) + if got := r.next(foreign); !bytes.Equal(got, []byte{protocol.CommandBye}) { + t.Fatalf("duplicate sub-conn retire wire = %v", got) + } + // A stale file list from the duplicate is ignored, the real one wins. + r.s.OnFilePacket(foreign, fileListPacket(fileRec{name: "stale"})) + r.s.OnFilePacket(real, fileListPacket(fileRec{name: "good"})) + + c := r.wait(ch) + if c.err != nil { + t.Fatalf("call failed: %d %s", c.err.Code, c.err.Message) + } + files := c.res.StructuredContent.(map[string]any)["files"].([]fileEntry) + if len(files) != 1 || files[0].Name != "good" { + t.Fatalf("files = %#v, want only the real sub-conn's answer", files) + } +} + +func TestListFilesBusyWhenCallPending(t *testing.T) { + r := newFileRig(t) + ch := r.call(`{"id":"1001"}`) + r.next(r.main) + if _, rerr := r.s.callListFiles(context.Background(), []byte(`{"id":"1001","path":"C:\\"}`)); rerr == nil || rerr.Code != CodeBusy { + t.Fatalf("second call err = %#v, want busy", rerr) + } + r.s.OnFilePacket(r.newSub(), driveListPacket()) + if c := r.wait(ch); c.err != nil { + t.Fatalf("first call failed: %d %s", c.err.Code, c.err.Message) + } +} + +// ----- parsers -------------------------------------------------------------- + +func TestParseDriveListStopsAtTerminator(t *testing.T) { + pkt := driveListPacket(driveRec{'D', 2, 10, 5, "CD 驱动器", "CDFS"}) + // The real client ends by buffer exhaustion; accept an explicit zero-letter + // terminator too and stop before the trailing garbage. + pkt = append(pkt, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) + got := parseDriveList(pkt[1:]) // the parsers see the payload, token removed + if len(got) != 1 || got[0].Drive != `D:\` || got[0].TypeName != "CD 驱动器" || got[0].Type != 2 { + t.Fatalf("drives = %#v", got) + } +} + +func TestParseDriveListTruncated(t *testing.T) { + payload := driveListPacket(driveRec{'D', 2, 10, 5, "CD 驱动器", "CDFS"})[1:] + for _, n := range []int{0, 5, 12, len(payload) - 3} { + if got := parseDriveList(payload[:n]); len(got) != 0 { + t.Fatalf("truncated at %d: %#v", n, got) + } + } +} + +func TestParseFileListBoundaries(t *testing.T) { + if got, truncated := parseFileList(nil); len(got) != 0 || truncated { + t.Fatalf("nil packet parsed to %#v", got) + } + payload := fileListPacket(fileRec{name: "a", size: 1, mtime: 1000})[1:] + // Chop any part of the fixed tail: the partial record is dropped. + for _, n := range []int{0, 1, 3, len(payload) - 1} { + if got, _ := parseFileList(payload[:n]); len(got) != 0 { + t.Fatalf("truncated at %d: %#v", n, got) + } + } + // Trailing zero padding ends the parse cleanly. + if got, truncated := parseFileList(append(payload, 0, 0, 0)); len(got) != 1 || truncated { + t.Fatalf("padded packet parsed to %#v", got) + } +} + +func TestListFilesTruncationNote(t *testing.T) { + // A directory beyond the parse cap must not look like "that is all there + // is": the summary says the list was cut. + r := newFileRig(t) + ch, sub := r.startDirectoryCall(`{"id":"1001","path":"C:\\huge"}`) + pkt := []byte{protocol.TokenFileList} + for i := 0; i < fileListMaxEntries+1; i++ { + pkt = append(pkt, 0, 'a', 0) + pkt = append(pkt, make([]byte, 16)...) + } + r.s.OnFilePacket(sub, pkt) + c := r.wait(ch) + if c.err != nil { + t.Fatalf("call failed: %d %s", c.err.Code, c.err.Message) + } + sc := c.res.StructuredContent.(map[string]any) + checkInt(t, sc, "total", fileListMaxEntries) + if !strings.Contains(c.res.Content[0].Text, "只解析了前") { + t.Fatalf("summary does not mention the cap: %q", c.res.Content[0].Text) + } +} + +func TestParseFileListEntryCap(t *testing.T) { + var buf []byte + for i := 0; i < fileListMaxEntries+5; i++ { + buf = append(buf, 0, 'a', 0) + buf = append(buf, make([]byte, 16)...) + } + files, truncated := parseFileList(buf) + if len(files) != fileListMaxEntries || !truncated { + t.Fatalf("parsed %d entries, truncated=%v", len(files), truncated) + } +} diff --git a/server/go/mcp/server.go b/server/go/mcp/server.go index 99ce3af..3ed98be 100644 --- a/server/go/mcp/server.go +++ b/server/go/mcp/server.go @@ -77,6 +77,7 @@ type Server struct { 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 // stop ends the idle reaper and reaperDone reports that it returned; // closeOnce makes Close idempotent (tests call it both explicitly and from @@ -116,6 +117,7 @@ func New(cfg Config) *Server { locks: newDeviceLocks(), sem: make(chan struct{}, cfg.MaxConcurrency), previews: newPreviewTable(), + files: newFileTable(), stop: make(chan struct{}), } if cfg.Hub != nil { @@ -125,6 +127,7 @@ func New(cfg Config) *Server { } s.registerLocalTools() s.registerScreenTools() + s.registerFileTools() s.registerTerminalTools() if cfg.Hub != nil { // No hub means no shell can ever be opened, so there is nothing to diff --git a/server/go/protocol/commands.go b/server/go/protocol/commands.go index cc6b799..88c5630 100644 --- a/server/go/protocol/commands.go +++ b/server/go/protocol/commands.go @@ -37,6 +37,28 @@ func GbkToUTF8(data []byte) string { return cleanString(buf.String()) } +// UTF8ToGBK converts a Go string to the GBK bytes a client-side C string field +// expects. Every platform's file manager works on GBK on the wire: Windows uses +// the ANSI APIs (CP936), and the POSIX clients convert with iconv before +// sending (`gbkToUtf8` in common/FileManager.h). Bytes that GBK cannot +// represent are replaced; callers that care should check the round trip. +func UTF8ToGBK(s string) []byte { + if s == "" { + return nil + } + var buf bytes.Buffer + w := transform.NewWriter(&buf, simplifiedchinese.GBK.NewEncoder()) + if _, err := w.Write([]byte(s)); err != nil { + // Unrepresentable input: fall back to the raw UTF-8 bytes, which is + // exactly what the C++ iconv helper does on failure. + return []byte(s) + } + if err := w.Close(); err != nil { + return []byte(s) + } + return buf.Bytes() +} + // Utf8CleanString trims at the first NUL and strips non-printables — the // UTF-8 counterpart of GbkToUTF8 for clients that have the CLIENT_CAP_UTF8 // capability bit. Decoding as GBK in that case would mangle multi-byte @@ -115,6 +137,8 @@ func DecodeClientString(data []byte, capability, clientType string) string { const ( // Server -> Client commands CommandActived byte = 0 // COMMAND_ACTIVED + CommandListDrive byte = 1 // COMMAND_LIST_DRIVE - open the file-manager sub-conn and list drives + CommandListFiles byte = 2 // COMMAND_LIST_FILES - [cmd][path\0] on the file-manager sub-conn CommandScreenSpy byte = 16 // COMMAND_SCREEN_SPY - start screen capture CommandScreenControl byte = 20 // COMMAND_SCREEN_CONTROL - mouse/keyboard input (MSG64 batches) CommandScreenGetClipboard byte = 24 // COMMAND_SCREEN_GET_CLIPBOARD - request remote clipboard (single byte) @@ -132,12 +156,18 @@ const ( TokenAuth byte = 100 // TOKEN_AUTH - authorization required TokenHeartbeat byte = 101 // TOKEN_HEARTBEAT TokenLogin byte = 102 // TOKEN_LOGIN - login packet + TokenDriveList byte = 103 // TOKEN_DRIVE_LIST - file-manager sub-conn first packet, drive records + TokenFileList byte = 104 // TOKEN_FILE_LIST - one directory's entries, [token] + records TokenBitmapInfo byte = 115 // TOKEN_BITMAPINFO - screen sub-connection header TokenFirstScreen byte = 116 // TOKEN_FIRSTSCREEN - raw BGRA baseline frame (NOT H264) TokenNextScreen byte = 117 // TOKEN_NEXTSCREEN - non-keyframe H264 (P-frame) TokenClipboardText byte = 118 // TOKEN_CLIPBOARD_TEXT - remote clipboard text [118][UTF-8][NUL] TokenShellStart byte = 128 // TOKEN_SHELL_START - legacy cmd-pipe shell sub-conn open TokenKeyframe byte = 134 // TOKEN_KEYFRAME - H264 IDR (sent on GOP boundary) + // TOKEN_FILE_LIST_ERROR is the modern clients' structured "cannot list this + // 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 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 diff --git a/server/go/protocol/commands_test.go b/server/go/protocol/commands_test.go index 1fd9e69..54066c8 100644 --- a/server/go/protocol/commands_test.go +++ b/server/go/protocol/commands_test.go @@ -1,6 +1,13 @@ package protocol -import "testing" +import ( + "go/ast" + "go/parser" + "go/token" + "strconv" + "strings" + "testing" +) func TestSignMessageHMACVector(t *testing.T) { // Standard HMAC-SHA256 sanity vector. Anchors that SignMessage matches @@ -45,3 +52,54 @@ func TestIsH264KeyframeBasic(t *testing.T) { t.Fatal("non-H264 bytes should not match") } } + +// TestCommandTokenConstantsAreUnique guards the command/token byte values in +// commands.go against a silent collision. The tokens are matched against the +// first byte of every inbound packet, so two names sharing a value means the +// wrong handler eats the packet — an added file-manager token that reuses +// TOKEN_LOGIN's value would swallow the device's login. Constants outside the +// Command*/Token*/Cmd* families are skipped: the file also defines field +// offsets, which legitimately repeat small numbers (offset 1 in two packets). +func TestCommandTokenConstantsAreUnique(t *testing.T) { + fset := token.NewFileSet() + file, err := parser.ParseFile(fset, "commands.go", nil, 0) + if err != nil { + t.Fatalf("parse commands.go: %v", err) + } + seen := make(map[int64]string) + ast.Inspect(file, func(n ast.Node) bool { + spec, ok := n.(*ast.ValueSpec) + if !ok { + return true + } + for i, name := range spec.Names { + // A name without its own expression repeats the previous value. + if i >= len(spec.Values) || !isCommandTokenName(name.Name) { + continue + } + lit, ok := spec.Values[i].(*ast.BasicLit) + if !ok || lit.Kind != token.INT { + continue + } + v, err := strconv.ParseInt(lit.Value, 0, 64) + if err != nil { + continue + } + if prev, dup := seen[v]; dup { + t.Errorf("%s and %s both use %d", prev, name.Name, v) + continue + } + seen[v] = name.Name + } + return true + }) + if len(seen) < 20 { + t.Fatalf("only %d command/token constants scanned; did commands.go change shape?", len(seen)) + } +} + +func isCommandTokenName(name string) bool { + return strings.HasPrefix(name, "Command") || + strings.HasPrefix(name, "Token") || + strings.HasPrefix(name, "Cmd") +}