|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 |
| -#[cfg(not(feature = "deflate"))] |
16 |
| -use ratchet::NoExtProvider; |
17 |
| -use ratchet::WebSocketStream; |
18 |
| -use std::marker::PhantomData; |
19 |
| -use std::num::NonZeroUsize; |
20 |
| -use swimos_remote::websocket::RatchetClient; |
| 15 | +use std::{marker::PhantomData, num::NonZeroUsize, sync::Arc}; |
21 | 16 |
|
22 | 17 | use futures_util::future::BoxFuture;
|
23 |
| -#[cfg(feature = "deflate")] |
24 |
| -use ratchet::deflate::{DeflateConfig, DeflateExtProvider}; |
| 18 | +use ratchet::{ |
| 19 | + deflate::{DeflateConfig, DeflateExtProvider}, |
| 20 | + WebSocketStream, |
| 21 | +}; |
| 22 | +use rustls::crypto::CryptoProvider; |
| 23 | +use tokio::{sync::mpsc, sync::mpsc::error::SendError, sync::oneshot::error::RecvError}; |
| 24 | +pub use url::Url; |
| 25 | + |
25 | 26 | use runtime::{
|
26 | 27 | start_runtime, ClientConfig, DownlinkRuntimeError, RawHandle, Transport, WebSocketConfig,
|
27 | 28 | };
|
28 | 29 | pub use runtime::{CommandError, Commander, RemotePath};
|
29 |
| -use std::sync::Arc; |
30 | 30 | pub use swimos_client_api::DownlinkConfig;
|
31 |
| -pub use swimos_downlink::lifecycle::{ |
32 |
| - BasicEventDownlinkLifecycle, BasicMapDownlinkLifecycle, BasicValueDownlinkLifecycle, |
33 |
| - EventDownlinkLifecycle, MapDownlinkLifecycle, ValueDownlinkLifecycle, |
| 31 | +pub use swimos_downlink::{ |
| 32 | + lifecycle::BasicEventDownlinkLifecycle, lifecycle::BasicMapDownlinkLifecycle, |
| 33 | + lifecycle::BasicValueDownlinkLifecycle, lifecycle::EventDownlinkLifecycle, |
| 34 | + lifecycle::MapDownlinkLifecycle, lifecycle::ValueDownlinkLifecycle, |
34 | 35 | };
|
35 | 36 | use swimos_downlink::{
|
36 | 37 | ChannelError, DownlinkTask, EventDownlinkModel, MapDownlinkHandle, MapDownlinkModel, MapKey,
|
37 | 38 | MapValue, NotYetSyncedError, ValueDownlinkModel, ValueDownlinkSet,
|
38 | 39 | };
|
39 | 40 | use swimos_form::Form;
|
40 |
| -use swimos_remote::dns::Resolver; |
41 |
| -use swimos_remote::plain::TokioPlainTextNetworking; |
42 |
| -#[cfg(feature = "tls")] |
43 |
| -use swimos_remote::tls::{ClientConfig as TlsConfig, RustlsClientNetworking, TlsError}; |
44 |
| -use swimos_remote::ClientConnections; |
| 41 | +pub use swimos_remote::tls::ClientConfig as TlsConfig; |
| 42 | +use swimos_remote::tls::TlsError; |
| 43 | +use swimos_remote::{ |
| 44 | + dns::Resolver, |
| 45 | + plain::TokioPlainTextNetworking, |
| 46 | + tls::{CryptoProviderConfig, RustlsClientNetworking}, |
| 47 | + websocket::RatchetClient, |
| 48 | + ClientConnections, |
| 49 | +}; |
45 | 50 | use swimos_runtime::downlink::{DownlinkOptions, DownlinkRuntimeConfig};
|
46 |
| -use swimos_utilities::trigger; |
47 |
| -use swimos_utilities::trigger::promise; |
48 |
| -use tokio::sync::mpsc; |
49 |
| -use tokio::sync::mpsc::error::SendError; |
50 |
| -use tokio::sync::oneshot::error::RecvError; |
51 |
| -pub use url::Url; |
| 51 | +use swimos_utilities::{trigger, trigger::promise}; |
52 | 52 |
|
53 | 53 | pub type DownlinkOperationResult<T> = Result<T, DownlinkRuntimeError>;
|
54 | 54 |
|
55 |
| -#[derive(Debug, Default)] |
| 55 | +#[derive(Default)] |
56 | 56 | pub struct SwimClientBuilder {
|
57 |
| - config: ClientConfig, |
| 57 | + client_config: ClientConfig, |
58 | 58 | }
|
59 | 59 |
|
60 | 60 | impl SwimClientBuilder {
|
61 |
| - pub fn new(config: ClientConfig) -> SwimClientBuilder { |
62 |
| - SwimClientBuilder { config } |
| 61 | + pub fn new(client_config: ClientConfig) -> SwimClientBuilder { |
| 62 | + SwimClientBuilder { client_config } |
63 | 63 | }
|
64 | 64 |
|
65 | 65 | /// Sets the websocket configuration.
|
66 | 66 | pub fn set_websocket_config(mut self, to: WebSocketConfig) -> SwimClientBuilder {
|
67 |
| - self.config.websocket = to; |
| 67 | + self.client_config.websocket = to; |
68 | 68 | self
|
69 | 69 | }
|
70 | 70 |
|
71 | 71 | /// Size of the buffers to communicate with the socket.
|
72 | 72 | pub fn set_remote_buffer_size(mut self, to: NonZeroUsize) -> SwimClientBuilder {
|
73 |
| - self.config.remote_buffer_size = to; |
| 73 | + self.client_config.remote_buffer_size = to; |
74 | 74 | self
|
75 | 75 | }
|
76 | 76 |
|
77 | 77 | /// Sets the buffer size between the runtime and transport tasks.
|
78 | 78 | pub fn set_transport_buffer_size(mut self, to: NonZeroUsize) -> SwimClientBuilder {
|
79 |
| - self.config.transport_buffer_size = to; |
| 79 | + self.client_config.transport_buffer_size = to; |
80 | 80 | self
|
81 | 81 | }
|
82 | 82 |
|
83 | 83 | /// Sets the deflate extension configuration for WebSocket connections.
|
84 | 84 | #[cfg(feature = "deflate")]
|
85 | 85 | pub fn set_deflate_config(mut self, to: DeflateConfig) -> SwimClientBuilder {
|
86 |
| - self.config.websocket.deflate_config = Some(to); |
| 86 | + self.client_config.websocket.deflate_config = Some(to); |
87 | 87 | self
|
88 | 88 | }
|
89 | 89 |
|
| 90 | + /// Enables TLS support. |
| 91 | + pub fn set_tls_config(self, tls_config: TlsConfig) -> SwimClientTlsBuilder { |
| 92 | + SwimClientTlsBuilder { |
| 93 | + client_config: self.client_config, |
| 94 | + tls_config, |
| 95 | + crypto_provider: Default::default(), |
| 96 | + } |
| 97 | + } |
| 98 | + |
90 | 99 | /// Builds the client.
|
91 | 100 | pub async fn build(self) -> (SwimClient, BoxFuture<'static, ()>) {
|
92 |
| - let SwimClientBuilder { config } = self; |
| 101 | + let SwimClientBuilder { client_config } = self; |
93 | 102 | open_client(
|
94 |
| - config, |
| 103 | + client_config, |
95 | 104 | TokioPlainTextNetworking::new(Arc::new(Resolver::new().await)),
|
96 | 105 | )
|
97 | 106 | .await
|
98 | 107 | }
|
| 108 | +} |
| 109 | + |
| 110 | +pub struct SwimClientTlsBuilder { |
| 111 | + client_config: ClientConfig, |
| 112 | + tls_config: TlsConfig, |
| 113 | + crypto_provider: CryptoProviderConfig, |
| 114 | +} |
| 115 | + |
| 116 | +impl SwimClientTlsBuilder { |
| 117 | + /// Uses the process-default [`CryptoProvider`] for any TLS connections. |
| 118 | + /// |
| 119 | + /// This is only used if the TLS configuration has been set. |
| 120 | + pub fn with_default_crypto_provider(mut self) -> Self { |
| 121 | + self.crypto_provider = CryptoProviderConfig::ProcessDefault; |
| 122 | + self |
| 123 | + } |
| 124 | + |
| 125 | + /// Uses the provided [`CryptoProvider`] for any TLS connections. |
| 126 | + pub fn with_crypto_provider(mut self, provider: Arc<CryptoProvider>) -> Self { |
| 127 | + self.crypto_provider = CryptoProviderConfig::Provided(provider); |
| 128 | + self |
| 129 | + } |
99 | 130 |
|
100 | 131 | /// Builds the client using the provided TLS configuration.
|
101 |
| - #[cfg(feature = "tls")] |
102 |
| - pub async fn build_tls( |
103 |
| - self, |
104 |
| - tls_config: TlsConfig, |
105 |
| - ) -> Result<(SwimClient, BoxFuture<'static, ()>), TlsError> { |
106 |
| - let SwimClientBuilder { config } = self; |
| 132 | + pub async fn build(self) -> Result<(SwimClient, BoxFuture<'static, ()>), TlsError> { |
| 133 | + let SwimClientTlsBuilder { |
| 134 | + client_config, |
| 135 | + tls_config, |
| 136 | + crypto_provider, |
| 137 | + } = self; |
107 | 138 | Ok(open_client(
|
108 |
| - config, |
109 |
| - RustlsClientNetworking::try_from_config(Arc::new(Resolver::new().await), tls_config)?, |
| 139 | + client_config, |
| 140 | + RustlsClientNetworking::build( |
| 141 | + Arc::new(Resolver::new().await), |
| 142 | + tls_config, |
| 143 | + crypto_provider.try_build()?, |
| 144 | + )?, |
110 | 145 | )
|
111 | 146 | .await)
|
112 | 147 | }
|
|
0 commit comments