Skip to content

Commit

Permalink
feat (script): Add post method to Script (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
charleywright authored Jun 3, 2024
1 parent 7cee285 commit e325343
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion frida/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licence: wxWindows Library Licence, Version 3.1
*/

use frida_sys::{FridaScriptOptions, _FridaScript};
use frida_sys::{FridaScriptOptions, _FridaScript, g_bytes_new, g_bytes_unref};
use std::marker::PhantomData;
use std::{
ffi::{c_char, c_void, CStr, CString},
Expand Down Expand Up @@ -104,6 +104,25 @@ impl<'a> Script<'a> {

Ok(())
}

/// Post a JSON-encoded message to the script with optional binary data
///
/// NOTE: `message` must be valid JSON otherwise the script will throw a SyntaxError
pub fn post<S: AsRef<str>>(&self, message: S, data: Option<&[u8]>) -> Result<()> {
let message = CString::new(message.as_ref()).map_err(|_| Error::CStringFailed)?;

unsafe {
let g_data = if let Some(data) = data {
g_bytes_new(data.as_ptr() as _, data.len() as _)
} else {
std::ptr::null_mut()
};
frida_sys::frida_script_post(self.script_ptr as _, message.as_ptr() as _, g_data);
g_bytes_unref(g_data);
}

Ok(())
}
}

impl<'a> Drop for Script<'a> {
Expand Down

0 comments on commit e325343

Please sign in to comment.