Pretty print plists with custom format

This commit is contained in:
Jackson Coxson
2025-02-05 19:23:51 -07:00
parent 5615059375
commit 50bee08a47
4 changed files with 88 additions and 7 deletions

View File

@@ -28,6 +28,8 @@ use std::io::{self, BufWriter};
use thiserror::Error; use thiserror::Error;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
pub use util::{pretty_print_dictionary, pretty_print_plist};
pub trait ReadWrite: AsyncRead + AsyncWrite + Unpin + Send + Sync + std::fmt::Debug {} pub trait ReadWrite: AsyncRead + AsyncWrite + Unpin + Send + Sync + std::fmt::Debug {}
impl<T: AsyncRead + AsyncWrite + Unpin + Send + Sync + std::fmt::Debug> ReadWrite for T {} impl<T: AsyncRead + AsyncWrite + Unpin + Send + Sync + std::fmt::Debug> ReadWrite for T {}
@@ -69,7 +71,7 @@ impl Idevice {
/// Sends a plist to the socket /// Sends a plist to the socket
async fn send_plist(&mut self, message: plist::Value) -> Result<(), IdeviceError> { async fn send_plist(&mut self, message: plist::Value) -> Result<(), IdeviceError> {
if let Some(socket) = &mut self.socket { if let Some(socket) = &mut self.socket {
debug!("Sending plist: {message:?}"); debug!("Sending plist: {}", pretty_print_plist(&message));
let buf = Vec::new(); let buf = Vec::new();
let mut writer = BufWriter::new(buf); let mut writer = BufWriter::new(buf);
@@ -88,7 +90,7 @@ impl Idevice {
/// Sends raw bytes to the socket /// Sends raw bytes to the socket
async fn send_raw(&mut self, message: &[u8]) -> Result<(), IdeviceError> { async fn send_raw(&mut self, message: &[u8]) -> Result<(), IdeviceError> {
if let Some(socket) = &mut self.socket { if let Some(socket) = &mut self.socket {
let message_parts = message.chunks(2048); let message_parts = message.chunks(1024 * 64);
let part_len = message_parts.len(); let part_len = message_parts.len();
let mut err = 5; let mut err = 5;
@@ -140,11 +142,11 @@ impl Idevice {
let mut buf = vec![0; len as usize]; let mut buf = vec![0; len as usize];
socket.read_exact(&mut buf).await?; socket.read_exact(&mut buf).await?;
let res: plist::Dictionary = plist::from_bytes(&buf)?; let res: plist::Dictionary = plist::from_bytes(&buf)?;
debug!("Received plist: {res:#?}"); debug!("Received plist: {}", pretty_print_dictionary(&res));
if let Some(e) = res.get("Error") { if let Some(e) = res.get("Error") {
let e: String = plist::from_value(e)?; let e: String = plist::from_value(e)?;
if let Some(e) = IdeviceError::from_device_error_type(e.as_str()) { if let Some(e) = IdeviceError::from_device_error_type(e.as_str(), &res) {
return Err(e); return Err(e);
} else { } else {
return Err(IdeviceError::UnknownErrorType(e)); return Err(IdeviceError::UnknownErrorType(e));
@@ -240,22 +242,40 @@ pub enum IdeviceError {
#[error("bad build manifest")] #[error("bad build manifest")]
BadBuildManifest, BadBuildManifest,
#[error("image not mounted")]
ImageNotMounted,
#[cfg(feature = "tss")] #[cfg(feature = "tss")]
#[error("http reqwest error")] #[error("http reqwest error")]
Reqwest(#[from] reqwest::Error), Reqwest(#[from] reqwest::Error),
#[error("internal error")]
InternalError(String),
#[error("unknown error `{0}` returned from device")] #[error("unknown error `{0}` returned from device")]
UnknownErrorType(String), UnknownErrorType(String),
} }
impl IdeviceError { impl IdeviceError {
fn from_device_error_type(e: &str) -> Option<Self> { fn from_device_error_type(e: &str, context: &plist::Dictionary) -> Option<Self> {
match e { match e {
"GetProhibited" => Some(Self::GetProhibited), "GetProhibited" => Some(Self::GetProhibited),
"InvalidHostID" => Some(Self::InvalidHostID), "InvalidHostID" => Some(Self::InvalidHostID),
"SessionInactive" => Some(Self::SessionInactive), "SessionInactive" => Some(Self::SessionInactive),
"DeviceLocked" => Some(Self::DeviceLocked), "DeviceLocked" => Some(Self::DeviceLocked),
"InternalError" => {
let detailed_error = context
.get("DetailedError")
.and_then(|d| d.as_string())
.unwrap_or("No context")
.to_string();
if detailed_error.contains("There is no matching entry in the device map for") {
Some(Self::ImageNotMounted)
} else {
Some(Self::InternalError(detailed_error))
}
}
_ => None, _ => None,
} }
} }

View File

@@ -33,7 +33,10 @@ impl TSSRequest {
} }
pub async fn send(&self) -> Result<plist::Value, IdeviceError> { pub async fn send(&self) -> Result<plist::Value, IdeviceError> {
debug!("Sending TSS request: {:#?}", self.inner); debug!(
"Sending TSS request: {}",
crate::pretty_print_dictionary(&self.inner)
);
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let res = client let res = client

View File

@@ -256,7 +256,7 @@ impl UsbmuxdConnection {
self.socket.read_exact(&mut body_buffer).await?; self.socket.read_exact(&mut body_buffer).await?;
let res = plist::from_bytes(&body_buffer)?; let res = plist::from_bytes(&body_buffer)?;
debug!("Read from muxer: {res:#?}"); debug!("Read from muxer: {}", crate::pretty_print_dictionary(&res));
Ok(res) Ok(res)
} }

View File

@@ -1,5 +1,7 @@
// Jackson Coxson // Jackson Coxson
use plist::Value;
pub fn plist_to_bytes(p: &plist::Dictionary) -> Vec<u8> { pub fn plist_to_bytes(p: &plist::Dictionary) -> Vec<u8> {
let buf = Vec::new(); let buf = Vec::new();
let mut writer = std::io::BufWriter::new(buf); let mut writer = std::io::BufWriter::new(buf);
@@ -7,3 +9,59 @@ pub fn plist_to_bytes(p: &plist::Dictionary) -> Vec<u8> {
writer.into_inner().unwrap() writer.into_inner().unwrap()
} }
pub fn pretty_print_plist(p: &Value) -> String {
print_plist(p, 0)
}
pub fn pretty_print_dictionary(dict: &plist::Dictionary) -> String {
let items: Vec<String> = dict
.iter()
.map(|(k, v)| format!("{}: {}", k, print_plist(v, 2)))
.collect();
format!("{{\n{}\n}}", items.join(",\n"))
}
fn print_plist(p: &Value, indentation: usize) -> String {
let indent = " ".repeat(indentation);
match p {
Value::Array(vec) => {
let items: Vec<String> = vec
.iter()
.map(|v| print_plist(v, indentation + 2))
.collect();
format!("[\n{}\n{}]", items.join(",\n"), indent)
}
Value::Dictionary(dict) => {
let items: Vec<String> = dict
.iter()
.map(|(k, v)| {
format!(
"{}{}: {}",
" ".repeat(indentation + 2),
k,
print_plist(v, indentation + 2)
)
})
.collect();
format!("{}{{\n{}\n{}}}", indent, items.join(",\n"), indent)
}
Value::Boolean(b) => format!("{}", b),
Value::Data(vec) => {
let len = vec.len();
let preview: String = vec
.iter()
.take(20)
.map(|b| format!("{:02X}", b))
.collect::<Vec<String>>()
.join(" ");
format!("Data({}... Len: {})", preview, len)
}
Value::Date(date) => format!("Date({})", date.to_xml_format()),
Value::Real(f) => format!("{}", f),
Value::Integer(i) => format!("{}", i),
Value::String(s) => format!("\"{}\"", s),
Value::Uid(_uid) => "Uid(?)".to_string(),
_ => "Unknown".to_string(),
}
}