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 = if zero_rtt_handshake {
match conn.into_0rtt() {
Ok((conn, _)) => 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?
}
}
@ -157,7 +158,7 @@ impl Endpoint {
let mut last_err = None;
for addr in self.server.resolve().await? {
match connect_to(
let res = connect_to(
&mut self.ep,
addr,
self.server.server_name(),
@ -166,9 +167,11 @@ impl Endpoint {
self.udp_relay_mode,
self.zero_rtt_handshake,
)
.await
{
.await;
match res {
Ok(conn) => {
log::info!("[connection] established");
tokio::spawn(conn.clone().init(
self.heartbeat,
self.gc_interval,
@ -309,6 +312,7 @@ impl Connection {
}
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 {
Err(err) => Err(Error::from(err)),
Ok(Task::Packet(pkt)) => match self.udp_relay_mode {
@ -321,7 +325,8 @@ impl Connection {
}
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(()),
Err(err) => Err(Error::from(err)),
@ -333,11 +338,12 @@ impl Connection {
match res {
Ok(()) => {}
Err(err) => eprintln!("{err}"),
Err(err) => log::error!("[connection] {err}"),
}
}
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 {
Err(err) => Err(Error::from(err)),
_ => unreachable!(),
@ -345,11 +351,12 @@ impl Connection {
match res {
Ok(()) => {}
Err(err) => eprintln!("{err}"),
Err(err) => log::error!("[connection] {err}"),
}
}
async fn handle_datagram(self, dg: Bytes) {
log::debug!("[connection] incoming datagram");
let res = match self.model.accept_datagram(dg) {
Err(err) => Err(Error::from(err)),
Ok(Task::Packet(pkt)) => match self.udp_relay_mode {
@ -362,7 +369,8 @@ impl Connection {
}
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(()),
Err(err) => Err(Error::from(err)),
@ -374,7 +382,7 @@ impl Connection {
match res {
Ok(()) => {}
Err(err) => eprintln!("{err}"),
Err(err) => log::error!("[connection] {err}"),
}
}
@ -384,8 +392,8 @@ impl Connection {
.authenticate(self.uuid, self.password.clone())
.await
{
Ok(()) => {}
Err(err) => eprintln!("{err}"),
Ok(()) => log::info!("[connection] authentication sent"),
Err(err) => log::warn!("[connection] authentication failed: {err}"),
}
}
@ -402,8 +410,8 @@ impl Connection {
}
match self.model.heartbeat().await {
Ok(()) => {}
Err(err) => eprintln!("{err}"),
Ok(()) => log::info!("[connection] heartbeat"),
Err(err) => log::warn!("[connection] heartbeat error: {err}"),
}
}
}
@ -416,6 +424,7 @@ impl Connection {
break;
}
log::debug!("[connection] packet garbage collection");
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) {
Ok(()) => {}

View File

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