b5c8b69547
* Experiment with serde * Framed encoding serde POC * Outfox framing * Outfox rest compat (#3333) * Outfox forwarding compat * Tidy up interface * PacketSize compat * Address PR comments * Rebase on develop commit342883fcbeAuthor: durch <durch@users.noreply.github.com> Date: Thu Apr 27 09:17:18 2023 +0200 Put back PacketType 1 commit61a0ee5a19Author: Tommy Verrall <tommyvez@protonmail.com> Date: Wed Apr 26 16:37:29 2023 +0100 change output for cpu-cycle management logs commit3956109c7eAuthor: Tommy Verrall <tommy@nymtech.net> Date: Wed Apr 26 12:13:22 2023 +0100 change the workflow file to build with cpucycles commit8d725b13c5Author: durch <durch@users.noreply.github.com> Date: Mon Apr 24 13:14:58 2023 +0200 Outfox client compat commit4d166c389bAuthor: durch <durch@users.noreply.github.com> Date: Fri Apr 21 00:30:46 2023 +0200 Address PR comments commit145c3c1223Author: durch <durch@users.noreply.github.com> Date: Fri Apr 21 00:12:35 2023 +0200 Rename PacketMode commitcbd654d6fdAuthor: Drazen Urch <drazen@urch.eu> Date: Thu Apr 20 23:59:40 2023 +0200 Outfox rest compat (#3333) * Outfox forwarding compat * Tidy up interface * PacketSize compat commite7be91a94cAuthor: durch <durch@users.noreply.github.com> Date: Wed Apr 19 16:36:48 2023 +0200 Remove serde cruft commit582e7d566aAuthor: durch <durch@users.noreply.github.com> Date: Wed Apr 19 16:24:09 2023 +0200 Outfox framing commit6464da5f01Author: durch <durch@users.noreply.github.com> Date: Tue Apr 18 22:23:02 2023 +0200 Framing compat commitd5e77e499bAuthor: durch <durch@users.noreply.github.com> Date: Tue Apr 18 18:18:54 2023 +0200 Framed encoding serde POC commitf086f9c35aAuthor: durch <durch@users.noreply.github.com> Date: Tue Apr 18 16:54:21 2023 +0200 Experiment with serde * Client tweaks * Speed up from_plaintext * SurbAcks * More work on the reciever end, and outfox format * Cleanup and fmt * Wrap up rebase * Happy clippy * Fix lock files * Final cleanup
78 lines
2.6 KiB
Rust
78 lines
2.6 KiB
Rust
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
use crate::client::{Client, Config, SendWithoutResponse};
|
|
use futures::channel::mpsc;
|
|
use futures::StreamExt;
|
|
use log::*;
|
|
use nym_sphinx::forwarding::packet::MixPacket;
|
|
use std::time::Duration;
|
|
|
|
pub type MixForwardingSender = mpsc::UnboundedSender<MixPacket>;
|
|
type MixForwardingReceiver = mpsc::UnboundedReceiver<MixPacket>;
|
|
|
|
/// A specialisation of client such that it forwards any received packets on the channel into the
|
|
/// mix network immediately, i.e. will not try to listen for any responses.
|
|
pub struct PacketForwarder {
|
|
mixnet_client: Client,
|
|
packet_receiver: MixForwardingReceiver,
|
|
shutdown: nym_task::TaskClient,
|
|
}
|
|
|
|
impl PacketForwarder {
|
|
pub fn new(
|
|
initial_reconnection_backoff: Duration,
|
|
maximum_reconnection_backoff: Duration,
|
|
initial_connection_timeout: Duration,
|
|
maximum_connection_buffer_size: usize,
|
|
use_legacy_version: bool,
|
|
shutdown: nym_task::TaskClient,
|
|
) -> (PacketForwarder, MixForwardingSender) {
|
|
let client_config = Config::new(
|
|
initial_reconnection_backoff,
|
|
maximum_reconnection_backoff,
|
|
initial_connection_timeout,
|
|
maximum_connection_buffer_size,
|
|
use_legacy_version,
|
|
);
|
|
|
|
let (packet_sender, packet_receiver) = mpsc::unbounded();
|
|
|
|
(
|
|
PacketForwarder {
|
|
mixnet_client: Client::new(client_config),
|
|
packet_receiver,
|
|
shutdown,
|
|
},
|
|
packet_sender,
|
|
)
|
|
}
|
|
|
|
pub async fn run(&mut self) {
|
|
while !self.shutdown.is_shutdown() {
|
|
tokio::select! {
|
|
biased;
|
|
_ = self.shutdown.recv() => {
|
|
log::trace!("PacketForwarder: Received shutdown");
|
|
}
|
|
Some(mix_packet) = self.packet_receiver.next() => {
|
|
trace!("Going to forward packet to {:?}", mix_packet.next_hop());
|
|
|
|
let next_hop = mix_packet.next_hop();
|
|
let packet_type = mix_packet.packet_type();
|
|
let packet = mix_packet.into_packet();
|
|
// we don't care about responses, we just want to fire packets
|
|
// as quickly as possible
|
|
|
|
if let Err(err) =
|
|
self.mixnet_client
|
|
.send_without_response(next_hop, packet, packet_type)
|
|
{
|
|
debug!("failed to forward the packet - {err}")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|