Skip to content

Commit d5d100c

Browse files
committed
Unified test: server+client
Ignore-this: 8096940d6198b4b477ca86293b6d62b6 darcs-hash:61237c45c448d7cafad078d24bc4b4cce71063ad
1 parent 2ef8115 commit d5d100c

File tree

1 file changed

+86
-49
lines changed

1 file changed

+86
-49
lines changed

src/bin/server.rs

Lines changed: 86 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,111 @@
11
extern crate thrussh;
2-
extern crate env_logger;
32
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;
76
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-
}
167

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>;
1913
type FutureUnit = futures::Finished<(), Error>;
2014
type FutureBool = futures::Finished<bool, Error>;
2115

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 {
2717
futures::finished(true)
2818
}
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(())
4623
}
24+
}
4725

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);
5040
futures::finished(true)
5141
}
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);
5544
futures::finished(())
5645
}
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));
6048
futures::finished(())
6149
}
6250
}
6351

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+
}
6488

6589
fn main() {
6690
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();
68104
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());
71105
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)
74111
}

0 commit comments

Comments
 (0)