Files
rustdesk/src/plugin/mod.rs
T

164 lines
3.8 KiB
Rust
Raw Normal View History

use hbb_common::{libc, log, ResultType};
#[cfg(target_os = "windows")]
use std::env;
2023-05-05 13:37:43 +08:00
use std::{
ffi::{c_char, c_int, c_void, CStr},
2023-05-09 19:47:26 +08:00
path::PathBuf,
2023-05-05 13:37:43 +08:00
ptr::null,
};
2023-04-18 23:02:37 +08:00
mod callback_ext;
2023-05-05 13:37:43 +08:00
mod callback_msg;
2023-04-18 23:02:37 +08:00
mod config;
pub mod desc;
2023-04-19 17:06:59 +08:00
mod errno;
2023-04-23 15:40:55 +08:00
pub mod ipc;
2023-05-09 19:47:26 +08:00
mod manager;
pub mod native;
pub mod native_handlers;
2023-05-05 13:37:43 +08:00
mod plog;
mod plugins;
2023-04-18 23:02:37 +08:00
2023-05-09 19:47:26 +08:00
pub use manager::{
install::install_plugin_with_url, install_plugin, load_plugin_list, uninstall_plugin,
2023-05-09 19:47:26 +08:00
};
2023-04-19 17:06:59 +08:00
pub use plugins::{
2023-04-27 11:24:19 +08:00
handle_client_event, handle_listen_event, handle_server_event, handle_ui_event, load_plugin,
reload_plugin, sync_ui, unload_plugin,
2023-04-19 17:06:59 +08:00
};
2023-04-18 23:02:37 +08:00
2023-04-21 21:40:34 +08:00
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";
2023-05-09 19:47:26 +08:00
const MSG_TO_UI_TYPE_PLUGIN_MANAGER: &str = "plugin_manager";
2023-04-21 21:40:34 +08:00
2023-04-27 11:24:19 +08:00
pub const EVENT_ON_CONN_CLIENT: &str = "on_conn_client";
pub const EVENT_ON_CONN_SERVER: &str = "on_conn_server";
pub const EVENT_ON_CONN_CLOSE_CLIENT: &str = "on_conn_close_client";
pub const EVENT_ON_CONN_CLOSE_SERVER: &str = "on_conn_close_server";
2023-05-09 22:59:38 +08:00
static PLUGIN_SOURCE_LOCAL_DIR: &str = "plugins";
2023-04-23 15:40:55 +08:00
pub use config::{ManagerConfig, PeerConfig, SharedConfig};
2023-04-20 20:57:47 +08:00
2023-05-09 19:47:26 +08:00
use crate::common::is_server;
2023-05-05 13:37:43 +08:00
/// Common plugin return.
///
/// [Note]
/// The msg must be nullptr if code is errno::ERR_SUCCESS.
/// The msg must be freed by caller if code is not errno::ERR_SUCCESS.
#[repr(C)]
#[derive(Debug)]
pub struct PluginReturn {
pub code: c_int,
pub msg: *const c_char,
}
impl PluginReturn {
pub fn success() -> Self {
Self {
code: errno::ERR_SUCCESS,
msg: null(),
}
}
#[inline]
pub fn is_success(&self) -> bool {
self.code == errno::ERR_SUCCESS
}
pub fn new(code: c_int, msg: &str) -> Self {
Self {
code,
msg: str_to_cstr_ret(msg),
}
}
pub fn get_code_msg(&mut self) -> (i32, String) {
if self.is_success() {
(self.code, "".to_owned())
} else {
assert!(!self.msg.is_null());
let msg = cstr_to_string(self.msg).unwrap_or_default();
free_c_ptr(self.msg as _);
self.msg = null();
(self.code as _, msg)
}
}
}
2023-05-09 19:47:26 +08:00
pub fn init() {
2023-05-10 18:58:45 +08:00
if !is_server() {
std::thread::spawn(move || manager::start_ipc());
} else {
if let Err(e) = manager::remove_plugins() {
log::error!("Failed to remove plugins: {}", e);
}
}
if let Err(e) = plugins::load_plugins() {
log::error!("Failed to load plugins: {}", e);
2023-05-09 19:47:26 +08:00
}
}
2023-05-11 16:54:57 +08:00
#[inline]
#[cfg(target_os = "windows")]
fn get_share_dir() -> ResultType<PathBuf> {
Ok(PathBuf::from(env::var("ProgramData")?))
}
#[inline]
#[cfg(target_os = "linux")]
fn get_share_dir() -> ResultType<PathBuf> {
Ok(PathBuf::from("/usr/share"))
}
#[inline]
#[cfg(target_os = "macos")]
fn get_share_dir() -> ResultType<PathBuf> {
Ok(PathBuf::from("/Library/Application Support"))
}
2023-05-09 19:47:26 +08:00
#[inline]
fn get_plugins_dir() -> ResultType<PathBuf> {
2023-05-11 16:54:57 +08:00
Ok(get_share_dir()?
2023-05-09 22:59:38 +08:00
.join("RustDesk")
.join(PLUGIN_SOURCE_LOCAL_DIR))
2023-05-09 19:47:26 +08:00
}
#[inline]
fn get_plugin_dir(id: &str) -> ResultType<PathBuf> {
Ok(get_plugins_dir()?.join(id))
}
2023-04-18 23:02:37 +08:00
#[inline]
fn cstr_to_string(cstr: *const c_char) -> ResultType<String> {
Ok(String::from_utf8(unsafe {
CStr::from_ptr(cstr).to_bytes().to_vec()
})?)
}
2023-04-19 17:06:59 +08:00
2023-04-24 18:45:22 +08:00
#[inline]
2023-04-27 11:24:19 +08:00
fn str_to_cstr_ret(s: &str) -> *const c_char {
2023-04-24 18:45:22 +08:00
let mut s = s.as_bytes().to_vec();
s.push(0);
unsafe {
let r = libc::malloc(s.len()) as *mut c_char;
libc::memcpy(
r as *mut libc::c_void,
s.as_ptr() as *const libc::c_void,
s.len(),
);
r
}
}
#[inline]
2023-05-10 18:58:45 +08:00
fn free_c_ptr(p: *mut c_void) {
if !p.is_null() {
2023-04-24 18:45:22 +08:00
unsafe {
2023-05-10 18:58:45 +08:00
libc::free(p);
2023-04-24 18:45:22 +08:00
}
}
}