|
1 | 1 | extern crate thrussh;
|
2 |
| -extern crate env_logger; |
3 | 2 | extern crate futures;
|
4 |
| -#[macro_use] |
5 |
| -extern crate log; |
6 |
| -use std::sync::{Arc, Mutex}; |
| 3 | +extern crate tokio_core; |
| 4 | +extern crate env_logger; |
| 5 | +use std::sync::Arc; |
7 | 6 | use thrussh::*;
|
8 |
| -use thrussh::server::*; |
9 |
| -#[derive(Debug, Clone, Default)] |
10 |
| -struct H(Arc<Mutex<HH>>); |
11 |
| -#[derive(Debug, Default)] |
12 |
| -struct HH { |
13 |
| - user: String, |
14 |
| - password: String, |
15 |
| -} |
16 | 7 |
|
17 |
| -impl thrussh::server::Handler for H { |
18 |
| - type FutureAuth = futures::Finished<Auth, Error>; |
| 8 | +#[derive(Clone)] |
| 9 | +struct H{} |
| 10 | + |
| 11 | +impl server::Handler for H { |
| 12 | + type FutureAuth = futures::Finished<server::Auth, Error>; |
19 | 13 | type FutureUnit = futures::Finished<(), Error>;
|
20 | 14 | type FutureBool = futures::Finished<bool, Error>;
|
21 | 15 |
|
22 |
| - fn auth_password(&mut self, user: &str, password: &str) -> Self::FutureBool { |
23 |
| - let mut h = self.0.lock().unwrap(); |
24 |
| - h.user.push_str(user); |
25 |
| - h.password.clear(); |
26 |
| - h.password.push_str(password); |
| 16 | + fn auth_publickey(&mut self, _: &str, _: &key::PublicKey) -> Self::FutureBool { |
27 | 17 | futures::finished(true)
|
28 | 18 | }
|
29 |
| - fn auth_keyboard_interactive(&mut self, user: &str, _: &str, response: Option<thrussh::server::Response>) -> Self::FutureAuth { |
30 |
| - println!("Keyboard interactive, user {:?}", user); |
31 |
| - if let Some(resp) = response { |
32 |
| - |
33 |
| - for resp in resp { |
34 |
| - println!("resp: {:?}", resp) |
35 |
| - } |
36 |
| - futures::finished(Auth::Accept) |
37 |
| - |
38 |
| - } else { |
39 |
| - |
40 |
| - futures::finished(Auth::Partial { |
41 |
| - name: "name".to_string(), |
42 |
| - instructions: "type your password".to_string(), |
43 |
| - prompts: vec![("Password: ".to_string(), false)] |
44 |
| - }) |
45 |
| - } |
| 19 | + fn data(&mut self, channel: ChannelId, data: &[u8], session: &mut server::Session) -> Self::FutureUnit { |
| 20 | + println!("data on channel {:?}: {:?}", channel, std::str::from_utf8(data)); |
| 21 | + session.data(channel, None, data).unwrap(); |
| 22 | + futures::finished(()) |
46 | 23 | }
|
| 24 | +} |
47 | 25 |
|
48 |
| - fn auth_publickey(&mut self, user: &str, public_key: &key::PublicKey) -> Self::FutureBool { |
49 |
| - debug!("publickey request by user {:?}, pub {:?}", user, public_key); |
| 26 | + |
| 27 | +use std::net::ToSocketAddrs; |
| 28 | +use futures::Future; |
| 29 | +use tokio_core::net::TcpStream; |
| 30 | +use tokio_core::reactor::Core; |
| 31 | + |
| 32 | + |
| 33 | +struct Client { } |
| 34 | + |
| 35 | +impl client::Handler for Client { |
| 36 | + type FutureBool = futures::Finished<bool, Error>; |
| 37 | + type FutureUnit = futures::Finished<(), Error>; |
| 38 | + fn check_server_key(&mut self, server_public_key: &key::PublicKey) -> Self::FutureBool { |
| 39 | + println!("check_server_key: {:?}", server_public_key); |
50 | 40 | futures::finished(true)
|
51 | 41 | }
|
52 |
| - fn channel_open_session(&mut self, channel: ChannelId, session: &mut Session) -> Self::FutureUnit { |
53 |
| - debug!("channel open {:?}", channel); |
54 |
| - session.data(channel, None, b"Test !").unwrap(); |
| 42 | + fn channel_open_confirmation(&mut self, channel: ChannelId, _: &mut client::Session) -> Self::FutureUnit { |
| 43 | + println!("channel_open_confirmation: {:?}", channel); |
55 | 44 | futures::finished(())
|
56 | 45 | }
|
57 |
| - fn data(&mut self, channel: ChannelId, data: &[u8], session: &mut Session) -> Self::FutureUnit { |
58 |
| - debug!("data on channel {:?}: {:?}", channel, std::str::from_utf8(data)); |
59 |
| - session.data(channel, None, data).unwrap(); |
| 46 | + fn data(&mut self, channel: ChannelId, ext: Option<u32>, data: &[u8], _: &mut client::Session) -> Self::FutureUnit { |
| 47 | + println!("data on channel {:?} {:?}: {:?}", ext, channel, std::str::from_utf8(data)); |
60 | 48 | futures::finished(())
|
61 | 49 | }
|
62 | 50 | }
|
63 | 51 |
|
| 52 | +impl Client { |
| 53 | + fn run(self, config: Arc<client::Config>, addr: &str) { |
| 54 | + |
| 55 | + let addr = addr.to_socket_addrs().unwrap().next().unwrap(); |
| 56 | + let mut l = Core::new().unwrap(); |
| 57 | + let handle = l.handle(); |
| 58 | + let done = |
| 59 | + TcpStream::connect(&addr, &handle).map_err(|err| thrussh::Error::IO(err)).and_then(|socket| { |
| 60 | + |
| 61 | + println!("connected"); |
| 62 | + let mut connection = client::Connection::new( |
| 63 | + config.clone(), |
| 64 | + socket, |
| 65 | + self, |
| 66 | + None |
| 67 | + ); |
| 68 | + |
| 69 | + connection.set_auth_user("pe"); |
| 70 | + connection.set_auth_public_key(thrussh::load_secret_key("/home/pe/.ssh/id_ed25519").unwrap()); |
| 71 | + // debug!("connection"); |
| 72 | + connection.authenticate().and_then(|connection| { |
| 73 | + |
| 74 | + connection.channel_open_session().and_then(|(mut connection, chan)| { |
| 75 | + |
| 76 | + connection.data(chan, None, b"First test").unwrap(); |
| 77 | + connection.data(chan, None, b"Second test").unwrap(); |
| 78 | + connection.disconnect(Disconnect::ByApplication, "Ciao", ""); |
| 79 | + connection |
| 80 | + |
| 81 | + }) |
| 82 | + }) |
| 83 | + }); |
| 84 | + l.run(done).unwrap(); |
| 85 | + } |
| 86 | + |
| 87 | +} |
64 | 88 |
|
65 | 89 | fn main() {
|
66 | 90 | env_logger::init().unwrap();
|
67 |
| - let mut config = thrussh::server::Config::default(); |
| 91 | + // Starting the server thread. |
| 92 | + let t = std::thread::spawn(|| { |
| 93 | + let mut config = thrussh::server::Config::default(); |
| 94 | + config.connection_timeout = Some(std::time::Duration::from_secs(600)); |
| 95 | + config.auth_rejection_time = std::time::Duration::from_secs(3); |
| 96 | + config.keys.push(thrussh::key::Algorithm::generate_keypair(thrussh::key::ED25519).unwrap()); |
| 97 | + let config = Arc::new(config); |
| 98 | + let sh = H{}; |
| 99 | + thrussh::server::run(config, "0.0.0.0:2222", sh); |
| 100 | + }); |
| 101 | + |
| 102 | + std::thread::sleep(std::time::Duration::from_secs(1)); |
| 103 | + let mut config = thrussh::client::Config::default(); |
68 | 104 | config.connection_timeout = Some(std::time::Duration::from_secs(600));
|
69 |
| - config.auth_rejection_time = std::time::Duration::from_secs(3); |
70 |
| - config.keys.push(thrussh::load_secret_key("ssh_host_ed25519_key").unwrap()); |
71 | 105 | let config = Arc::new(config);
|
72 |
| - let sh = H::default(); |
73 |
| - thrussh::server::run(config, "0.0.0.0:2222", sh); |
| 106 | + let sh = Client {}; |
| 107 | + sh.run(config, "127.0.0.1:2222"); |
| 108 | + |
| 109 | + // Kill the server thread after the client has ended. |
| 110 | + std::mem::forget(t) |
74 | 111 | }
|
0 commit comments