| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "regexp" |
| 13 | "strings" |
| 14 | |
| 15 | "github.com/google/uuid" |
| 16 | ) |
| 17 | |
| 18 | |
| 19 | |
| 20 | type WindowsProfile struct { |
| 21 | Version int `json:"version"` |
| 22 | VMID uuid.UUID `json:"vm_id"` |
| 23 | GUIUserSID string `json:"gui_user_sid"` |
| 24 | GUIUser string `json:"gui_user,omitempty"` |
| 25 | InstanceDirectory string `json:"instance_directory"` |
| 26 | RelayExecutable string `json:"relay_executable"` |
| 27 | PairingFile string `json:"pairing_file"` |
| 28 | NetworkPairingFile string `json:"network_pairing_file,omitempty"` |
| 29 | } |
| 30 | |
| 31 | var windowsGUISID = regexp.MustCompile(`^S-1-5-21-[0-9]+-[0-9]+-[0-9]+-[0-9]+$`) |
| 32 | |
| 33 | func WindowsProfilePath(root string) string { return filepath.Join(root, "windows-guest.json") } |
| 34 | |
| 35 | func (p WindowsProfile) validate() error { |
| 36 | if p.Version != 1 || p.VMID == uuid.Nil || !windowsGUISID.MatchString(p.GUIUserSID) || |
| 37 | len(p.GUIUser) > 256 || strings.ContainsAny(p.GUIUser, "\x00\r\n") { |
| 38 | return errors.New("Windows 客机标识或图形用户无效") |
| 39 | } |
| 40 | for _, path := range []string{p.InstanceDirectory, p.RelayExecutable, p.PairingFile} { |
| 41 | if !filepath.IsAbs(path) || strings.ContainsAny(path, "\x00\r\n") { |
| 42 | return errors.New("Windows 客机配置需要绝对本机路径") |
| 43 | } |
| 44 | } |
| 45 | if p.NetworkPairingFile != "" && (!filepath.IsAbs(p.NetworkPairingFile) || strings.ContainsAny(p.NetworkPairingFile, "\x00\r\n")) { |
| 46 | return errors.New("Windows 客机网络配对需要绝对本机路径") |
| 47 | } |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | func LoadWindowsProfile(root string) (*WindowsProfile, error) { |
| 52 | path := WindowsProfilePath(root) |
| 53 | info, err := os.Lstat(path) |
| 54 | if errors.Is(err, os.ErrNotExist) { |
| 55 | return nil, nil |
| 56 | } |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | if !info.Mode().IsRegular() || info.Size() == 0 || info.Size() > 16<<10 { |
| 61 | return nil, errors.New("Windows 客机配置文件无效") |
| 62 | } |
| 63 | data, err := os.ReadFile(path) |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | var binding WindowsProfile |
| 68 | decoder := json.NewDecoder(bytes.NewReader(data)) |
| 69 | decoder.DisallowUnknownFields() |
| 70 | if decoder.Decode(&binding) != nil || decoder.Decode(new(any)) != io.EOF { |
| 71 | return nil, errors.New("Windows 客机配置格式无效") |
| 72 | } |
| 73 | if err := binding.validate(); err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | return &binding, nil |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | func BindWindowsProfile(root string, binding WindowsProfile) error { |
| 83 | if err := binding.validate(); err != nil { |
| 84 | return err |
| 85 | } |
| 86 | data, err := json.Marshal(binding) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | if err := publishBinding(root, "windows-guest.json", append(data, '\n')); err != nil { |
| 91 | return err |
| 92 | } |
| 93 | return protectWindowsProfile(WindowsProfilePath(root)) |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
| 98 | func WindowsRelayCommand(ctx context.Context, root, operation string, args ...string) (*exec.Cmd, error) { |
| 99 | p, err := LoadWindowsProfile(root) |
| 100 | if err != nil { |
| 101 | return nil, err |
| 102 | } |
| 103 | if p == nil { |
| 104 | return nil, errors.New("此配置尚未绑定 Windows 客机") |
| 105 | } |
| 106 | switch operation { |
| 107 | case "versions": |
| 108 | case "stream", "status", "import", "export", "terminal", "browser", "desktop", "attachments", "audio", "resources", "diagnostics", "safety", "project-mount", "network-stream", "network-settings", "cli": |
| 109 | default: |
| 110 | return nil, errors.New("Windows 客机剪贴板操作无效") |
| 111 | } |
| 112 | if len(args) != 0 && !(operation == "export" && len(args) == 1 && args[0] == "--paths-stdin") { |
| 113 | return nil, errors.New("Windows 客机文件路径必须通过标准输入传递") |
| 114 | } |
| 115 | argv := append([]string{"--relay-profile", WindowsProfilePath(root), operation}, args...) |
| 116 | return exec.CommandContext(ctx, p.RelayExecutable, argv...), nil |
| 117 | } |