MCPcopy Create free account
hub / github.com/evilsocket/medusa / create_exec

Function create_exec

src/shell/docker.rs:29–75  ·  view source on GitHub ↗
(container_id: &str, command: &str)

Source from the content-addressed store, hash-verified

27}
28
29fn create_exec(container_id: &str, command: &str) -> Result<String, String> {
30 debug!(
31 "creating exec operation for container {}: {}",
32 container_id, command
33 );
34
35 let mut stream = UnixStream::connect(DOCKER_SOCKET)
36 .map_err(|e| format!("could not connect to {}: {}", DOCKER_SOCKET, e))?;
37
38 stream
39 .set_read_timeout(Some(Duration::from_secs(15)))
40 .map_err(|e| format!("could not set read timeout for {}: {}", DOCKER_SOCKET, e))?;
41
42 stream
43 .set_write_timeout(Some(Duration::from_secs(15)))
44 .map_err(|e| format!("could not set write timeout for {}: {}", DOCKER_SOCKET, e))?;
45
46 let content = format!(
47 "{{
48 \"AttachStdout\": true,
49 \"AttachStderr\": true,
50 \"Tty\": false,
51 \"Cmd\": [ \"sh\", \"-c\", {}]
52 }}",
53 serde_json::to_string(command).unwrap()
54 );
55
56 let request = format!("POST /containers/{}/exec HTTP/1.0\r\n", container_id)
57 + "Content-Type: application/json\r\n"
58 + &format!("Content-Length: {}\r\n", content.len())
59 + &format!("\r\n{}", content);
60
61 let response = do_request(&request, &mut stream)?;
62 let response = String::from_utf8_lossy(&response).to_string();
63
64 // split headers from response body
65 let response = format!("{{{}", response.split_once('{').unwrap().1);
66 // parse as generic hashmap
67 let response: HashMap<String, String> = serde_json::from_str(&response).unwrap();
68 // get exec id
69 if let Some(exec_id) = response.get("Id") {
70 debug!("exec_id = '{}'", &exec_id);
71 return Ok(exec_id.to_owned());
72 }
73
74 Err(format!("{:?}", response))
75}
76
77fn do_exec(exec_id: &str) -> Result<Vec<u8>, String> {
78 debug!("dispatching exec operation {}", exec_id);

Callers 1

execFunction · 0.85

Calls 1

do_requestFunction · 0.85

Tested by

no test coverage detected