From 6933f333cc119cfd03cdd418e4144d27e53d2fb7 Mon Sep 17 00:00:00 2001 From: parazyd Date: Sun, 19 Nov 2023 16:13:44 +0100 Subject: [PATCH] rpc/server: Implement stop-gap hack for handling some XMRig requests. --- Makefile | 1 + src/rpc/server.rs | 53 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 8452e84a3..595518a3f 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,7 @@ BINS = \ darkfid2 \ faucetd \ darkirc \ + darkfi-mmproxy \ genev/genev-cli \ genev/genevd \ lilith \ diff --git a/src/rpc/server.rs b/src/rpc/server.rs index ddf340ac3..57144fba6 100644 --- a/src/rpc/server.rs +++ b/src/rpc/server.rs @@ -105,8 +105,46 @@ pub async fn accept( let _ = read_from_stream(&mut reader_lock, &mut buf, false).await?; drop(reader_lock); - let val: JsonValue = String::from_utf8(buf)?.trim().parse()?; - let req = JsonRequest::try_from(&val)?; + let read_string = match String::from_utf8(buf) { + Ok(v) => v, + Err(e) => { + error!( + target: "rpc::server::accept()", + "[RPC SERVER] Failed parsing string from read buffer: {}", e, + ); + return Err(e.into()) + } + }; + + // Implementation note: + // When using this JSON-RPC server with XMRig, an issue arises. + // XMRig tends to send something we do not parse as a line in + // read_from_stream(), so as a stop-gap hack we do this: + let line = read_string.trim().lines().take(1).next().unwrap(); + + // Parse the line as JSON + let val: JsonValue = match line.parse() { + Ok(v) => v, + Err(e) => { + error!( + target: "rpc::server::accept()", + "[RPC SERVER] Failed parsing JSON string: {}", e, + ); + return Err(e.into()) + } + }; + + // Cast to JsonRequest + let req = match JsonRequest::try_from(&val) { + Ok(v) => v, + Err(e) => { + error!( + target: "rpc::server::accept()", + "[RPC SERVER] Failed casting JSON to a JsonRequest: {}", e, + ); + return Err(e.into()) + } + }; debug!(target: "rpc::server", "{} --> {}", addr, val.stringify()?); @@ -115,8 +153,6 @@ pub async fn accept( match rep { JsonResult::Subscriber(subscriber) => { let task = StoppableTask::new(); - debug!(target: "rpc::server", "Adding background task {} to map", task.task_id); - tasks.lock().await.insert(task.clone()); // Clone what needs to go in the background let task_ = task.clone(); @@ -155,6 +191,9 @@ pub async fn accept( Error::DetachedTaskStopped, ex.clone(), ); + + debug!(target: "rpc::server", "Adding background task {} to map", task.task_id); + tasks.lock().await.insert(task.clone()); } JsonResult::SubscriberWithReply(subscriber, reply) => { @@ -165,9 +204,6 @@ pub async fn accept( drop(writer_lock); let task = StoppableTask::new(); - debug!(target: "rpc::server", "Adding background task {} to map", task.task_id); - tasks.lock().await.insert(task.clone()); - // Clone what needs to go in the background let task_ = task.clone(); let addr_ = addr.clone(); @@ -206,6 +242,9 @@ pub async fn accept( Error::DetachedTaskStopped, ex.clone(), ); + + debug!(target: "rpc::server", "Adding background task {} to map", task.task_id); + tasks.lock().await.insert(task.clone()); } JsonResult::Request(_) | JsonResult::Notification(_) => {