handle scp uploads

This commit is contained in:
Astro 2022-09-25 22:35:15 +02:00
parent 3a61ef8448
commit 17238e22b7
1 changed files with 68 additions and 21 deletions

View File

@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fs::{OpenOptions, File};
use std::io::Write;
use std::pin::Pin;
@ -12,11 +13,22 @@ fn send_str(session: &mut Session, channel: ChannelId, s: String) {
session.data(channel, data);
}
enum ChannelState {
Shell,
Scp,
}
enum CommandStatus {
Done,
Scp,
}
pub struct Handler {
filename: String,
lazy_file: Option<File>,
buffer: Vec<u8>,
user: String,
channels: HashMap<ChannelId, ChannelState>,
}
impl Handler {
@ -26,6 +38,7 @@ impl Handler {
lazy_file: None,
buffer: vec![],
user: "root".into(),
channels: HashMap::new(),
}
}
@ -45,7 +58,7 @@ impl Handler {
send_str(session, channel, format!("{}@fnordister:~$ ", self.user));
}
fn handle_command<F: FnMut(&str)>(&self, command: String, mut respond: F) {
fn handle_command<F: FnMut(&str)>(&self, command: String, mut respond: F) -> CommandStatus {
let program_len = command.find(|c: char| c.is_whitespace())
.unwrap_or(command.len());
let program = &command[..program_len];
@ -60,12 +73,15 @@ impl Handler {
respond("drwxr-xr-x 18 root root 18 Jan 4 1969 ..");
respond("drwxr-xr-x 18 root root 18 Jan 4 0000 ...");
},
"scp" => return CommandStatus::Scp,
"bash" => {}
"sh" => {}
"cd" => {}
"" => {}
_ => respond(&format!("{}: command not found", program)),
}
CommandStatus::Done
}
}
@ -112,7 +128,6 @@ impl server::Handler for Handler {
match status {
CommandStatus::Scp => {
session.data(channel, vec![0].into());
info!("nul");
self.channels.insert(channel, ChannelState::Scp);
}
CommandStatus::Done => {
@ -142,7 +157,7 @@ impl server::Handler for Handler {
// mut session: Session
// ) -> Self::FutureBool {
// info!("channel_open_session");
// session.channel_success(channel);
// // session.channel_success(channel);
// self.finished_bool(true, session)
// }
@ -153,11 +168,21 @@ impl server::Handler for Handler {
// window_size: u32,
// mut session: Session
// ) -> Self::FutureUnit {
// info!("channel_open_confirmation");
// self.finished(session)
// }
// fn channel_close(self, channel: ChannelId, session: Session) -> Self::FutureUnit {
// info!("channel_close {}", channel);
// self.finished(session)
// }
fn channel_eof(mut self, channel: ChannelId, mut session: Session) -> Self::FutureUnit {
session.close(channel);
self.channels.remove(&channel);
self.finished(session)
}
fn auth_password(mut self, user: &str, password: &str) -> Self::FutureAuth {
writeln!(self.file(), "Authenticated as {} with {}\n", user, password)
.unwrap();
@ -168,24 +193,46 @@ impl server::Handler for Handler {
fn data(mut self, channel: ChannelId, data: &[u8], mut session: Session) -> Self::FutureUnit {
self.file().write(data)
.unwrap();
// echo input back
session.data(channel, data.to_vec().into());
if self.buffer.len() < 1024 {
self.buffer.extend_from_slice(data);
}
if let Some(newline) = self.buffer.iter().position(|b| *b == b'\r') {
let rest = self.buffer.split_off(newline + 1);
let line: String = String::from_utf8_lossy(&self.buffer).into();
self.buffer = rest;
match self.channels.get(&channel) {
Some(ChannelState::Shell) => {
// echo input back
session.data(channel, data.to_vec().into());
session.data(channel, b"\n".to_vec().into());
self.handle_command(line, |response| {
let mut data = Vec::from(response);
data.extend_from_slice(b"\r\n");
session.data(channel, data.into());
});
self.send_prompt(&mut session, channel);
if self.buffer.len() < 1024 {
self.buffer.extend_from_slice(data);
}
if let Some(newline) = self.buffer.iter().position(|b| *b == b'\r') {
let rest = self.buffer.split_off(newline + 1);
// let line: String = String::from_utf8_lossy(&self.buffer).into();
self.buffer = rest;
session.data(channel, b"\n".to_vec().into());
self.handle_command(line, |response| {
let mut data = Vec::from(response);
data.extend_from_slice(b"\r\n");
session.data(channel, data.into());
});
self.send_prompt(&mut session, channel);
}
}
Some(ChannelState::Scp) => {
if self.buffer.len() < 1024 {
self.buffer.extend_from_slice(data);
}
if let Some(newline) = self.buffer.iter().position(|b| *b == b'\n') {
let rest = self.buffer.split_off(newline + 1);
let line: String = String::from_utf8_lossy(&self.buffer).into();
self.buffer = rest;
session.data(channel, vec![0].into());
}
}
None => {
info!("data on unidentified channel {}", channel);
}
}
self.finished(session)