-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NC-43] Route packets back to WG peer (#3965)
* Initial work on reverse nat * wip * Refine key gen * Rename to wg_tunnel * Forward packet to peer * Remove source_addr * Check if allowed to write to tunnel * Extract out network_table * Move map struc definitions to udp_listener * Delegate ip network table calls * Fix mac compilation * Add TunTaskTx type
- Loading branch information
Showing
8 changed files
with
183 additions
and
86 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,48 @@ | ||
#![cfg_attr(not(target_os = "linux"), allow(dead_code))] | ||
|
||
use nym_task::TaskClient; | ||
|
||
mod error; | ||
mod event; | ||
mod network_table; | ||
mod platform; | ||
mod setup; | ||
mod tun; | ||
mod udp_listener; | ||
mod wg_tunnel; | ||
|
||
// Currently the module related to setting up the virtual network device is platform specific. | ||
#[cfg(target_os = "linux")] | ||
use platform::linux::tun_device; | ||
|
||
type ActivePeers = | ||
dashmap::DashMap<std::net::SocketAddr, tokio::sync::mpsc::UnboundedSender<crate::event::Event>>; | ||
#[derive(Clone)] | ||
struct TunTaskTx(tokio::sync::mpsc::UnboundedSender<Vec<u8>>); | ||
|
||
impl TunTaskTx { | ||
fn send(&self, packet: Vec<u8>) -> Result<(), tokio::sync::mpsc::error::SendError<Vec<u8>>> { | ||
self.0.send(packet) | ||
} | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
pub async fn start_wireguard( | ||
task_client: TaskClient, | ||
task_client: nym_task::TaskClient, | ||
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { | ||
use std::sync::Arc; | ||
|
||
// The set of active tunnels indexed by the peer's address | ||
let active_peers = std::sync::Arc::new(ActivePeers::new()); | ||
let active_peers = Arc::new(udp_listener::ActivePeers::new()); | ||
let peers_by_ip = Arc::new(std::sync::Mutex::new(network_table::NetworkTable::new())); | ||
|
||
// Start the tun device that is used to relay traffic outbound | ||
let tun_task_tx = tun_device::start_tun_device(active_peers.clone()); | ||
let tun_task_tx = tun_device::start_tun_device(peers_by_ip.clone()); | ||
|
||
// Start the UDP listener that clients connect to | ||
udp_listener::start_udp_listener(tun_task_tx, active_peers, task_client).await?; | ||
udp_listener::start_udp_listener(tun_task_tx, active_peers, peers_by_ip, task_client).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(not(target_os = "linux"))] | ||
pub async fn start_wireguard( | ||
_task_client: TaskClient, | ||
_task_client: nym_task::TaskClient, | ||
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { | ||
todo!("WireGuard is currently only supported on Linux") | ||
} |
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,25 @@ | ||
use std::net::IpAddr; | ||
|
||
use ip_network::IpNetwork; | ||
use ip_network_table::IpNetworkTable; | ||
|
||
#[derive(Default)] | ||
pub(crate) struct NetworkTable<T> { | ||
ips: IpNetworkTable<T>, | ||
} | ||
|
||
impl<T> NetworkTable<T> { | ||
pub(crate) fn new() -> Self { | ||
Self { | ||
ips: IpNetworkTable::new(), | ||
} | ||
} | ||
|
||
pub fn insert<N: Into<IpNetwork>>(&mut self, network: N, data: T) -> Option<T> { | ||
self.ips.insert(network, data) | ||
} | ||
|
||
pub fn longest_match<I: Into<IpAddr>>(&self, ip: I) -> Option<(IpNetwork, &T)> { | ||
self.ips.longest_match(ip) | ||
} | ||
} |
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
Oops, something went wrong.