2023-05-09 19:47:26 +08:00
|
|
|
use hbb_common::{libc, tokio, ResultType};
|
2023-05-05 13:37:43 +08:00
|
|
|
use std::{
|
2023-05-09 19:47:26 +08:00
|
|
|
env,
|
2023-05-05 13:37:43 +08:00
|
|
|
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
|
|
|
|
2023-05-04 19:21:13 +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;
|
2023-05-04 19:21:13 +08:00
|
|
|
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 as privileged_install_plugin, install_plugin as user_install_plugin,
|
|
|
|
load_plugin_list, uninstall_plugin,
|
|
|
|
};
|
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,
|
2023-05-09 19:47:26 +08:00
|
|
|
reload_plugin, sync_ui, unload_plugin, unload_plugins,
|
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_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";
|
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-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() {
|
|
|
|
std::thread::spawn(move || manager::start_ipc());
|
|
|
|
if is_server() {
|
|
|
|
manager::remove_plugins();
|
|
|
|
}
|
|
|
|
load_plugin_list(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn get_plugins_dir() -> ResultType<PathBuf> {
|
|
|
|
// to-do: linux and macos
|
|
|
|
Ok(PathBuf::from(env::var("ProgramData")?).join(manager::PLUGIN_SOURCE_LOCAL_URL))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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]
|
|
|
|
fn free_c_ptr(ret: *mut c_void) {
|
|
|
|
if !ret.is_null() {
|
|
|
|
unsafe {
|
|
|
|
libc::free(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|