Skip to content

Commit 00e7f6b

Browse files
committed
Librarify
1 parent f8f9a65 commit 00e7f6b

File tree

12 files changed

+85
-36
lines changed

12 files changed

+85
-36
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ smoltcp = { version = "0.10.0", default-features = false, features = ["socket",
2424
tokio = { version = "1.29.1", features = ["rt", "net", "sync", "macros", "io-util", "time"] }
2525
tracing = "0.1.37"
2626
tracing-subscriber = {version="0.3.17", optional=true}
27+
libwgslirpy = {version = "0.2.0", path="crates/libwgslirpy"}
2728

2829
[features]
2930
default = ["tracing-subscriber", "tracing/release_max_level_debug"]
31+
32+
[workspace]
33+
members = ["crates/*"]

crates/libwgslirpy/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "libwgslirpy"
3+
version = "0.2.0"
4+
edition = "2021"
5+
repository = "https://github.com/vi/wgslirpy"
6+
license = "MIT/Apache-2.0"
7+
description = "User-space Wireguard to internet router, like SLiRP, but with Wireguard instead of PPP (library part of the CLI tool)"
8+
categories = ["network-programming"]
9+
keywords = ["wireguard","onetun","slirp","nat"]
10+
11+
[dependencies]
12+
anyhow = "1.0.72"
13+
base64 = "0.21.2"
14+
boringtun = "0.6.0"
15+
bytes = "1.4.0"
16+
hashbrown = "0.14.0"
17+
simple-dns = "0.5.3"
18+
smoltcp = { version = "0.10.0", default-features = false, features = ["socket", "socket-tcp", "socket-udp", "std", "proto-ipv4", "proto-ipv4-fragmentation", "proto-ipv6", "fragmentation-buffer-size-65536", "assembler-max-segment-count-32", "log", "medium-ip"] }
19+
tokio = { version = "1.29.1", features = ["rt", "net", "sync", "macros", "io-util", "time"] }
20+
tracing = "0.1.37"

src/channelized_smoltcp_device.rs renamed to crates/libwgslirpy/src/channelized_smoltcp_device.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use smoltcp::phy::{Device,RxToken,TxToken, Checksum};
55
use tokio::sync::mpsc::Sender;
66
use tracing::warn;
77

8-
use crate::TEAR_OF_ALLOCATION;
8+
use crate::TEAR_OF_ALLOCATION_SIZE;
99

1010
pub struct ChannelizedDevice {
1111
pub tx : Sender<BytesMut>,
@@ -19,7 +19,7 @@ impl ChannelizedDevice {
1919
ChannelizedDevice {
2020
tx,
2121
rx: None,
22-
tear_off_buffer: BytesMut::with_capacity(TEAR_OF_ALLOCATION),
22+
tear_off_buffer: BytesMut::with_capacity(TEAR_OF_ALLOCATION_SIZE),
2323
mtu,
2424
}
2525
}
@@ -84,7 +84,7 @@ impl<'a> TxToken for &'a mut ChannelizedDevice {
8484
warn!("Failed to transmit into a ChannelizedDevice");
8585
}
8686
if self.tear_off_buffer.capacity() < 2048 {
87-
self.tear_off_buffer = BytesMut::with_capacity(TEAR_OF_ALLOCATION);
87+
self.tear_off_buffer = BytesMut::with_capacity(TEAR_OF_ALLOCATION_SIZE);
8888
}
8989
ret
9090
}

crates/libwgslirpy/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const TEAR_OF_ALLOCATION_SIZE : usize = 65536;
2+
3+
pub mod wg;
4+
pub mod router;
5+
pub mod channelized_smoltcp_device;
6+
7+
use tracing::warn;
8+
9+
pub use wg::parsebase64_32;
10+
11+
pub async fn run(wireguard_options: wg::Opts, router_options: router::Opts, transmit_queue_capacity: usize) -> anyhow::Result<()> {
12+
let (tx_towg, rx_towg) = tokio::sync::mpsc::channel(transmit_queue_capacity);
13+
let (tx_fromwg, rx_fromwg) = tokio::sync::mpsc::channel(4);
14+
let _jh = ArmedJoinHandle(tokio::spawn(async move {
15+
match wireguard_options.start(tx_fromwg, rx_towg).await {
16+
Ok(()) => warn!("Exited from Wireguard loop"),
17+
Err(e) => warn!("Exiten from Wireguard loop: {e}"),
18+
}
19+
}));
20+
21+
router::run(rx_fromwg, tx_towg, router_options).await?;
22+
23+
Ok(())
24+
}
25+
struct ArmedJoinHandle(tokio::task::JoinHandle<()>);
26+
27+
impl Drop for ArmedJoinHandle {
28+
fn drop(&mut self) {
29+
self.0.abort();
30+
}
31+
}
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use smoltcp::wire::{IpEndpoint, IpVersion, TcpPacket};
99
use tokio::sync::mpsc::{channel, Receiver, Sender};
1010
use tracing::{debug, error, info, warn};
1111

12+
use crate::ArmedJoinHandle;
13+
1214
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
1315
enum NatKey {
1416
Tcp {
@@ -60,14 +62,6 @@ mod serve_pingable;
6062
mod serve_tcp;
6163
mod serve_udp;
6264

63-
struct ArmedJoinHandle(tokio::task::JoinHandle<()>);
64-
65-
impl Drop for ArmedJoinHandle {
66-
fn drop(&mut self) {
67-
self.0.abort();
68-
}
69-
}
70-
7165
pub async fn run(
7266
mut rx_from_wg: Receiver<BytesMut>,
7367
tx_to_wg: Sender<BytesMut>,
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/router/serve_udp.rs renamed to crates/libwgslirpy/src/router/serve_udp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tokio::{
1414
};
1515
use tracing::{warn, debug};
1616

17-
use crate::TEAR_OF_ALLOCATION;
17+
use crate::TEAR_OF_ALLOCATION_SIZE;
1818

1919
pub const UDP_CONNECTION_EXPIRATION_SECONDS : u64 = 92;
2020

@@ -51,7 +51,7 @@ pub async fn serve_udp(
5151
checksummer.ipv4 = Checksum::Tx;
5252
checksummer.tcp = Checksum::Tx;
5353

54-
let mut tear_off_buffer = BytesMut::with_capacity(TEAR_OF_ALLOCATION);
54+
let mut tear_off_buffer = BytesMut::with_capacity(TEAR_OF_ALLOCATION_SIZE);
5555

5656
enum SelectOutcome {
5757
FromWg(Option<BytesMut>),
@@ -176,7 +176,7 @@ pub async fn serve_udp(
176176
let buf = tear_off_buffer.split();
177177
tx_to_wg.send(buf).await?;
178178
if tear_off_buffer.capacity() < 2048 {
179-
tear_off_buffer = BytesMut::with_capacity(TEAR_OF_ALLOCATION);
179+
tear_off_buffer = BytesMut::with_capacity(TEAR_OF_ALLOCATION_SIZE);
180180
}
181181
}
182182
SelectOutcome::Timeout => {

0 commit comments

Comments
 (0)