Skip to content

Commit bf68ad7

Browse files
committed
fix: fix keepalive not working
1 parent 244ab92 commit bf68ad7

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

examples/read_async.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2+
// Version 2, December 2004
3+
//
4+
// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
5+
//
6+
// Everyone is permitted to copy and distribute verbatim or modified
7+
// copies of this license document, and changing it is allowed as long
8+
// as the name is changed.
9+
//
10+
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11+
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12+
//
13+
// 0. You just DO WHAT THE FUCK YOU WANT TO.
14+
15+
use std::thread::{sleep, Builder};
16+
use std::time::Duration;
17+
use tokio::io::AsyncReadExt;
18+
use tokio_util::sync::CancellationToken;
19+
use tun::{AbstractDevice, BoxError};
20+
21+
#[tokio::main]
22+
async fn main() -> Result<(), BoxError> {
23+
main_entry().await?;
24+
loop {
25+
tokio::time::sleep(Duration::from_secs(10)).await;
26+
}
27+
}
28+
29+
async fn main_entry() -> Result<(), BoxError> {
30+
let mut config = tun::Configuration::default();
31+
32+
config
33+
.address((10, 0, 0, 9))
34+
.netmask((255, 255, 255, 0))
35+
.destination((10, 0, 0, 1))
36+
.mtu(tun::DEFAULT_MTU)
37+
.up();
38+
39+
#[cfg(target_os = "linux")]
40+
config.platform_config(|config| {
41+
config.ensure_root_privileges(true);
42+
});
43+
44+
let mut dev = tun::create_as_async(&config)?;
45+
let size = dev.mtu()? as usize + tun::PACKET_INFORMATION_LENGTH;
46+
let mut buf = vec![0; size];
47+
loop {
48+
tokio::select! {
49+
len = dev.read(&mut buf) => {
50+
println!("pkt: {:?}", &buf[..len?]);
51+
}
52+
};
53+
}
54+
Ok(())
55+
}

src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct Args {
3434
pub keepalive_interval: u64,
3535

3636
/// Keep-alive threshold (reconnect after this many failures)
37-
#[arg(long, default_value = "5")]
37+
#[arg(long, default_value = "3")]
3838
pub keepalive_threshold: u8,
3939

4040
/// Enable P2P direct connection (disabled by default, uses relay only)

src/client/relay.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,18 @@ impl RelayClient {
5959
let port = self.cfg.port;
6060
let stun_ip = self.cfg.stun_ip.clone();
6161
let stun_port = self.cfg.stun_port;
62+
63+
let mut last_active = Instant::now();
64+
let timeout_secs = (self.cfg.keep_alive_thresh - 1) as u64 * self.cfg.keepalive_interval.as_secs();
6265
loop {
6366
tokio::select! {
6467
_ = keepalive_ticker.tick() => {
68+
if last_active.elapsed().as_secs() > timeout_secs {
69+
tracing::warn!("keepalive threshold {:?} exceeded", last_active.elapsed());
70+
break;
71+
}
72+
73+
tracing::debug!("sending keepalive frame");
6574
let keepalive_frame = Frame::KeepAlive(KeepAliveFrame {
6675
identity: self.cfg.identity.clone(),
6776
ipv6: current_ipv6.clone(),
@@ -87,6 +96,7 @@ impl RelayClient {
8796

8897
// Periodic IPv6 address update check
8998
_ = ipv6_update_ticker.tick() => {
99+
tracing::debug!("ipv6 update tick");
90100
if let Some(new_ipv6) = utils::get_ipv6() {
91101
tracing::info!("IPv6 address updated: {} -> {}", current_ipv6, new_ipv6);
92102
current_ipv6 = new_ipv6.clone();
@@ -98,6 +108,7 @@ impl RelayClient {
98108

99109
// inbound
100110
result = conn.read_frame() => {
111+
last_active = Instant::now();
101112
match result {
102113
Ok(frame) => {
103114
tracing::debug!("received frame {}", frame);

src/server/server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ impl Handler {
194194
};
195195
}
196196
}
197+
// TODO: check last_active
197198
}
198199
}
199200

0 commit comments

Comments
 (0)