| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "sync" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | type agentNativeItem struct { |
| 11 | ID string `json:"id"` |
| 12 | Connection string `json:"connection_id"` |
| 13 | Request json.RawMessage `json:"request,omitempty"` |
| 14 | Status string `json:"status"` |
| 15 | Result json.RawMessage `json:"result,omitempty"` |
| 16 | Error string `json:"error,omitempty"` |
| 17 | created time.Time |
| 18 | } |
| 19 | type agentNativeQueue struct { |
| 20 | mu sync.Mutex |
| 21 | items []*agentNativeItem |
| 22 | windows map[string]time.Time |
| 23 | } |
| 24 | |
| 25 | |
| 26 | |
| 27 | func (s *Server) agentNativeRoutes(mux routeRegistrar) { |
| 28 | mux.HandleFunc("GET /api/agent/native", s.agentNative) |
| 29 | mux.HandleFunc("POST /api/agent/native", s.agentNative) |
| 30 | mux.HandleFunc("POST /api/agent/native/window", s.agentNativeWindow) |
| 31 | mux.HandleFunc("GET /api/agent/native/{id}", s.agentNative) |
| 32 | mux.HandleFunc("POST /api/agent/native/{id}/complete", s.agentNative) |
| 33 | } |
| 34 | |
| 35 | func (s *Server) agentNative(w http.ResponseWriter, r *http.Request) { |
| 36 | q := &s.agentNativeQueue |
| 37 | id := r.PathValue("id") |
| 38 | if r.Method == "POST" && id == "" { |
| 39 | if !s.hasTrustedView(r) { |
| 40 | http.Error(w, "Native submission requires the local launcher", 403) |
| 41 | return |
| 42 | } |
| 43 | var in struct { |
| 44 | Request json.RawMessage `json:"request"` |
| 45 | Connection string `json:"connection_id"` |
| 46 | } |
| 47 | if !readJSON(w, r, &in, 12<<20) { |
| 48 | return |
| 49 | } |
| 50 | var request map[string]json.RawMessage |
| 51 | if json.Unmarshal(in.Request, &request) != nil || request == nil || in.Connection == "" { |
| 52 | http.Error(w, "Native request and explicit connection_id required", 400) |
| 53 | return |
| 54 | } |
| 55 | var action string |
| 56 | _ = json.Unmarshal(request["action"], &action) |
| 57 | if action == "" { |
| 58 | http.Error(w, "Native action required", 400) |
| 59 | return |
| 60 | } |
| 61 | if s.remote != nil && s.remote.context().ConnectionID != in.Connection { |
| 62 | http.Error(w, "Connection changed", 409) |
| 63 | return |
| 64 | } |
| 65 | q.mu.Lock() |
| 66 | defer q.mu.Unlock() |
| 67 | now := time.Now() |
| 68 | kept := q.items[:0] |
| 69 | for _, item := range q.items { |
| 70 | if item.Status == "pending" && now.Sub(item.created) > 2*time.Minute { |
| 71 | item.Status = "failed" |
| 72 | item.Error = "Desktop did not claim this request in time. No native action was executed." |
| 73 | item.Request = nil |
| 74 | } |
| 75 | if now.Sub(item.created) < 30*time.Minute { |
| 76 | kept = append(kept, item) |
| 77 | } |
| 78 | } |
| 79 | q.items = kept |
| 80 | if len(q.items) >= 32 { |
| 81 | for i, item := range q.items { |
| 82 | if item.Status == "completed" || item.Status == "failed" { |
| 83 | q.items = append(q.items[:i], q.items[i+1:]...) |
| 84 | break |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | if len(q.items) >= 32 { |
| 89 | http.Error(w, "Read or finish outstanding native requests before submitting more", 409) |
| 90 | return |
| 91 | } |
| 92 | item := &agentNativeItem{ID: randomToken()[:24], Request: append(json.RawMessage(nil), in.Request...), Connection: in.Connection, Status: "pending", created: now} |
| 93 | q.items = append(q.items, item) |
| 94 | w.WriteHeader(http.StatusAccepted) |
| 95 | jsonReply(w, item) |
| 96 | return |
| 97 | } |
| 98 | q.mu.Lock() |
| 99 | defer q.mu.Unlock() |
| 100 | if (r.Method == "GET" && id == "") || r.Method == "POST" { |
| 101 | until, ok := q.windows[r.Header.Get("X-Cage-Agent-Window")] |
| 102 | if !ok || time.Now().After(until) { |
| 103 | http.Error(w, "Native window proof required", 403) |
| 104 | return |
| 105 | } |
| 106 | } |
| 107 | if r.Method == "GET" && id == "" { |
| 108 | if r.URL.Query().Get("consumer") != "window" { |
| 109 | http.Error(w, "Window consumer required", 400) |
| 110 | return |
| 111 | } |
| 112 | for _, item := range q.items { |
| 113 | if item.Status == "pending" && item.Connection == requestConnection(r) { |
| 114 | if time.Since(item.created) > 2*time.Minute { |
| 115 | item.Status = "failed" |
| 116 | item.Error = "Desktop did not claim this request in time. No native action was executed." |
| 117 | item.Request = nil |
| 118 | continue |
| 119 | } |
| 120 | item.Status = "running" |
| 121 | jsonReply(w, item) |
| 122 | return |
| 123 | } |
| 124 | } |
| 125 | jsonReply(w, map[string]any{}) |
| 126 | return |
| 127 | } |
| 128 | for _, item := range q.items { |
| 129 | if item.ID == id { |
| 130 | if item.Status == "pending" && time.Since(item.created) > 2*time.Minute { |
| 131 | item.Status = "failed" |
| 132 | item.Error = "Desktop did not claim this request in time. No native action was executed." |
| 133 | item.Request = nil |
| 134 | } |
| 135 | if r.Method == "POST" { |
| 136 | if item.Status == "completed" || item.Status == "failed" { |
| 137 | jsonReply(w, map[string]bool{"ok": true}) |
| 138 | return |
| 139 | } |
| 140 | if item.Status != "running" { |
| 141 | http.Error(w, "Native request was not claimed", 409) |
| 142 | return |
| 143 | } |
| 144 | var in struct { |
| 145 | Result json.RawMessage `json:"result"` |
| 146 | Error string `json:"error"` |
| 147 | } |
| 148 | if !readJSON(w, r, &in, 30<<20) { |
| 149 | return |
| 150 | } |
| 151 | item.Result = in.Result |
| 152 | item.Error = in.Error |
| 153 | item.Request = nil |
| 154 | item.Status = "completed" |
| 155 | if in.Error != "" { |
| 156 | item.Status = "failed" |
| 157 | } |
| 158 | jsonReply(w, map[string]bool{"ok": true}) |
| 159 | return |
| 160 | } |
| 161 | if !s.hasTrustedView(r) { |
| 162 | http.Error(w, "Native results require the local launcher", 403) |
| 163 | return |
| 164 | } |
| 165 | jsonReply(w, item) |
| 166 | return |
| 167 | } |
| 168 | } |
| 169 | http.Error(w, "Native request unavailable; inspect state before repeating it", 410) |
| 170 | } |
| 171 | |
| 172 | func (s *Server) agentNativeWindow(w http.ResponseWriter, r *http.Request) { |
| 173 | var in struct { |
| 174 | Ticket string `json:"ticket"` |
| 175 | } |
| 176 | if !readJSON(w, r, &in) { |
| 177 | return |
| 178 | } |
| 179 | if !s.consumeTicket(in.Ticket) { |
| 180 | http.Error(w, "A fresh native launch ticket is required", 403) |
| 181 | return |
| 182 | } |
| 183 | q := &s.agentNativeQueue |
| 184 | q.mu.Lock() |
| 185 | defer q.mu.Unlock() |
| 186 | if q.windows == nil { |
| 187 | q.windows = map[string]time.Time{} |
| 188 | } |
| 189 | now := time.Now() |
| 190 | for proof, until := range q.windows { |
| 191 | if now.After(until) { |
| 192 | delete(q.windows, proof) |
| 193 | } |
| 194 | } |
| 195 | if len(q.windows) >= 64 { |
| 196 | var oldest string |
| 197 | var earliest time.Time |
| 198 | for proof, until := range q.windows { |
| 199 | if oldest == "" || until.Before(earliest) { |
| 200 | oldest, earliest = proof, until |
| 201 | } |
| 202 | } |
| 203 | delete(q.windows, oldest) |
| 204 | } |
| 205 | proof := randomToken() |
| 206 | q.windows[proof] = now.Add(12 * time.Hour) |
| 207 | jsonReply(w, map[string]string{"window_proof": proof}) |
| 208 | } |