| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "encoding/binary" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 13 | ) |
| 14 | |
| 15 | func validWindowsAttachmentPath(path, name string) bool { |
| 16 | if len(path) < 3 || len(path) > 32767 || path[1] != ':' || path[2] != '\\' || !((path[0] >= 'A' && path[0] <= 'Z') || (path[0] >= 'a' && path[0] <= 'z')) || strings.ContainsAny(path[2:], ":/\x00\r\n") { |
| 17 | return false |
| 18 | } |
| 19 | parts := strings.Split(path[3:], `\`) |
| 20 | root := len(parts) - 4 |
| 21 | if root >= 1 && parts[root] != "Cage" && parts[root-1] == "Cage" && parts[root] == "Attachments" { |
| 22 | root-- |
| 23 | } |
| 24 | if root < 0 || parts[root] != "Cage" || parts[root+1] != "Attachments" || parts[len(parts)-1] != name { |
| 25 | return false |
| 26 | } |
| 27 | for _, id := range parts[root+2 : len(parts)-1] { |
| 28 | if len(id) != 32 || strings.Trim(id, "0123456789abcdef") != "" { |
| 29 | return false |
| 30 | } |
| 31 | } |
| 32 | for _, part := range parts { |
| 33 | if part == "" || part == "." || part == ".." { |
| 34 | return false |
| 35 | } |
| 36 | } |
| 37 | return true |
| 38 | } |
| 39 | |
| 40 | func (s *Server) windowsSaveAttachment(ctx context.Context, name string, body io.Reader) (map[string]any, error) { |
| 41 | return s.windowsUploadAttachment(ctx, name, "", body) |
| 42 | } |
| 43 | |
| 44 | func (s *Server) windowsUploadAttachment(ctx context.Context, name, id string, body io.Reader) (map[string]any, error) { |
| 45 | cmd, err := guestos.WindowsRelayCommand(ctx, s.profile, "attachments") |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | input, err := cmd.StdinPipe() |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | output, err := cmd.StdoutPipe() |
| 54 | if err != nil { |
| 55 | input.Close() |
| 56 | return nil, err |
| 57 | } |
| 58 | cmd.Stderr = io.Discard |
| 59 | if err = cmd.Start(); err != nil { |
| 60 | input.Close() |
| 61 | output.Close() |
| 62 | return nil, err |
| 63 | } |
| 64 | done := false |
| 65 | defer func() { |
| 66 | input.Close() |
| 67 | output.Close() |
| 68 | if !done { |
| 69 | _ = cmd.Process.Kill() |
| 70 | _ = cmd.Wait() |
| 71 | } |
| 72 | }() |
| 73 | request := map[string]string{"action": "save", "name": name} |
| 74 | if id != "" { |
| 75 | request["id"] = id |
| 76 | } |
| 77 | if err = json.NewEncoder(input).Encode(request); err == nil { |
| 78 | err = writeWindowsAttachmentFrames(input, body) |
| 79 | } |
| 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | input.Close() |
| 84 | var result map[string]any |
| 85 | reader := bufio.NewScanner(output) |
| 86 | reader.Buffer(make([]byte, 4096), 65536) |
| 87 | if !reader.Scan() || json.Unmarshal(reader.Bytes(), &result) != nil { |
| 88 | return nil, errors.New("Windows 附件没有完整保存,请重新传递") |
| 89 | } |
| 90 | err = cmd.Wait() |
| 91 | done = true |
| 92 | path, _ := result["path"].(string) |
| 93 | if err != nil || result["ok"] != true || result["scope"] != "attachments" || !validWindowsAttachmentPath(path, name) { |
| 94 | return nil, errors.New("Windows 附件保存失败,请确认客机仍已解锁,且文件名有效") |
| 95 | } |
| 96 | if id != "" { |
| 97 | |
| 98 | |
| 99 | public := map[string]any{"ok": true, "id": id, "name": name} |
| 100 | if raw, exists := result["size"]; exists { |
| 101 | size, ok := raw.(float64) |
| 102 | if !ok || size < 0 || size > float64(browserFileLimit) || size != float64(int64(size)) { |
| 103 | return nil, errors.New("Windows 附件保存结果无效") |
| 104 | } |
| 105 | public["size"] = size |
| 106 | } |
| 107 | return public, nil |
| 108 | } |
| 109 | return result, nil |
| 110 | } |
| 111 | |
| 112 | func writeWindowsAttachmentFrames(output io.Writer, input io.Reader) error { |
| 113 | buffer := make([]byte, 64<<10) |
| 114 | header := make([]byte, 4) |
| 115 | var count int64 |
| 116 | for { |
| 117 | n, err := input.Read(buffer) |
| 118 | count += int64(n) |
| 119 | if count > browserFileLimit { |
| 120 | return errors.New("每个文件不能超过 2 GB") |
| 121 | } |
| 122 | if n > 0 { |
| 123 | binary.LittleEndian.PutUint32(header, uint32(n)) |
| 124 | if writeErr := writeWindowsAttachmentPart(output, header); writeErr != nil { |
| 125 | return writeErr |
| 126 | } |
| 127 | if writeErr := writeWindowsAttachmentPart(output, buffer[:n]); writeErr != nil { |
| 128 | return writeErr |
| 129 | } |
| 130 | } |
| 131 | if err != nil { |
| 132 | if err != io.EOF { |
| 133 | return err |
| 134 | } |
| 135 | break |
| 136 | } |
| 137 | } |
| 138 | return writeWindowsAttachmentPart(output, []byte{0, 0, 0, 0}) |
| 139 | } |
| 140 | |
| 141 | func writeWindowsAttachmentPart(output io.Writer, data []byte) error { |
| 142 | for len(data) != 0 { |
| 143 | n, err := output.Write(data) |
| 144 | if err != nil { |
| 145 | return err |
| 146 | } |
| 147 | if n <= 0 || n > len(data) { |
| 148 | return io.ErrShortWrite |
| 149 | } |
| 150 | data = data[n:] |
| 151 | } |
| 152 | return nil |
| 153 | } |