Client example
[?]
Nov 28, 2020, 10:18 AM
HDEDMPBT6TKIKQ67T2UYC7QEKF7PG5I6Y4CMRPBDACFY4S3XEWZQCDependencies
- [2]
7FRJYUI6Reboot because of a bad change
Change contents
- file addition: examples[2.165397]
- file addition: client.rs[0.11]
extern crate env_logger;extern crate futures;extern crate thrussh;extern crate thrussh_keys;extern crate tokio;use anyhow::Context;use std::sync::Arc;use thrussh::*;use thrussh_keys::*;struct Client {}impl client::Handler for Client {type FutureUnit = futures::future::Ready<Result<(Self, client::Session), anyhow::Error>>;type FutureBool = futures::future::Ready<Result<(Self, bool), anyhow::Error>>;fn finished_bool(self, b: bool) -> Self::FutureBool {futures::future::ready(Ok((self, b)))}fn finished(self, session: client::Session) -> Self::FutureUnit {futures::future::ready(Ok((self, session)))}fn check_server_key(self, server_public_key: &key::PublicKey) -> Self::FutureBool {println!("check_server_key: {:?}", server_public_key);self.finished_bool(true)}}#[tokio::main]async fn main() {env_logger::init();let config = thrussh::client::Config::default();let config = Arc::new(config);let sh = Client {};let mut agent = thrussh_keys::agent::client::AgentClient::connect_env().await.unwrap();let mut identities = agent.request_identities().await.unwrap();let mut session = thrussh::client::connect(config, "127.0.0.1:2200", sh).await.unwrap();let (_, auth_res) = session.authenticate_future("pe", identities.pop().unwrap(), agent).await.unwrap();println!("=== auth: {}", auth_res);let mut channel = session.channel_open_direct_tcpip("localhost", 8000, "localhost", 3333).await.unwrap();// let mut channel = session.channel_open_session().await.unwrap();println!("=== after open channel");let data = b"GET /les_affames.mkv HTTP/1.1\nUser-Agent: curl/7.68.0\nAccept: */*\nConnection: close\n\n";channel.data(&data[..]).await.unwrap();let mut f = std::fs::File::create("les_affames.mkv").unwrap();while let Some(msg) = channel.wait().await {use std::io::Write;match msg {thrussh::ChannelMsg::Data { ref data } => {f.write_all(&data).unwrap();}thrussh::ChannelMsg::Eof => {f.flush().unwrap();break;}_ => {}}}session.disconnect(Disconnect::ByApplication, "", "English").await.unwrap();let res = session.await.context("session await");println!("{:#?}", res);}