Add telnet command

This commit is contained in:
William Brawner 2022-07-27 21:18:25 -06:00
parent 997d6e1f8a
commit 9f17d6c2e1
3 changed files with 51 additions and 0 deletions

7
Cargo.lock generated
View file

@ -25,6 +25,7 @@ name = "gsm-agent"
version = "0.1.0"
dependencies = [
"minreq",
"telnet",
]
[[package]]
@ -148,6 +149,12 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "telnet"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99f2cc260bea5219955ab4832c9600a41b87101d280939edae6dd10b3c68a4f3"
[[package]]
name = "unicode-ident"
version = "1.0.2"

View file

@ -10,3 +10,4 @@ lto = true
[dependencies]
minreq = { version = "2.6.0", features = ["https"] }
telnet = "0.2.1"

View file

@ -8,8 +8,11 @@ use std::net::TcpListener;
use std::net::TcpStream;
use std::path::Path;
use std::process::Command;
use std::time::Duration;
use gsm_agent::ThreadPool;
use telnet::Event;
use telnet::Telnet;
fn main() {
let listener = TcpListener::bind("127.0.0.1:4762").unwrap();
@ -68,6 +71,38 @@ where
String::from_utf8(Command::new(command).args(args).output().unwrap().stdout).unwrap()
}
fn telnet(stream: &mut TcpStream, host: &str, port: &u16) -> Result<String, std::io::Error> {
let mut telnet = Telnet::connect((host, *port), 4096)?;
let mut stream_buffer = [0; 4096];
stream.set_read_timeout(Some(Duration::new(1, 0)))?;
'main: loop {
match stream.read(&mut stream_buffer) {
Ok(read) => {
match String::from_utf8_lossy(&stream_buffer[0..read])
.as_ref()
.trim()
{
"quit" => break 'main,
_ => telnet.write(&stream_buffer[0..read]).unwrap(),
};
}
Err(_) => {}
};
match telnet.read_timeout(Duration::new(1, 0)) {
Ok(event) => {
if let Event::Data(buffer) = event {
stream.write(&buffer).unwrap();
}
}
Err(e) => {
println!("telnet read error: {:?}\n", e);
break 'main;
}
}
}
Ok(String::from("Telnet connection closed"))
}
fn handle_connection(mut stream: TcpStream) {
loop {
let mut buffer = [0; 1024];
@ -104,6 +139,14 @@ fn handle_connection(mut stream: TcpStream) {
"ping" => String::from("pong"),
"pwd" => pwd(),
"shell" => shell(command_iter.next().unwrap(), command_iter),
"telnet" => {
let host = command_iter.next().unwrap();
let port = u16::from_str_radix(command_iter.next().unwrap(), 10).unwrap();
match telnet(&mut stream, host, &port) {
Ok(s) => s,
Err(e) => e.to_string(),
}
}
_ => {
format!(
"unknown command: {:?}",