Files
SimpleRemoter/server/go/mcp/filemgr.go
T
yuanyuanxianganddeepseek-flash d7accf1d30 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
2026-11-12 22:34:17 +01:00

579 lines
19 KiB
Go

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)
}