//go:build windows // SPDX-License-Identifier: Apache-2.0 // Sidecar registration and scoped host layout adapted from mango6i's // reims-go-usb-passthrough v0.1.0-beta.6 (6d76f0b918ab). package main import ( "context" "encoding/json" "errors" "fmt" "os" "path/filepath" "strings" "sync" "time" "github.com/Nwflower/reims-go/internal/atomicfile" "github.com/Nwflower/reims-go/pkg/pluginsdk" ) const pluginID = "reimscommunity.usb-redir" type platformHost interface { InitInfo() pluginsdk.InitInfo CallHost(context.Context, string, any, any) error SpawnVMProcess(context.Context, string, string, []string, string, []string) (int, error) IsVMProcessRunning(context.Context, int) (bool, error) } type plugin struct { host platformHost mu sync.Mutex scan *inventory } type commandArgs struct { VM string `json:"vm"` Key string `json:"key"` Path string `json:"path"` } type settings struct { Path string `json:"path"` } type response struct { Path string `json:"path"` Ready bool `json:"ready"` VMs []vmEntry `json:"vms,omitempty"` Devices []usbEntry `json:"devices,omitempty"` Session *sessionUI `json:"session,omitempty"` ScanError string `json:"scan_error,omitempty"` Scanning bool `json:"scanning"` } func main() { host := pluginsdk.New() p := &plugin{host: host} host.Handle("command/invoke", p.command) if err := host.Serve(context.Background()); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } } func (p *plugin) text(en, zh string) string { if strings.HasPrefix(strings.ToLower(p.host.InitInfo().Locale), "zh") { return zh } return en } func (p *plugin) directories() (root, data string, err error) { info := p.host.InitInfo() data = filepath.Clean(info.DataDir) plugData := filepath.Dir(data) plugins := filepath.Dir(plugData) if info.PluginID != pluginID || !filepath.IsAbs(data) || filepath.Base(data) != pluginID || filepath.Base(plugData) != "data" || filepath.Base(plugins) != "plugins" { return "", "", errors.New(p.text("The plugin data folder is unavailable.", "无法访问插件数据目录。")) } return filepath.Dir(plugins), data, nil } func (p *plugin) command(ctx context.Context, body json.RawMessage) (any, error) { var req struct { Command string `json:"command"` Args commandArgs `json:"args"` } if err := json.Unmarshal(body, &req); err != nil { return nil, err } root, data, err := p.directories() if err != nil { return nil, err } ctx, cancel := context.WithTimeout(ctx, 20*time.Second) defer cancel() p.mu.Lock() defer p.mu.Unlock() var cfg settings if err := readJSON(filepath.Join(data, "settings.json"), &cfg); err != nil && !errors.Is(err, os.ErrNotExist) { return nil, err } tools, toolsErr := resolveBundle(cfg.Path) session, err := p.sessionStatus(ctx, data) if err != nil { return nil, err } result := response{Path: cfg.Path, Ready: toolsErr == nil, Session: session} if toolsErr == nil && p.scan != nil && p.scan.root == tools.Root { result.Devices, result.Scanning = p.scan.devices, p.scan.running if p.scan.failed { result.ScanError = p.text("Could not scan Windows USB devices. Check the Windows USB helper.", "无法扫描 Windows USB 设备,检查 Windows USB 助手后重试。") } } switch req.Command { case "status": return result, nil case "scan": result.VMs, err = p.listVMs(ctx, root) if err != nil { return nil, err } if toolsErr == nil { p.startScan(tools) result.Scanning = true result.ScanError = "" } return result, nil case "configure": if session != nil && session.Active { return nil, errors.New(p.text("Release the current USB session before changing the helper folder.", "释放当前 USB 会话后再更改助手目录。")) } newTools, err := resolveBundle(req.Args.Path) if err != nil { return nil, errors.New(p.text("Choose the preview kit folder that contains a complete Windows-USB helper.", "选择包含完整 Windows-USB 助手的预览包目录。")) } cfg.Path = newTools.Root if err := atomicfile.WriteJSON(filepath.Join(data, "settings.json"), cfg, 0o600); err != nil { return nil, err } p.scan = nil return response{Path: cfg.Path, Ready: true, Session: session}, nil case "attach": if session != nil && session.Active { return nil, errors.New(p.text("Release the current USB session first.", "先释放当前 USB 会话。")) } if toolsErr != nil { return nil, errors.New(p.text("Set the Windows USB helper folder first.", "先设置 Windows USB 助手目录。")) } return p.attach(ctx, root, data, tools, req.Args, session) case "release": if session == nil || !session.Active { return session, nil } if err := requestRelease(data, session.record.Dir); err != nil { return nil, err } session.CancelRequested = true return session, nil default: return nil, errors.New(p.text("Unknown USB action.", "无法识别该 USB 操作。")) } }