Add telnet command
This commit is contained in:
parent
997d6e1f8a
commit
9f17d6c2e1
3 changed files with 51 additions and 0 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -25,6 +25,7 @@ name = "gsm-agent"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"minreq",
|
"minreq",
|
||||||
|
"telnet",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -148,6 +149,12 @@ dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "telnet"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "99f2cc260bea5219955ab4832c9600a41b87101d280939edae6dd10b3c68a4f3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.2"
|
version = "1.0.2"
|
||||||
|
|
|
@ -10,3 +10,4 @@ lto = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
minreq = { version = "2.6.0", features = ["https"] }
|
minreq = { version = "2.6.0", features = ["https"] }
|
||||||
|
telnet = "0.2.1"
|
||||||
|
|
43
src/main.rs
43
src/main.rs
|
@ -8,8 +8,11 @@ use std::net::TcpListener;
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use gsm_agent::ThreadPool;
|
use gsm_agent::ThreadPool;
|
||||||
|
use telnet::Event;
|
||||||
|
use telnet::Telnet;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let listener = TcpListener::bind("127.0.0.1:4762").unwrap();
|
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()
|
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) {
|
fn handle_connection(mut stream: TcpStream) {
|
||||||
loop {
|
loop {
|
||||||
let mut buffer = [0; 1024];
|
let mut buffer = [0; 1024];
|
||||||
|
@ -104,6 +139,14 @@ fn handle_connection(mut stream: TcpStream) {
|
||||||
"ping" => String::from("pong"),
|
"ping" => String::from("pong"),
|
||||||
"pwd" => pwd(),
|
"pwd" => pwd(),
|
||||||
"shell" => shell(command_iter.next().unwrap(), command_iter),
|
"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!(
|
format!(
|
||||||
"unknown command: {:?}",
|
"unknown command: {:?}",
|
||||||
|
|
Loading…
Reference in a new issue