(w http.ResponseWriter, r *http.Request)
| 216 | } |
| 217 | |
| 218 | func (mod *RestAPI) runSessionCommand(w http.ResponseWriter, r *http.Request) { |
| 219 | var err error |
| 220 | var cmd CommandRequest |
| 221 | |
| 222 | if r.Body == nil { |
| 223 | http.Error(w, "Bad Request", 400) |
| 224 | } else if err = json.NewDecoder(r.Body).Decode(&cmd); err != nil { |
| 225 | http.Error(w, "Bad Request", 400) |
| 226 | } |
| 227 | |
| 228 | rescueStdout := os.Stdout |
| 229 | stdoutReader, stdoutWriter, _ := os.Pipe() |
| 230 | os.Stdout = stdoutWriter |
| 231 | |
| 232 | for _, aCommand := range session.ParseCommands(cmd.Command) { |
| 233 | if err = mod.Session.Run(aCommand); err != nil { |
| 234 | http.Error(w, err.Error(), 400) |
| 235 | return |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | stdoutWriter.Close() |
| 240 | out, _ := io.ReadAll(stdoutReader) |
| 241 | os.Stdout = rescueStdout |
| 242 | |
| 243 | // remove ANSI escape sequences (bash color codes) from output |
| 244 | mod.toJSON(w, APIResponse{Success: true, Message: ansiEscapeRegex.ReplaceAllString(string(out), "")}) |
| 245 | } |
| 246 | |
| 247 | func (mod *RestAPI) getEvents(limit int) []session.Event { |
| 248 | events := make([]session.Event, 0) |
no test coverage detected