Просмотр исходного кода

rpc/server: don't perform seppuku if a malformed request arrives

skoupidi 8 месяцев назад
Родитель
Сommit
d45570f11f
2 измененных файлов с 53 добавлено и 40 удалено
  1. 5 24
      src/net/acceptor.rs
  2. 48 16
      src/rpc/server.rs

+ 5 - 24
src/net/acceptor.rs

@@ -25,7 +25,7 @@ use std::{
 };
 
 use smol::Executor;
-use tracing::{error, warn};
+use tracing::warn;
 use url::Url;
 
 use super::{
@@ -208,18 +208,6 @@ impl Acceptor {
                             "[P2P] Unhandled OS Error: {e} {x}"
                         );
                         continue
-
-                        /*
-                        error!(
-                            target: "net::acceptor::run_accept_loop()",
-                            "[P2P] Acceptor failed listening: {e} ({x})"
-                        );
-                        error!(
-                            target: "net::acceptor::run_accept_loop()",
-                            "[P2P] Closing listener loop"
-                        );
-                        return Err(e.into())
-                        */
                     }
                 },
 
@@ -230,7 +218,7 @@ impl Acceptor {
                 Err(e) if e.kind() == ErrorKind::Other => {
                     if let Some(inner) = std::error::Error::source(&e) {
                         if let Some(inner) = inner.downcast_ref::<futures_rustls::rustls::Error>() {
-                            error!(
+                            warn!(
                                 target: "net::acceptor::run_accept_loop()",
                                 "[P2P] rustls listener error: {inner:?}"
                             );
@@ -238,26 +226,19 @@ impl Acceptor {
                         }
                     }
 
-                    error!(
+                    warn!(
                         target: "net::acceptor::run_accept_loop()",
                         "[P2P] Unhandled ErrorKind::Other error: {e:?}"
                     );
-                    return Err(e.into())
+                    continue
                 }
 
                 // Errors we didn't handle above:
                 Err(e) => {
-                    error!(
+                    warn!(
                         target: "net::acceptor::run_accept_loop()",
                         "[P2P] Unhandled listener.next() error: {e}"
                     );
-                    /*
-                    error!(
-                        target: "net::acceptor::run_accept_loop()",
-                        "[P2P] Closing listener loop"
-                    );
-                    return Err(e.into())
-                    */
                     continue
                 }
             }

+ 48 - 16
src/rpc/server.rs

@@ -24,7 +24,7 @@ use smol::{
     lock::{Mutex, MutexGuard},
 };
 use tinyjson::JsonValue;
-use tracing::{debug, error, info};
+use tracing::{debug, info, warn};
 use url::Url;
 
 use super::{
@@ -287,7 +287,7 @@ pub async fn accept<'a, T: 'a>(
         let line = match String::from_utf8(buf) {
             Ok(v) => v,
             Err(e) => {
-                error!(
+                warn!(
                     target: "rpc::server::accept()",
                     "[RPC SERVER] Failed parsing string from read buffer: {e}"
                 );
@@ -299,7 +299,7 @@ pub async fn accept<'a, T: 'a>(
         let val: JsonValue = match line.trim().parse() {
             Ok(v) => v,
             Err(e) => {
-                error!(
+                warn!(
                     target: "rpc::server::accept()",
                     "[RPC SERVER] Failed parsing JSON string: {e}"
                 );
@@ -311,7 +311,7 @@ pub async fn accept<'a, T: 'a>(
         let req = match JsonRequest::try_from(&val) {
             Ok(v) => v,
             Err(e) => {
-                error!(
+                warn!(
                     target: "rpc::server::accept()",
                     "[RPC SERVER] Failed casting JSON to a JsonRequest: {e}"
                 );
@@ -401,33 +401,65 @@ async fn run_accept_loop<'a, T: 'a>(
             // As per accept(2) recommendation:
             Err(e) if e.raw_os_error().is_some() => match e.raw_os_error().unwrap() {
                 libc::EAGAIN | libc::ECONNABORTED | libc::EPROTO | libc::EINTR => continue,
-                _ => {
-                    error!(
+                libc::ECONNRESET => {
+                    warn!(
                         target: "rpc::server::run_accept_loop()",
-                        "[RPC] Server failed listening: {e}"
+                        "[RPC] Connection reset by peer in accept_loop"
                     );
-                    error!(
+                    continue
+                }
+                libc::ETIMEDOUT => {
+                    warn!(
                         target: "rpc::server::run_accept_loop()",
-                        "[RPC] Closing accept loop"
+                        "[RPC] Connection timed out in accept_loop"
                     );
-                    return Err(e.into())
+                    continue
+                }
+                libc::EPIPE => {
+                    warn!(
+                        target: "rpc::server::run_accept_loop()",
+                        "[RPC] Broken pipe in accept_loop"
+                    );
+                    continue
+                }
+                x => {
+                    warn!(
+                        target: "rpc::server::run_accept_loop()",
+                        "[RPC] Unhandled OS Error: {e} {x}"
+                    );
+                    continue
                 }
             },
 
             // In case a TLS handshake fails, we'll get this:
             Err(e) if e.kind() == ErrorKind::UnexpectedEof => continue,
 
+            // Handle ErrorKind::Other
+            Err(e) if e.kind() == ErrorKind::Other => {
+                if let Some(inner) = std::error::Error::source(&e) {
+                    if let Some(inner) = inner.downcast_ref::<futures_rustls::rustls::Error>() {
+                        warn!(
+                            target: "rpc::server::run_accept_loop()",
+                            "[RPC] rustls listener error: {inner:?}"
+                        );
+                        continue
+                    }
+                }
+
+                warn!(
+                    target: "rpc::server::run_accept_loop()",
+                    "[RPC] Unhandled ErrorKind::Other error: {e:?}"
+                );
+                continue
+            }
+
             // Errors we didn't handle above:
             Err(e) => {
-                error!(
+                warn!(
                     target: "rpc::server::run_accept_loop()",
                     "[RPC] Unhandled listener.next() error: {e}"
                 );
-                error!(
-                    target: "rpc::server::run_accept_loop()",
-                    "[RPC] Closing acceptloop"
-                );
-                return Err(e.into())
+                continue
             }
         }
     }