1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use super::channel::ChannelBroker;
use crate::constant::{ssh_msg_code, ssh_str};
use crate::error::SshResult;
use crate::model::Data;
use std::ops::{Deref, DerefMut};
pub struct ShellBrocker(ChannelBroker);
impl ShellBrocker {
pub(crate) fn open(channel: ChannelBroker) -> SshResult<Self> {
let mut channel_shell = ShellBrocker(channel);
channel_shell.request_pty()?;
channel_shell.get_shell()?;
Ok(channel_shell)
}
fn request_pty(&mut self) -> SshResult<()> {
let mut data = Data::new();
data.put_u8(ssh_msg_code::SSH_MSG_CHANNEL_REQUEST)
.put_u32(self.server_channel_no)
.put_str(ssh_str::PTY_REQ)
.put_u8(true as u8)
.put_str(ssh_str::XTERM_VAR)
.put_u32(80)
.put_u32(24)
.put_u32(640)
.put_u32(480);
let model = [
128, 0, 1, 0xc2, 0, 129, 0, 1, 0xc2, 0, 0_u8, ];
data.put_u8s(&model);
self.send(data)
}
fn get_shell(&mut self) -> SshResult<()> {
let mut data = Data::new();
data.put_u8(ssh_msg_code::SSH_MSG_CHANNEL_REQUEST)
.put_u32(self.server_channel_no)
.put_str(ssh_str::SHELL)
.put_u8(true as u8);
self.send(data)
}
pub fn read(&mut self) -> SshResult<Vec<u8>> {
let mut out = self.recv()?;
while let Ok(Some(mut data)) = self.try_recv() {
out.append(&mut data)
}
Ok(out)
}
pub fn write(&mut self, buf: &[u8]) -> SshResult<()> {
self.send_data(buf.to_vec().into())?;
Ok(())
}
pub fn close(self) -> SshResult<()> {
self.0.close()
}
}
impl Deref for ShellBrocker {
type Target = ChannelBroker;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ShellBrocker {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}