opt flink creation

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2023-01-29 17:36:37 +08:00
parent 61389bc11f
commit fb81f206b7
2 changed files with 38 additions and 40 deletions

View File

@ -1745,3 +1745,13 @@ pub fn create_process_with_logon(user: &str, pwd: &str, exe: &str, arg: &str) ->
} }
return Ok(()); return Ok(());
} }
pub fn set_path_permission(dir: &PathBuf, permission: &str) -> ResultType<()> {
std::process::Command::new("icacls")
.arg(dir.as_os_str())
.arg("/grant")
.arg(format!("Everyone:(OI)(CI){}", permission))
.arg("/T")
.spawn()?;
Ok(())
}

View File

@ -2,9 +2,7 @@ use core::slice;
use hbb_common::{ use hbb_common::{
allow_err, allow_err,
anyhow::anyhow, anyhow::anyhow,
bail, bail, log,
config::Config,
log,
message_proto::{KeyEvent, MouseEvent}, message_proto::{KeyEvent, MouseEvent},
protobuf::Message, protobuf::Message,
tokio::{self, sync::mpsc}, tokio::{self, sync::mpsc},
@ -15,6 +13,7 @@ use shared_memory::*;
use std::{ use std::{
mem::size_of, mem::size_of,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
path::PathBuf,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
time::Duration, time::Duration,
}; };
@ -25,6 +24,7 @@ use winapi::{
use crate::{ use crate::{
ipc::{self, new_listener, Connection, Data, DataPortableService}, ipc::{self, new_listener, Connection, Data, DataPortableService},
platform::set_path_permission,
video_service::get_current_display, video_service::get_current_display,
}; };
@ -72,7 +72,7 @@ impl DerefMut for SharedMemory {
impl SharedMemory { impl SharedMemory {
pub fn create(name: &str, size: usize) -> ResultType<Self> { pub fn create(name: &str, size: usize) -> ResultType<Self> {
let flink = Self::flink(name.to_string()); let flink = Self::flink(name.to_string())?;
let shmem = match ShmemConf::new() let shmem = match ShmemConf::new()
.size(size) .size(size)
.flink(&flink) .flink(&flink)
@ -91,12 +91,12 @@ impl SharedMemory {
} }
}; };
log::info!("Create shared memory, size:{}, flink:{}", size, flink); log::info!("Create shared memory, size:{}, flink:{}", size, flink);
Self::set_all_perm(&flink); set_path_permission(&PathBuf::from(flink), "F").ok();
Ok(SharedMemory { inner: shmem }) Ok(SharedMemory { inner: shmem })
} }
pub fn open_existing(name: &str) -> ResultType<Self> { pub fn open_existing(name: &str) -> ResultType<Self> {
let flink = Self::flink(name.to_string()); let flink = Self::flink(name.to_string())?;
let shmem = match ShmemConf::new().flink(&flink).allow_raw(true).open() { let shmem = match ShmemConf::new().flink(&flink).allow_raw(true).open() {
Ok(m) => m, Ok(m) => m,
Err(e) => { Err(e) => {
@ -116,30 +116,29 @@ impl SharedMemory {
} }
} }
fn flink(name: String) -> String { fn flink(name: String) -> ResultType<String> {
let mut shmem_flink = format!("shared_memory{}", name); let disk = std::env::var("SystemDrive").unwrap_or("C:".to_string());
if cfg!(windows) { let mut dir = PathBuf::from(disk);
let df = "C:\\ProgramData"; let dir1 = dir.join("ProgramData");
let df = if std::path::Path::new(df).exists() { let dir2 = std::env::var("TEMP")
df.to_owned() .map(|d| PathBuf::from(d))
} else { .unwrap_or(dir.join("Windows").join("Temp"));
std::env::var("TEMP").unwrap_or("C:\\Windows\\TEMP".to_owned()) if dir1.exists() {
}; dir = dir1;
let df = format!("{}\\{}", df, *hbb_common::config::APP_NAME.read().unwrap()); } else if dir2.exists() {
std::fs::create_dir(&df).ok(); dir = dir2;
shmem_flink = format!("{}\\{}", df, shmem_flink);
} else { } else {
shmem_flink = Config::ipc_path("").replace("ipc", "") + &shmem_flink; bail!("no vaild flink directory");
} }
return shmem_flink; dir = dir.join(hbb_common::config::APP_NAME.read().unwrap().clone());
} if !dir.exists() {
std::fs::create_dir(&dir)?;
fn set_all_perm(_p: &str) { set_path_permission(&dir, "F").ok();
#[cfg(not(windows))]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(_p, std::fs::Permissions::from_mode(0o0777)).ok();
} }
Ok(dir
.join(format!("shared_memory{}", name))
.to_string_lossy()
.to_string())
} }
} }
@ -451,7 +450,6 @@ pub mod server {
// functions called in main process. // functions called in main process.
pub mod client { pub mod client {
use hbb_common::anyhow::Context; use hbb_common::anyhow::Context;
use std::path::PathBuf;
use super::*; use super::*;
@ -515,7 +513,7 @@ pub mod client {
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
{ {
if let Some(dir) = PathBuf::from(&exe).parent() { if let Some(dir) = PathBuf::from(&exe).parent() {
if !set_dir_permission(&PathBuf::from(dir)) { if set_path_permission(&PathBuf::from(dir), "RX").is_err() {
*SHMEM.lock().unwrap() = None; *SHMEM.lock().unwrap() = None;
bail!("Failed to set permission of {:?}", dir); bail!("Failed to set permission of {:?}", dir);
} }
@ -533,7 +531,7 @@ pub mod client {
let dst = dir.join("rustdesk.exe"); let dst = dir.join("rustdesk.exe");
if std::fs::copy(&exe, &dst).is_ok() { if std::fs::copy(&exe, &dst).is_ok() {
if dst.exists() { if dst.exists() {
if set_dir_permission(&dir) { if set_path_permission(&dir, "RX").is_ok() {
exe = dst.to_string_lossy().to_string(); exe = dst.to_string_lossy().to_string();
} }
} }
@ -566,16 +564,6 @@ pub mod client {
*QUICK_SUPPORT.lock().unwrap() = v; *QUICK_SUPPORT.lock().unwrap() = v;
} }
fn set_dir_permission(dir: &PathBuf) -> bool {
// // give Everyone RX permission
std::process::Command::new("icacls")
.arg(dir.as_os_str())
.arg("/grant")
.arg("Everyone:(OI)(CI)RX")
.arg("/T")
.spawn()
.is_ok()
}
pub struct CapturerPortable; pub struct CapturerPortable;
impl CapturerPortable { impl CapturerPortable {