Skip to content

Commit f21e781

Browse files
committed
Fix: keep alive
1 parent 2006e4b commit f21e781

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ uuid = { version = "1.3.4", features = ["v4"] }
1616
quinn = "0.10.2"
1717
rustls = { version = "0.21.6", default-features = false, features = ["quic", "dangerous_configuration"] }
1818
rcgen = "0.11.1"
19+
macro-log = "0.2.0"

core/src/client.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::{io::{Error, ErrorKind}, future::Future};
1+
use std::{io::{Error, ErrorKind}, future::Future, time::Duration};
22

3-
use tokio::{net::TcpStream, task::JoinHandle};
3+
use tokio::{net::TcpStream, task::JoinHandle, time::timeout};
44

55
use crate::{
66
cmd::{read_cmd, write_cmd, Command},
@@ -39,7 +39,11 @@ impl Client {
3939
R: Future<Output = ()>,
4040
{
4141
loop {
42-
let cmd: Command = read_cmd(&mut self.stream, "").await;
42+
let Ok(cmd) = timeout(Duration::from_millis(10000), read_cmd(&mut self.stream, "")).await
43+
else {
44+
e!("连接不活跃!");
45+
break;
46+
};
4347
let local_service = local_service.clone();
4448
wtf!(&cmd);
4549
match cmd {
@@ -97,8 +101,12 @@ impl Client {
97101
e!("会话异常:{}", e);
98102
break;
99103
}
100-
Command::Nothing => {
101-
let _ = write_cmd(&mut self.stream, Command::Nothing, "").await;
104+
Command::KeepAlive => {
105+
let Ok(_) = write_cmd(&mut self.stream, Command::KeepAlive, "").await
106+
else {
107+
e!("连接中断!");
108+
break;
109+
};
102110
},
103111
_ => continue,
104112
};

core/src/cmd.rs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub enum Command {
3030
nat_type: NatType,
3131
udp_addr: String, // Udp公网地址
3232
},
33+
KeepAlive,
3334
Nothing,
3435
Success,
3536
Failure {
@@ -142,6 +143,7 @@ pub async fn read_cmd(tcp: &mut TcpStream, password: &str) -> Command {
142143
Some("failure") => Command::Failure {
143144
reason: cmds.into_iter().collect::<Vec<&str>>().join(" "),
144145
},
146+
Some("keepalive") => Command::KeepAlive,
145147
_ => Command::Nothing,
146148
}
147149
}
@@ -168,6 +170,7 @@ pub async fn write_cmd(tcp: &mut TcpStream, cmd: Command, password: &str) -> std
168170
Command::Failure { reason } => {
169171
format!("failure {reason}")
170172
}
173+
Command::KeepAlive => "keepalive".into(),
171174
_ => "".into(),
172175
};
173176
let len_password = password.len() as u8;

core/src/server.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{borrow::BorrowMut, sync::Arc, collections::HashMap, io::ErrorKind, tim
33
use once_cell::sync::Lazy;
44
use tokio::{
55
net::{TcpListener, TcpStream},
6-
sync::Mutex, task::JoinHandle, time::sleep,
6+
sync::Mutex, task::JoinHandle, time::{sleep, timeout},
77
};
88

99
use crate::{
@@ -138,20 +138,20 @@ impl Server {
138138
// Bug: 代理人已经死掉还能写入命令
139139
let mut agent = agent.lock().await;
140140
let agent = agent.borrow_mut();
141-
match write_cmd(agent, Command::Nothing, "").await {
141+
match write_cmd(agent, Command::KeepAlive, "").await {
142142
Err(_) => {
143-
i!("PORT({port}) -> Agent {agent_addr} offline, release the port!");
143+
e!("PORT({port}) -> Agent {agent_addr} offline, release the port!");
144144
task.abort();
145145
break;
146146
}
147147
_ => ()
148148
}
149-
match read_cmd(agent, "").await {
150-
Command::Nothing => {
149+
match timeout(Duration::from_millis(2000), read_cmd(agent, "")).await {
150+
Ok(Command::KeepAlive) => {
151151
// i!("AGENT({agent_addr}) -> Living");
152152
}
153153
_ => {
154-
i!("PORT({port}) -> Agent {agent_addr} no response, release the port!");
154+
e!("PORT({port}) -> Agent {agent_addr} no response, release the port!");
155155
task.abort();
156156
break;
157157
}

0 commit comments

Comments
 (0)