MCPcopy
hub / github.com/bettercap/bettercap / runSessionCommand

Method runSessionCommand

modules/api_rest/api_rest_controller.go:218–245  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

216}
217
218func (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
247func (mod *RestAPI) getEvents(limit int) []session.Event {
248 events := make([]session.Event, 0)

Callers 1

sessionRouteMethod · 0.95

Calls 5

toJSONMethod · 0.95
ParseCommandsFunction · 0.92
ErrorMethod · 0.45
RunMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected