Skip to content

Commit d1de446

Browse files
author
pe
committed
More typing
Ignore-this: 87fe0aa8644251e1f6c3139df626ac0e darcs-hash:aaeed3a08cef00ac6566b7c1a0b0fa4976ed07a3
1 parent ab4115f commit d1de446

File tree

9 files changed

+331
-154
lines changed

9 files changed

+331
-154
lines changed

src/bin/client.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn run<H: Handler + 'static>(config: Arc<Config>, addr: &str, handler: H) {
1919
let addr = addr.to_socket_addrs().unwrap().next().unwrap();
2020
let mut l = Core::new().unwrap();
2121
let handle = l.handle();
22-
let done:futures::future::AndThen<_,Connection<TcpStream, H>,_> =
22+
let done =
2323
TcpStream::connect(&addr, &handle).map_err(|err| thrussh::Error::IO(err)).and_then(|socket| {
2424

2525
println!("connected");
@@ -33,7 +33,15 @@ fn run<H: Handler + 'static>(config: Arc<Config>, addr: &str, handler: H) {
3333
connection.set_auth_user("pe");
3434
connection.set_auth_public_key(thrussh::load_secret_key("/home/pe/.ssh/id_ed25519").unwrap());
3535
debug!("connection");
36-
connection
36+
connection.authenticate().and_then(|mut connection| {
37+
38+
connection.channel_open_session().and_then(|(mut connection, chan)| {
39+
40+
try!(connection.data(chan, None, b"blabla"));
41+
Ok(())
42+
43+
})
44+
})
3745
});
3846
l.run(done).unwrap();
3947
}
@@ -54,5 +62,5 @@ fn main() {
5462

5563
let config = Arc::new(config);
5664
let sh = H {};
57-
run(config, "127.0.0.1:22", sh);
65+
run(config, "127.0.0.1:2222", sh);
5866
}

src/bin/server.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct HH {
1111
user: String,
1212
password: String,
1313
}
14-
x
14+
1515
impl thrussh::server::Handler for H {
1616
type FutureAuth = futures::Finished<Auth, Error>;
1717
type FutureUnit = futures::Finished<(), Error>;
@@ -44,26 +44,26 @@ impl thrussh::server::Handler for H {
4444
}
4545

4646
fn auth_none(&mut self, user: &str) -> Self::FutureBool {
47-
futures::finished(false)
47+
futures::finished(true)
4848
}
4949

5050
fn auth_publickey(&mut self, user: &str, public_key: &key::PublicKey) -> Self::FutureBool {
51-
futures::finished(false)
51+
futures::finished(true)
5252
}
5353

54-
fn channel_close(&mut self, channel: u32, session: &mut Session) -> Self::FutureUnit {
54+
fn channel_close(&mut self, channel: ChannelId, session: &mut Session) -> Self::FutureUnit {
5555
futures::finished(())
5656
}
5757

58-
fn channel_eof(&mut self, channel: u32, session: &mut Session) -> Self::FutureUnit {
58+
fn channel_eof(&mut self, channel: ChannelId, session: &mut Session) -> Self::FutureUnit {
5959
futures::finished(())
6060
}
6161

62-
fn channel_open_session(&mut self, channel: u32, session: &mut Session) -> Self::FutureUnit {
62+
fn channel_open_session(&mut self, channel: ChannelId, session: &mut Session) -> Self::FutureUnit {
6363
futures::finished(())
6464
}
6565
fn channel_open_x11(&mut self,
66-
channel: u32,
66+
channel: ChannelId,
6767
originator_address: &str,
6868
originator_port: u32,
6969
session: &mut Session)
@@ -72,7 +72,7 @@ impl thrussh::server::Handler for H {
7272
}
7373

7474
fn channel_open_direct_tcpip(&mut self,
75-
channel: u32,
75+
channel: ChannelId,
7676
host_to_connect: &str,
7777
port_to_connect: u32,
7878
originator_address: &str,
@@ -82,20 +82,21 @@ impl thrussh::server::Handler for H {
8282
futures::finished(())
8383
}
8484

85-
fn data(&mut self, channel: u32, data: &[u8], session: &mut Session) -> Self::FutureUnit {
85+
fn data(&mut self, channel: ChannelId, data: &[u8], session: &mut Session) -> Self::FutureUnit {
86+
println!("======== data: {:?} =======", std::str::from_utf8(data));
8687
futures::finished(())
8788
}
8889

89-
fn extended_data(&mut self, channel: u32, code: u32, data: &[u8], session: &mut Session) -> Self::FutureUnit {
90+
fn extended_data(&mut self, channel: ChannelId, code: u32, data: &[u8], session: &mut Session) -> Self::FutureUnit {
9091
futures::finished(())
9192
}
9293

93-
fn window_adjusted(&mut self, channel: u32, session: &mut Session) -> Self::FutureUnit {
94+
fn window_adjusted(&mut self, channel: ChannelId, session: &mut Session) -> Self::FutureUnit {
9495
futures::finished(())
9596
}
9697

9798
fn pty_request(&mut self,
98-
channel: u32,
99+
channel: ChannelId,
99100
term: &str,
100101
col_width: u32,
101102
row_height: u32,
@@ -108,7 +109,7 @@ impl thrussh::server::Handler for H {
108109
}
109110

110111
fn x11_request(&mut self,
111-
channel: u32,
112+
channel: ChannelId,
112113
single_connection: bool,
113114
x11_auth_protocol: &str,
114115
x11_auth_cookie: &str,
@@ -119,28 +120,28 @@ impl thrussh::server::Handler for H {
119120
}
120121

121122
fn env_request(&mut self,
122-
channel: u32,
123+
channel: ChannelId,
123124
variable_name: &str,
124125
variable_value: &str,
125126
session: &mut Session)
126127
-> Self::FutureUnit {
127128
futures::finished(())
128129
}
129130

130-
fn shell_request(&mut self, channel: u32, session: &mut Session) -> Self::FutureUnit {
131+
fn shell_request(&mut self, channel: ChannelId, session: &mut Session) -> Self::FutureUnit {
131132
futures::finished(())
132133
}
133134

134-
fn exec_request(&mut self, channel: u32, data: &[u8], session: &mut Session) -> Self::FutureUnit {
135+
fn exec_request(&mut self, channel: ChannelId, data: &[u8], session: &mut Session) -> Self::FutureUnit {
135136
futures::finished(())
136137
}
137138

138-
fn subsystem_request(&mut self, channel: u32, name: &str, session: &mut Session) -> Self::FutureUnit {
139+
fn subsystem_request(&mut self, channel: ChannelId, name: &str, session: &mut Session) -> Self::FutureUnit {
139140
futures::finished(())
140141
}
141142

142143
fn window_change_request(&mut self,
143-
channel: u32,
144+
channel: ChannelId,
144145
col_width: u32,
145146
row_height: u32,
146147
pix_width: u32,
@@ -150,7 +151,7 @@ impl thrussh::server::Handler for H {
150151
futures::finished(())
151152
}
152153

153-
fn signal(&mut self, channel: u32, signal_name: Sig, session: &mut Session) -> Self::FutureUnit {
154+
fn signal(&mut self, channel: ChannelId, signal_name: Sig, session: &mut Session) -> Self::FutureUnit {
154155
futures::finished(())
155156
}
156157

src/cipher/mod.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,29 +150,21 @@ impl CipherPair {
150150
buffer.buffer.clear();
151151
let mut len = [0; 4];
152152
try!(stream.read_exact(&mut len));
153-
debug!("len = {:?}", len);
154153
buffer.buffer.extend(&len);
155154
let len = key.decrypt_packet_length(seqn, len);
156155
buffer.len = BigEndian::read_u32(&len) as usize + key.tag_len();
157156
}
158-
debug!("buffer len: {:?} {:?}", buffer.len, key.tag_len());
159157

160158
try!(read(stream, &mut buffer.buffer, buffer.len, &mut buffer.bytes));
161-
use std::ops::Deref;
162-
{
163-
let a: &[u8] = buffer.buffer.deref();
164-
debug!("buffer: {:?}", a);
165-
}
159+
166160
let ciphertext_len = buffer.buffer.len() - key.tag_len();
167161
let (ciphertext, tag) = buffer.buffer.split_at_mut(ciphertext_len);
168162
let plaintext = try!(key.open(seqn, ciphertext, tag));
169163

170-
debug!("clear: {:?}", plaintext);
171164
let padding_length = plaintext[0] as usize;
172165
let plaintext_end = try!(plaintext.len()
173166
.checked_sub(padding_length)
174167
.ok_or(Error::IndexOutOfBounds));
175-
debug!("padding length {:?} {:?}", padding_length, plaintext);
176168

177169
// Sequence numbers are on 32 bits and wrap.
178170
// https://tools.ietf.org/html/rfc4253#section-6.4
@@ -204,13 +196,10 @@ impl CipherPair {
204196
key.fill_padding(buffer.buffer.reserve(padding_length));
205197
buffer.buffer.reserve(key.tag_len());
206198

207-
{
208-
let (plaintext, tag) = buffer.buffer[offset..]
209-
.split_at_mut(PACKET_LENGTH_LEN + packet_length);
199+
let (plaintext, tag) = buffer.buffer[offset..]
200+
.split_at_mut(PACKET_LENGTH_LEN + packet_length);
210201

211-
key.seal(buffer.seqn.0, plaintext, tag);
212-
debug!("write sealed: {:?}", plaintext);
213-
}
202+
key.seal(buffer.seqn.0, plaintext, tag);
214203

215204
// Sequence numbers are on 32 bits and wrap.
216205
// https://tools.ietf.org/html/rfc4253#section-6.4

src/client/encrypted.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414
//
1515
use cryptovec::CryptoVec;
16-
use {Sig, Error, ChannelOpenFailure};
16+
use {Sig, Error, ChannelOpenFailure, ChannelId};
1717
use std;
1818
use auth;
1919
use session::*;
@@ -133,7 +133,7 @@ impl super::Session {
133133
msg::CHANNEL_OPEN_CONFIRMATION => {
134134
debug!("channel_confirmation? {:?}", buf);
135135
let mut reader = buf.reader(1);
136-
let id_send = try!(reader.read_u32());
136+
let id_send = ChannelId(try!(reader.read_u32()));
137137
let id_recv = try!(reader.read_u32());
138138
let window = try!(reader.read_u32());
139139
let max_packet = try!(reader.read_u32());
@@ -156,20 +156,20 @@ impl super::Session {
156156
}
157157
msg::CHANNEL_CLOSE => {
158158
let mut r = buf.reader(1);
159-
let channel_num = try!(r.read_u32());
159+
let channel_num = ChannelId(try!(r.read_u32()));
160160
if let Some(ref mut enc) = self.0.encrypted {
161161
enc.channels.remove(&channel_num);
162162
}
163163
try!(client.channel_close(channel_num, self));
164164
}
165165
msg::CHANNEL_EOF => {
166166
let mut r = buf.reader(1);
167-
let channel_num = try!(r.read_u32());
167+
let channel_num = ChannelId(try!(r.read_u32()));
168168
try!(client.channel_eof(channel_num, self));
169169
}
170170
msg::CHANNEL_OPEN_FAILURE => {
171171
let mut r = buf.reader(1);
172-
let channel_num = try!(r.read_u32());
172+
let channel_num = ChannelId(try!(r.read_u32()));
173173
let reason_code = ChannelOpenFailure::from_u32(try!(r.read_u32())).unwrap();
174174
let descr = try!(std::str::from_utf8(try!(r.read_string())));
175175
let language = try!(std::str::from_utf8(try!(r.read_string())));
@@ -182,7 +182,7 @@ impl super::Session {
182182
}
183183
msg::CHANNEL_DATA => {
184184
let mut r = buf.reader(1);
185-
let channel_num = try!(r.read_u32());
185+
let channel_num = ChannelId(try!(r.read_u32()));
186186
let data = try!(r.read_string());
187187
try!(client.data(channel_num, None, &data, self));
188188
let target = self.0.config.window_size;
@@ -192,7 +192,7 @@ impl super::Session {
192192
}
193193
msg::CHANNEL_EXTENDED_DATA => {
194194
let mut r = buf.reader(1);
195-
let channel_num = try!(r.read_u32());
195+
let channel_num = ChannelId(try!(r.read_u32()));
196196
let extended_code = try!(r.read_u32());
197197
let data = try!(r.read_string());
198198
try!(client.data(channel_num, Some(extended_code), &data, self));
@@ -203,7 +203,7 @@ impl super::Session {
203203
}
204204
msg::CHANNEL_REQUEST => {
205205
let mut r = buf.reader(1);
206-
let channel_num = try!(r.read_u32());
206+
let channel_num = ChannelId(try!(r.read_u32()));
207207
let req = try!(r.read_string());
208208
match req {
209209
b"forwarded_tcpip" => {
@@ -241,7 +241,7 @@ impl super::Session {
241241
}
242242
msg::CHANNEL_WINDOW_ADJUST => {
243243
let mut r = buf.reader(1);
244-
let channel_num = try!(r.read_u32());
244+
let channel_num = ChannelId(try!(r.read_u32()));
245245
let amount = try!(r.read_u32());
246246
if let Some(ref mut enc) = self.0.encrypted {
247247
if let Some(ref mut channel) = enc.channels.get_mut(&channel_num) {

0 commit comments

Comments
 (0)