open-net 0.3.2

A cross-platform async networking library with WebSocket clients and network status monitoring.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Immutable network policy used as an engine default or overridden per client.
//!
//! The current release applies this policy to WebSocket clients; it does not create HTTP executors.
//! Credentials and certificates are supplied in memory. A client-specific
//! policy replaces engine defaults, including TLS settings, and remains
//! immutable for that client's lifetime.

use crate::api::tls::CryptoProvider;
use crate::error::NetError;
use base64::Engine;
use rustls::pki_types::pem::PemObject;
use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer};
use rustls::sign::CertifiedKey;
use std::fmt;
use std::sync::Arc;
use tokio_tungstenite::tungstenite::http::Uri;

const MAX_PEM_BYTES: usize = 1024 * 1024;
const MAX_CERTIFICATES: usize = 128;

/// Proxy, TLS, and network-status policy; defaults to direct connections and WebPKI roots.
///
/// Use [`crate::OpenNet::new_with_network_config`] for the engine default or
/// [`crate::OpenNet::create_ws_client_with_network_config`] for a client override.
/// Passing `NetworkConfig::default()` explicitly selects direct connections and
/// the built-in trust roots; clone an existing value when changing one setting.
#[derive(Clone, Debug, Default)]
pub struct NetworkConfig {
    pub(crate) proxy: ProxyConfig,
    pub(crate) tls: TlsConfig,
    pub(crate) network_status_policy: NetworkStatusPolicy,
}

impl NetworkConfig {
    /// Replaces the proxy policy.
    pub fn with_proxy(mut self, proxy: ProxyConfig) -> Self {
        self.proxy = proxy;
        self
    }

    /// Replaces the TLS policy.
    pub fn with_tls(mut self, tls: TlsConfig) -> Self {
        self.tls = tls;
        self
    }

    /// Selects how connections respond to network-monitor state changes.
    /// The default is [`NetworkStatusPolicy::Ignore`].
    pub fn with_network_status_policy(mut self, policy: NetworkStatusPolicy) -> Self {
        self.network_status_policy = policy;
        self
    }

    /// Returns the current proxy policy by reference without exposing credentials.
    pub fn proxy(&self) -> &ProxyConfig {
        &self.proxy
    }

    /// Returns the current TLS policy by reference.
    pub fn tls(&self) -> &TlsConfig {
        &self.tls
    }

    /// Returns the policy used when monitor reachability changes.
    pub fn network_status_policy(&self) -> NetworkStatusPolicy {
        self.network_status_policy
    }

    /// Validates this configuration without creating connections, runtimes, or
    /// background tasks. Trust roots and client identity are checked here;
    /// policy interactions with reconnect budgets are checked by the connector.
    pub fn validate(&self) -> Result<(), NetError> {
        if self
            .proxy
            .endpoint
            .as_ref()
            .is_some_and(|proxy| proxy.host.is_empty() || proxy.port == 0)
        {
            return Err(NetError::config(
                "proxy.url",
                "requires a host and nonzero port",
            ));
        }
        if self.tls.roots.len() > MAX_CERTIFICATES
            || (self.tls.root_mode == RootCertificateMode::Replace && self.tls.roots.is_empty())
        {
            return Err(NetError::config(
                "tls.root_certificates",
                "replacement roots must be nonempty and bounded",
            ));
        }
        let mut roots = rustls::RootCertStore::empty();
        for certificate in &self.tls.roots {
            roots.add(certificate.clone()).map_err(|_| {
                NetError::config("tls.root_certificates", "invalid root certificate")
            })?;
        }
        if let Some(identity) = &self.tls.identity {
            let provider = crate::module::tls::select_provider(self.tls.provider.as_ref())?;
            identity.compile(&provider)?;
        }
        Ok(())
    }
}

/// Controls whether local reachability participates in WebSocket connection control.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum NetworkStatusPolicy {
    /// Preserve existing behavior and rely on heartbeats and transport errors.
    #[default]
    Ignore,
    /// Invalidate active connections and pause attempts while the monitor reports
    /// unavailable, retaining eligible connection intent for later recovery.
    /// Recovery remains bounded by the reconnect policy's elapsed-time budget;
    /// connections using this policy must configure
    /// `ReconnectPolicy::max_elapsed` as `Some(...)`.
    PauseOnUnavailable,
}

/// Explicit proxy routing. Defaults to direct connections and never reads proxy environment variables.
#[derive(Clone, Default)]
pub struct ProxyConfig {
    pub(crate) endpoint: Option<ProxyEndpoint>,
}

#[derive(Clone)]
pub(crate) struct ProxyEndpoint {
    pub(crate) host: String,
    pub(crate) port: u16,
    pub(crate) auth: Option<ProxyBasicAuth>,
    normalized_url: String,
}

impl ProxyConfig {
    /// Selects direct connections without consulting proxy environment variables.
    pub fn direct() -> Self {
        Self::default()
    }

    /// Routes `ws` and `wss` targets through an HTTP CONNECT tunnel.
    ///
    /// Accepts only `http://host[:port]`; credentials must be supplied separately.
    /// A plain HTTP proxy does not encrypt Basic credentials, even for `wss` targets.
    pub fn http_connect(url: &str, auth: Option<ProxyBasicAuth>) -> Result<Self, NetError> {
        if url.len() > 2048 || url.contains('#') {
            return Err(NetError::config("proxy.url", "requires a valid HTTP CONNECT URL with a nonzero port and no credentials, path, query, or fragment"));
        }
        let uri: Uri = url.parse().map_err(|_| NetError::config("proxy.url", "requires a valid HTTP CONNECT URL with a nonzero port and no credentials, path, query, or fragment"))?;
        let authority = uri.authority().ok_or(NetError::config("proxy.url", "requires a valid HTTP CONNECT URL with a nonzero port and no credentials, path, query, or fragment"))?;
        if uri.scheme_str() != Some("http")
            || authority.as_str().contains('@')
            || uri
                .path_and_query()
                .is_some_and(|path| path.as_str() != "/")
        {
            return Err(NetError::config("proxy.url", "requires a valid HTTP CONNECT URL with a nonzero port and no credentials, path, query, or fragment"));
        }
        let host = authority.host();
        if normalize_host(host).is_empty() {
            return Err(NetError::config("proxy.url", "requires a valid HTTP CONNECT URL with a nonzero port and no credentials, path, query, or fragment"));
        }
        if host.starts_with('[') && normalize_host(host).parse::<std::net::Ipv6Addr>().is_err() {
            return Err(NetError::config("proxy.url", "requires a valid HTTP CONNECT URL with a nonzero port and no credentials, path, query, or fragment"));
        }
        // Authority::port() is also None for malformed numeric ports; distinguish
        // an omitted port from an explicitly invalid one before applying port 80.
        let suffix = authority
            .as_str()
            .strip_prefix(host)
            .ok_or(NetError::config("proxy.url", "requires a valid HTTP CONNECT URL with a nonzero port and no credentials, path, query, or fragment"))?;
        let port = match suffix {
            "" => 80,
            value => {
                let digits = value.strip_prefix(':').ok_or(NetError::config("proxy.url", "requires a valid HTTP CONNECT URL with a nonzero port and no credentials, path, query, or fragment"))?;
                if digits.is_empty() || !digits.bytes().all(|byte| byte.is_ascii_digit()) {
                    return Err(NetError::config("proxy.url", "requires a valid HTTP CONNECT URL with a nonzero port and no credentials, path, query, or fragment"));
                }
                digits.parse::<u16>().map_err(|_| NetError::config("proxy.url", "requires a valid HTTP CONNECT URL with a nonzero port and no credentials, path, query, or fragment"))?
            }
        };
        if port == 0 {
            return Err(NetError::config("proxy.url", "requires a valid HTTP CONNECT URL with a nonzero port and no credentials, path, query, or fragment"));
        }
        Ok(Self {
            endpoint: Some(ProxyEndpoint {
                host: normalize_host(host).to_owned(),
                port,
                auth,
                normalized_url: format!("http://{}:{port}", host.to_ascii_lowercase()),
            }),
        })
    }

    /// Returns `true` when connections bypass a proxy.
    pub fn is_direct(&self) -> bool {
        self.endpoint.is_none()
    }

    /// Returns the credential-free normalized HTTP proxy URL, or `None` for direct mode.
    pub fn endpoint(&self) -> Option<&str> {
        self.endpoint
            .as_ref()
            .map(|proxy| proxy.normalized_url.as_str())
    }

    /// Returns whether proxy credentials are configured.
    pub fn is_authenticated(&self) -> bool {
        self.endpoint
            .as_ref()
            .is_some_and(|proxy| proxy.auth.is_some())
    }
}

impl fmt::Debug for ProxyConfig {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("ProxyConfig")
            .field(
                "mode",
                &if self.endpoint.is_some() {
                    "HttpConnect"
                } else {
                    "Direct"
                },
            )
            .field(
                "authenticated",
                &self
                    .endpoint
                    .as_ref()
                    .is_some_and(|proxy| proxy.auth.is_some()),
            )
            .finish()
    }
}

/// ASCII Basic proxy credentials; `Debug` output always redacts the secret.
#[derive(Clone)]
pub struct ProxyBasicAuth {
    pub(crate) encoded: Arc<str>,
}

impl ProxyBasicAuth {
    /// Rejects non-ASCII/control characters and colons in usernames. Passwords
    /// may contain ordinary colons; each component is limited to 4096 bytes.
    pub fn new(username: &str, password: &str) -> Result<Self, NetError> {
        if username.len() > 4096
            || username.contains(':')
            || username
                .bytes()
                .any(|byte| !byte.is_ascii() || byte.is_ascii_control())
        {
            return Err(NetError::config(
                "proxy.username",
                "must be at most 4096 ASCII bytes without control characters or a colon",
            ));
        }
        if password.len() > 4096
            || password
                .bytes()
                .any(|byte| !byte.is_ascii() || byte.is_ascii_control())
        {
            return Err(NetError::config(
                "proxy.password",
                "must be at most 4096 ASCII bytes without control characters",
            ));
        }
        let credentials = format!("{username}:{password}");
        Ok(Self {
            encoded: base64::engine::general_purpose::STANDARD
                .encode(credentials)
                .into(),
        })
    }
}

impl fmt::Debug for ProxyBasicAuth {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("ProxyBasicAuth([redacted])")
    }
}

/// Defines how supplied CA certificates combine with built-in WebPKI roots.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum RootCertificateMode {
    #[default]
    /// Append custom roots to the built-in WebPKI roots.
    Append,
    /// Replace built-in roots with the custom set.
    Replace,
}

/// Strict server-certificate validation with optional client authentication.
#[derive(Clone, Default)]
pub struct TlsConfig {
    pub(crate) provider: Option<Arc<CryptoProvider>>,
    pub(crate) roots: Vec<CertificateDer<'static>>,
    pub(crate) root_mode: RootCertificateMode,
    pub(crate) identity: Option<ClientIdentity>,
}

impl fmt::Debug for TlsConfig {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("TlsConfig")
            .field("crypto_provider_configured", &self.provider.is_some())
            .field("custom_certificate_count", &self.roots.len())
            .field("root_mode", &self.root_mode)
            .field("client_identity_configured", &self.identity.is_some())
            .finish()
    }
}

impl TlsConfig {
    /// Selects this client's rustls cryptography without changing global state.
    ///
    /// Takes precedence over the application's process default.
    /// The provider also loads and validates this client's private key when the
    /// network configuration is validated or compiled.
    pub fn with_crypto_provider(mut self, provider: Arc<CryptoProvider>) -> Self {
        self.provider = Some(provider);
        self
    }

    /// Returns the explicitly selected provider, if any, without resolving defaults.
    pub fn crypto_provider(&self) -> Option<&Arc<CryptoProvider>> {
        self.provider.as_ref()
    }

    /// Replaces the custom CA bundle and selects its trust mode. Accepts one to
    /// 128 PEM certificates with a total size of at most 1 MiB; malformed,
    /// mismatched, or trailing non-whitespace blocks are rejected.
    pub fn with_root_certificates(
        mut self,
        pem: impl AsRef<[u8]>,
        mode: RootCertificateMode,
    ) -> Result<Self, NetError> {
        self.roots = parse_certificates(pem.as_ref(), "tls.root_certificates")?;
        self.root_mode = mode;
        Ok(self)
    }

    /// Configures a client certificate identity.
    pub fn with_client_identity(mut self, identity: ClientIdentity) -> Self {
        self.identity = Some(identity);
        self
    }

    /// Returns how custom roots are combined with built-in roots.
    pub fn root_mode(&self) -> RootCertificateMode {
        self.root_mode
    }

    /// Returns the number of custom roots, excluding built-in WebPKI roots.
    pub fn custom_root_count(&self) -> usize {
        self.roots.len()
    }

    /// Returns the configured client identity, if any.
    pub fn client_identity(&self) -> Option<&ClientIdentity> {
        self.identity.as_ref()
    }
}

/// Parsed certificate material, validated cryptographically by the selected provider.
pub struct ClientIdentity {
    certificates: Vec<CertificateDer<'static>>,
    private_key: PrivatePkcs8KeyDer<'static>,
}

impl Clone for ClientIdentity {
    fn clone(&self) -> Self {
        Self {
            certificates: self.certificates.clone(),
            private_key: self.private_key.clone_key(),
        }
    }
}

impl ClientIdentity {
    /// Loads a PEM certificate chain and exactly one unencrypted PKCS#8 key.
    /// Each input is limited to 1 MiB and the chain to 128 certificates.
    /// This parses the PEM material without selecting cryptography. Key loading
    /// and certificate/key matching happen during [`NetworkConfig::validate`]
    /// and client construction, using the client's final provider.
    pub fn from_pem(
        certificates: impl AsRef<[u8]>,
        private_key: impl AsRef<[u8]>,
    ) -> Result<Self, NetError> {
        let certificates =
            parse_certificates(certificates.as_ref(), "tls.client_identity.certificates")?;
        let blocks = strict_pem_blocks(
            private_key.as_ref(),
            "PRIVATE KEY",
            "tls.client_identity.private_key",
        )?;
        if blocks.len() != 1 {
            return Err(NetError::config(
                "tls.client_identity.private_key",
                "requires one valid PKCS#8 private key matching the certificate",
            ));
        }
        let block = blocks.first().ok_or(NetError::config(
            "tls.client_identity.private_key",
            "requires one valid PKCS#8 private key matching the certificate",
        ))?;
        let key = PrivatePkcs8KeyDer::from_pem_slice(block).map_err(|_| {
            NetError::config(
                "tls.client_identity.private_key",
                "requires one valid PKCS#8 private key matching the certificate",
            )
        })?;
        Ok(Self {
            certificates,
            private_key: key,
        })
    }

    pub(crate) fn compile(&self, provider: &CryptoProvider) -> Result<Arc<CertifiedKey>, NetError> {
        let certified_key = CertifiedKey::from_der(
            self.certificates.clone(),
            PrivateKeyDer::Pkcs8(self.private_key.clone_key()),
            provider,
        )
        .map_err(|_| {
            NetError::config(
                "tls.client_identity.private_key",
                "requires one valid PKCS#8 private key matching the certificate",
            )
        })?;
        certified_key.keys_match().map_err(|_| {
            NetError::config(
                "tls.client_identity.private_key",
                "requires one valid PKCS#8 private key matching the certificate",
            )
        })?;
        Ok(Arc::new(certified_key))
    }

    /// Returns the number of certificates in the parsed client chain.
    pub fn certificate_count(&self) -> usize {
        self.certificates.len()
    }
}

impl fmt::Debug for ClientIdentity {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("ClientIdentity")
            .field("certificate_count", &self.certificates.len())
            .field("private_key", &"[redacted]")
            .finish()
    }
}

pub(crate) fn normalize_host(host: &str) -> &str {
    host.strip_prefix('[')
        .and_then(|host| host.strip_suffix(']'))
        .unwrap_or(host)
}

fn parse_certificates(pem: &[u8], field: &str) -> Result<Vec<CertificateDer<'static>>, NetError> {
    let blocks = strict_pem_blocks(pem, "CERTIFICATE", field)?;
    let mut certificates = Vec::new();
    certificates
        .try_reserve(blocks.len())
        .map_err(|_| NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"))?;
    let mut validation = rustls::RootCertStore::empty();
    for block in blocks {
        let cert = CertificateDer::from_pem_slice(block).map_err(|_| NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"))?;
        validation
            .add(cert.clone())
            .map_err(|_| NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"))?;
        certificates.push(cert);
    }
    Ok(certificates)
}

fn strict_pem_blocks<'a>(
    pem: &'a [u8],
    label: &str,
    field: &str,
) -> Result<Vec<&'a [u8]>, NetError> {
    if pem.is_empty() || pem.len() > MAX_PEM_BYTES {
        return Err(NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"));
    }
    let mut remaining = std::str::from_utf8(pem)
        .map_err(|_| NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"))?
        .trim();
    let begin = format!("-----BEGIN {label}-----");
    let end = format!("-----END {label}-----");
    let mut blocks = Vec::new();
    while !remaining.is_empty() {
        if blocks.len() == MAX_CERTIFICATES || !remaining.starts_with(&begin) {
            return Err(NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"));
        }
        let boundary = remaining
            .find(&end)
            .and_then(|index| index.checked_add(end.len()))
            .ok_or(NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"))?;
        let block = remaining.get(..boundary).ok_or(NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"))?;
        if block
            .get(begin.len()..)
            .ok_or(NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"))?
            .contains("-----BEGIN ")
        {
            return Err(NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"));
        }
        blocks.try_reserve(1).map_err(|_| NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"))?;
        blocks.push(block.as_bytes());
        remaining = remaining
            .get(boundary..)
            .ok_or(NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"))?
            .trim();
    }
    if blocks.is_empty() {
        return Err(NetError::config(field, "requires a nonempty bounded PEM bundle containing only valid blocks of the requested type"));
    }
    Ok(blocks)
}