-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TestServer to test SSH connections.
Also add a very basic test to check the ssh_connect NASL function.
- Loading branch information
Showing
8 changed files
with
760 additions
and
51 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
mod error; | ||
mod libssh; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
type SessionId = i32; | ||
pub use self::libssh::Ssh; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
mod server; | ||
|
||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
use russh::server::Server as _; | ||
use server::TestServer; | ||
|
||
use crate::nasl::test_prelude::TestBuilder; | ||
use crate::nasl::NoOpLoader; | ||
use crate::storage::DefaultDispatcher; | ||
|
||
const PORT: u16 = 2223; | ||
|
||
#[tokio::test] | ||
async fn ssh_connect() { | ||
run_test(|mut t| { | ||
t.ok(format!("id = ssh_connect(port:{});", PORT), 9000); | ||
t.ok(format!("id = ssh_connect(port:{});", PORT), 9001); | ||
}) | ||
.await | ||
} | ||
|
||
async fn run_test(f: impl Fn(TestBuilder<NoOpLoader, DefaultDispatcher>) -> () + Send + 'static) { | ||
let server = tokio::time::timeout(Duration::from_millis(2000), run_server()); | ||
let client = tokio::task::spawn_blocking(move || { | ||
std::thread::sleep(Duration::from_millis(1000)); | ||
let t = TestBuilder::default(); | ||
f(t) | ||
}); | ||
let (ser, res) = futures::join!(server, client); | ||
assert!(ser.is_err()); | ||
res.unwrap() | ||
} | ||
|
||
async fn run_server() { | ||
let config = russh::server::Config { | ||
inactivity_timeout: Some(Duration::from_secs(3600)), | ||
auth_rejection_time: Duration::from_secs(3), | ||
auth_rejection_time_initial: Some(Duration::from_secs(0)), | ||
keys: vec![russh_keys::key::KeyPair::generate_ed25519().unwrap()], | ||
..Default::default() | ||
}; | ||
let config = Arc::new(config); | ||
let mut server = TestServer::default(); | ||
server | ||
.run_on_address(config, ("0.0.0.0", PORT)) | ||
.await | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use russh::keys::*; | ||
use russh::server::{Msg, Session}; | ||
use russh::*; | ||
use tokio::sync::Mutex; | ||
|
||
#[derive(Clone, Default)] | ||
pub struct TestServer { | ||
clients: Arc<Mutex<HashMap<(usize, ChannelId), russh::server::Handle>>>, | ||
id: usize, | ||
} | ||
|
||
impl TestServer { | ||
async fn post(&mut self, data: CryptoVec) { | ||
let mut clients = self.clients.lock().await; | ||
for ((id, channel), ref mut s) in clients.iter_mut() { | ||
if *id != self.id { | ||
let _ = s.data(*channel, data.clone()).await; | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl server::Server for TestServer { | ||
type Handler = Self; | ||
|
||
fn new_client(&mut self, _: Option<std::net::SocketAddr>) -> Self { | ||
let s = self.clone(); | ||
self.id += 1; | ||
s | ||
} | ||
|
||
fn handle_session_error(&mut self, _error: <Self::Handler as russh::server::Handler>::Error) { | ||
eprintln!("Session error: {:#?}", _error); | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl server::Handler for TestServer { | ||
type Error = russh::Error; | ||
|
||
async fn channel_open_session( | ||
&mut self, | ||
channel: Channel<Msg>, | ||
session: &mut Session, | ||
) -> Result<bool, Self::Error> { | ||
{ | ||
let mut clients = self.clients.lock().await; | ||
clients.insert((self.id, channel.id()), session.handle()); | ||
} | ||
Ok(true) | ||
} | ||
|
||
async fn auth_publickey( | ||
&mut self, | ||
_: &str, | ||
_: &key::PublicKey, | ||
) -> Result<server::Auth, Self::Error> { | ||
Ok(server::Auth::Accept) | ||
} | ||
|
||
async fn data( | ||
&mut self, | ||
channel: ChannelId, | ||
data: &[u8], | ||
session: &mut Session, | ||
) -> Result<(), Self::Error> { | ||
// Sending Ctrl+C ends the session and disconnects the client | ||
if data == [3] { | ||
return Err(russh::Error::Disconnect); | ||
} | ||
|
||
let data = CryptoVec::from(format!("Got data: {}\r\n", String::from_utf8_lossy(data))); | ||
self.post(data.clone()).await; | ||
session.data(channel, data); | ||
Ok(()) | ||
} | ||
|
||
async fn tcpip_forward( | ||
&mut self, | ||
address: &str, | ||
port: &mut u32, | ||
session: &mut Session, | ||
) -> Result<bool, Self::Error> { | ||
let handle = session.handle(); | ||
let address = address.to_string(); | ||
let port = *port; | ||
tokio::spawn(async move { | ||
let channel = handle | ||
.channel_open_forwarded_tcpip(address, port, "1.2.3.4", 1234) | ||
.await | ||
.unwrap(); | ||
let _ = channel.data(&b"Hello from a forwarded port"[..]).await; | ||
let _ = channel.eof().await; | ||
}); | ||
Ok(true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters