From d417149949b4c2f3d5ab57d46b80220bd7cf3bd2 Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 15 May 2023 09:57:13 +0800 Subject: [PATCH] plugin_framework, mid commit Signed-off-by: fufesou --- src/plugin/callback_msg.rs | 52 ++++++++++++++++++++++++++++++++++++++ src/plugin/errno.rs | 2 ++ src/plugin/plugins.rs | 22 +++++++++------- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/plugin/callback_msg.rs b/src/plugin/callback_msg.rs index 6967e6853..1884240ee 100644 --- a/src/plugin/callback_msg.rs +++ b/src/plugin/callback_msg.rs @@ -9,11 +9,14 @@ use std::{ sync::Arc, }; +const MSG_TO_RUSTDESK_TARGET: &str = "rustdesk"; const MSG_TO_PEER_TARGET: &str = "peer"; const MSG_TO_UI_TARGET: &str = "ui"; const MSG_TO_CONFIG_TARGET: &str = "config"; const MSG_TO_EXT_SUPPORT_TARGET: &str = "ext-support"; +const MSG_TO_RUSTDESK_SIGNATURE_VERIFICATION: &str = "signature_verification"; + #[allow(dead_code)] const MSG_TO_UI_FLUTTER_CHANNEL_MAIN: u16 = 0x01 << 0; #[allow(dead_code)] @@ -37,6 +40,18 @@ lazy_static::lazy_static! { }; } +#[derive(Deserialize)] +pub struct MsgToRustDesk { + pub r#type: String, + pub data: Vec, +} + +#[derive(Deserialize)] +pub struct SignatureVerification { + pub version: String, + pub data: Vec, +} + #[derive(Debug, Deserialize)] struct ConfigToUi { channel: u16, @@ -192,6 +207,43 @@ pub(super) extern "C" fn cb_msg( ); super::callback_ext::ext_support_callback(&id, &peer, &msg) } + MSG_TO_RUSTDESK_TARGET => { + let s = early_return_value!( + std::str::from_utf8(unsafe { std::slice::from_raw_parts(content as _, len) }), + ERR_CALLBACK_INVALID_MSG, + "parse msg string" + ); + let msg_to_rustdesk = early_return_value!( + serde_json::from_str::(s), + ERR_CALLBACK_INVALID_MSG, + "parse msg '{}'", + s + ); + match &msg_to_rustdesk.r#type as &str { + MSG_TO_RUSTDESK_SIGNATURE_VERIFICATION => { + // let signature_data = early_return_value!( + // std::str::from_utf8(&msg_to_rustdesk.data), + // ERR_CALLBACK_INVALID_MSG, + // "parse signature data string" + // ); + // let signature_data = early_return_value!( + // serde_json::from_str::(signature_data), + // ERR_CALLBACK_INVALID_MSG, + // "parse signature data '{}'", + // s + // ); + // to-do: Request server to sign the data. + PluginReturn::success() + } + t => PluginReturn::new( + errno::ERR_CALLBACK_TARGET_TYPE, + &format!( + "Unknown target type '{}' for target {}", + t, MSG_TO_RUSTDESK_TARGET + ), + ), + } + } _ => PluginReturn::new( errno::ERR_CALLBACK_TARGET, &format!("Unknown target '{}'", target), diff --git a/src/plugin/errno.rs b/src/plugin/errno.rs index 410df5132..6b1e3612d 100644 --- a/src/plugin/errno.rs +++ b/src/plugin/errno.rs @@ -13,6 +13,8 @@ pub const ERR_PLUGIN_LOAD: i32 = 10001; pub const ERR_PLUGIN_MSG_INIT: i32 = 10101; pub const ERR_PLUGIN_MSG_INIT_INVALID: i32 = 10102; pub const ERR_PLUGIN_MSG_GET_LOCAL_PEER_ID: i32 = 10103; +pub const ERR_PLUGIN_SIGNATURE_NOT_VERIFIED: i32 = 10104; +pub const ERR_PLUGIN_SIGNATURE_VERIFICATION_FAILED: i32 = 10105; // invalid pub const ERR_CALL_UNIMPLEMENTED: i32 = 10201; pub const ERR_CALL_INVALID_METHOD: i32 = 10202; diff --git a/src/plugin/plugins.rs b/src/plugin/plugins.rs index 2207e4757..61287b1b0 100644 --- a/src/plugin/plugins.rs +++ b/src/plugin/plugins.rs @@ -17,6 +17,7 @@ use std::{ sync::{Arc, RwLock}, }; +pub const METHOD_HANDLE_SIGNATURE_VERIFICATION: &[u8; 30] = b"handle_signature_verification\0"; const METHOD_HANDLE_UI: &[u8; 10] = b"handle_ui\0"; const METHOD_HANDLE_PEER: &[u8; 12] = b"handle_peer\0"; pub const METHOD_HANDLE_LISTEN_EVENT: &[u8; 20] = b"handle_listen_event\0"; @@ -93,19 +94,21 @@ type CallbackNative = extern "C" fn( raw: *const c_void, raw_len: usize, ) -> super::native::NativeReturnValue; -/// The main function of the plugin on the client(self) side. +/// The main function of the plugin. /// /// method: The method. "handle_ui" or "handle_peer" /// peer: The peer id. /// args: The arguments. /// len: The length of the arguments. -type PluginFuncClientCall = extern "C" fn( +type PluginFuncCall = extern "C" fn( method: *const c_char, peer: *const c_char, args: *const c_void, len: usize, ) -> PluginReturn; -/// The main function of the plugin on the server(remote) side. +/// The main function of the plugin. +/// This function is called mainly for handling messages from the peer, +/// and then send messages back to the peer. /// /// method: The method. "handle_ui" or "handle_peer" /// peer: The peer id. @@ -114,7 +117,7 @@ type PluginFuncClientCall = extern "C" fn( /// out: The output. /// The plugin allocate memory with `libc::malloc` and return the pointer. /// out_len: The length of the output. -type PluginFuncServerCall = extern "C" fn( +type PluginFuncCallWithOutData = extern "C" fn( method: *const c_char, peer: *const c_char, args: *const c_void, @@ -138,6 +141,7 @@ struct Callbacks { } #[derive(Serialize)] +#[repr(C)] struct InitInfo { is_server: bool, } @@ -247,8 +251,8 @@ make_plugin!( reset: PluginFuncReset, clear: PluginFuncClear, desc: PluginFuncDesc, - client_call: PluginFuncClientCall, - server_call: PluginFuncServerCall + call: PluginFuncCall, + server_with_out_data: PluginFuncCallWithOutData ); #[derive(Serialize)] @@ -400,7 +404,7 @@ fn handle_event(method: &[u8], id: &str, peer: &str, event: &[u8]) -> ResultType peer.push('\0'); match PLUGINS.read().unwrap().get(id) { Some(plugin) => { - let mut ret = (plugin.client_call)( + let mut ret = (plugin.call)( method.as_ptr() as _, peer.as_ptr() as _, event.as_ptr() as _, @@ -455,7 +459,7 @@ fn _handle_listen_event(event: String, peer: String) { for id in plugins { match PLUGINS.read().unwrap().get(&id) { Some(plugin) => { - let mut ret = (plugin.client_call)( + let mut ret = (plugin.call)( METHOD_HANDLE_LISTEN_EVENT.as_ptr() as _, peer.as_ptr() as _, evt_bytes.as_ptr() as _, @@ -493,7 +497,7 @@ pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Message { Some(plugin) => { let mut out = std::ptr::null_mut(); let mut out_len: usize = 0; - let mut ret = (plugin.server_call)( + let mut ret = (plugin.server_with_out_data)( METHOD_HANDLE_PEER.as_ptr() as _, peer.as_ptr() as _, event.as_ptr() as _,