1
0

logging errors in client

This commit is contained in:
EAimTY 2023-02-12 18:17:49 +09:00
parent 81a5819c98
commit 540618769c
3 changed files with 66 additions and 39 deletions

View File

@ -138,12 +138,13 @@ impl Endpoint {
} }
let conn = ep.connect(addr, server_name)?; let conn = ep.connect(addr, server_name)?;
let conn = if zero_rtt_handshake { let conn = if zero_rtt_handshake {
match conn.into_0rtt() { match conn.into_0rtt() {
Ok((conn, _)) => conn, Ok((conn, _)) => conn,
Err(conn) => { Err(conn) => {
eprintln!("0-RTT handshake failed, fallback to 1-RTT handshake"); log::info!(
"[connection] 0-RTT handshake failed, fallback to 1-RTT handshake"
);
conn.await? conn.await?
} }
} }
@ -157,7 +158,7 @@ impl Endpoint {
let mut last_err = None; let mut last_err = None;
for addr in self.server.resolve().await? { for addr in self.server.resolve().await? {
match connect_to( let res = connect_to(
&mut self.ep, &mut self.ep,
addr, addr,
self.server.server_name(), self.server.server_name(),
@ -166,9 +167,11 @@ impl Endpoint {
self.udp_relay_mode, self.udp_relay_mode,
self.zero_rtt_handshake, self.zero_rtt_handshake,
) )
.await .await;
{
match res {
Ok(conn) => { Ok(conn) => {
log::info!("[connection] established");
tokio::spawn(conn.clone().init( tokio::spawn(conn.clone().init(
self.heartbeat, self.heartbeat,
self.gc_interval, self.gc_interval,
@ -309,6 +312,7 @@ impl Connection {
} }
async fn handle_uni_stream(self, recv: RecvStream, _reg: Register) { async fn handle_uni_stream(self, recv: RecvStream, _reg: Register) {
log::debug!("[connection] incoming unidirectional stream");
let res = match self.model.accept_uni_stream(recv).await { let res = match self.model.accept_uni_stream(recv).await {
Err(err) => Err(Error::from(err)), Err(err) => Err(Error::from(err)),
Ok(Task::Packet(pkt)) => match self.udp_relay_mode { Ok(Task::Packet(pkt)) => match self.udp_relay_mode {
@ -321,7 +325,8 @@ impl Connection {
} }
Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr), Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr),
}; };
Socks5Server::recv_pkt(pkt, addr, assoc_id).await Socks5Server::recv_pkt(pkt, addr, assoc_id).await;
Ok(())
} }
Ok(None) => Ok(()), Ok(None) => Ok(()),
Err(err) => Err(Error::from(err)), Err(err) => Err(Error::from(err)),
@ -333,11 +338,12 @@ impl Connection {
match res { match res {
Ok(()) => {} Ok(()) => {}
Err(err) => eprintln!("{err}"), Err(err) => log::error!("[connection] {err}"),
} }
} }
async fn handle_bi_stream(self, send: SendStream, recv: RecvStream, _reg: Register) { async fn handle_bi_stream(self, send: SendStream, recv: RecvStream, _reg: Register) {
log::debug!("[connection] incoming bidirectional stream");
let res = match self.model.accept_bi_stream(send, recv).await { let res = match self.model.accept_bi_stream(send, recv).await {
Err(err) => Err(Error::from(err)), Err(err) => Err(Error::from(err)),
_ => unreachable!(), _ => unreachable!(),
@ -345,11 +351,12 @@ impl Connection {
match res { match res {
Ok(()) => {} Ok(()) => {}
Err(err) => eprintln!("{err}"), Err(err) => log::error!("[connection] {err}"),
} }
} }
async fn handle_datagram(self, dg: Bytes) { async fn handle_datagram(self, dg: Bytes) {
log::debug!("[connection] incoming datagram");
let res = match self.model.accept_datagram(dg) { let res = match self.model.accept_datagram(dg) {
Err(err) => Err(Error::from(err)), Err(err) => Err(Error::from(err)),
Ok(Task::Packet(pkt)) => match self.udp_relay_mode { Ok(Task::Packet(pkt)) => match self.udp_relay_mode {
@ -362,7 +369,8 @@ impl Connection {
} }
Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr), Address::SocketAddress(addr) => Socks5Address::SocketAddress(addr),
}; };
Socks5Server::recv_pkt(pkt, addr, assoc_id).await Socks5Server::recv_pkt(pkt, addr, assoc_id).await;
Ok(())
} }
Ok(None) => Ok(()), Ok(None) => Ok(()),
Err(err) => Err(Error::from(err)), Err(err) => Err(Error::from(err)),
@ -374,7 +382,7 @@ impl Connection {
match res { match res {
Ok(()) => {} Ok(()) => {}
Err(err) => eprintln!("{err}"), Err(err) => log::error!("[connection] {err}"),
} }
} }
@ -384,8 +392,8 @@ impl Connection {
.authenticate(self.uuid, self.password.clone()) .authenticate(self.uuid, self.password.clone())
.await .await
{ {
Ok(()) => {} Ok(()) => log::info!("[connection] authentication sent"),
Err(err) => eprintln!("{err}"), Err(err) => log::warn!("[connection] authentication failed: {err}"),
} }
} }
@ -402,8 +410,8 @@ impl Connection {
} }
match self.model.heartbeat().await { match self.model.heartbeat().await {
Ok(()) => {} Ok(()) => log::info!("[connection] heartbeat"),
Err(err) => eprintln!("{err}"), Err(err) => log::warn!("[connection] heartbeat error: {err}"),
} }
} }
} }
@ -416,6 +424,7 @@ impl Connection {
break; break;
} }
log::debug!("[connection] packet garbage collection");
self.model.collect_garbage(gc_lifetime); self.model.collect_garbage(gc_lifetime);
} }
} }
@ -442,6 +451,6 @@ impl Connection {
}; };
}; };
eprintln!("{err}"); log::error!("[connection] {err}");
} }
} }

View File

@ -29,7 +29,10 @@ async fn main() {
} }
}; };
LoggerBuilder::new().filter_level(cfg.log_level).init(); LoggerBuilder::new()
.filter_level(cfg.log_level)
.format_module_path(false)
.init();
match Endpoint::set_config(cfg.relay) { match Endpoint::set_config(cfg.relay) {
Ok(()) => {} Ok(()) => {}

View File

@ -52,6 +52,7 @@ impl Server {
socket.set_reuse_address(true)?; socket.set_reuse_address(true)?;
socket.bind(&SockAddr::from(cfg.server))?; socket.bind(&SockAddr::from(cfg.server))?;
socket.listen(128)?;
TcpListener::from_std(StdTcpListener::from(socket))? TcpListener::from_std(StdTcpListener::from(socket))?
}; };
@ -82,10 +83,12 @@ impl Server {
pub async fn start() { pub async fn start() {
let server = SERVER.get().unwrap(); let server = SERVER.get().unwrap();
log::warn!("[socks5] server started, listening on {}", server.addr);
loop { loop {
match server.inner.accept().await { match server.inner.accept().await {
Ok((conn, _)) => { Ok((conn, addr)) => {
log::debug!("[socks5] [{addr}] connection established");
tokio::spawn(async move { tokio::spawn(async move {
let res = match conn.handshake().await { let res = match conn.handshake().await {
Ok(Connection::Associate(associate, addr)) => { Ok(Connection::Associate(associate, addr)) => {
@ -99,12 +102,12 @@ impl Server {
}; };
match res { match res {
Ok(_) => {} Ok(()) => log::debug!("[socks5] [{addr}] connection closed"),
Err(err) => eprintln!("{err}"), Err(err) => log::warn!("[socks5] [{addr}] {err}"),
} }
}); });
} }
Err(err) => eprintln!("{err}"), Err(err) => log::warn!("[socks5] failed to establish connection: {err}"),
} }
} }
} }
@ -144,11 +147,12 @@ impl Server {
Self::send_pkt(assoc, assoc_socket).await Self::send_pkt(assoc, assoc_socket).await
} }
Err(err) => { Err(err) => {
log::warn!("[socks5] failed to create associated socket: {err}");
let mut assoc = assoc let mut assoc = assoc
.reply(Reply::GeneralFailure, Address::unspecified()) .reply(Reply::GeneralFailure, Address::unspecified())
.await?; .await?;
let _ = assoc.shutdown().await; let _ = assoc.shutdown().await;
Err(Error::from(err)) Ok(())
} }
} }
} }
@ -175,9 +179,8 @@ impl Server {
match relay { match relay {
Ok(relay) => { Ok(relay) => {
let mut relay = relay.compat(); let mut relay = relay.compat();
let conn = conn.reply(Reply::Succeeded, Address::unspecified()).await;
match conn { match conn.reply(Reply::Succeeded, Address::unspecified()).await {
Ok(mut conn) => match io::copy_bidirectional(&mut conn, &mut relay).await { Ok(mut conn) => match io::copy_bidirectional(&mut conn, &mut relay).await {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(err) => { Err(err) => {
@ -192,12 +195,13 @@ impl Server {
} }
} }
} }
Err(err) => { Err(relay_err) => {
log::error!("[connection] {relay_err}");
let mut conn = conn let mut conn = conn
.reply(Reply::GeneralFailure, Address::unspecified()) .reply(Reply::GeneralFailure, Address::unspecified())
.await?; .await?;
let _ = conn.shutdown().await; let _ = conn.shutdown().await;
Err(err) Ok(())
} }
} }
} }
@ -252,17 +256,24 @@ impl Server {
Address::SocketAddress(addr) => TuicAddress::SocketAddress(addr), Address::SocketAddress(addr) => TuicAddress::SocketAddress(addr),
}; };
TuicConnection::get() let res = match TuicConnection::get().await {
.await? Ok(conn) => conn.packet(pkt, target_addr, assoc_id).await,
.packet(pkt, target_addr, assoc_id) Err(err) => Err(err),
.await };
match res {
Ok(()) => {}
Err(err) => log::error!("[connection] {err}"),
}
Ok(())
} }
let res = tokio::select! { let res = tokio::select! {
res = assoc.wait_until_closed() => res, res = assoc.wait_until_closed() => res,
_ = async { loop { _ = async { loop {
if let Err(err) = accept_pkt(&assoc_socket, &mut connected, assoc_id).await { if let Err(err) = accept_pkt(&assoc_socket, &mut connected, assoc_id).await {
eprintln!("{err}"); log::warn!("[socks5] {err}");
} }
}} => unreachable!(), }} => unreachable!(),
}; };
@ -270,25 +281,29 @@ impl Server {
let _ = assoc.shutdown().await; let _ = assoc.shutdown().await;
SERVER.get().unwrap().udp_sessions.lock().remove(&assoc_id); SERVER.get().unwrap().udp_sessions.lock().remove(&assoc_id);
match TuicConnection::get().await { let dissoc_res = match TuicConnection::get().await {
Ok(conn) => match conn.dissociate(assoc_id).await { Ok(conn) => conn.dissociate(assoc_id).await,
Ok(_) => {} Err(err) => Err(err),
Err(err) => eprintln!("{err}"), };
},
Err(err) => eprintln!("{err}"), match dissoc_res {
Ok(()) => {}
Err(err) => log::error!("[connection] [dissociate] {err}"),
} }
Ok(res?) Ok(res?)
} }
pub async fn recv_pkt(pkt: Bytes, addr: Address, assoc_id: u16) -> Result<(), Error> { pub async fn recv_pkt(pkt: Bytes, addr: Address, assoc_id: u16) {
let assoc_socket = { let assoc_socket = {
let sessions = SERVER.get().unwrap().udp_sessions.lock(); let sessions = SERVER.get().unwrap().udp_sessions.lock();
let Some(assoc_socket) = sessions.get(&assoc_id) else { unreachable!() }; let Some(assoc_socket) = sessions.get(&assoc_id) else { unreachable!() };
assoc_socket.clone() assoc_socket.clone()
}; };
assoc_socket.send(pkt, 0, addr).await?; match assoc_socket.send(pkt, 0, addr).await {
Ok(()) Ok(_) => {}
Err(err) => log::error!("[socks5] [send] {err}"),
}
} }
} }