Merge pull request #4151 from fufesou/feat/plugin_framework

plugin_framework, flutter event handlers
This commit is contained in:
RustDesk
2023-04-21 22:09:28 +08:00
committed by GitHub
15 changed files with 301 additions and 192 deletions

View File

@@ -1301,7 +1301,7 @@ impl<T: InvokeUiSession> Remote<T> {
#[cfg(all(feature = "flutter", feature = "plugin_framework"))]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
Some(misc::Union::PluginRequest(p)) => {
allow_err!(crate::plugin::handle_server_event(&p.id, &p.content));
allow_err!(crate::plugin::handle_server_event(&p.id, &self.handler.id, &p.content));
// to-do: show message box on UI when error occurs?
}
#[cfg(all(feature = "flutter", feature = "plugin_framework"))]

View File

@@ -1397,16 +1397,20 @@ pub fn send_url_scheme(_url: String) {
}
#[inline]
pub fn plugin_event(_id: String, _event: Vec<u8>) {
pub fn plugin_event(_id: String, _peer: String, _event: Vec<u8>) {
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
allow_err!(crate::plugin::handle_ui_event(&_id, &_event));
allow_err!(crate::plugin::handle_ui_event(&_id, &_peer, &_event));
}
}
#[inline]
pub fn plugin_get_session_option(_id: String, _peer: String, _key: String) -> SyncReturn<Option<String>> {
pub fn plugin_get_session_option(
_id: String,
_peer: String,
_key: String,
) -> SyncReturn<Option<String>> {
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
@@ -1427,7 +1431,7 @@ pub fn plugin_set_session_option(_id: String, _peer: String, _key: String, _valu
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
crate::plugin::PeerConfig::set(&_id, &_peer, &_key, &_value);
let _res = crate::plugin::PeerConfig::set(&_id, &_peer, &_key, &_value);
}
}
@@ -1453,7 +1457,7 @@ pub fn plugin_set_local_option(_id: String, _key: String, _value: String) {
#[cfg(feature = "plugin_framework")]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
crate::plugin::LocalConfig::set(&_id, &_key, &_value);
let _res = crate::plugin::LocalConfig::set(&_id, &_key, &_value);
}
}

View File

@@ -1,6 +1,7 @@
use super::cstr_to_string;
use super::*;
use crate::flutter::{self, APP_TYPE_CM, APP_TYPE_MAIN, SESSIONS};
use hbb_common::{lazy_static, log, message_proto::PluginRequest};
use serde_derive::Deserialize;
use serde_json;
use std::{
collections::HashMap,
@@ -10,6 +11,7 @@ use std::{
const MSG_TO_PEER_TARGET: &str = "peer";
const MSG_TO_UI_TARGET: &str = "ui";
const MSG_TO_CONFIG_TARGET: &str = "config";
#[allow(dead_code)]
const MSG_TO_UI_FLUTTER_CHANNEL_MAIN: u16 = 0x01 << 0;
@@ -34,6 +36,22 @@ lazy_static::lazy_static! {
};
}
#[derive(Deserialize)]
struct ConfigToUi {
channel: u16,
location: String,
}
#[derive(Deserialize)]
struct MsgToConfig {
id: String,
r#type: String,
key: String,
value: String,
#[serde(skip_serializing_if = "Option::is_none")]
ui: Option<ConfigToUi>, // If not None, send msg to ui.
}
/// Callback to send message to peer or ui.
/// peer, target, id are utf8 strings(null terminated).
///
@@ -83,25 +101,42 @@ pub fn callback_msg(
let channel = u16::from_be_bytes([content_slice[0], content_slice[1]]);
let content = std::string::String::from_utf8(content_slice[2..].to_vec())
.unwrap_or("".to_string());
let mut m = HashMap::new();
m.insert("name", "plugin_event");
m.insert("peer", &peer);
m.insert("content", &content);
let event = serde_json::to_string(&m).unwrap_or("".to_string());
for (k, v) in MSG_TO_UI_FLUTTER_CHANNELS.iter() {
if channel & k != 0 {
let _res = flutter::push_global_event(v as _, event.clone());
}
}
if channel & MSG_TO_UI_FLUTTER_CHANNEL_REMOTE != 0
|| channel & MSG_TO_UI_FLUTTER_CHANNEL_TRANSFER != 0
|| channel & MSG_TO_UI_FLUTTER_CHANNEL_FORWARD != 0
push_event_to_ui(channel, &peer, &content);
}
MSG_TO_CONFIG_TARGET => {
if let Ok(s) =
std::str::from_utf8(unsafe { std::slice::from_raw_parts(content as _, len) })
{
let _res = flutter::push_session_event(
&peer,
"plugin_event",
vec![("peer", &peer), ("content", &content)],
);
if let Ok(msg) = serde_json::from_str::<MsgToConfig>(s) {
match &msg.r#type as _ {
config::CONFIG_TYPE_LOCAL => {
match config::LocalConfig::set(&msg.id, &msg.key, &msg.value) {
Ok(_) => {
if let Some(ui) = &msg.ui {
// No need to set the peer id for location config.
push_option_to_ui(ui.channel, "", &msg, ui);
}
}
Err(e) => {
log::error!("Failed to set local config, {}", e);
}
}
}
config::CONFIG_TYPE_PEER => {
match config::PeerConfig::set(&msg.id, &peer, &msg.key, &msg.value) {
Ok(_) => {
if let Some(ui) = &msg.ui {
push_option_to_ui(ui.channel, &peer, &msg, ui);
}
}
Err(e) => {
log::error!("Failed to set peer config, {}", e);
}
}
}
_ => {}
}
}
}
}
_ => {
@@ -109,3 +144,51 @@ pub fn callback_msg(
}
}
}
#[inline]
fn is_peer_channel(channel: u16) -> bool {
channel & MSG_TO_UI_FLUTTER_CHANNEL_REMOTE != 0
|| channel & MSG_TO_UI_FLUTTER_CHANNEL_TRANSFER != 0
|| channel & MSG_TO_UI_FLUTTER_CHANNEL_FORWARD != 0
}
fn push_event_to_ui(channel: u16, peer: &str, content: &str) {
let mut m = HashMap::new();
m.insert("name", MSG_TO_UI_TYPE_PLUGIN_EVENT);
m.insert("peer", &peer);
m.insert("content", &content);
let event = serde_json::to_string(&m).unwrap_or("".to_string());
for (k, v) in MSG_TO_UI_FLUTTER_CHANNELS.iter() {
if channel & k != 0 {
let _res = flutter::push_global_event(v as _, event.to_string());
}
}
if is_peer_channel(channel) {
let _res = flutter::push_session_event(
&peer,
MSG_TO_UI_TYPE_PLUGIN_EVENT,
vec![("peer", &peer), ("content", &content)],
);
}
}
fn push_option_to_ui(channel: u16, peer: &str, msg: &MsgToConfig, ui: &ConfigToUi) {
let v = [
("name", MSG_TO_UI_TYPE_PLUGIN_OPTION),
("id", &msg.id),
("location", &ui.location),
("key", &msg.key),
("value", &msg.value),
];
let event = serde_json::to_string(&HashMap::from(v)).unwrap_or("".to_string());
for (k, v) in MSG_TO_UI_FLUTTER_CHANNELS.iter() {
if channel & k != 0 {
let _res = flutter::push_global_event(v as _, event.to_string());
}
}
let mut v = v.to_vec();
v.push(("peer", &peer));
if is_peer_channel(channel) {
let _res = flutter::push_session_event(&peer, MSG_TO_UI_TYPE_PLUGIN_OPTION, v.to_vec());
}
}

View File

@@ -14,6 +14,9 @@ lazy_static::lazy_static! {
static ref CONFIG_PEER_ITEMS: Arc<Mutex<HashMap<String, Vec<ConfigItem>>>> = Default::default();
}
pub(super) const CONFIG_TYPE_LOCAL: &str = "local";
pub(super) const CONFIG_TYPE_PEER: &str = "peer";
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct LocalConfig(HashMap<String, String>);
#[derive(Debug, Default, Serialize, Deserialize)]
@@ -72,14 +75,6 @@ impl LocalConfig {
CONFIG_LOCAL.lock().unwrap().insert(id.to_owned(), conf);
}
#[inline]
fn save(id: &str) -> ResultType<()> {
match CONFIG_LOCAL.lock().unwrap().get(id) {
Some(config) => hbb_common::config::store_path(Self::path(id), config),
None => bail!("No such plugin {}", id),
}
}
#[inline]
pub fn get(id: &str, key: &str) -> Option<String> {
if let Some(conf) = CONFIG_LOCAL.lock().unwrap().get(id) {
@@ -136,17 +131,6 @@ impl PeerConfig {
}
}
#[inline]
fn save(id: &str, peer: &str) -> ResultType<()> {
match CONFIG_PEERS.lock().unwrap().get(id) {
Some(peers) => match peers.get(peer) {
Some(config) => hbb_common::config::store_path(Self::path(id, peer), config),
None => bail!("No such peer {}", peer),
},
None => bail!("No such plugin {}", id),
}
}
#[inline]
pub fn get(id: &str, peer: &str, key: &str) -> Option<String> {
if let Some(peers) = CONFIG_PEERS.lock().unwrap().get(id) {

View File

@@ -12,6 +12,11 @@ pub use plugins::{
reload_plugin, unload_plugin,
};
const MSG_TO_UI_TYPE_PLUGIN_DESC: &str = "plugin_desc";
const MSG_TO_UI_TYPE_PLUGIN_EVENT: &str = "plugin_event";
const MSG_TO_UI_TYPE_PLUGIN_RELOAD: &str = "plugin_reload";
const MSG_TO_UI_TYPE_PLUGIN_OPTION: &str = "plugin_option";
pub use config::{LocalConfig, PeerConfig};
#[inline]

View File

@@ -5,8 +5,8 @@ use std::{
sync::{Arc, RwLock},
};
use super::{callback_msg, desc::Desc, errno::*, get_code_msg_from_ret};
use crate::flutter;
use super::{desc::Desc, errno::*, *};
use crate::{flutter, ui_interface::get_id};
use hbb_common::{
bail,
dlopen::symbor::Library,
@@ -20,6 +20,7 @@ const METHOD_HANDLE_PEER: &[u8; 12] = b"handle_peer\0";
lazy_static::lazy_static! {
pub static ref PLUGINS: Arc<RwLock<HashMap<String, Plugin>>> = Default::default();
pub static ref LOCAL_PEER_ID: Arc<RwLock<String>> = Default::default();
}
/// Initialize the plugins.
@@ -59,22 +60,32 @@ type PluginFuncCallbackMsg = fn(
len: usize,
);
pub type PluginFuncSetCallbackMsg = fn(PluginFuncCallbackMsg);
/// Callback to get the id of local peer id.
/// The returned string is utf8 string(null terminated).
/// Don't free the returned ptr.
type GetIdFuncCallback = fn() -> *const c_char;
pub type PluginFuncGetIdCallback = fn(GetIdFuncCallback);
/// The main function of the plugin.
/// method: The method. "handle_ui" or "handle_peer"
/// peer: The peer id.
/// args: The arguments.
///
/// Return null ptr if success.
/// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string.
/// The plugin allocate memory with `libc::malloc` and return the pointer.
pub type PluginFuncCall =
fn(method: *const c_char, args: *const c_void, len: usize) -> *const c_void;
pub type PluginFuncCall = fn(
method: *const c_char,
peer: *const c_char,
args: *const c_void,
len: usize,
) -> *const c_void;
macro_rules! make_plugin {
($($field:ident : $tp:ty),+) => {
pub struct Plugin {
_lib: Library,
path: String,
desc: Option<Desc>,
desc_v: Option<Desc>,
$($field: $tp),+
}
@@ -101,7 +112,7 @@ macro_rules! make_plugin {
Ok(Self {
_lib: lib,
path: path.to_string(),
desc: None,
desc_v: None,
$( $field ),+
})
}
@@ -110,12 +121,13 @@ macro_rules! make_plugin {
}
make_plugin!(
fn_init: PluginFuncInit,
fn_reset: PluginFuncReset,
fn_clear: PluginFuncClear,
fn_desc: PluginFuncDesc,
fn_set_cb_msg: PluginFuncSetCallbackMsg,
fn_call: PluginFuncCall
init: PluginFuncInit,
reset: PluginFuncReset,
clear: PluginFuncClear,
desc: PluginFuncDesc,
call: PluginFuncCall,
set_cb_msg: PluginFuncSetCallbackMsg,
set_cb_get_id: PluginFuncGetIdCallback
);
pub fn load_plugins<P: AsRef<Path>>(dir: P) -> ResultType<()> {
@@ -142,7 +154,7 @@ pub fn load_plugins<P: AsRef<Path>>(dir: P) -> ResultType<()> {
pub fn unload_plugin(id: &str) {
if let Some(plugin) = PLUGINS.write().unwrap().remove(id) {
let _ret = (plugin.fn_clear)();
let _ret = (plugin.clear)();
}
}
@@ -155,9 +167,24 @@ pub fn reload_plugin(id: &str) -> ResultType<()> {
load_plugin(&path)
}
#[no_mangle]
fn get_local_peer_id() -> *const c_char {
let mut id = (*LOCAL_PEER_ID.read().unwrap()).clone();
if id.is_empty() {
let mut lock = LOCAL_PEER_ID.write().unwrap();
id = (*lock).clone();
if id.is_empty() {
id = get_id();
id.push('\0');
*lock = id.clone();
}
}
id.as_ptr() as _
}
pub fn load_plugin(path: &str) -> ResultType<()> {
let mut plugin = Plugin::new(path)?;
let desc = (plugin.fn_desc)();
let desc = (plugin.desc)();
let desc_res = Desc::from_cstr(desc);
unsafe {
libc::free(desc as _);
@@ -166,19 +193,27 @@ pub fn load_plugin(path: &str) -> ResultType<()> {
let id = desc.id().to_string();
// to-do validate plugin
// to-do check the plugin id (make sure it does not use another plugin's id)
(plugin.fn_set_cb_msg)(callback_msg::callback_msg);
(plugin.set_cb_msg)(callback_msg::callback_msg);
(plugin.set_cb_get_id)(get_local_peer_id as _);
update_ui_plugin_desc(&desc);
update_config(&desc);
reload_ui(&desc);
plugin.desc = Some(desc);
plugin.desc_v = Some(desc);
PLUGINS.write().unwrap().insert(id, plugin);
Ok(())
}
fn handle_event(method: &[u8], id: &str, event: &[u8]) -> ResultType<()> {
fn handle_event(method: &[u8], id: &str, peer: &str, event: &[u8]) -> ResultType<()> {
let mut peer: String = peer.to_owned();
peer.push('\0');
match PLUGINS.read().unwrap().get(id) {
Some(plugin) => {
let ret = (plugin.fn_call)(method.as_ptr() as _, event.as_ptr() as _, event.len());
let ret = (plugin.call)(
method.as_ptr() as _,
peer.as_ptr() as _,
event.as_ptr() as _,
event.len(),
);
if ret.is_null() {
Ok(())
} else {
@@ -200,21 +235,24 @@ fn handle_event(method: &[u8], id: &str, event: &[u8]) -> ResultType<()> {
}
#[inline]
pub fn handle_ui_event(id: &str, event: &[u8]) -> ResultType<()> {
handle_event(METHOD_HANDLE_UI, id, event)
pub fn handle_ui_event(id: &str, peer: &str, event: &[u8]) -> ResultType<()> {
handle_event(METHOD_HANDLE_UI, id, peer, event)
}
#[inline]
pub fn handle_server_event(id: &str, event: &[u8]) -> ResultType<()> {
handle_event(METHOD_HANDLE_PEER, id, event)
pub fn handle_server_event(id: &str, peer: &str, event: &[u8]) -> ResultType<()> {
handle_event(METHOD_HANDLE_PEER, id, peer, event)
}
#[inline]
pub fn handle_client_event(id: &str, event: &[u8]) -> Option<Message> {
pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Option<Message> {
let mut peer: String = peer.to_owned();
peer.push('\0');
match PLUGINS.read().unwrap().get(id) {
Some(plugin) => {
let ret = (plugin.fn_call)(
let ret = (plugin.call)(
METHOD_HANDLE_PEER.as_ptr() as _,
peer.as_ptr() as _,
event.as_ptr() as _,
event.len(),
);
@@ -226,7 +264,7 @@ pub fn handle_client_event(id: &str, event: &[u8]) -> Option<Message> {
libc::free(ret as _);
}
if code > ERR_RUSTDESK_HANDLE_BASE && code < ERR_PLUGIN_HANDLE_BASE {
let name = plugin.desc.as_ref().unwrap().name();
let name = plugin.desc_v.as_ref().unwrap().name();
match code {
ERR_CALL_NOT_SUPPORTED_METHOD => Some(make_plugin_response(
id,
@@ -288,7 +326,7 @@ fn reload_ui(desc: &Desc) {
if available_channels.contains(&v[1]) {
if let Ok(ui) = serde_json::to_string(&ui) {
let mut m = HashMap::new();
m.insert("name", "plugin_reload");
m.insert("name", MSG_TO_UI_TYPE_PLUGIN_RELOAD);
m.insert("id", desc.id());
m.insert("location", &location);
m.insert("ui", &ui);
@@ -303,7 +341,7 @@ fn update_ui_plugin_desc(desc: &Desc) {
// This function is rarely used. There's no need to care about serialization efficiency here.
if let Ok(desc_str) = serde_json::to_string(desc) {
let mut m = HashMap::new();
m.insert("name", "plugin_desc");
m.insert("name", MSG_TO_UI_TYPE_PLUGIN_DESC);
m.insert("desc", &desc_str);
flutter::push_global_event(flutter::APP_TYPE_MAIN, serde_json::to_string(&m).unwrap());
flutter::push_global_event(

View File

@@ -1813,7 +1813,9 @@ impl Connection {
#[cfg(all(feature = "flutter", feature = "plugin_framework"))]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
Some(misc::Union::PluginRequest(p)) => {
if let Some(msg) = crate::plugin::handle_client_event(&p.id, &p.content) {
if let Some(msg) =
crate::plugin::handle_client_event(&p.id, &self.lr.my_id, &p.content)
{
self.send(msg).await;
}
}