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
This commit is contained in:
1 parent
d7accf1d30
commit
9ce1a58bc3
7 files changed
+1790
-71
No files matched your search
@@ -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
|
||||
|
||||
+86
-50
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -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)
|
||||
}
|
||||
}
|
||||
+16
-14
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user