-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blocking): initial implementation (#158)
- Loading branch information
Showing
21 changed files
with
826 additions
and
79 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "ironrdp-blocking" | ||
version = "0.1.0" | ||
readme = "README.md" | ||
description = "Blocking I/O abstraction wrapping the IronRDP state machines conveniently" | ||
edition.workspace = true | ||
license.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
authors.workspace = true | ||
keywords.workspace = true | ||
categories.workspace = true | ||
|
||
[lib] | ||
doctest = false | ||
test = false | ||
|
||
[dependencies] | ||
bytes = "1" | ||
ironrdp-connector.workspace = true | ||
ironrdp-pdu.workspace = true | ||
# ironrdp-session.workspace = true | ||
tap = "1" | ||
tracing.workspace = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# IronRDP Blocking | ||
|
||
Blocking I/O abstraction wrapping the IronRDP state machines conveniently. | ||
|
||
This crate is a higher level abstraction for IronRDP state machines using blocking I/O instead of | ||
asynchronous I/O. This results in a simpler API with fewer dependencies that should be used | ||
instead of `ironrdp-async` when concurrency is not a requirement. |
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,116 @@ | ||
use std::io::{Read, Write}; | ||
|
||
use ironrdp_connector::{ | ||
ClientConnector, ClientConnectorState, ConnectionResult, ConnectorResult, Sequence as _, State as _, | ||
}; | ||
use ironrdp_pdu::write_buf::WriteBuf; | ||
|
||
use crate::framed::Framed; | ||
|
||
pub struct ShouldUpgrade { | ||
_priv: (), | ||
} | ||
|
||
#[instrument(skip_all)] | ||
pub fn connect_begin<S>(framed: &mut Framed<S>, connector: &mut ClientConnector) -> ConnectorResult<ShouldUpgrade> | ||
where | ||
S: Sync + Read + Write, | ||
{ | ||
let mut buf = WriteBuf::new(); | ||
|
||
info!("Begin connection procedure"); | ||
|
||
while !connector.should_perform_security_upgrade() { | ||
single_connect_step(framed, connector, &mut buf)?; | ||
} | ||
|
||
Ok(ShouldUpgrade { _priv: () }) | ||
} | ||
|
||
pub fn skip_connect_begin(connector: &mut ClientConnector) -> ShouldUpgrade { | ||
assert!(connector.should_perform_security_upgrade()); | ||
ShouldUpgrade { _priv: () } | ||
} | ||
|
||
pub struct Upgraded { | ||
_priv: (), | ||
} | ||
|
||
#[instrument(skip_all)] | ||
pub fn mark_as_upgraded(_: ShouldUpgrade, connector: &mut ClientConnector, server_public_key: Vec<u8>) -> Upgraded { | ||
trace!("marked as upgraded"); | ||
connector.attach_server_public_key(server_public_key); | ||
connector.mark_security_upgrade_as_done(); | ||
Upgraded { _priv: () } | ||
} | ||
|
||
#[instrument(skip_all)] | ||
pub fn connect_finalize<S>( | ||
_: Upgraded, | ||
framed: &mut Framed<S>, | ||
mut connector: ClientConnector, | ||
) -> ConnectorResult<ConnectionResult> | ||
where | ||
S: Read + Write, | ||
{ | ||
let mut buf = WriteBuf::new(); | ||
|
||
debug!("CredSSP procedure"); | ||
|
||
while connector.is_credssp_step() { | ||
single_connect_step(framed, &mut connector, &mut buf)?; | ||
} | ||
|
||
debug!("Remaining of connection sequence"); | ||
|
||
let result = loop { | ||
single_connect_step(framed, &mut connector, &mut buf)?; | ||
|
||
if let ClientConnectorState::Connected { result } = connector.state { | ||
break result; | ||
} | ||
}; | ||
|
||
info!("Connected with success"); | ||
|
||
Ok(result) | ||
} | ||
|
||
pub fn single_connect_step<S>( | ||
framed: &mut Framed<S>, | ||
connector: &mut ClientConnector, | ||
buf: &mut WriteBuf, | ||
) -> ConnectorResult<ironrdp_connector::Written> | ||
where | ||
S: Read + Write, | ||
{ | ||
buf.clear(); | ||
|
||
let written = if let Some(next_pdu_hint) = connector.next_pdu_hint() { | ||
debug!( | ||
connector.state = connector.state.name(), | ||
hint = ?next_pdu_hint, | ||
"Wait for PDU" | ||
); | ||
|
||
let pdu = framed | ||
.read_by_hint(next_pdu_hint) | ||
.map_err(|e| ironrdp_connector::custom_err!("read frame by hint", e))?; | ||
|
||
trace!(length = pdu.len(), "PDU received"); | ||
|
||
connector.step(&pdu, buf)? | ||
} else { | ||
connector.step_no_input(buf)? | ||
}; | ||
|
||
if let Some(response_len) = written.size() { | ||
let response = &buf[..response_len]; | ||
trace!(response_len, "Send response"); | ||
framed | ||
.write_all(response) | ||
.map_err(|e| ironrdp_connector::custom_err!("write all", e))?; | ||
} | ||
|
||
Ok(written) | ||
} |
Oops, something went wrong.