mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
112
src/plugin/callback_msg.rs
Normal file
112
src/plugin/callback_msg.rs
Normal file
@@ -0,0 +1,112 @@
|
||||
use super::cstr_to_string;
|
||||
use crate::flutter::{self, APP_TYPE_CM, APP_TYPE_MAIN, SESSIONS};
|
||||
use hbb_common::{lazy_static, libc, log, message_proto::Plugin, ResultType};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
ffi::{c_char, CStr},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
const MSG_TO_PEER_TARGET: &str = "peer";
|
||||
const MSG_TO_UI_TARGET: &str = "ui";
|
||||
|
||||
#[allow(dead_code)]
|
||||
const MSG_TO_UI_FLUTTER_CHANNEL_MAIN: u16 = 0x01 << 0;
|
||||
#[allow(dead_code)]
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
const MSG_TO_UI_FLUTTER_CHANNEL_CM: u16 = 0x01 << 1;
|
||||
#[cfg(any(target_os = "android", target_os = "ios"))]
|
||||
const MSG_TO_UI_FLUTTER_CHANNEL_CM: u16 = 0x01;
|
||||
const MSG_TO_UI_FLUTTER_CHANNEL_REMOTE: u16 = 0x01 << 2;
|
||||
#[allow(dead_code)]
|
||||
const MSG_TO_UI_FLUTTER_CHANNEL_TRANSFER: u16 = 0x01 << 3;
|
||||
#[allow(dead_code)]
|
||||
const MSG_TO_UI_FLUTTER_CHANNEL_FORWARD: u16 = 0x01 << 4;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref MSG_TO_UI_FLUTTER_CHANNELS: Arc<HashMap<u16, String>> = {
|
||||
let channels = HashMap::from([
|
||||
(MSG_TO_UI_FLUTTER_CHANNEL_MAIN, APP_TYPE_MAIN.to_string()),
|
||||
(MSG_TO_UI_FLUTTER_CHANNEL_CM, APP_TYPE_CM.to_string()),
|
||||
]);
|
||||
Arc::new(channels)
|
||||
};
|
||||
}
|
||||
|
||||
/// Callback to send message to peer or ui.
|
||||
/// peer, target, id are utf8 strings(null terminated).
|
||||
///
|
||||
/// peer: The peer id.
|
||||
/// target: "peer" or "ui".
|
||||
/// id: The id of this plugin.
|
||||
/// content: The content.
|
||||
/// len: The length of the content.
|
||||
pub fn callback_msg(
|
||||
peer: *const c_char,
|
||||
target: *const c_char,
|
||||
id: *const c_char,
|
||||
content: *const c_char,
|
||||
len: usize,
|
||||
) {
|
||||
macro_rules! callback_msg_field {
|
||||
($field: ident) => {
|
||||
let $field = match cstr_to_string($field) {
|
||||
Err(e) => {
|
||||
log::error!("Failed to convert {} to string, {}", stringify!($field), e);
|
||||
return;
|
||||
}
|
||||
Ok(v) => v,
|
||||
};
|
||||
};
|
||||
}
|
||||
callback_msg_field!(peer);
|
||||
callback_msg_field!(target);
|
||||
callback_msg_field!(id);
|
||||
|
||||
match &target as _ {
|
||||
MSG_TO_PEER_TARGET => {
|
||||
if let Some(session) = SESSIONS.write().unwrap().get_mut(&peer) {
|
||||
let content_slice =
|
||||
unsafe { std::slice::from_raw_parts(content as *const u8, len) };
|
||||
let content_vec = Vec::from(content_slice);
|
||||
let plugin = Plugin {
|
||||
id,
|
||||
content: bytes::Bytes::from(content_vec),
|
||||
..Default::default()
|
||||
};
|
||||
session.send_plugin(plugin);
|
||||
}
|
||||
}
|
||||
MSG_TO_UI_TARGET => {
|
||||
let content_slice = unsafe { std::slice::from_raw_parts(content as *const u8, len) };
|
||||
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");
|
||||
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
|
||||
{
|
||||
let _res = flutter::push_session_event(
|
||||
APP_TYPE_CM,
|
||||
"plugin",
|
||||
vec![("peer", &peer), ("content", &content)],
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
log::error!("Unknown target {}", target);
|
||||
}
|
||||
}
|
||||
}
|
||||
0
src/plugin/config.rs
Normal file
0
src/plugin/config.rs
Normal file
126
src/plugin/desc.rs
Normal file
126
src/plugin/desc.rs
Normal file
@@ -0,0 +1,126 @@
|
||||
use hbb_common::ResultType;
|
||||
use serde_derive::Deserialize;
|
||||
use serde_json;
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::{c_char, CStr};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct UiButton {
|
||||
key: String,
|
||||
text: String,
|
||||
icon: String,
|
||||
tooltip: String,
|
||||
action: String, // The action to be triggered when the button is clicked.
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct UiCheckbox {
|
||||
key: String,
|
||||
text: String,
|
||||
tooltip: String,
|
||||
action: String, // The action to be triggered when the checkbox is checked or unchecked.
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(tag = "t", content = "c")]
|
||||
pub enum UiType {
|
||||
Button(UiButton),
|
||||
Checkbox(UiCheckbox),
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Location {
|
||||
core: String,
|
||||
ui: HashMap<String, UiType>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ConfigItem {
|
||||
key: String,
|
||||
value: String,
|
||||
default: String,
|
||||
description: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Configs {
|
||||
pub local: Vec<ConfigItem>,
|
||||
pub session: Vec<ConfigItem>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
pub host: Configs,
|
||||
pub client: Configs,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Desc {
|
||||
id: String,
|
||||
name: String,
|
||||
version: String,
|
||||
description: String,
|
||||
author: String,
|
||||
home: String,
|
||||
license: String,
|
||||
published: String,
|
||||
released: String,
|
||||
github: String,
|
||||
location: Location,
|
||||
config: Config,
|
||||
}
|
||||
|
||||
impl Desc {
|
||||
pub fn from_cstr(s: *const c_char) -> ResultType<Self> {
|
||||
let s = unsafe { CStr::from_ptr(s) };
|
||||
Ok(serde_json::from_str(s.to_str()?)?)
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &str {
|
||||
&self.id
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
pub fn version(&self) -> &str {
|
||||
&self.version
|
||||
}
|
||||
|
||||
pub fn description(&self) -> &str {
|
||||
&self.description
|
||||
}
|
||||
|
||||
pub fn author(&self) -> &str {
|
||||
&self.author
|
||||
}
|
||||
|
||||
pub fn home(&self) -> &str {
|
||||
&self.home
|
||||
}
|
||||
|
||||
pub fn license(&self) -> &str {
|
||||
&self.license
|
||||
}
|
||||
|
||||
pub fn published(&self) -> &str {
|
||||
&self.published
|
||||
}
|
||||
|
||||
pub fn released(&self) -> &str {
|
||||
&self.released
|
||||
}
|
||||
|
||||
pub fn github(&self) -> &str {
|
||||
&self.github
|
||||
}
|
||||
|
||||
pub fn location(&self) -> &Location {
|
||||
&self.location
|
||||
}
|
||||
|
||||
pub fn config(&self) -> &Config {
|
||||
&self.config
|
||||
}
|
||||
}
|
||||
19
src/plugin/mod.rs
Normal file
19
src/plugin/mod.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use hbb_common::{dlopen::symbor::Library, log, ResultType};
|
||||
use std::{
|
||||
ffi::{c_char, CStr},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
mod callback_msg;
|
||||
mod config;
|
||||
pub mod desc;
|
||||
mod plugins;
|
||||
|
||||
pub use plugins::load_plugins;
|
||||
|
||||
#[inline]
|
||||
fn cstr_to_string(cstr: *const c_char) -> ResultType<String> {
|
||||
Ok(String::from_utf8(unsafe {
|
||||
CStr::from_ptr(cstr).to_bytes().to_vec()
|
||||
})?)
|
||||
}
|
||||
162
src/plugin/plugins.rs
Normal file
162
src/plugin/plugins.rs
Normal file
@@ -0,0 +1,162 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
ffi::{c_char, CStr},
|
||||
path::PathBuf,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
use super::{callback_msg, desc::Desc};
|
||||
use hbb_common::{
|
||||
anyhow::Error,
|
||||
bail,
|
||||
dlopen::symbor::Library,
|
||||
lazy_static, libc, log,
|
||||
log::{debug, error},
|
||||
ResultType,
|
||||
};
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
pub static ref PLUGINS: Arc<RwLock<HashMap<String, Plugin>>> = Default::default();
|
||||
}
|
||||
|
||||
/// Initialize the plugins.
|
||||
/// Return 0 if success.
|
||||
pub type PluginFuncInit = fn() -> i32;
|
||||
/// Reset the plugin.
|
||||
/// Return 0 if success.
|
||||
pub type PluginFuncReset = fn() -> i32;
|
||||
/// Clear the plugin.
|
||||
/// Return 0 if success.
|
||||
pub type PluginFuncClear = fn() -> i32;
|
||||
/// Get the description of the plugin.
|
||||
/// Return the description. The plugin allocate memory with `libc::malloc` and return the pointer.
|
||||
pub type PluginFuncDesc = fn() -> *const c_char;
|
||||
/// Callback to send message to peer or ui.
|
||||
/// peer, target, id are utf8 strings(null terminated).
|
||||
///
|
||||
/// peer: The peer id.
|
||||
/// target: "peer" or "ui".
|
||||
/// id: The id of this plugin.
|
||||
/// content: The content.
|
||||
/// len: The length of the content.
|
||||
type PluginFuncCallbackMsg = fn(
|
||||
peer: *const c_char,
|
||||
target: *const c_char,
|
||||
id: *const c_char,
|
||||
content: *const c_char,
|
||||
len: usize,
|
||||
);
|
||||
pub type PluginFuncSetCallbackMsg = fn(PluginFuncCallbackMsg);
|
||||
/// The main function of the plugin.
|
||||
/// method: The method. "handle_ui" or "handle_peer"
|
||||
/// args: The arguments.
|
||||
/// out: The output. The plugin allocate memory with `libc::malloc` and set the pointer to `out`.
|
||||
/// out_len: The length of the output.
|
||||
/// Return 0 if success.
|
||||
pub type PluginFuncCall = fn(
|
||||
method: *const c_char,
|
||||
args: *const c_char,
|
||||
out: *mut *mut c_char,
|
||||
out_len: *mut usize,
|
||||
) -> i32;
|
||||
|
||||
macro_rules! make_plugin {
|
||||
($($field:ident : $tp:ty),+) => {
|
||||
pub struct Plugin {
|
||||
_lib: Library,
|
||||
path: String,
|
||||
desc: Option<Desc>,
|
||||
$($field: $tp),+
|
||||
}
|
||||
|
||||
impl Plugin {
|
||||
fn new(path: &str) -> ResultType<Self> {
|
||||
let lib = match Library::open(path) {
|
||||
Ok(lib) => lib,
|
||||
Err(e) => {
|
||||
bail!("Failed to load library {}, {}", path, e);
|
||||
}
|
||||
};
|
||||
|
||||
$(let $field = match unsafe { lib.symbol::<$tp>(stringify!($field)) } {
|
||||
Ok(m) => {
|
||||
log::info!("method found {}", stringify!($field));
|
||||
*m
|
||||
},
|
||||
Err(e) => {
|
||||
bail!("Failed to load {} func {}, {}", path, stringify!($field), e);
|
||||
}
|
||||
}
|
||||
;)+
|
||||
|
||||
Ok(Self {
|
||||
_lib: lib,
|
||||
path: path.to_string(),
|
||||
desc: None,
|
||||
$( $field ),+
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
make_plugin!(
|
||||
fn_init: PluginFuncInit,
|
||||
fn_reset: PluginFuncReset,
|
||||
fn_clear: PluginFuncClear,
|
||||
fn_desc: PluginFuncDesc,
|
||||
fn_set_cb_msg: PluginFuncSetCallbackMsg,
|
||||
fn_call: PluginFuncCall
|
||||
);
|
||||
|
||||
pub fn load_plugins(dir: &str) -> ResultType<()> {
|
||||
for entry in std::fs::read_dir(dir)? {
|
||||
match entry {
|
||||
Ok(entry) => {
|
||||
let path = entry.path();
|
||||
if path.is_file() {
|
||||
let path = path.to_str().unwrap_or("");
|
||||
if path.ends_with(".so") {
|
||||
if let Err(e) = load_plugin(path) {
|
||||
log::error!("{e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Failed to read dir entry, {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unload_plugin(id: &str) {
|
||||
if let Some(plugin) = PLUGINS.write().unwrap().remove(id) {
|
||||
let _ret = (plugin.fn_clear)();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reload_plugin(id: &str) -> ResultType<()> {
|
||||
let path = match PLUGINS.read().unwrap().get(id) {
|
||||
Some(plugin) => plugin.path.clone(),
|
||||
None => bail!("Plugin {} not found", id),
|
||||
};
|
||||
unload_plugin(id);
|
||||
load_plugin(&path)
|
||||
}
|
||||
|
||||
fn load_plugin(path: &str) -> ResultType<()> {
|
||||
let mut plugin = Plugin::new(path)?;
|
||||
let desc = (plugin.fn_desc)();
|
||||
let desc_res = Desc::from_cstr(desc);
|
||||
unsafe {
|
||||
libc::free(desc as _);
|
||||
}
|
||||
let desc = desc_res?;
|
||||
let id = desc.id().to_string();
|
||||
(plugin.fn_set_cb_msg)(callback_msg::callback_msg);
|
||||
plugin.desc = Some(desc);
|
||||
PLUGINS.write().unwrap().insert(id, plugin);
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user