2024-05-09 11:03:59 +08:00
|
|
|
|
use crate::{
|
|
|
|
|
privacy_mode::PrivacyModeState,
|
|
|
|
|
ui_interface::{get_local_option, set_local_option},
|
|
|
|
|
};
|
2022-08-01 14:33:08 +08:00
|
|
|
|
use bytes::Bytes;
|
2022-07-25 16:23:45 +08:00
|
|
|
|
use parity_tokio_ipc::{
|
|
|
|
|
Connection as Conn, ConnectionClient as ConnClient, Endpoint, Incoming, SecurityAttributes,
|
|
|
|
|
};
|
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2024-07-30 11:35:39 +08:00
|
|
|
|
use std::{
|
|
|
|
|
collections::HashMap,
|
|
|
|
|
sync::atomic::{AtomicBool, Ordering},
|
|
|
|
|
};
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
|
use std::{fs::File, io::prelude::*};
|
2022-07-25 16:23:45 +08:00
|
|
|
|
|
2023-04-23 15:40:55 +08:00
|
|
|
|
#[cfg(all(feature = "flutter", feature = "plugin_framework"))]
|
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
|
|
|
|
use crate::plugin::ipc::Plugin;
|
2022-05-12 17:35:25 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
2023-01-09 02:30:21 -05:00
|
|
|
|
pub use clipboard::ClipboardFile;
|
2022-01-13 16:07:18 +08:00
|
|
|
|
use hbb_common::{
|
|
|
|
|
allow_err, bail, bytes,
|
|
|
|
|
bytes_codec::BytesCodec,
|
2022-04-26 11:19:45 +08:00
|
|
|
|
config::{self, Config, Config2},
|
2022-01-13 16:07:18 +08:00
|
|
|
|
futures::StreamExt as _,
|
|
|
|
|
futures_util::sink::SinkExt,
|
2024-07-30 11:35:39 +08:00
|
|
|
|
log, password_security as password, timeout,
|
|
|
|
|
tokio::{
|
|
|
|
|
self,
|
|
|
|
|
io::{AsyncRead, AsyncWrite},
|
|
|
|
|
},
|
2022-01-13 16:07:18 +08:00
|
|
|
|
tokio_util::codec::Framed,
|
2023-02-08 19:17:59 +08:00
|
|
|
|
ResultType,
|
2022-01-13 16:07:18 +08:00
|
|
|
|
};
|
2022-07-25 16:23:45 +08:00
|
|
|
|
|
2023-11-14 12:11:38 +08:00
|
|
|
|
use crate::{common::is_server, privacy_mode, rendezvous_mediator::RendezvousMediator};
|
2021-03-29 15:59:14 +08:00
|
|
|
|
|
2023-05-02 23:48:49 +08:00
|
|
|
|
// IPC actions here.
|
|
|
|
|
pub const IPC_ACTION_CLOSE: &str = "close";
|
2023-06-07 14:25:34 +08:00
|
|
|
|
pub static EXIT_RECV_CLOSE: AtomicBool = AtomicBool::new(true);
|
2022-04-25 12:28:28 +08:00
|
|
|
|
|
2021-03-29 15:59:14 +08:00
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
|
#[serde(tag = "t", content = "c")]
|
|
|
|
|
pub enum FS {
|
|
|
|
|
ReadDir {
|
|
|
|
|
dir: String,
|
|
|
|
|
include_hidden: bool,
|
|
|
|
|
},
|
|
|
|
|
RemoveDir {
|
|
|
|
|
path: String,
|
|
|
|
|
id: i32,
|
|
|
|
|
recursive: bool,
|
|
|
|
|
},
|
|
|
|
|
RemoveFile {
|
|
|
|
|
path: String,
|
|
|
|
|
id: i32,
|
|
|
|
|
file_num: i32,
|
|
|
|
|
},
|
|
|
|
|
CreateDir {
|
|
|
|
|
path: String,
|
|
|
|
|
id: i32,
|
|
|
|
|
},
|
|
|
|
|
NewWrite {
|
|
|
|
|
path: String,
|
|
|
|
|
id: i32,
|
2022-05-13 11:23:30 +08:00
|
|
|
|
file_num: i32,
|
2021-03-29 15:59:14 +08:00
|
|
|
|
files: Vec<(String, u64)>,
|
2022-08-04 17:24:02 +08:00
|
|
|
|
overwrite_detection: bool,
|
2023-09-06 16:56:39 +08:00
|
|
|
|
total_size: u64,
|
|
|
|
|
conn_id: i32,
|
2021-03-29 15:59:14 +08:00
|
|
|
|
},
|
|
|
|
|
CancelWrite {
|
|
|
|
|
id: i32,
|
|
|
|
|
},
|
|
|
|
|
WriteBlock {
|
|
|
|
|
id: i32,
|
|
|
|
|
file_num: i32,
|
2022-07-21 16:07:19 +08:00
|
|
|
|
data: Bytes,
|
2021-03-29 15:59:14 +08:00
|
|
|
|
compressed: bool,
|
|
|
|
|
},
|
|
|
|
|
WriteDone {
|
|
|
|
|
id: i32,
|
|
|
|
|
file_num: i32,
|
|
|
|
|
},
|
2022-12-06 12:11:26 +08:00
|
|
|
|
WriteError {
|
|
|
|
|
id: i32,
|
|
|
|
|
file_num: i32,
|
2022-12-09 19:43:26 +08:00
|
|
|
|
err: String,
|
2022-12-06 12:11:26 +08:00
|
|
|
|
},
|
2022-05-12 14:16:06 +08:00
|
|
|
|
WriteOffset {
|
|
|
|
|
id: i32,
|
|
|
|
|
file_num: i32,
|
2022-07-25 16:23:45 +08:00
|
|
|
|
offset_blk: u32,
|
2022-05-12 14:16:06 +08:00
|
|
|
|
},
|
2022-04-27 10:45:20 +08:00
|
|
|
|
CheckDigest {
|
|
|
|
|
id: i32,
|
|
|
|
|
file_num: i32,
|
|
|
|
|
file_size: u64,
|
2022-04-29 16:47:45 +08:00
|
|
|
|
last_modified: u64,
|
2022-04-28 17:42:22 +08:00
|
|
|
|
is_upload: bool,
|
2022-04-27 10:45:20 +08:00
|
|
|
|
},
|
2021-03-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-30 11:35:39 +08:00
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
|
#[serde(tag = "t")]
|
|
|
|
|
pub struct ClipboardNonFile {
|
|
|
|
|
pub compress: bool,
|
|
|
|
|
pub content: bytes::Bytes,
|
|
|
|
|
pub content_len: usize,
|
|
|
|
|
pub next_raw: bool,
|
|
|
|
|
pub width: i32,
|
|
|
|
|
pub height: i32,
|
|
|
|
|
// message.proto: ClipboardFormat
|
|
|
|
|
pub format: i32,
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 22:06:59 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
2022-07-07 01:27:21 +08:00
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
|
#[serde(tag = "t", content = "c")]
|
|
|
|
|
pub enum DataKeyboard {
|
|
|
|
|
Sequence(String),
|
|
|
|
|
KeyDown(enigo::Key),
|
|
|
|
|
KeyUp(enigo::Key),
|
|
|
|
|
KeyClick(enigo::Key),
|
|
|
|
|
GetKeyState(enigo::Key),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
|
#[serde(tag = "t", content = "c")]
|
|
|
|
|
pub enum DataKeyboardResponse {
|
|
|
|
|
GetKeyState(bool),
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 14:17:47 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
2022-07-07 01:27:21 +08:00
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
|
#[serde(tag = "t", content = "c")]
|
|
|
|
|
pub enum DataMouse {
|
|
|
|
|
MoveTo(i32, i32),
|
|
|
|
|
MoveRelative(i32, i32),
|
|
|
|
|
Down(enigo::MouseButton),
|
|
|
|
|
Up(enigo::MouseButton),
|
|
|
|
|
Click(enigo::MouseButton),
|
|
|
|
|
ScrollX(i32),
|
|
|
|
|
ScrollY(i32),
|
2022-11-25 15:22:44 +08:00
|
|
|
|
Refresh,
|
2022-07-07 01:27:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
|
#[serde(tag = "t", content = "c")]
|
|
|
|
|
pub enum DataControl {
|
|
|
|
|
Resolution {
|
|
|
|
|
minx: i32,
|
|
|
|
|
maxx: i32,
|
|
|
|
|
miny: i32,
|
|
|
|
|
maxy: i32,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 10:27:13 +08:00
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
|
#[serde(tag = "t", content = "c")]
|
|
|
|
|
pub enum DataPortableService {
|
|
|
|
|
Ping,
|
|
|
|
|
Pong,
|
|
|
|
|
ConnCount(Option<usize>),
|
2023-05-05 18:11:04 +08:00
|
|
|
|
Mouse((Vec<u8>, i32)),
|
2023-07-19 01:18:10 +08:00
|
|
|
|
Pointer((Vec<u8>, i32)),
|
2022-11-10 10:27:13 +08:00
|
|
|
|
Key(Vec<u8>),
|
|
|
|
|
RequestStart,
|
|
|
|
|
WillClose,
|
|
|
|
|
CmShowElevation(bool),
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 15:59:14 +08:00
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
|
#[serde(tag = "t", content = "c")]
|
|
|
|
|
pub enum Data {
|
|
|
|
|
Login {
|
|
|
|
|
id: i32,
|
|
|
|
|
is_file_transfer: bool,
|
|
|
|
|
peer_id: String,
|
|
|
|
|
name: String,
|
|
|
|
|
authorized: bool,
|
|
|
|
|
port_forward: String,
|
|
|
|
|
keyboard: bool,
|
|
|
|
|
clipboard: bool,
|
|
|
|
|
audio: bool,
|
2022-02-15 14:46:08 +08:00
|
|
|
|
file: bool,
|
2022-02-22 22:26:22 +08:00
|
|
|
|
file_transfer_enabled: bool,
|
2022-07-25 19:35:15 +08:00
|
|
|
|
restart: bool,
|
2022-09-22 09:55:34 +08:00
|
|
|
|
recording: bool,
|
2023-11-05 21:53:21 +08:00
|
|
|
|
block_input: bool,
|
2023-01-17 13:28:33 +08:00
|
|
|
|
from_switch: bool,
|
2021-03-29 15:59:14 +08:00
|
|
|
|
},
|
|
|
|
|
ChatMessage {
|
|
|
|
|
text: String,
|
|
|
|
|
},
|
|
|
|
|
SwitchPermission {
|
|
|
|
|
name: String,
|
|
|
|
|
enabled: bool,
|
|
|
|
|
},
|
|
|
|
|
SystemInfo(Option<String>),
|
2022-05-12 17:35:25 +08:00
|
|
|
|
ClickTime(i64),
|
2023-06-24 21:09:45 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
2022-05-12 17:35:25 +08:00
|
|
|
|
MouseMoveTime(i64),
|
2021-03-29 15:59:14 +08:00
|
|
|
|
Authorize,
|
|
|
|
|
Close,
|
2024-06-27 13:05:45 +08:00
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
|
InputControl(bool),
|
2024-02-26 13:22:21 +08:00
|
|
|
|
#[cfg(windows)]
|
2021-03-29 15:59:14 +08:00
|
|
|
|
SAS,
|
2024-02-14 21:29:17 +05:30
|
|
|
|
UserSid(Option<u32>),
|
2021-03-29 15:59:14 +08:00
|
|
|
|
OnlineStatus(Option<(i64, bool)>),
|
|
|
|
|
Config((String, Option<String>)),
|
|
|
|
|
Options(Option<HashMap<String, String>>),
|
|
|
|
|
NatType(Option<i32>),
|
|
|
|
|
ConfirmedKey(Option<(Vec<u8>, Vec<u8>)>),
|
|
|
|
|
RawMessage(Vec<u8>),
|
2022-01-05 18:34:30 +08:00
|
|
|
|
Socks(Option<config::Socks5Server>),
|
2021-03-29 15:59:14 +08:00
|
|
|
|
FS(FS),
|
|
|
|
|
Test,
|
2023-02-08 19:17:59 +08:00
|
|
|
|
SyncConfig(Option<Box<(Config, Config2)>>),
|
2022-04-26 11:19:45 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
2023-01-09 02:30:21 -05:00
|
|
|
|
ClipboardFile(ClipboardFile),
|
2023-06-20 00:05:23 +08:00
|
|
|
|
ClipboardFileEnabled(bool),
|
2024-07-30 11:35:39 +08:00
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
ClipboardNonFile(Option<(String, Vec<ClipboardNonFile>)>),
|
2023-11-14 12:11:38 +08:00
|
|
|
|
PrivacyModeState((i32, PrivacyModeState, String)),
|
2022-07-14 15:21:26 +08:00
|
|
|
|
TestRendezvousServer,
|
2022-12-29 14:17:47 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
2022-07-07 01:27:21 +08:00
|
|
|
|
Keyboard(DataKeyboard),
|
2022-12-29 14:17:47 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
2022-07-07 01:27:21 +08:00
|
|
|
|
KeyboardResponse(DataKeyboardResponse),
|
2022-12-29 14:17:47 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
2022-07-07 01:27:21 +08:00
|
|
|
|
Mouse(DataMouse),
|
|
|
|
|
Control(DataControl),
|
2022-09-21 23:32:59 +08:00
|
|
|
|
Theme(String),
|
2022-09-08 08:52:56 +08:00
|
|
|
|
Language(String),
|
2022-07-07 01:27:21 +08:00
|
|
|
|
Empty,
|
2022-10-08 20:15:02 +08:00
|
|
|
|
Disconnected,
|
2022-11-10 10:27:13 +08:00
|
|
|
|
DataPortableService(DataPortableService),
|
2023-01-17 20:16:36 +08:00
|
|
|
|
SwitchSidesRequest(String),
|
|
|
|
|
SwitchSidesBack,
|
2023-02-06 11:42:25 +08:00
|
|
|
|
UrlLink(String),
|
|
|
|
|
VoiceCallIncoming,
|
2023-02-06 12:53:57 +08:00
|
|
|
|
StartVoiceCall,
|
2023-02-06 11:42:25 +08:00
|
|
|
|
VoiceCallResponse(bool),
|
|
|
|
|
CloseVoiceCall(String),
|
2023-04-23 15:40:55 +08:00
|
|
|
|
#[cfg(all(feature = "flutter", feature = "plugin_framework"))]
|
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
|
|
|
|
Plugin(Plugin),
|
2023-07-20 21:16:38 +08:00
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
SyncWinCpuUsage(Option<f64>),
|
2023-11-05 15:55:09 +08:00
|
|
|
|
FileTransferLog((String, String)),
|
2023-09-16 21:46:44 +08:00
|
|
|
|
#[cfg(windows)]
|
2023-09-09 09:45:05 +08:00
|
|
|
|
ControlledSessionCount(usize),
|
2023-11-07 17:19:33 +08:00
|
|
|
|
CmErr(String),
|
2024-04-18 13:12:45 +08:00
|
|
|
|
CheckHwcodec,
|
2024-04-28 21:08:49 +08:00
|
|
|
|
VideoConnCount(Option<usize>),
|
2024-05-09 11:03:59 +08:00
|
|
|
|
// Although the key is not neccessary, it is used to avoid hardcoding the key.
|
|
|
|
|
WaylandScreencastRestoreToken((String, String)),
|
hwcodec, only process that start ipc server start check process (#8325)
check process send config to ipc server, other process get config from ipc server. Process will save config to toml, and the toml will be used if the config is none.
when start check process: ipc server process start or option changed
from disable to enable
when get config: main window start or option changed from disable to
enable, start_video_audio_threads.
Only windows implements signature, which is used to mark whether the gpu software and hardware information changes. After reboot, the signature doesn't change. https://asawicki.info/news_1773_how_to_programmatically_check_graphics_driver_version, use dxgi way to get software version, it's not consistent with the visible driver version, after updating intel driver with small version change, the signature doesn't change. Linux doesn't use toml file.
Signed-off-by: 21pages <sunboeasy@gmail.com>
2024-06-12 20:40:35 +08:00
|
|
|
|
HwCodecConfig(Option<String>),
|
2021-03-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 19:42:51 +08:00
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
2021-03-29 15:59:14 +08:00
|
|
|
|
pub async fn start(postfix: &str) -> ResultType<()> {
|
|
|
|
|
let mut incoming = new_listener(postfix).await?;
|
|
|
|
|
loop {
|
|
|
|
|
if let Some(result) = incoming.next().await {
|
|
|
|
|
match result {
|
|
|
|
|
Ok(stream) => {
|
|
|
|
|
let mut stream = Connection::new(stream);
|
|
|
|
|
let postfix = postfix.to_owned();
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
loop {
|
|
|
|
|
match stream.next().await {
|
|
|
|
|
Err(err) => {
|
2023-06-18 20:23:54 +08:00
|
|
|
|
log::trace!("ipc '{}' connection closed: {}", postfix, err);
|
2021-03-29 15:59:14 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Ok(Some(data)) => {
|
|
|
|
|
handle(data, &mut stream).await;
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("Couldn't get client: {:?}", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn new_listener(postfix: &str) -> ResultType<Incoming> {
|
|
|
|
|
let path = Config::ipc_path(postfix);
|
2023-03-11 01:34:33 +08:00
|
|
|
|
#[cfg(not(any(windows, target_os = "android", target_os = "ios")))]
|
2022-01-13 16:07:18 +08:00
|
|
|
|
check_pid(postfix).await;
|
2021-03-29 15:59:14 +08:00
|
|
|
|
let mut endpoint = Endpoint::new(path.clone());
|
|
|
|
|
match SecurityAttributes::allow_everyone_create() {
|
|
|
|
|
Ok(attr) => endpoint.set_security_attributes(attr),
|
|
|
|
|
Err(err) => log::error!("Failed to set ipc{} security: {}", postfix, err),
|
|
|
|
|
};
|
|
|
|
|
match endpoint.incoming() {
|
|
|
|
|
Ok(incoming) => {
|
|
|
|
|
log::info!("Started ipc{} server at path: {}", postfix, &path);
|
|
|
|
|
#[cfg(not(windows))]
|
2022-01-13 16:07:18 +08:00
|
|
|
|
{
|
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
|
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o0777)).ok();
|
|
|
|
|
write_pid(postfix);
|
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
|
Ok(incoming)
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!(
|
2021-11-14 18:52:05 +03:30
|
|
|
|
"Failed to start ipc{} server at path {}: {}",
|
2021-03-29 15:59:14 +08:00
|
|
|
|
postfix,
|
|
|
|
|
path,
|
|
|
|
|
err
|
|
|
|
|
);
|
|
|
|
|
Err(err.into())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-14 04:07:02 +08:00
|
|
|
|
pub struct CheckIfRestart(String, Vec<String>, String, String);
|
2022-04-26 11:19:45 +08:00
|
|
|
|
|
|
|
|
|
impl CheckIfRestart {
|
|
|
|
|
pub fn new() -> CheckIfRestart {
|
|
|
|
|
CheckIfRestart(
|
|
|
|
|
Config::get_option("stop-service"),
|
|
|
|
|
Config::get_rendezvous_servers(),
|
|
|
|
|
Config::get_option("audio-input"),
|
2024-07-14 04:07:02 +08:00
|
|
|
|
Config::get_option("voice-call-input"),
|
2022-04-26 11:19:45 +08:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl Drop for CheckIfRestart {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
if self.0 != Config::get_option("stop-service")
|
|
|
|
|
|| self.1 != Config::get_rendezvous_servers()
|
|
|
|
|
{
|
|
|
|
|
RendezvousMediator::restart();
|
|
|
|
|
}
|
|
|
|
|
if self.2 != Config::get_option("audio-input") {
|
|
|
|
|
crate::audio_service::restart();
|
|
|
|
|
}
|
2024-07-14 04:07:02 +08:00
|
|
|
|
if self.3 != Config::get_option("voice-call-input") {
|
|
|
|
|
crate::audio_service::set_voice_call_input_device(
|
|
|
|
|
Some(Config::get_option("voice-call-input")),
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
}
|
2022-04-26 11:19:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 15:59:14 +08:00
|
|
|
|
async fn handle(data: Data, stream: &mut Connection) {
|
|
|
|
|
match data {
|
|
|
|
|
Data::SystemInfo(_) => {
|
|
|
|
|
let info = format!(
|
|
|
|
|
"log_path: {}, config: {}, username: {}",
|
|
|
|
|
Config::log_path().to_str().unwrap_or(""),
|
|
|
|
|
Config::file().to_str().unwrap_or(""),
|
|
|
|
|
crate::username(),
|
|
|
|
|
);
|
|
|
|
|
allow_err!(stream.send(&Data::SystemInfo(Some(info))).await);
|
|
|
|
|
}
|
2022-05-12 17:35:25 +08:00
|
|
|
|
Data::ClickTime(_) => {
|
|
|
|
|
let t = crate::server::CLICK_TIME.load(Ordering::SeqCst);
|
|
|
|
|
allow_err!(stream.send(&Data::ClickTime(t)).await);
|
|
|
|
|
}
|
2023-06-24 21:09:45 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
2022-05-12 17:35:25 +08:00
|
|
|
|
Data::MouseMoveTime(_) => {
|
|
|
|
|
let t = crate::server::MOUSE_MOVE_TIME.load(Ordering::SeqCst);
|
|
|
|
|
allow_err!(stream.send(&Data::MouseMoveTime(t)).await);
|
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
|
Data::Close => {
|
|
|
|
|
log::info!("Receive close message");
|
2023-06-07 14:25:34 +08:00
|
|
|
|
if EXIT_RECV_CLOSE.load(Ordering::SeqCst) {
|
|
|
|
|
#[cfg(not(target_os = "android"))]
|
|
|
|
|
crate::server::input_service::fix_key_down_timeout_at_exit();
|
2023-11-14 12:11:38 +08:00
|
|
|
|
if is_server() {
|
|
|
|
|
let _ = privacy_mode::turn_off_privacy(0, Some(PrivacyModeState::OffByPeer));
|
|
|
|
|
}
|
2024-06-19 15:42:53 +08:00
|
|
|
|
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
2024-06-19 09:27:29 +08:00
|
|
|
|
if crate::is_main() {
|
|
|
|
|
// below part is for main windows can be reopen during rustdesk installation and installing service from UI
|
|
|
|
|
// this make new ipc server (domain socket) can be created.
|
|
|
|
|
std::fs::remove_file(&Config::ipc_path("")).ok();
|
2024-06-19 15:42:53 +08:00
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
{
|
|
|
|
|
hbb_common::sleep((crate::platform::SERVICE_INTERVAL * 2) as f32 / 1000.0)
|
|
|
|
|
.await;
|
|
|
|
|
crate::run_me::<&str>(vec![]).ok();
|
|
|
|
|
}
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
{
|
|
|
|
|
// our launchagent interval is 1 second
|
|
|
|
|
hbb_common::sleep(1.5).await;
|
|
|
|
|
std::process::Command::new("open")
|
|
|
|
|
.arg("-n")
|
|
|
|
|
.arg(&format!("/Applications/{}.app", crate::get_app_name()))
|
|
|
|
|
.spawn()
|
|
|
|
|
.ok();
|
|
|
|
|
}
|
2024-06-19 21:17:26 +08:00
|
|
|
|
// leave above open a little time
|
|
|
|
|
hbb_common::sleep(0.3).await;
|
2024-06-19 21:21:51 +08:00
|
|
|
|
// in case below exit failed
|
|
|
|
|
crate::platform::quit_gui();
|
2024-06-19 09:27:29 +08:00
|
|
|
|
}
|
|
|
|
|
std::process::exit(-1); // to make sure --server luauchagent process can restart because SuccessfulExit used
|
2023-06-07 14:25:34 +08:00
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
Data::OnlineStatus(_) => {
|
2023-07-01 14:43:00 +08:00
|
|
|
|
let x = config::get_online_state();
|
2021-03-29 15:59:14 +08:00
|
|
|
|
let confirmed = Config::get_key_confirmed();
|
|
|
|
|
allow_err!(stream.send(&Data::OnlineStatus(Some((x, confirmed)))).await);
|
|
|
|
|
}
|
|
|
|
|
Data::ConfirmedKey(None) => {
|
|
|
|
|
let out = if Config::get_key_confirmed() {
|
|
|
|
|
Some(Config::get_key_pair())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
allow_err!(stream.send(&Data::ConfirmedKey(out)).await);
|
|
|
|
|
}
|
2022-01-05 18:34:30 +08:00
|
|
|
|
Data::Socks(s) => match s {
|
|
|
|
|
None => {
|
|
|
|
|
allow_err!(stream.send(&Data::Socks(Config::get_socks())).await);
|
|
|
|
|
}
|
|
|
|
|
Some(data) => {
|
|
|
|
|
if data.proxy.is_empty() {
|
|
|
|
|
Config::set_socks(None);
|
|
|
|
|
} else {
|
|
|
|
|
Config::set_socks(Some(data));
|
|
|
|
|
}
|
2022-01-24 03:15:01 +08:00
|
|
|
|
crate::common::test_nat_type();
|
2022-02-07 18:39:49 +08:00
|
|
|
|
RendezvousMediator::restart();
|
2022-01-05 18:34:30 +08:00
|
|
|
|
log::info!("socks updated");
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-04-28 21:08:49 +08:00
|
|
|
|
Data::VideoConnCount(None) => {
|
|
|
|
|
let n = crate::server::AUTHED_CONNS
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|x| x.1 == crate::server::AuthConnType::Remote)
|
|
|
|
|
.count();
|
|
|
|
|
allow_err!(stream.send(&Data::VideoConnCount(Some(n))).await);
|
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
|
Data::Config((name, value)) => match value {
|
|
|
|
|
None => {
|
|
|
|
|
let value;
|
|
|
|
|
if name == "id" {
|
|
|
|
|
value = Some(Config::get_id());
|
2022-07-24 16:41:12 +08:00
|
|
|
|
} else if name == "temporary-password" {
|
|
|
|
|
value = Some(password::temporary_password());
|
|
|
|
|
} else if name == "permanent-password" {
|
|
|
|
|
value = Some(Config::get_permanent_password());
|
2021-03-29 15:59:14 +08:00
|
|
|
|
} else if name == "salt" {
|
|
|
|
|
value = Some(Config::get_salt());
|
|
|
|
|
} else if name == "rendezvous_server" {
|
2022-07-14 15:21:26 +08:00
|
|
|
|
value = Some(format!(
|
|
|
|
|
"{},{}",
|
|
|
|
|
Config::get_rendezvous_server(),
|
|
|
|
|
Config::get_rendezvous_servers().join(",")
|
|
|
|
|
));
|
2022-01-05 23:50:13 +08:00
|
|
|
|
} else if name == "rendezvous_servers" {
|
|
|
|
|
value = Some(Config::get_rendezvous_servers().join(","));
|
2023-04-19 14:39:22 +08:00
|
|
|
|
} else if name == "fingerprint" {
|
|
|
|
|
value = if Config::get_key_confirmed() {
|
|
|
|
|
Some(crate::common::pk_to_fingerprint(Config::get_key_pair().1))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2023-11-08 11:28:59 +08:00
|
|
|
|
} else if name == "hide_cm" {
|
|
|
|
|
value = if crate::hbbs_http::sync::is_pro() {
|
|
|
|
|
Some(hbb_common::password_security::hide_cm().to_string())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2024-07-14 04:07:02 +08:00
|
|
|
|
} else if name == "voice-call-input" {
|
|
|
|
|
value = crate::audio_service::get_voice_call_input_device();
|
2024-08-07 16:21:38 +08:00
|
|
|
|
} else if name == "unlock-pin" {
|
|
|
|
|
value = Some(Config::get_unlock_pin());
|
2021-03-29 15:59:14 +08:00
|
|
|
|
} else {
|
|
|
|
|
value = None;
|
|
|
|
|
}
|
|
|
|
|
allow_err!(stream.send(&Data::Config((name, value))).await);
|
|
|
|
|
}
|
|
|
|
|
Some(value) => {
|
|
|
|
|
if name == "id" {
|
2022-01-05 23:50:13 +08:00
|
|
|
|
Config::set_key_confirmed(false);
|
2021-03-29 15:59:14 +08:00
|
|
|
|
Config::set_id(&value);
|
2022-07-24 16:41:12 +08:00
|
|
|
|
} else if name == "temporary-password" {
|
|
|
|
|
password::update_temporary_password();
|
|
|
|
|
} else if name == "permanent-password" {
|
|
|
|
|
Config::set_permanent_password(&value);
|
2021-03-29 15:59:14 +08:00
|
|
|
|
} else if name == "salt" {
|
|
|
|
|
Config::set_salt(&value);
|
2024-07-14 04:07:02 +08:00
|
|
|
|
} else if name == "voice-call-input" {
|
|
|
|
|
crate::audio_service::set_voice_call_input_device(Some(value), true);
|
2024-08-07 16:21:38 +08:00
|
|
|
|
} else if name == "unlock-pin" {
|
|
|
|
|
Config::set_unlock_pin(&value);
|
2021-03-29 15:59:14 +08:00
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
log::info!("{} updated", name);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Data::Options(value) => match value {
|
|
|
|
|
None => {
|
|
|
|
|
let v = Config::get_options();
|
|
|
|
|
allow_err!(stream.send(&Data::Options(Some(v))).await);
|
|
|
|
|
}
|
|
|
|
|
Some(value) => {
|
2022-05-04 20:39:07 +08:00
|
|
|
|
let _chk = CheckIfRestart::new();
|
2023-11-14 12:11:38 +08:00
|
|
|
|
if let Some(v) = value.get("privacy-mode-impl-key") {
|
|
|
|
|
crate::privacy_mode::switch(v);
|
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
|
Config::set_options(value);
|
2022-02-08 18:09:45 +08:00
|
|
|
|
allow_err!(stream.send(&Data::Options(None)).await);
|
2021-03-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Data::NatType(_) => {
|
|
|
|
|
let t = Config::get_nat_type();
|
|
|
|
|
allow_err!(stream.send(&Data::NatType(Some(t))).await);
|
|
|
|
|
}
|
2023-02-08 19:17:59 +08:00
|
|
|
|
Data::SyncConfig(Some(configs)) => {
|
|
|
|
|
let (config, config2) = *configs;
|
2022-05-04 20:39:07 +08:00
|
|
|
|
let _chk = CheckIfRestart::new();
|
2022-04-26 11:19:45 +08:00
|
|
|
|
Config::set(config);
|
|
|
|
|
Config2::set(config2);
|
|
|
|
|
allow_err!(stream.send(&Data::SyncConfig(None)).await);
|
2022-01-13 15:26:57 +08:00
|
|
|
|
}
|
2022-04-26 11:19:45 +08:00
|
|
|
|
Data::SyncConfig(None) => {
|
2022-01-13 15:26:57 +08:00
|
|
|
|
allow_err!(
|
|
|
|
|
stream
|
2023-02-08 19:17:59 +08:00
|
|
|
|
.send(&Data::SyncConfig(Some(
|
|
|
|
|
(Config::get(), Config2::get()).into()
|
|
|
|
|
)))
|
2022-01-13 15:26:57 +08:00
|
|
|
|
.await
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-07-20 21:16:38 +08:00
|
|
|
|
#[cfg(windows)]
|
|
|
|
|
Data::SyncWinCpuUsage(None) => {
|
|
|
|
|
allow_err!(
|
|
|
|
|
stream
|
|
|
|
|
.send(&Data::SyncWinCpuUsage(
|
|
|
|
|
hbb_common::platform::windows::cpu_uage_one_minute()
|
|
|
|
|
))
|
|
|
|
|
.await
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-07-14 15:21:26 +08:00
|
|
|
|
Data::TestRendezvousServer => {
|
|
|
|
|
crate::test_rendezvous_server();
|
|
|
|
|
}
|
2023-01-17 20:16:36 +08:00
|
|
|
|
Data::SwitchSidesRequest(id) => {
|
|
|
|
|
let uuid = uuid::Uuid::new_v4();
|
|
|
|
|
crate::server::insert_switch_sides_uuid(id, uuid.clone());
|
|
|
|
|
allow_err!(
|
|
|
|
|
stream
|
|
|
|
|
.send(&Data::SwitchSidesRequest(uuid.to_string()))
|
|
|
|
|
.await
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-04-23 15:40:55 +08:00
|
|
|
|
#[cfg(all(feature = "flutter", feature = "plugin_framework"))]
|
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
|
|
|
|
Data::Plugin(plugin) => crate::plugin::ipc::handle_plugin(plugin, stream).await,
|
2023-09-16 21:46:44 +08:00
|
|
|
|
#[cfg(windows)]
|
2023-09-09 09:45:05 +08:00
|
|
|
|
Data::ControlledSessionCount(_) => {
|
|
|
|
|
allow_err!(
|
|
|
|
|
stream
|
|
|
|
|
.send(&Data::ControlledSessionCount(
|
|
|
|
|
crate::Connection::alive_conns().len()
|
|
|
|
|
))
|
|
|
|
|
.await
|
|
|
|
|
);
|
|
|
|
|
}
|
hwcodec, only process that start ipc server start check process (#8325)
check process send config to ipc server, other process get config from ipc server. Process will save config to toml, and the toml will be used if the config is none.
when start check process: ipc server process start or option changed
from disable to enable
when get config: main window start or option changed from disable to
enable, start_video_audio_threads.
Only windows implements signature, which is used to mark whether the gpu software and hardware information changes. After reboot, the signature doesn't change. https://asawicki.info/news_1773_how_to_programmatically_check_graphics_driver_version, use dxgi way to get software version, it's not consistent with the visible driver version, after updating intel driver with small version change, the signature doesn't change. Linux doesn't use toml file.
Signed-off-by: 21pages <sunboeasy@gmail.com>
2024-06-12 20:40:35 +08:00
|
|
|
|
#[cfg(feature = "hwcodec")]
|
2024-07-12 11:08:51 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
hwcodec, only process that start ipc server start check process (#8325)
check process send config to ipc server, other process get config from ipc server. Process will save config to toml, and the toml will be used if the config is none.
when start check process: ipc server process start or option changed
from disable to enable
when get config: main window start or option changed from disable to
enable, start_video_audio_threads.
Only windows implements signature, which is used to mark whether the gpu software and hardware information changes. After reboot, the signature doesn't change. https://asawicki.info/news_1773_how_to_programmatically_check_graphics_driver_version, use dxgi way to get software version, it's not consistent with the visible driver version, after updating intel driver with small version change, the signature doesn't change. Linux doesn't use toml file.
Signed-off-by: 21pages <sunboeasy@gmail.com>
2024-06-12 20:40:35 +08:00
|
|
|
|
Data::CheckHwcodec => {
|
|
|
|
|
scrap::hwcodec::start_check_process();
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature = "hwcodec")]
|
2024-07-12 11:08:51 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
hwcodec, only process that start ipc server start check process (#8325)
check process send config to ipc server, other process get config from ipc server. Process will save config to toml, and the toml will be used if the config is none.
when start check process: ipc server process start or option changed
from disable to enable
when get config: main window start or option changed from disable to
enable, start_video_audio_threads.
Only windows implements signature, which is used to mark whether the gpu software and hardware information changes. After reboot, the signature doesn't change. https://asawicki.info/news_1773_how_to_programmatically_check_graphics_driver_version, use dxgi way to get software version, it's not consistent with the visible driver version, after updating intel driver with small version change, the signature doesn't change. Linux doesn't use toml file.
Signed-off-by: 21pages <sunboeasy@gmail.com>
2024-06-12 20:40:35 +08:00
|
|
|
|
Data::HwCodecConfig(c) => {
|
|
|
|
|
match c {
|
|
|
|
|
None => {
|
|
|
|
|
let v = match scrap::hwcodec::HwCodecConfig::get_set_value() {
|
|
|
|
|
Some(v) => Some(serde_json::to_string(&v).unwrap_or_default()),
|
|
|
|
|
None => None,
|
|
|
|
|
};
|
|
|
|
|
allow_err!(stream.send(&Data::HwCodecConfig(v)).await);
|
|
|
|
|
}
|
|
|
|
|
Some(v) => {
|
|
|
|
|
// --server and portable
|
|
|
|
|
scrap::hwcodec::HwCodecConfig::set(v);
|
|
|
|
|
}
|
2024-04-18 13:12:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-09 11:03:59 +08:00
|
|
|
|
Data::WaylandScreencastRestoreToken((key, value)) => {
|
|
|
|
|
let v = if value == "get" {
|
|
|
|
|
let opt = get_local_option(key.clone());
|
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
|
|
|
|
{
|
|
|
|
|
Some(opt)
|
|
|
|
|
}
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
{
|
|
|
|
|
let v = if opt.is_empty() {
|
|
|
|
|
if scrap::wayland::pipewire::is_rdp_session_hold() {
|
|
|
|
|
"fake token".to_string()
|
|
|
|
|
} else {
|
|
|
|
|
"".to_owned()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
opt
|
|
|
|
|
};
|
|
|
|
|
Some(v)
|
|
|
|
|
}
|
|
|
|
|
} else if value == "clear" {
|
|
|
|
|
set_local_option(key.clone(), "".to_owned());
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
scrap::wayland::pipewire::close_session();
|
|
|
|
|
Some("".to_owned())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
if let Some(v) = v {
|
|
|
|
|
allow_err!(
|
|
|
|
|
stream
|
|
|
|
|
.send(&Data::WaylandScreencastRestoreToken((key, v)))
|
|
|
|
|
.await
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 01:14:22 +08:00
|
|
|
|
pub async fn connect(ms_timeout: u64, postfix: &str) -> ResultType<ConnectionTmpl<ConnClient>> {
|
2021-03-29 15:59:14 +08:00
|
|
|
|
let path = Config::ipc_path(postfix);
|
|
|
|
|
let client = timeout(ms_timeout, Endpoint::connect(&path)).await??;
|
2021-06-26 01:14:22 +08:00
|
|
|
|
Ok(ConnectionTmpl::new(client))
|
2021-03-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 17:23:55 +08:00
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
pub async fn start_pa() {
|
|
|
|
|
use crate::audio_service::AUDIO_DATA_SIZE_U8;
|
|
|
|
|
|
|
|
|
|
match new_listener("_pa").await {
|
|
|
|
|
Ok(mut incoming) => {
|
|
|
|
|
loop {
|
|
|
|
|
if let Some(result) = incoming.next().await {
|
|
|
|
|
match result {
|
|
|
|
|
Ok(stream) => {
|
|
|
|
|
let mut stream = Connection::new(stream);
|
|
|
|
|
let mut device: String = "".to_owned();
|
|
|
|
|
if let Some(Ok(Some(Data::Config((_, Some(x)))))) =
|
|
|
|
|
stream.next_timeout2(1000).await
|
|
|
|
|
{
|
|
|
|
|
device = x;
|
|
|
|
|
}
|
|
|
|
|
if !device.is_empty() {
|
|
|
|
|
device = crate::platform::linux::get_pa_source_name(&device);
|
|
|
|
|
}
|
|
|
|
|
if device.is_empty() {
|
|
|
|
|
device = crate::platform::linux::get_pa_monitor();
|
|
|
|
|
}
|
|
|
|
|
if device.is_empty() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let spec = pulse::sample::Spec {
|
|
|
|
|
format: pulse::sample::Format::F32le,
|
|
|
|
|
channels: 2,
|
|
|
|
|
rate: crate::platform::PA_SAMPLE_RATE,
|
|
|
|
|
};
|
|
|
|
|
log::info!("pa monitor: {:?}", device);
|
|
|
|
|
// systemctl --user status pulseaudio.service
|
|
|
|
|
let mut buf: Vec<u8> = vec![0; AUDIO_DATA_SIZE_U8];
|
|
|
|
|
match psimple::Simple::new(
|
|
|
|
|
None, // Use the default server
|
|
|
|
|
&crate::get_app_name(), // Our application’s name
|
|
|
|
|
pulse::stream::Direction::Record, // We want a record stream
|
|
|
|
|
Some(&device), // Use the default device
|
|
|
|
|
"record", // Description of our stream
|
|
|
|
|
&spec, // Our sample format
|
|
|
|
|
None, // Use default channel map
|
|
|
|
|
None, // Use default buffering attributes
|
|
|
|
|
) {
|
|
|
|
|
Ok(s) => loop {
|
|
|
|
|
if let Ok(_) = s.read(&mut buf) {
|
|
|
|
|
let out =
|
|
|
|
|
if buf.iter().filter(|x| **x != 0).next().is_none() {
|
|
|
|
|
vec![]
|
|
|
|
|
} else {
|
|
|
|
|
buf.clone()
|
|
|
|
|
};
|
|
|
|
|
if let Err(err) = stream.send_raw(out.into()).await {
|
|
|
|
|
log::error!("Failed to send audio data:{}", err);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("Could not create simple pulse: {}", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("Couldn't get pa client: {:?}", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("Failed to start pa ipc server: {}", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 15:59:14 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
|
fn get_pid_file(postfix: &str) -> String {
|
|
|
|
|
let path = Config::ipc_path(postfix);
|
|
|
|
|
format!("{}.pid", path)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-11 01:34:33 +08:00
|
|
|
|
#[cfg(not(any(windows, target_os = "android", target_os = "ios")))]
|
2021-03-29 15:59:14 +08:00
|
|
|
|
async fn check_pid(postfix: &str) {
|
|
|
|
|
let pid_file = get_pid_file(postfix);
|
|
|
|
|
if let Ok(mut file) = File::open(&pid_file) {
|
|
|
|
|
let mut content = String::new();
|
|
|
|
|
file.read_to_string(&mut content).ok();
|
2023-03-10 23:41:01 +08:00
|
|
|
|
let pid = content.parse::<usize>().unwrap_or(0);
|
2021-03-29 15:59:14 +08:00
|
|
|
|
if pid > 0 {
|
2023-11-14 12:11:38 +08:00
|
|
|
|
use hbb_common::sysinfo::System;
|
2022-01-15 14:08:24 +08:00
|
|
|
|
let mut sys = System::new();
|
|
|
|
|
sys.refresh_processes();
|
2022-01-24 02:26:07 +08:00
|
|
|
|
if let Some(p) = sys.process(pid.into()) {
|
2023-03-10 23:41:01 +08:00
|
|
|
|
if let Some(current) = sys.process((std::process::id() as usize).into()) {
|
2022-01-15 13:47:57 +08:00
|
|
|
|
if current.name() == p.name() {
|
2021-03-29 15:59:14 +08:00
|
|
|
|
// double check with connect
|
|
|
|
|
if connect(1000, postfix).await.is_ok() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-18 22:04:34 +08:00
|
|
|
|
// if not remove old ipc file, the new ipc creation will fail
|
2024-06-19 09:27:29 +08:00
|
|
|
|
// if we remove a ipc file, but the old ipc process is still running,
|
2024-06-18 22:04:34 +08:00
|
|
|
|
// new connection to the ipc will connect to new ipc, old connection to old ipc still keep alive
|
2022-12-09 19:43:26 +08:00
|
|
|
|
std::fs::remove_file(&Config::ipc_path(postfix)).ok();
|
2021-03-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
|
fn write_pid(postfix: &str) {
|
|
|
|
|
let path = get_pid_file(postfix);
|
|
|
|
|
if let Ok(mut file) = File::create(&path) {
|
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
|
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o0777)).ok();
|
|
|
|
|
file.write_all(&std::process::id().to_string().into_bytes())
|
|
|
|
|
.ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 01:14:22 +08:00
|
|
|
|
pub struct ConnectionTmpl<T> {
|
|
|
|
|
inner: Framed<T, BytesCodec>,
|
2021-03-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 01:14:22 +08:00
|
|
|
|
pub type Connection = ConnectionTmpl<Conn>;
|
|
|
|
|
|
|
|
|
|
impl<T> ConnectionTmpl<T>
|
2022-01-13 16:07:18 +08:00
|
|
|
|
where
|
|
|
|
|
T: AsyncRead + AsyncWrite + std::marker::Unpin,
|
2021-06-26 01:14:22 +08:00
|
|
|
|
{
|
|
|
|
|
pub fn new(conn: T) -> Self {
|
2021-03-29 15:59:14 +08:00
|
|
|
|
Self {
|
|
|
|
|
inner: Framed::new(conn, BytesCodec::new()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn send(&mut self, data: &Data) -> ResultType<()> {
|
|
|
|
|
let v = serde_json::to_vec(data)?;
|
|
|
|
|
self.inner.send(bytes::Bytes::from(v)).await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn send_config(&mut self, name: &str, value: String) -> ResultType<()> {
|
|
|
|
|
self.send(&Data::Config((name.to_owned(), Some(value))))
|
|
|
|
|
.await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn next_timeout(&mut self, ms_timeout: u64) -> ResultType<Option<Data>> {
|
|
|
|
|
Ok(timeout(ms_timeout, self.next()).await??)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn next_timeout2(&mut self, ms_timeout: u64) -> Option<ResultType<Option<Data>>> {
|
|
|
|
|
if let Ok(x) = timeout(ms_timeout, self.next()).await {
|
|
|
|
|
Some(x)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn next(&mut self) -> ResultType<Option<Data>> {
|
|
|
|
|
match self.inner.next().await {
|
|
|
|
|
Some(res) => {
|
|
|
|
|
let bytes = res?;
|
|
|
|
|
if let Ok(s) = std::str::from_utf8(&bytes) {
|
|
|
|
|
if let Ok(data) = serde_json::from_str::<Data>(s) {
|
|
|
|
|
return Ok(Some(data));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
bail!("reset by the peer");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-22 20:45:49 +08:00
|
|
|
|
|
2022-07-21 17:54:07 +08:00
|
|
|
|
pub async fn send_raw(&mut self, data: Bytes) -> ResultType<()> {
|
|
|
|
|
self.inner.send(data).await?;
|
2022-02-22 20:45:49 +08:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn next_raw(&mut self) -> ResultType<bytes::BytesMut> {
|
|
|
|
|
match self.inner.next().await {
|
|
|
|
|
Some(Ok(res)) => Ok(res),
|
|
|
|
|
_ => {
|
|
|
|
|
bail!("reset by the peer");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 19:42:51 +08:00
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
2023-11-08 11:28:59 +08:00
|
|
|
|
pub async fn get_config(name: &str) -> ResultType<Option<String>> {
|
2021-03-29 15:59:14 +08:00
|
|
|
|
get_config_async(name, 1_000).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn get_config_async(name: &str, ms_timeout: u64) -> ResultType<Option<String>> {
|
|
|
|
|
let mut c = connect(ms_timeout, "").await?;
|
|
|
|
|
c.send(&Data::Config((name.to_owned(), None))).await?;
|
|
|
|
|
if let Some(Data::Config((name2, value))) = c.next_timeout(ms_timeout).await? {
|
|
|
|
|
if name == name2 {
|
|
|
|
|
return Ok(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-12 17:35:25 +08:00
|
|
|
|
pub async fn set_config_async(name: &str, value: String) -> ResultType<()> {
|
2021-03-29 15:59:14 +08:00
|
|
|
|
let mut c = connect(1000, "").await?;
|
|
|
|
|
c.send_config(name, value).await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-12 17:35:25 +08:00
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
pub async fn set_config(name: &str, value: String) -> ResultType<()> {
|
|
|
|
|
set_config_async(name, value).await
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-24 16:41:12 +08:00
|
|
|
|
pub fn update_temporary_password() -> ResultType<()> {
|
|
|
|
|
set_config("temporary-password", "".to_owned())
|
2022-06-20 10:41:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-24 16:41:12 +08:00
|
|
|
|
pub fn get_permanent_password() -> String {
|
|
|
|
|
if let Ok(Some(v)) = get_config("permanent-password") {
|
|
|
|
|
Config::set_permanent_password(&v);
|
|
|
|
|
v
|
2022-06-20 10:41:46 +08:00
|
|
|
|
} else {
|
2022-07-24 16:41:12 +08:00
|
|
|
|
Config::get_permanent_password()
|
2022-06-20 10:41:46 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 14:39:22 +08:00
|
|
|
|
pub fn get_fingerprint() -> String {
|
|
|
|
|
get_config("fingerprint")
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-24 16:41:12 +08:00
|
|
|
|
pub fn set_permanent_password(v: String) -> ResultType<()> {
|
|
|
|
|
Config::set_permanent_password(&v);
|
|
|
|
|
set_config("permanent-password", v)
|
2021-03-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 16:21:38 +08:00
|
|
|
|
#[cfg(feature = "flutter")]
|
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
|
|
|
|
pub fn set_unlock_pin(v: String, translate: bool) -> ResultType<()> {
|
|
|
|
|
let v = v.trim().to_owned();
|
|
|
|
|
let min_len = 4;
|
|
|
|
|
if !v.is_empty() && v.len() < min_len {
|
|
|
|
|
let err = if translate {
|
|
|
|
|
crate::lang::translate(
|
|
|
|
|
"Requires at least {".to_string() + &format!("{min_len}") + "} characters",
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
// Sometimes, translated can't show normally in command line
|
|
|
|
|
format!("Requires at least {} characters", min_len)
|
|
|
|
|
};
|
|
|
|
|
bail!(err);
|
|
|
|
|
}
|
|
|
|
|
Config::set_unlock_pin(&v);
|
|
|
|
|
set_config("unlock-pin", v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "flutter")]
|
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
|
|
|
|
pub fn get_unlock_pin() -> String {
|
|
|
|
|
if let Ok(Some(v)) = get_config("unlock-pin") {
|
|
|
|
|
Config::set_unlock_pin(&v);
|
|
|
|
|
v
|
|
|
|
|
} else {
|
|
|
|
|
Config::get_unlock_pin()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 15:59:14 +08:00
|
|
|
|
pub fn get_id() -> String {
|
|
|
|
|
if let Ok(Some(v)) = get_config("id") {
|
2021-11-14 18:52:05 +03:30
|
|
|
|
// update salt also, so that next time reinstallation not causing first-time auto-login failure
|
2021-03-29 15:59:14 +08:00
|
|
|
|
if let Ok(Some(v2)) = get_config("salt") {
|
|
|
|
|
Config::set_salt(&v2);
|
2021-12-21 00:10:14 +08:00
|
|
|
|
}
|
|
|
|
|
if v != Config::get_id() {
|
|
|
|
|
Config::set_key_confirmed(false);
|
|
|
|
|
Config::set_id(&v);
|
|
|
|
|
}
|
|
|
|
|
v
|
|
|
|
|
} else {
|
|
|
|
|
Config::get_id()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 15:21:26 +08:00
|
|
|
|
pub async fn get_rendezvous_server(ms_timeout: u64) -> (String, Vec<String>) {
|
2021-03-29 15:59:14 +08:00
|
|
|
|
if let Ok(Some(v)) = get_config_async("rendezvous_server", ms_timeout).await {
|
2022-07-14 15:21:26 +08:00
|
|
|
|
let mut urls = v.split(",");
|
|
|
|
|
let a = urls.next().unwrap_or_default().to_owned();
|
|
|
|
|
let b: Vec<String> = urls.map(|x| x.to_owned()).collect();
|
|
|
|
|
(a, b)
|
2022-01-05 13:21:14 +08:00
|
|
|
|
} else {
|
2022-07-14 15:21:26 +08:00
|
|
|
|
(
|
|
|
|
|
Config::get_rendezvous_server(),
|
|
|
|
|
Config::get_rendezvous_servers(),
|
|
|
|
|
)
|
2021-03-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn get_options_(ms_timeout: u64) -> ResultType<HashMap<String, String>> {
|
|
|
|
|
let mut c = connect(ms_timeout, "").await?;
|
|
|
|
|
c.send(&Data::Options(None)).await?;
|
|
|
|
|
if let Some(Data::Options(Some(value))) = c.next_timeout(ms_timeout).await? {
|
|
|
|
|
Config::set_options(value.clone());
|
|
|
|
|
Ok(value)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Config::get_options())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-12 17:35:25 +08:00
|
|
|
|
pub async fn get_options_async() -> HashMap<String, String> {
|
|
|
|
|
get_options_(1000).await.unwrap_or(Config::get_options())
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 19:42:51 +08:00
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
2021-03-29 15:59:14 +08:00
|
|
|
|
pub async fn get_options() -> HashMap<String, String> {
|
2022-05-12 17:35:25 +08:00
|
|
|
|
get_options_async().await
|
2021-03-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-12 17:35:25 +08:00
|
|
|
|
pub async fn get_option_async(key: &str) -> String {
|
|
|
|
|
if let Some(v) = get_options_async().await.get(key) {
|
2021-03-29 15:59:14 +08:00
|
|
|
|
v.clone()
|
|
|
|
|
} else {
|
|
|
|
|
"".to_owned()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_option(key: &str, value: &str) {
|
|
|
|
|
let mut options = get_options();
|
|
|
|
|
if value.is_empty() {
|
|
|
|
|
options.remove(key);
|
|
|
|
|
} else {
|
|
|
|
|
options.insert(key.to_owned(), value.to_owned());
|
|
|
|
|
}
|
|
|
|
|
set_options(options).ok();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 19:42:51 +08:00
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
2021-03-29 15:59:14 +08:00
|
|
|
|
pub async fn set_options(value: HashMap<String, String>) -> ResultType<()> {
|
2022-03-21 00:53:35 +08:00
|
|
|
|
if let Ok(mut c) = connect(1000, "").await {
|
|
|
|
|
c.send(&Data::Options(Some(value.clone()))).await?;
|
|
|
|
|
// do not put below before connect, because we need to check should_exit
|
|
|
|
|
c.next_timeout(1000).await.ok();
|
|
|
|
|
}
|
2022-02-08 18:09:45 +08:00
|
|
|
|
Config::set_options(value);
|
2021-03-29 15:59:14 +08:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
async fn get_nat_type_(ms_timeout: u64) -> ResultType<i32> {
|
|
|
|
|
let mut c = connect(ms_timeout, "").await?;
|
|
|
|
|
c.send(&Data::NatType(None)).await?;
|
|
|
|
|
if let Some(Data::NatType(Some(value))) = c.next_timeout(ms_timeout).await? {
|
|
|
|
|
Config::set_nat_type(value);
|
|
|
|
|
Ok(value)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Config::get_nat_type())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn get_nat_type(ms_timeout: u64) -> i32 {
|
|
|
|
|
get_nat_type_(ms_timeout)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap_or(Config::get_nat_type())
|
|
|
|
|
}
|
2021-08-11 21:08:05 +08:00
|
|
|
|
|
2022-05-12 17:35:25 +08:00
|
|
|
|
pub async fn get_rendezvous_servers(ms_timeout: u64) -> Vec<String> {
|
|
|
|
|
if let Ok(Some(v)) = get_config_async("rendezvous_servers", ms_timeout).await {
|
|
|
|
|
return v.split(',').map(|x| x.to_owned()).collect();
|
|
|
|
|
}
|
|
|
|
|
return Config::get_rendezvous_servers();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 18:34:30 +08:00
|
|
|
|
#[inline]
|
|
|
|
|
async fn get_socks_(ms_timeout: u64) -> ResultType<Option<config::Socks5Server>> {
|
|
|
|
|
let mut c = connect(ms_timeout, "").await?;
|
|
|
|
|
c.send(&Data::Socks(None)).await?;
|
|
|
|
|
if let Some(Data::Socks(value)) = c.next_timeout(ms_timeout).await? {
|
|
|
|
|
Config::set_socks(value.clone());
|
|
|
|
|
Ok(value)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Config::get_socks())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn get_socks_async(ms_timeout: u64) -> Option<config::Socks5Server> {
|
|
|
|
|
get_socks_(ms_timeout).await.unwrap_or(Config::get_socks())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
pub async fn get_socks() -> Option<config::Socks5Server> {
|
|
|
|
|
get_socks_async(1_000).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
pub async fn set_socks(value: config::Socks5Server) -> ResultType<()> {
|
|
|
|
|
Config::set_socks(if value.proxy.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(value.clone())
|
|
|
|
|
});
|
|
|
|
|
connect(1_000, "")
|
|
|
|
|
.await?
|
|
|
|
|
.send(&Data::Socks(Some(value)))
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2022-07-14 15:21:26 +08:00
|
|
|
|
|
2024-04-25 11:46:21 +08:00
|
|
|
|
pub fn get_proxy_status() -> bool {
|
2024-04-28 21:08:49 +08:00
|
|
|
|
Config::get_socks().is_some()
|
2024-04-25 11:46:21 +08:00
|
|
|
|
}
|
2022-07-14 15:21:26 +08:00
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
pub async fn test_rendezvous_server() -> ResultType<()> {
|
|
|
|
|
let mut c = connect(1000, "").await?;
|
|
|
|
|
c.send(&Data::TestRendezvousServer).await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-02-04 11:23:36 +08:00
|
|
|
|
|
2024-05-18 23:14:42 +08:00
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
2023-02-04 11:23:36 +08:00
|
|
|
|
pub async fn send_url_scheme(url: String) -> ResultType<()> {
|
2023-02-08 19:17:59 +08:00
|
|
|
|
connect(1_000, "_url")
|
|
|
|
|
.await?
|
|
|
|
|
.send(&Data::UrlLink(url))
|
|
|
|
|
.await?;
|
2023-02-04 11:23:36 +08:00
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-02-08 19:17:59 +08:00
|
|
|
|
|
2023-05-02 23:48:49 +08:00
|
|
|
|
// Emit `close` events to ipc.
|
|
|
|
|
pub fn close_all_instances() -> ResultType<bool> {
|
|
|
|
|
match crate::ipc::send_url_scheme(IPC_ACTION_CLOSE.to_owned()) {
|
|
|
|
|
Ok(_) => Ok(true),
|
|
|
|
|
Err(err) => Err(err),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 21:29:17 +05:30
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
pub async fn connect_to_user_session(usid: Option<u32>) -> ResultType<()> {
|
|
|
|
|
let mut stream = crate::ipc::connect(1000, crate::POSTFIX_SERVICE).await?;
|
|
|
|
|
timeout(1000, stream.send(&crate::ipc::Data::UserSid(usid))).await??;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-18 13:12:45 +08:00
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
pub async fn notify_server_to_check_hwcodec() -> ResultType<()> {
|
|
|
|
|
connect(1_000, "").await?.send(&&Data::CheckHwcodec).await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
hwcodec, only process that start ipc server start check process (#8325)
check process send config to ipc server, other process get config from ipc server. Process will save config to toml, and the toml will be used if the config is none.
when start check process: ipc server process start or option changed
from disable to enable
when get config: main window start or option changed from disable to
enable, start_video_audio_threads.
Only windows implements signature, which is used to mark whether the gpu software and hardware information changes. After reboot, the signature doesn't change. https://asawicki.info/news_1773_how_to_programmatically_check_graphics_driver_version, use dxgi way to get software version, it's not consistent with the visible driver version, after updating intel driver with small version change, the signature doesn't change. Linux doesn't use toml file.
Signed-off-by: 21pages <sunboeasy@gmail.com>
2024-06-12 20:40:35 +08:00
|
|
|
|
#[cfg(feature = "hwcodec")]
|
2024-07-12 11:08:51 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
hwcodec, only process that start ipc server start check process (#8325)
check process send config to ipc server, other process get config from ipc server. Process will save config to toml, and the toml will be used if the config is none.
when start check process: ipc server process start or option changed
from disable to enable
when get config: main window start or option changed from disable to
enable, start_video_audio_threads.
Only windows implements signature, which is used to mark whether the gpu software and hardware information changes. After reboot, the signature doesn't change. https://asawicki.info/news_1773_how_to_programmatically_check_graphics_driver_version, use dxgi way to get software version, it's not consistent with the visible driver version, after updating intel driver with small version change, the signature doesn't change. Linux doesn't use toml file.
Signed-off-by: 21pages <sunboeasy@gmail.com>
2024-06-12 20:40:35 +08:00
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
pub async fn get_hwcodec_config_from_server() -> ResultType<()> {
|
|
|
|
|
if !scrap::codec::enable_hwcodec_option() || scrap::hwcodec::HwCodecConfig::already_set() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
let mut c = connect(50, "").await?;
|
|
|
|
|
c.send(&Data::HwCodecConfig(None)).await?;
|
|
|
|
|
if let Some(Data::HwCodecConfig(v)) = c.next_timeout(50).await? {
|
|
|
|
|
match v {
|
|
|
|
|
Some(v) => {
|
|
|
|
|
scrap::hwcodec::HwCodecConfig::set(v);
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
bail!("hwcodec config is none");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
bail!("failed to get hwcodec config");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "hwcodec")]
|
2024-07-12 11:08:51 +08:00
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
hwcodec, only process that start ipc server start check process (#8325)
check process send config to ipc server, other process get config from ipc server. Process will save config to toml, and the toml will be used if the config is none.
when start check process: ipc server process start or option changed
from disable to enable
when get config: main window start or option changed from disable to
enable, start_video_audio_threads.
Only windows implements signature, which is used to mark whether the gpu software and hardware information changes. After reboot, the signature doesn't change. https://asawicki.info/news_1773_how_to_programmatically_check_graphics_driver_version, use dxgi way to get software version, it's not consistent with the visible driver version, after updating intel driver with small version change, the signature doesn't change. Linux doesn't use toml file.
Signed-off-by: 21pages <sunboeasy@gmail.com>
2024-06-12 20:40:35 +08:00
|
|
|
|
pub fn client_get_hwcodec_config_thread(wait_sec: u64) {
|
|
|
|
|
static ONCE: std::sync::Once = std::sync::Once::new();
|
|
|
|
|
if !crate::platform::is_installed()
|
|
|
|
|
|| !scrap::codec::enable_hwcodec_option()
|
|
|
|
|
|| scrap::hwcodec::HwCodecConfig::already_set()
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ONCE.call_once(move || {
|
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
|
let mut intervals: Vec<u64> = vec![wait_sec, 3, 3, 6, 9];
|
|
|
|
|
for i in intervals.drain(..) {
|
|
|
|
|
if i > 0 {
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(i));
|
|
|
|
|
}
|
|
|
|
|
if get_hwcodec_config_from_server().is_ok() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "hwcodec")]
|
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
pub async fn hwcodec_process() {
|
|
|
|
|
let s = scrap::hwcodec::check_available_hwcodec();
|
|
|
|
|
for _ in 0..5 {
|
|
|
|
|
match crate::ipc::connect(1000, "").await {
|
|
|
|
|
Ok(mut conn) => {
|
|
|
|
|
match conn
|
|
|
|
|
.send(&crate::ipc::Data::HwCodecConfig(Some(s.clone())))
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
Ok(()) => {
|
|
|
|
|
log::info!("send ok");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log::error!("send failed: {e:?}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log::error!("connect failed: {e:?}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 11:03:59 +08:00
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
pub async fn get_wayland_screencast_restore_token(key: String) -> ResultType<String> {
|
|
|
|
|
let v = handle_wayland_screencast_restore_token(key, "get".to_owned()).await?;
|
|
|
|
|
Ok(v.unwrap_or_default())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
pub async fn clear_wayland_screencast_restore_token(key: String) -> ResultType<bool> {
|
|
|
|
|
if let Some(v) = handle_wayland_screencast_restore_token(key, "clear".to_owned()).await? {
|
|
|
|
|
return Ok(v.is_empty());
|
|
|
|
|
}
|
|
|
|
|
return Ok(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn handle_wayland_screencast_restore_token(
|
|
|
|
|
key: String,
|
|
|
|
|
value: String,
|
|
|
|
|
) -> ResultType<Option<String>> {
|
|
|
|
|
let ms_timeout = 1_000;
|
|
|
|
|
let mut c = connect(ms_timeout, "").await?;
|
|
|
|
|
c.send(&Data::WaylandScreencastRestoreToken((key, value)))
|
|
|
|
|
.await?;
|
|
|
|
|
if let Some(Data::WaylandScreencastRestoreToken((_key, v))) = c.next_timeout(ms_timeout).await?
|
|
|
|
|
{
|
|
|
|
|
return Ok(Some(v));
|
|
|
|
|
}
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 19:17:59 +08:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use super::*;
|
|
|
|
|
#[test]
|
|
|
|
|
fn verify_ffi_enum_data_size() {
|
|
|
|
|
println!("{}", std::mem::size_of::<Data>());
|
|
|
|
|
assert!(std::mem::size_of::<Data>() < 96);
|
|
|
|
|
}
|
|
|
|
|
}
|