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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
mod codec;
pub mod error;

use encoding::DecoderTrap;
use encoding::{all::GBK, Encoding};
use futures::stream::StreamExt;
use tokio::{
    io::AsyncWriteExt,
    net::TcpStream,
    time::{self, Duration},
};
use tokio_util::codec::FramedRead;

use crate::codec::{Item, TelnetCodec};
use crate::error::TelnetError;

pub struct Telnet {
    content: Vec<u8>,
    stream: TcpStream,
    prompt: String,
    username_prompt: String,
    password_prompt: String,
}

impl Telnet {
    // Format the end of the string as a `\n`
    fn format_enter_str(s: &str) -> String {
        if !s.ends_with('\n') {
            format!("{}\n", s)
        } else {
            s.to_string()
        }
    }

    // Connect, default prompt is openwrt's prompt.
    pub async fn connect(addr: &str, timeout: Duration) -> Result<Self, TelnetError> {
        let res = time::timeout(timeout, TcpStream::connect(addr)).await?;
        Ok(Telnet {
            content: vec![],
            stream: res?,
            prompt: String::from("~# "),
            username_prompt: String::from("login: "),
            password_prompt: String::from("Password: "),
        })
    }

    pub async fn login(
        &mut self,
        username: &str,
        password: &str,
        timeout: Duration,
    ) -> Result<(), TelnetError> {
        let user = Telnet::format_enter_str(username);
        let pass = Telnet::format_enter_str(password);

        let (read, mut write) = self.stream.split();
        let mut telnet = FramedRead::new(read, TelnetCodec::default());

        loop {
            match time::timeout(timeout, telnet.next()).await {
                Ok(res) => {
                    if let Some(res) = res {
                        match res? {
                            Item::Do(i) | Item::Dont(i) => {
                                // set window size
                                if i == 0x1f {
                                    write
                                        .write(&[
                                            0xff, 0xfb, 0x1f, 0xff, 0xfa, 0x1f, 0x00, 0xfc, 0x00,
                                            0x1b, 0xff, 0xf0,
                                        ])
                                        .await?;
                                } else {
                                    write.write(&[0xff, 0xfc, i]).await?;
                                }
                            }
                            Item::Will(i) | Item::Wont(i) => {
                                write.write(&[0xff, 0xfe, i]).await?;
                            }
                            Item::Line(content) => {
                                if content.ends_with(self.username_prompt.as_bytes()) {
                                    write.write(user.as_bytes()).await?;
                                } else if content.ends_with(self.password_prompt.as_bytes()) {
                                    write.write(pass.as_bytes()).await?;
                                } else if content.ends_with(self.prompt.as_bytes()) {
                                    return Ok(());
                                }
                            }
                            item => return Err(TelnetError::UnknownIAC(format!("{:?}", item))),
                        }
                    }
                }
                Err(e) => return Err(TelnetError::Timeout(e)),
            }
        }
    }

    pub async fn execute(&mut self, cmd: &str, timeout: Duration) -> Result<String, TelnetError> {
        let command = Telnet::format_enter_str(cmd);
        let (read, mut write) = self.stream.split();
        match time::timeout(timeout, write.write(command.as_bytes())).await {
            Ok(res) => res?,
            Err(e) => return Err(TelnetError::Timeout(e)),
        };
        let mut telnet = FramedRead::new(read, TelnetCodec::default());

        loop {
            match time::timeout(timeout, telnet.next()).await {
                Ok(res) => {
                    if let Some(item) = res {
                        if let Item::Line(mut line) = item? {
                            if line.ends_with(self.prompt.as_bytes()) {
                                break;
                            }
                            if line.starts_with(cmd.as_bytes()) {
                                continue;
                            }
                            self.content.append(&mut line);
                        }
                    }
                }
                Err(e) => return Err(TelnetError::Timeout(e)),
            }
        }
        let output = String::from_utf8(self.content.clone());
        let result = match output {
            Ok(s) => Ok(s),
            Err(e) => match GBK.decode(&self.content, DecoderTrap::Strict) {
                Ok(gbk_out) => Ok(gbk_out),
                Err(_) => Err(TelnetError::ParseError(e)),
            },
        };
        self.content.clear();
        result
    }

    pub fn set_prompt(&mut self, prompt: &str) -> &mut Self {
        self.prompt = prompt.to_string();
        self
    }

    pub fn set_username_prompt(&mut self, prompt: &str) -> &mut Self {
        self.username_prompt = prompt.to_string();
        self
    }

    pub fn set_password_prompt(&mut self, prompt: &str) -> &mut Self {
        self.password_prompt = prompt.to_string();
        self
    }
}