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
@@ -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