Skip to content

Commit

Permalink
print error when connect failed
Browse files Browse the repository at this point in the history
  • Loading branch information
lazytiger committed Nov 11, 2024
1 parent 9df91fa commit 13242d4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
19 changes: 13 additions & 6 deletions src/aproxy/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ pub async fn run_tcp(
sender.send(dst_addr.ip())?;
}
client.set_nodelay(true)?;
spawn(start_tcp_proxy(
client,
server_name.clone(),
connector.clone(),
dst_addr,
));
let server_name_clone = server_name.clone();
let connector_clone = connector.clone();
spawn(async move {
let ret = start_tcp_proxy(
client,
server_name_clone,
connector_clone,
dst_addr,
).await;
if let Err(err) = ret {
log::error!("tcp proxy routine exit with:{:?}", err);
}
});
}
}

Expand Down
31 changes: 17 additions & 14 deletions src/aproxy/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,25 @@ async fn local_to_remote(
src_addr: SocketAddr,
sender: Sender<SocketAddr>,
) {
let mut remote = if let Ok(mut remote) = init_tls_conn(connector, server_name).await {
if let Err(err) = remote.write_all(request.as_ref()).await {
let _ = remote.shutdown().await;
let mut remote = match init_tls_conn(connector, server_name).await {
Ok(mut remote) => {
if let Err(err) = remote.write_all(request.as_ref()).await {
let _ = remote.shutdown().await;
let _ = sender.send(src_addr).await;
log::error!("send handshake to remote failed:{}", err);
return;
}
let (read_half, write_half) = split(remote);
spawn(remote_to_local_with_wait(
read_half, socket, src_addr, sender,
));
write_half
}
Err(err) => {
log::error!("connect to remote server failed:{:?}", err);
let _ = sender.send(src_addr).await;
log::error!("send handshake to remote failed:{}", err);
return;
}
let (read_half, write_half) = split(remote);
spawn(remote_to_local_with_wait(
read_half, socket, src_addr, sender,
));
write_half
} else {
log::error!("connect to remote server failed");
let _ = sender.send(src_addr).await;
return;
};

let mut header = BytesMut::new();
Expand Down Expand Up @@ -191,7 +194,7 @@ async fn remote_to_local(
Duration::from_secs(OPTIONS.udp_idle_timeout),
remote.read_buf(&mut buffer),
)
.await
.await
{
Ok(Ok(n)) if n > 0 => loop {
match UdpAssociate::parse(buffer.as_ref()) {
Expand Down
11 changes: 5 additions & 6 deletions wintool/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::{
ffi::CStr,
mem::MaybeUninit,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
ptr,
};
Expand Down Expand Up @@ -252,8 +251,8 @@ pub fn get_main_adapter_gwif() -> Option<(String, u32)> {

/// # Safety
pub unsafe fn get_adapters_addresses<F>(mut callback: F) -> bool
where
F: FnMut(&AdapterAddresses) -> bool,
where
F: FnMut(&AdapterAddresses) -> bool,
{
let mut buffer_length: u32 = 0;
let status = iphlpapi::GetAdaptersAddresses(
Expand Down Expand Up @@ -295,13 +294,13 @@ pub unsafe fn get_adapters_addresses<F>(mut callback: F) -> bool

fn get_error_message(err_code: u32) -> String {
const LEN: usize = 256;
let mut buf = MaybeUninit::<[u16; LEN]>::uninit();
let mut buf = [0u16; LEN];

//SAFETY: name is a allocated on the stack above therefore it must be valid, non-null and
//aligned for u16
let first = unsafe { *buf.as_mut_ptr() }.as_mut_ptr();
buf[0] = 0;
let first = buf.as_mut_ptr();
//Write default null terminator in case WintunGetAdapterName leaves name unchanged
unsafe { first.write(0u16) };
let chars_written = unsafe {
winbase::FormatMessageW(
winbase::FORMAT_MESSAGE_FROM_SYSTEM | winbase::FORMAT_MESSAGE_IGNORE_INSERTS,
Expand Down

0 comments on commit 13242d4

Please sign in to comment.