忽略绑定失败的错误

This commit is contained in:
lbl8603
2024-08-07 23:14:13 +08:00
parent 803af55246
commit 7e0de2c4e6
4 changed files with 23 additions and 8 deletions
+1
View File
@@ -41,6 +41,7 @@ fn main0(config: Config, _show_cmd: bool) {
let vnt_util = match Vnt::new(config, callback::VntHandler {}) { let vnt_util = match Vnt::new(config, callback::VntHandler {}) {
Ok(vnt) => vnt, Ok(vnt) => vnt,
Err(e) => { Err(e) => {
log::error!("vnt create error {:?}", e);
println!("error: {:?}", e); println!("error: {:?}", e);
std::process::exit(1); std::process::exit(1);
} }
+6 -2
View File
@@ -262,9 +262,13 @@ pub(crate) fn init_context(
let socket = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None)?; let socket = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None)?;
(socket, address) (socket, address)
}; };
socket.set_reuse_address(true)?; socket
.set_reuse_address(true)
.context("set_reuse_address")?;
#[cfg(unix)] #[cfg(unix)]
socket.set_reuse_port(true)?; if let Err(e) = socket.set_reuse_port(true) {
log::warn!("set_reuse_port {:?}", e)
}
if let Err(e) = socket.bind(&address.into()) { if let Err(e) = socket.bind(&address.into()) {
if ports[0] == 0 { if ports[0] == 0 {
//端口可能冲突,则使用任意端口 //端口可能冲突,则使用任意端口
+3 -1
View File
@@ -63,7 +63,9 @@ pub fn create_tcp0(
if bind_port != 0 { if bind_port != 0 {
socket.set_reuse_address(true)?; socket.set_reuse_address(true)?;
#[cfg(unix)] #[cfg(unix)]
socket.set_reuse_port(true)?; if let Err(e) = socket.set_reuse_port(true) {
log::warn!("set_reuse_port {:?}", e)
}
if v4 { if v4 {
let addr: SocketAddr = format!("0.0.0.0:{}", bind_port).parse().unwrap(); let addr: SocketAddr = format!("0.0.0.0:{}", bind_port).parse().unwrap();
socket.bind(&addr.into())?; socket.bind(&addr.into())?;
+13 -5
View File
@@ -14,6 +14,7 @@ use tokio::sync::mpsc::{channel, Receiver};
use crate::channel::context::ChannelContext; use crate::channel::context::ChannelContext;
use crate::channel::handler::RecvChannelHandler; use crate::channel::handler::RecvChannelHandler;
use crate::channel::sender::PacketSender; use crate::channel::sender::PacketSender;
use crate::channel::socket::create_tcp0;
use crate::channel::{ConnectProtocol, RouteKey, BUFFER_SIZE, TCP_MAX_PACKET_SIZE}; use crate::channel::{ConnectProtocol, RouteKey, BUFFER_SIZE, TCP_MAX_PACKET_SIZE};
use crate::util::StopManager; use crate::util::StopManager;
@@ -99,11 +100,18 @@ async fn connect_tcp0<H>(
where where
H: RecvChannelHandler, H: RecvChannelHandler,
{ {
let mut stream = tokio::time::timeout( let socket = if bind_port != 0 {
Duration::from_secs(3), match create_tcp0(addr.is_ipv4(), bind_port, context.default_interface()) {
crate::channel::socket::connect_tcp(addr, bind_port, context.default_interface()), Ok(socket) => socket,
) Err(e) => {
.await??; log::warn!("{:?}", e);
create_tcp0(addr.is_ipv4(), 0, context.default_interface())?
}
}
} else {
create_tcp0(addr.is_ipv4(), 0, context.default_interface())?
};
let mut stream = tokio::time::timeout(Duration::from_secs(3), socket.connect(addr)).await??;
tcp_write(&mut stream, &data).await?; tcp_write(&mut stream, &data).await?;
tcp_stream_handle(stream, addr, recv_handler, context).await; tcp_stream_handle(stream, addr, recv_handler, context).await;