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
This commit is contained in:
1 parent
5227d4d78e
commit
d7accf1d30
7 files changed
+1416
-1
No files matched your search
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
Reference in new issue
Block a user