Skip to content

Commit

Permalink
Update sftp examples (#357)
Browse files Browse the repository at this point in the history
The current examples are a bit outdated and have some issues like
AspectUnk/russh-sftp#52 (comment)
  • Loading branch information
AspectUnk authored Sep 29, 2024
1 parent 4cc3e09 commit cd84f4d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion russh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ features = ["openssl"]

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
openssl = { workspace = true, optional = true }
russh-sftp = "2.0.0-beta.2"
russh-sftp = "2.0.5"
tokio = { workspace = true }
21 changes: 15 additions & 6 deletions russh/examples/sftp_client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::sync::Arc;

use async_trait::async_trait;
use log::{error, info, LevelFilter};
use russh::keys::*;
use russh::*;
use russh_sftp::client::SftpSession;
use russh_keys::*;
use russh_sftp::{client::SftpSession, protocol::OpenFlags};
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};

struct Client;
Expand Down Expand Up @@ -43,7 +42,11 @@ async fn main() {
let mut session = russh::client::connect(Arc::new(config), ("localhost", 22), sh)
.await
.unwrap();
if session.authenticate_password("root", "pass").await.unwrap() {
if session
.authenticate_password("root", "password")
.await
.unwrap()
{
let channel = session.channel_open_session().await.unwrap();
channel.request_subsystem(true, "sftp").await.unwrap();
let sftp = SftpSession::new(channel.into_stream()).await.unwrap();
Expand Down Expand Up @@ -72,7 +75,13 @@ async fn main() {

// interaction with i/o
let filename = "test_new.txt";
let mut file = sftp.create(filename).await.unwrap();
let mut file = sftp
.open_with_flags(
filename,
OpenFlags::CREATE | OpenFlags::TRUNCATE | OpenFlags::WRITE | OpenFlags::READ,
)
.await
.unwrap();
info!("metadata by handle: {:?}", file.metadata().await.unwrap());

file.write_all(b"magic text").await.unwrap();
Expand Down
46 changes: 22 additions & 24 deletions russh/examples/sftp_server.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use log::{error, info, LevelFilter};
use russh::keys::key::KeyPair;
use russh::server::{Auth, Msg, Server as _, Session};
use russh::{Channel, ChannelId};
use russh::{
server::{Auth, Msg, Server as _, Session},
Channel, ChannelId,
};
use russh_keys::key::KeyPair;
use russh_sftp::protocol::{File, FileAttributes, Handle, Name, Status, StatusCode, Version};
use std::{collections::HashMap, net::SocketAddr, sync::Arc, time::Duration};
use tokio::sync::Mutex;

#[derive(Clone)]
Expand Down Expand Up @@ -71,6 +69,17 @@ impl russh::server::Handler for SshSession {
Ok(true)
}

async fn channel_eof(
&mut self,
channel: ChannelId,
session: &mut Session,
) -> Result<(), Self::Error> {
// After a client has sent an EOF, indicating that they don't want
// to send more data in this session, the channel can be closed.
session.close(channel);
Ok(())
}

async fn subsystem_request(
&mut self,
channel_id: ChannelId,
Expand Down Expand Up @@ -143,31 +152,20 @@ impl russh_sftp::server::Handler for SftpSession {
return Ok(Name {
id,
files: vec![
File {
filename: "foo".to_string(),
longname: "".to_string(),
attrs: FileAttributes::default(),
},
File {
filename: "bar".to_string(),
longname: "".to_string(),
attrs: FileAttributes::default(),
},
File::new("foo", FileAttributes::default()),
File::new("bar", FileAttributes::default()),
],
});
}
Ok(Name { id, files: vec![] })
// If all files have been sent to the client, respond with an EOF
Err(StatusCode::Eof)
}

async fn realpath(&mut self, id: u32, path: String) -> Result<Name, Self::Error> {
info!("realpath: {}", path);
Ok(Name {
id,
files: vec![File {
filename: "/".to_string(),
longname: "".to_string(),
attrs: FileAttributes::default(),
}],
files: vec![File::dummy("/")],
})
}
}
Expand Down

0 comments on commit cd84f4d

Please sign in to comment.