mirror of
https://gitlab.redox-os.org/CoffeeCode/redox-ssh.git
synced 2025-12-28 17:02:19 +01:00
Change TcpStream to Read + Write trait
This commit is contained in:
parent
29f16ed240
commit
44a24eb38f
2 changed files with 20 additions and 42 deletions
|
|
@ -61,11 +61,11 @@ impl<'a> Connection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(&mut self, mut stream: TcpStream) -> Result<()> {
|
pub fn run<S: Read + Write>(&mut self, mut stream: &mut S) -> Result<()> {
|
||||||
self.send_id(&mut stream)?;
|
self.send_id(stream)?;
|
||||||
self.read_id(&stream)?;
|
self.read_id(stream)?;
|
||||||
|
|
||||||
let mut reader = BufReader::new(&stream);
|
let mut reader = BufReader::new(stream);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let packet = self.recv(&mut reader)?;
|
let packet = self.recv(&mut reader)?;
|
||||||
|
|
@ -142,7 +142,7 @@ impl<'a> Connection {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_id(&mut self, stream: &mut TcpStream) -> io::Result<()> {
|
fn send_id(&mut self, stream: &mut Write) -> io::Result<()> {
|
||||||
let id = format!("SSH-2.0-RedoxSSH_{}", env!("CARGO_PKG_VERSION"));
|
let id = format!("SSH-2.0-RedoxSSH_{}", env!("CARGO_PKG_VERSION"));
|
||||||
info!("Identifying as {:?}", id);
|
info!("Identifying as {:?}", id);
|
||||||
|
|
||||||
|
|
@ -155,26 +155,19 @@ impl<'a> Connection {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_id(&mut self, stream: &TcpStream) -> io::Result<()> {
|
fn read_id(&mut self, stream: &mut Read) -> io::Result<()> {
|
||||||
use std::ascii::AsciiExt;
|
use std::str;
|
||||||
|
|
||||||
let mut id = String::new();
|
let mut buf = [0; 255];
|
||||||
|
let count = stream.read(&mut buf)?;
|
||||||
|
|
||||||
for byte in stream.take(255).bytes() {
|
let id = str::from_utf8(&buf[0..count]).map(str::trim).or(Err(
|
||||||
match byte
|
io::Error::new(io::ErrorKind::InvalidData, "invalid id"),
|
||||||
{
|
))?;
|
||||||
Ok(b'\n') => break,
|
|
||||||
Ok(b) if b.is_ascii() => id.push(b as char),
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_) => break,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let id = id.trim().to_owned();
|
|
||||||
|
|
||||||
if id.starts_with("SSH-") {
|
if id.starts_with("SSH-") {
|
||||||
info!("Peer identifies as {:?}", id);
|
info!("Peer identifies as {:?}", id);
|
||||||
self.hash_data.client_id = Some(id);
|
self.hash_data.client_id = Some(id.to_owned());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -338,10 +331,10 @@ impl<'a> Connection {
|
||||||
{
|
{
|
||||||
"pty-req" => Some(ChannelRequest::Pty {
|
"pty-req" => Some(ChannelRequest::Pty {
|
||||||
term: reader.read_utf8()?,
|
term: reader.read_utf8()?,
|
||||||
char_width: reader.read_uint32()?,
|
chars: reader.read_uint32()? as u16,
|
||||||
row_height: reader.read_uint32()?,
|
rows: reader.read_uint32()? as u16,
|
||||||
pixel_width: reader.read_uint32()?,
|
pixel_width: reader.read_uint32()? as u16,
|
||||||
pixel_height: reader.read_uint32()?,
|
pixel_height: reader.read_uint32()? as u16,
|
||||||
modes: reader.read_string()?,
|
modes: reader.read_string()?,
|
||||||
}),
|
}),
|
||||||
"shell" => Some(ChannelRequest::Shell),
|
"shell" => Some(ChannelRequest::Shell),
|
||||||
|
|
@ -375,22 +368,7 @@ impl<'a> Connection {
|
||||||
let mut channel = self.channels.get_mut(&channel_id).unwrap();
|
let mut channel = self.channels.get_mut(&channel_id).unwrap();
|
||||||
channel.data(data.as_slice())?;
|
channel.data(data.as_slice())?;
|
||||||
|
|
||||||
let data = channel.read()?;
|
Ok(None)
|
||||||
|
|
||||||
if data.len() > 0 {
|
|
||||||
let mut res = Packet::new(MessageType::ChannelData);
|
|
||||||
res.with_writer(&|w| {
|
|
||||||
w.write_uint32(0)?;
|
|
||||||
w.write_bytes(data.as_slice())?;
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
|
|
||||||
Ok(Some(res))
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn kex_init(&mut self, packet: Packet) -> Result<Option<Packet>> {
|
fn kex_init(&mut self, packet: Packet) -> Result<Option<Packet>> {
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ impl Server {
|
||||||
TcpListener::bind((&*self.config.host, self.config.port))?;
|
TcpListener::bind((&*self.config.host, self.config.port))?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let (stream, addr) = listener.accept()?;
|
let (mut stream, addr) = listener.accept()?;
|
||||||
let config = self.config.clone();
|
let config = self.config.clone();
|
||||||
|
|
||||||
debug!("Incoming connection from {}", addr);
|
debug!("Incoming connection from {}", addr);
|
||||||
|
|
@ -35,7 +35,7 @@ impl Server {
|
||||||
let mut connection =
|
let mut connection =
|
||||||
Connection::new(ConnectionType::Server(config));
|
Connection::new(ConnectionType::Server(config));
|
||||||
|
|
||||||
let result = connection.run(stream);
|
let result = connection.run(&mut stream);
|
||||||
|
|
||||||
if let Some(error) = result.err() {
|
if let Some(error) = result.err() {
|
||||||
println!("sshd: {}", error)
|
println!("sshd: {}", error)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue