-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.rs
More file actions
1192 lines (1041 loc) · 39.7 KB
/
Copy pathserver.rs
File metadata and controls
1192 lines (1041 loc) · 39.7 KB
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Server-specific protocol implementation.
//!
//! # Unidirectional Event Stream Handling
//!
//! The `review-protocol` crate provides high-level APIs for handling
//! unidirectional event streams from agents. This encapsulates all
//! protocol-level details and provides a clean interface for applications.
//!
//! ## Basic Usage
//!
//! Implement the `EventStreamHandler` trait to define how events should be
//! processed:
//!
//! ```rust,ignore
//! use review_protocol::{server::EventStreamHandler, types::EventMessage};
//!
//! struct MyEventHandler {
//! event_count: usize,
//! }
//!
//! #[async_trait::async_trait]
//! impl EventStreamHandler for MyEventHandler {
//! async fn handle_event(&mut self, event: EventMessage) -> std::io::Result<()> {
//! self.event_count += 1;
//! println!("Received event #{}: {:?}", self.event_count, event.kind);
//! Ok(())
//! }
//! }
//! ```
//!
//! Then use the handler to process incoming event streams:
//!
//! ```rust,ignore
//! # use review_protocol::{server::{Connection, EventStreamHandler}, types::EventMessage};
//! # struct MyEventHandler { event_count: usize }
//! # #[async_trait::async_trait]
//! # impl EventStreamHandler for MyEventHandler {
//! # async fn handle_event(&mut self, event: EventMessage) -> std::io::Result<()> { Ok(()) }
//! # }
//! # async fn example(connection: Connection) -> anyhow::Result<()> {
//! let handler = MyEventHandler { event_count: 0 };
//! connection.accept_event_stream(handler).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Multiple Concurrent Streams
//!
//! Drive `accept_uni` directly and dispatch each stream to
//! [`Connection::handle_event_stream`]. The caller owns the spawn
//! lifetime, concurrency policy, and error handling:
//!
//! ```rust,ignore
//! # use std::sync::Arc;
//! # use review_protocol::{server::{Connection, EventStreamHandler}, types::EventMessage};
//! # struct MyEventHandler;
//! # #[async_trait::async_trait]
//! # impl EventStreamHandler for MyEventHandler {
//! # async fn handle_event(&mut self, event: EventMessage) -> std::io::Result<()> { Ok(()) }
//! # }
//! # impl MyEventHandler { fn new() -> Self { MyEventHandler } }
//! # async fn example(connection: Connection) -> anyhow::Result<()> {
//! let semaphore = Arc::new(tokio::sync::Semaphore::new(5));
//! while let Ok(recv) = connection.accept_uni().await {
//! let permit = semaphore.clone().acquire_owned().await?;
//! let handler = MyEventHandler::new();
//! tokio::spawn(async move {
//! let _permit = permit;
//! if let Err(e) = Connection::handle_event_stream(recv, handler).await {
//! tracing::warn!(error = %e, "event stream ended with error");
//! }
//! });
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Integration with Connection Loop
//!
//! Combine bidirectional and unidirectional stream handling:
//!
//! ```rust,ignore
//! # use review_protocol::{server::{Connection, EventStreamHandler}, types::EventMessage};
//! # struct MyEventHandler;
//! # #[async_trait::async_trait]
//! # impl EventStreamHandler for MyEventHandler {
//! # async fn handle_event(&mut self, event: EventMessage) -> std::io::Result<()> { Ok(()) }
//! # }
//! # impl MyEventHandler {
//! # fn new() -> Self { MyEventHandler }
//! # }
//! # async fn example(connection: Connection) -> anyhow::Result<()> {
//! loop {
//! tokio::select! {
//! // Handle bidirectional requests
//! res = connection.open_bi() => {
//! // Handle request/response
//! }
//!
//! // Handle unidirectional event streams
//! res = connection.accept_event_stream(MyEventHandler::new()) => {
//! if let Err(e) = res {
//! tracing::warn!(error = %e, "event stream ended with error");
//! }
//! }
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Error Handling
//!
//! The `EventStreamHandler` trait provides hooks for custom error handling:
//!
//! ```rust,ignore
//! use review_protocol::{server::EventStreamHandler, types::EventMessage};
//!
//! struct ResilientHandler {
//! max_errors: usize,
//! error_count: usize,
//! }
//!
//! #[async_trait::async_trait]
//! impl EventStreamHandler for ResilientHandler {
//! async fn handle_event(&mut self, event: EventMessage) -> std::io::Result<()> {
//! // Process event
//! Ok(())
//! }
//!
//! async fn on_error(&mut self, error: &str) -> std::io::Result<()> {
//! self.error_count += 1;
//! tracing::warn!(error, count = self.error_count, "stream error");
//!
//! if self.error_count >= self.max_errors {
//! Err(std::io::Error::other(format!("Too many errors: {}", self.error_count)))
//! } else {
//! Ok(()) // Continue processing
//! }
//! }
//!
//! async fn on_stream_end(&mut self) -> std::io::Result<()> {
//! println!("Stream ended after {} errors", self.error_count);
//! Ok(())
//! }
//! }
//! ```
#[cfg(feature = "server")]
mod api;
#[cfg(feature = "server")]
mod handler;
#[cfg(feature = "server")]
pub mod node;
#[cfg(feature = "server")]
pub mod stream;
#[cfg(feature = "server")]
use std::io;
#[cfg(feature = "server")]
use std::net::SocketAddr;
#[cfg(any(feature = "client", feature = "server"))]
use num_enum::{FromPrimitive, IntoPrimitive};
#[cfg(feature = "server")]
use oinq::{
frame,
message::{send_err, send_ok},
};
#[cfg(feature = "server")]
use semver::{Version, VersionReq};
#[cfg(feature = "server")]
pub use self::handler::{Handler, handle, handle_authorized, handle_authorized_with_context};
#[cfg(feature = "server")]
pub use self::stream::process_event_stream;
#[cfg(feature = "server")]
use crate::types::EventMessage;
#[cfg(feature = "server")]
use crate::{
AgentInfo, HandshakeError, client, decode_agent_info, handle_handshake_recv_io_error,
handle_handshake_send_io_error, types::LabelDb,
};
/// Trait for handling incoming event messages from unidirectional streams
///
/// This trait provides a standardized interface for processing event messages
/// received from unidirectional streams, abstracting away protocol-level details.
#[cfg(feature = "server")]
#[async_trait::async_trait]
pub trait EventStreamHandler {
/// Handles a single event message
///
/// Called for each successfully deserialized `EventMessage` received
/// from the unidirectional stream.
///
/// # Arguments
/// * `event` - The deserialized `EventMessage`
///
/// # Returns
/// * `Ok(())` - Continue processing messages
/// * `Err(msg)` - Stop processing and return error
///
/// # Errors
///
/// This function will return an error if:
/// * The event processing logic fails
/// * The handler determines the event is invalid or cannot be processed
/// * Any downstream processing of the event fails
async fn handle_event(&mut self, event: EventMessage) -> io::Result<()>;
/// Called when the stream ends normally
///
/// This is called when the peer closes the stream gracefully
/// (EOF received). Default implementation does nothing.
///
/// # Errors
///
/// This function will return an error if:
/// * Cleanup operations fail
/// * Final processing steps cannot be completed
async fn on_stream_end(&mut self) -> io::Result<()> {
Ok(())
}
/// Called when an error occurs during stream processing
///
/// This includes deserialization errors, network errors, etc.
/// The handler can decide whether to treat the error as fatal:
/// return `Ok(())` to continue processing subsequent messages, or
/// `Err(_)` to stop the stream and propagate the error to the
/// caller of [`Connection::handle_event_stream`].
///
/// The default implementation is a no-op; the library does not
/// log on the application's behalf. Override this method to wire
/// stream-level errors into your own logging or metrics.
///
/// # Arguments
/// * `error` - Description of the error that occurred
async fn on_error(&mut self, _error: &str) -> io::Result<()> {
Ok(())
}
}
/// Numeric representation of the message types that a server should handle.
#[cfg(any(feature = "client", feature = "server"))]
#[derive(Clone, Copy, Debug, Eq, FromPrimitive, IntoPrimitive, PartialEq)]
#[repr(u32)]
pub(crate) enum RequestCode {
GetDataSource = 0,
GetIndicator = 1,
GetMaxEventIdNum = 2,
GetModel = 3,
GetModelNames = 4,
InsertColumnStatistics = 5,
InsertModel = 6,
InsertTimeSeries = 7,
RemoveModel = 8,
RemoveOutliers = 9,
UpdateClusters = 10,
UpdateModel = 11,
UpdateOutliers = 12,
InsertEventLabels = 13,
GetDataSourceList = 14,
GetLabelDbPatterns = 15,
InsertDataSource = 20,
RenewCertificate = 23,
GetTrustedDomainList = 24,
GetOutliers = 25,
GetTorExitNodeList = 26,
GetInternalNetworkList = 31,
GetAllowlist = 32,
GetBlocklist = 33,
GetPretrainedModel = 34,
GetTrustedUserAgentList = 35,
GetConfig = 36,
UpdateHostOpenedPorts = 37,
UpdateHostOsAgents = 38,
GetSamplingPolicyList = 39,
ReportCustomerDataDeletion = 40,
/// Unknown request
#[num_enum(default)]
Unknown = u32::MAX,
}
#[cfg(all(test, any(feature = "client", feature = "server")))]
mod request_code_tests {
use num_enum::FromPrimitive;
use super::RequestCode;
#[test]
fn customer_data_deletion_report_uses_code_40() {
assert_eq!(u32::from(RequestCode::ReportCustomerDataDeletion), 40);
assert_eq!(
RequestCode::from_primitive(40),
RequestCode::ReportCustomerDataDeletion
);
}
}
#[cfg(feature = "server")]
/// A connection to a single agent (node).
///
/// Each `Connection` wraps an underlying QUIC connection and
/// provides methods for sending requests to the agent on the other
/// end. Because a `Connection` is bound to exactly one agent,
/// there is no additional node-selection parameter — calling any
/// method on this type sends the request to that agent.
///
/// The API is organized in three layers:
///
/// - **[`node()`](Self::node) handle** — the recommended entry
/// point for new code. Returns a [`node::Node`] handle that
/// groups all node-family methods under a single namespace
/// (e.g. `conn.node().power(req)`).
/// - **`node_*` methods** — typed, per-feature-family methods that
/// accept a `Node*Request` enum and return a `Node*Response`.
/// Each request variant carries a
/// [`ServiceId`](crate::service_id::ServiceId) suitable for
/// fine-grained authorization. These remain available as
/// compatibility wrappers.
/// - **Legacy flat methods** — simpler, backward-compatible
/// wrappers. They do not expose `ServiceId` and cannot
/// participate in [`Authorizer`](crate::auth::Authorizer)-based
/// access control.
///
/// See the [`impl` block documentation](Self#node-api-vs-legacy-flat-api)
/// for migration guidance and the [`server::node`](node) module
/// for the service-family entry point.
#[derive(Clone, Debug)]
pub struct Connection {
conn: quinn::Connection,
}
#[cfg(feature = "server")]
impl Connection {
/// Creates a new connection from a QUIC connection from the `quinn` crate.
#[must_use]
pub fn from_quinn(conn: quinn::Connection) -> Self {
Self { conn }
}
/// Returns a [`Node`](node::Node) handle for issuing
/// node-family requests over this connection.
///
/// The returned handle borrows this connection and exposes
/// the node API family through a service-family-oriented
/// interface. **Prefer this for new code** over calling
/// the `node_*` methods on `Connection` directly.
///
/// # Example
///
/// ```rust,no_run
/// # use review_protocol::server::Connection;
/// # async fn example(conn: Connection) -> anyhow::Result<()> {
/// use review_protocol::types::node::NodePowerRequest;
///
/// let node = conn.node();
/// let resp = node.power(NodePowerRequest::GracefulReboot).await?;
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn node(&self) -> node::Node<'_> {
node::Node::new(self)
}
/// Returns the QUIC connection compatible with the `quinn` crate.
///
/// This is for backward compatibility only and will be removed in a future
/// release.
#[cfg(any(test, feature = "test-support"))]
#[must_use]
#[doc(hidden)]
pub fn as_quinn(&self) -> &quinn::Connection {
&self.conn
}
/// Returns the cryptographic identity of the peer.
///
/// This directly corresponds to the `peer_identity` method of the underlying
/// `quinn::Connection`. In the future, this method may be removed in favor
/// of this crate's own implementation to provide additional features.
#[must_use]
pub fn peer_identity(&self) -> Option<Box<dyn std::any::Any>> {
self.conn.peer_identity()
}
/// Initiates an outgoing bidirectional stream.
///
/// This directly corresponds to the `open_bi` method of the underlying
/// `quinn::Connection`. In the future, this method may be removed in favor
/// of this crate's own implementation to provide additional features.
#[must_use]
pub fn open_bi(&self) -> quinn::OpenBi<'_> {
self.conn.open_bi()
}
/// Accepts the next incoming unidirectional stream.
///
/// This directly corresponds to the `accept_uni` method of the underlying
/// `quinn::Connection`. Pair it with [`Connection::handle_event_stream`]
/// to dispatch each stream with caller-controlled concurrency and error
/// handling.
#[must_use]
pub fn accept_uni(&self) -> quinn::AcceptUni<'_> {
self.conn.accept_uni()
}
#[cfg(any(test, feature = "test-support"))]
#[doc(hidden)]
pub fn close(&self) {
self.conn.close(0u32.into(), b"");
}
/// Accepts and handles the next unidirectional event stream
///
/// This method waits for the next unidirectional stream from the agent
/// and processes it using the provided event handler. The method returns
/// when the stream ends or an error occurs.
///
/// # Arguments
/// * `handler` - Implementation of `EventStreamHandler` trait
///
/// # Returns
/// * `Ok(())` - Stream processed successfully
/// * `Err(e)` - Connection error, protocol error, or handler error
///
/// # Errors
///
/// This function will return an error if:
/// * Failed to accept unidirectional stream (connection error)
/// * Stream processing failed (protocol error or handler error)
///
/// # Example
/// ```rust,ignore
/// # use review_protocol::server::{Connection, EventStreamHandler};
/// # use review_protocol::types::EventMessage;
/// # struct MyEventHandler;
/// # #[async_trait::async_trait]
/// # impl EventStreamHandler for MyEventHandler {
/// # async fn handle_event(&mut self, event: EventMessage) -> std::io::Result<()> {
/// # Ok(())
/// # }
/// # }
/// # async fn example(connection: Connection) -> std::io::Result<()> {
/// let handler = MyEventHandler;
/// connection.accept_event_stream(handler).await?;
/// # Ok(())
/// # }
/// ```
pub async fn accept_event_stream<H>(&self, handler: H) -> io::Result<()>
where
H: EventStreamHandler + Send + 'static,
{
let recv_stream = self.conn.accept_uni().await?;
self::stream::process_event_stream(recv_stream, handler).await
}
/// Handles a specific unidirectional stream with the given handler
///
/// This is a lower-level method that processes a specific `RecvStream`.
/// Useful for testing or when you already have a stream to process.
///
/// # Arguments
/// * `recv_stream` - The unidirectional receive stream
/// * `handler` - Implementation of `EventStreamHandler` trait
///
/// # Returns
/// * `Ok(())` - Stream processed successfully
/// * `Err(e)` - Protocol error or handler error
///
/// # Errors
///
/// This function will return an error if:
/// * Stream processing failed (protocol error or handler error)
///
/// # Example
/// ```rust,ignore
/// # use review_protocol::server::{Connection, EventStreamHandler};
/// # use review_protocol::types::EventMessage;
/// # struct MyEventHandler;
/// # #[async_trait::async_trait]
/// # impl EventStreamHandler for MyEventHandler {
/// # async fn handle_event(&mut self, event: EventMessage) -> std::io::Result<()> {
/// # Ok(())
/// # }
/// # }
/// # async fn example(recv_stream: quinn::RecvStream) -> anyhow::Result<()> {
/// let handler = MyEventHandler;
/// Connection::handle_event_stream(recv_stream, handler).await?;
/// # Ok(())
/// # }
/// ```
pub async fn handle_event_stream<H>(
recv_stream: quinn::RecvStream,
handler: H,
) -> io::Result<()>
where
H: EventStreamHandler + Send + 'static,
{
self::stream::process_event_stream(recv_stream, handler).await
}
}
#[cfg(feature = "server")]
/// Processes a handshake message and sends a response.
///
/// # Errors
///
/// Returns `HandshakeError` if the handshake failed.
///
/// # Panics
///
/// * panic if it failed to parse version requirement string.
pub async fn handshake(
conn: &quinn::Connection,
addr: SocketAddr,
version_req: &str,
highest_protocol_version: &str,
) -> Result<AgentInfo, HandshakeError> {
let (mut send, mut recv) = conn
.accept_bi()
.await
.map_err(HandshakeError::ConnectionLost)?;
let mut buf = Vec::new();
// The frame is read raw because `AgentInfo` carries a conditional tail: the
// base struct is followed by however many tail fields the peer sent, and
// decoding those needs the leftover bytes rather than a decoded value. A
// tail that is present but does not decode is rejected here rather than
// read as an absent tail.
frame::recv_raw(&mut recv, &mut buf)
.await
.map_err(handle_handshake_recv_io_error)?;
let mut agent_info = decode_agent_info(&buf).map_err(|_| HandshakeError::InvalidMessage)?;
agent_info.addr = addr;
let version_req = VersionReq::parse(version_req).expect("valid version requirement");
let protocol_version = Version::parse(&agent_info.protocol_version).map_err(|_| {
HandshakeError::IncompatibleProtocol(
agent_info.protocol_version.clone(),
version_req.to_string(),
)
})?;
if version_req.matches(&protocol_version) {
let highest_protocol_version =
Version::parse(highest_protocol_version).expect("valid semver");
if protocol_version <= highest_protocol_version {
send_ok(&mut send, &mut buf, highest_protocol_version.to_string())
.await
.map_err(handle_handshake_send_io_error)?;
Ok(agent_info)
} else {
send_err(&mut send, &mut buf, &highest_protocol_version)
.await
.map_err(handle_handshake_send_io_error)?;
send.finish().ok();
Err(HandshakeError::IncompatibleProtocol(
protocol_version.to_string(),
version_req.to_string(),
))
}
} else {
send_err(&mut send, &mut buf, version_req.to_string())
.await
.map_err(handle_handshake_send_io_error)?;
send.finish().ok();
Err(HandshakeError::IncompatibleProtocol(
protocol_version.to_string(),
version_req.to_string(),
))
}
}
#[cfg(feature = "server")]
/// Sends patterns from a label database.
///
/// # Errors
///
/// Returns an error if serialization failed or communication with the client failed.
#[deprecated(since = "0.8.1", note = "`handle` sends the response")]
pub async fn respond_with_labeldb_patterns(
send: &mut quinn::SendStream,
patterns: &[(String, Option<LabelDb>)],
) -> anyhow::Result<()> {
use anyhow::Context;
let mut buf = Vec::new();
oinq::frame::send(send, &mut buf, Ok(patterns) as Result<_, &str>)
.await
.context("failed to send response")
}
#[cfg(feature = "server")]
/// Sends a list of trusted domains to the client.
///
/// # Errors
///
/// Returns an error if serialization failed or communication with the client failed.
#[deprecated(
since = "0.8.1",
note = "Use Connection::send_trusted_domain_list directly"
)]
pub async fn send_trusted_domain_list(
conn: &quinn::Connection,
list: &[String],
) -> anyhow::Result<()> {
Connection::from_quinn(conn.clone())
.send_trusted_domain_list(list)
.await
}
#[cfg(feature = "server")]
/// Notifies the client that it should update its configuration.
///
/// # Errors
///
/// Returns an error if serialization failed or communication with the client failed.
pub async fn notify_config_update(conn: &quinn::Connection) -> anyhow::Result<()> {
use anyhow::anyhow;
let code: u32 = client::RequestCode::UpdateConfig.into();
let Ok(msg) =
bincode::serde::encode_to_vec(code, bincode::config::standard().with_fixed_int_encoding())
else {
unreachable!("serialization of u32 into memory buffer should not fail")
};
let (mut send, mut recv) = conn.open_bi().await?;
frame::send_raw(&mut send, &msg).await?;
let mut response = vec![];
frame::recv::<Result<(), String>>(&mut recv, &mut response)
.await?
.map_err(|e| anyhow!(e))
}
#[cfg(test)]
mod tests {
#[cfg(feature = "server")]
use std::io;
#[cfg(feature = "server")]
use crate::EventStreamHandler;
#[cfg(feature = "server")]
use crate::types::EventMessage;
#[cfg(feature = "server")]
struct TestEventHandler {
events: Vec<EventMessage>,
errors: Vec<String>,
stream_ended: bool,
}
#[cfg(feature = "server")]
impl TestEventHandler {
fn new() -> Self {
Self {
events: Vec::new(),
errors: Vec::new(),
stream_ended: false,
}
}
}
#[cfg(feature = "server")]
#[async_trait::async_trait]
impl EventStreamHandler for TestEventHandler {
async fn handle_event(&mut self, event: EventMessage) -> io::Result<()> {
self.events.push(event);
Ok(())
}
async fn on_error(&mut self, error: &str) -> io::Result<()> {
self.errors.push(error.to_string());
Ok(())
}
async fn on_stream_end(&mut self) -> io::Result<()> {
self.stream_ended = true;
Ok(())
}
}
#[tokio::test]
#[cfg(feature = "server")]
async fn test_event_stream_handler_interface() {
use crate::types::EventKind;
let mut handler = TestEventHandler::new();
let event = EventMessage {
time: jiff::Timestamp::now(),
kind: EventKind::DnsCovertChannel,
fields: vec![1, 2, 3, 4],
};
assert!(handler.handle_event(event.clone()).await.is_ok());
assert_eq!(handler.events.len(), 1);
assert_eq!(handler.events[0].kind, EventKind::DnsCovertChannel);
assert_eq!(handler.events[0].fields, vec![1, 2, 3, 4]);
assert!(handler.on_error("test error").await.is_ok());
assert_eq!(handler.errors.len(), 1);
assert_eq!(handler.errors[0], "test error");
assert!(handler.on_stream_end().await.is_ok());
assert!(handler.stream_ended);
}
#[tokio::test]
#[cfg(feature = "server")]
async fn test_event_handler_error_handling() {
use crate::types::EventKind;
struct FailingHandler;
#[async_trait::async_trait]
impl EventStreamHandler for FailingHandler {
async fn handle_event(&mut self, _event: EventMessage) -> io::Result<()> {
Err(io::Error::other("processing failed"))
}
async fn on_error(&mut self, _error: &str) -> io::Result<()> {
Err(io::Error::other("error handling failed"))
}
async fn on_stream_end(&mut self) -> io::Result<()> {
Err(io::Error::other("stream end failed"))
}
}
let mut handler = FailingHandler;
let event = EventMessage {
time: jiff::Timestamp::now(),
kind: EventKind::HttpThreat,
fields: vec![],
};
assert!(handler.handle_event(event).await.is_err());
assert!(handler.on_error("test").await.is_err());
assert!(handler.on_stream_end().await.is_err());
}
#[tokio::test]
#[cfg(feature = "server")]
async fn test_default_implementations() {
struct MinimalHandler;
#[async_trait::async_trait]
impl EventStreamHandler for MinimalHandler {
async fn handle_event(&mut self, _event: EventMessage) -> io::Result<()> {
Ok(())
}
}
let mut handler = MinimalHandler;
assert!(handler.on_stream_end().await.is_ok());
assert!(handler.on_error("test error").await.is_ok());
}
#[tokio::test]
#[cfg(all(feature = "client", feature = "server"))]
async fn trusted_domain_list() {
use crate::test::TEST_ENV;
struct Handler {}
#[async_trait::async_trait]
impl crate::request::Handler for Handler {
async fn trusted_domain_list(&mut self, domains: &[&str]) -> Result<(), String> {
if domains == TRUSTED_DOMAIN_LIST {
Ok(())
} else {
Err("unexpected domain list".to_string())
}
}
}
const TRUSTED_DOMAIN_LIST: &[&str] = &["example.com", "example.org"];
let test_env = TEST_ENV.lock().await;
let (server_conn, client_conn) = test_env.setup().await;
// Test `server::send_trusted_domain_list`
let domains_to_send = TRUSTED_DOMAIN_LIST
.iter()
.map(|&domain| domain.to_string())
.collect::<Vec<_>>();
let mut handler = Handler {};
let handler_conn = client_conn.clone();
let client_handle = tokio::spawn(async move {
let (mut send, mut recv) = handler_conn.accept_bi().await.unwrap();
crate::request::handle(&mut handler, &mut send, &mut recv).await
});
let server_res = server_conn.send_trusted_domain_list(&domains_to_send).await;
assert!(server_res.is_ok());
let client_res = client_handle.await.unwrap();
assert!(client_res.is_ok());
test_env.teardown(&server_conn);
}
#[tokio::test]
#[cfg(all(feature = "client", feature = "server"))]
async fn notify_config_update() {
use crate::test::TEST_ENV;
struct Handler {}
#[async_trait::async_trait]
impl crate::request::Handler for Handler {
async fn update_config(&mut self) -> Result<(), String> {
Ok(())
}
}
let test_env = TEST_ENV.lock().await;
let (server_conn, client_conn) = test_env.setup().await;
let mut handler = Handler {};
let handler_conn = client_conn.clone();
let client_handle = tokio::spawn(async move {
let (mut send, mut recv) = handler_conn.accept_bi().await.unwrap();
crate::request::handle(&mut handler, &mut send, &mut recv).await
});
let server_res = crate::server::notify_config_update(server_conn.as_quinn()).await;
assert!(server_res.is_ok());
let client_res = client_handle.await.unwrap();
assert!(client_res.is_ok());
test_env.teardown(&server_conn);
}
#[tokio::test]
#[cfg(all(feature = "client", feature = "server"))]
async fn test_accept_event_stream() {
use std::sync::{Arc, Mutex};
struct TestHandler {
events: Arc<Mutex<Vec<EventMessage>>>,
}
#[async_trait::async_trait]
impl EventStreamHandler for TestHandler {
async fn handle_event(&mut self, event: EventMessage) -> io::Result<()> {
self.events.lock().unwrap().push(event);
Ok(())
}
}
let test_env = crate::test::TEST_ENV.lock().await;
let (server_conn, client_conn) = test_env.setup().await;
let events = Arc::new(Mutex::new(Vec::new()));
let events_clone = events.clone();
let handler = TestHandler {
events: events_clone,
};
// Clone server connection for teardown
let server_conn_for_teardown = server_conn.clone();
// Client opens unidirectional stream and sends events
let client_handle = tokio::spawn(async move {
let mut send = client_conn.open_uni().await.unwrap();
// Write protocol header
send.write_all(&[0, 0]).await.unwrap();
// Send test event
let event = EventMessage {
time: jiff::Timestamp::now(),
kind: crate::types::EventKind::DnsCovertChannel,
fields: vec![1, 2, 3, 4],
};
let codec = bincode::config::standard();
let serialized = bincode::serde::encode_to_vec(&event, codec).unwrap();
#[allow(clippy::cast_possible_truncation)]
let len = serialized.len() as u32;
send.write_all(&len.to_be_bytes()).await.unwrap();
send.write_all(&serialized).await.unwrap();
// Close stream properly to signal EOF
send.finish().unwrap();
// Give server time to accept the stream
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
});
// Wait for client to send data
client_handle.await.unwrap();
// Now accept and process the stream
let server_res = server_conn.accept_event_stream(handler).await;
assert!(server_res.is_ok());
let received_events = events.lock().unwrap();
assert_eq!(received_events.len(), 1);
assert_eq!(
received_events[0].kind,
crate::types::EventKind::DnsCovertChannel
);
assert_eq!(received_events[0].fields, vec![1, 2, 3, 4]);
test_env.teardown(&server_conn_for_teardown);
}
#[tokio::test]
#[cfg(all(feature = "client", feature = "server"))]
async fn test_handle_event_stream() {
use std::sync::{Arc, Mutex};
struct TestHandler {
events: Arc<Mutex<Vec<EventMessage>>>,
}
#[async_trait::async_trait]
impl EventStreamHandler for TestHandler {
async fn handle_event(&mut self, event: EventMessage) -> io::Result<()> {
self.events.lock().unwrap().push(event);
Ok(())
}
}
let test_env = crate::test::TEST_ENV.lock().await;
let (server_conn, client_conn) = test_env.setup().await;
let events = Arc::new(Mutex::new(Vec::new()));
let events_clone = events.clone();
let handler = TestHandler {
events: events_clone,
};
// Client opens unidirectional stream and sends events
let client_handle = tokio::spawn(async move {
let mut send = client_conn.open_uni().await.unwrap();
// Write protocol header
send.write_all(&[0, 0]).await.unwrap();
// Send test event
let event = EventMessage {
time: jiff::Timestamp::now(),
kind: crate::types::EventKind::HttpThreat,
fields: vec![5, 6, 7],
};
let codec = bincode::config::standard();
let serialized = bincode::serde::encode_to_vec(&event, codec).unwrap();
#[allow(clippy::cast_possible_truncation)]
let len = serialized.len() as u32;
send.write_all(&len.to_be_bytes()).await.unwrap();
send.write_all(&serialized).await.unwrap();
// Close stream properly to signal EOF
send.finish().unwrap();
// Give server time to accept the stream
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
});
// Wait for client to send data
client_handle.await.unwrap();
// Now accept uni stream and handle it
let server_conn_clone = server_conn.clone();
let recv_stream = server_conn_clone.conn.accept_uni().await.unwrap();
let server_res = crate::server::Connection::handle_event_stream(recv_stream, handler).await;
assert!(server_res.is_ok());
let received_events = events.lock().unwrap();
assert_eq!(received_events.len(), 1);
assert_eq!(received_events[0].kind, crate::types::EventKind::HttpThreat);
assert_eq!(received_events[0].fields, vec![5, 6, 7]);
test_env.teardown(&server_conn);
}
#[tokio::test]
#[cfg(all(feature = "client", feature = "server"))]
async fn test_accept_multiple_streams() {
use std::sync::{Arc, Mutex};
struct TestHandler {
events: Arc<Mutex<Vec<EventMessage>>>,
}