Redirect stdio over TLS

EDIT 3 Nov 2020

After getting help from other forum users, I managed to pipe the stdio between my program and a "sh" process. However, any other input/output I tried after that to send over TLS to the remote ncat instance is not working as I don't see it in the remote ncat's terminal.

It seems that after the stdio is redirected, I am having problems trying to verify if any other redirection is working after that as I could not see any output anywhere.

What should be the correct approach to get output from "sh" and send it over TLS and to receive commands over TLS from ncat and send it to "sh" for execution?

image

I am trying to accomplish something similar to what this author has done (Writing a high performance TLS terminating proxy in Rust).

		let mut command_output = std::process::Command::new("/bin/sh")
									.stdin(Stdio::piped())
									.stdout(Stdio::piped())
									.stderr(Stdio::piped())
									.spawn()
									.expect("cannot execute command");

		let mut command_stdin = command_output.stdin.unwrap();
		let copy_stdin_thread = std::thread::spawn(move || {
			std::io::copy(&mut std::io::stdin(), &mut command_stdin);
		});
		
		let mut command_stdout = command_output.stdout.unwrap();
		let copy_stdout_thread = std::thread::spawn(move || {
			std::io::copy(&mut command_stdout, &mut std::io::stdout());
		});

		let mut command_stderr = command_output.stderr.unwrap();
		let copy_stderr_thread = std::thread::spawn(move || {
			std::io::copy(&mut command_stderr, &mut std::io::stderr());
		});

		copy_stdin_thread.join().unwrap();
		copy_stdout_thread.join().unwrap();
		copy_stderr_thread.join().unwrap();

        // Does not seem to work or perhaps I was unable to see the output?
		tokio::io::copy(&mut tokio::io::stdin(),&mut tls_socket).await;
		tokio::io::copy(&mut tls_socket, &mut tokio::io::stdout()).await;
		tokio::io::copy(&mut tls_socket, &mut tokio::io::stderr()).await;