2024-08-07 08:37:14 +08:00
|
|
|
use clap::{App, Arg};
|
2024-07-24 13:19:40 +08:00
|
|
|
use lws_client::LwsVfsIns;
|
|
|
|
use std::thread;
|
2024-08-04 17:57:26 +08:00
|
|
|
extern crate log;
|
|
|
|
|
2024-08-06 08:38:35 +08:00
|
|
|
use std::env;
|
|
|
|
|
|
|
|
const DEFAULT_PORT: u16 = 33444;
|
2024-08-07 08:37:14 +08:00
|
|
|
const DEFAULT_CACHE_LIFE: u32 = 10_000;
|
2024-08-06 08:38:35 +08:00
|
|
|
// use std::process::{Command,Stdio};
|
|
|
|
// fn get_ssh_clinet() -> String {
|
|
|
|
// // Run `who` command
|
|
|
|
// let who = Command::new("who")
|
|
|
|
// .stdout(Stdio::piped())
|
|
|
|
// .spawn().unwrap();
|
|
|
|
|
|
|
|
// // Run `grep $(whoami)` command
|
|
|
|
// let whoami = Command::new("whoami")
|
|
|
|
// .stdout(Stdio::piped())
|
|
|
|
// .output().unwrap();
|
|
|
|
// let user = String::from_utf8_lossy(&whoami.stdout).trim().to_string();
|
2024-08-07 08:37:14 +08:00
|
|
|
|
2024-08-06 08:38:35 +08:00
|
|
|
// let grep = Command::new("grep")
|
|
|
|
// .arg(&user)
|
|
|
|
// .stdin(Stdio::from(who.stdout.unwrap()))
|
|
|
|
// .stdout(Stdio::piped())
|
|
|
|
// .spawn().unwrap();
|
|
|
|
|
|
|
|
// // Run `awk '{print $5}'` command
|
|
|
|
// let awk = Command::new("awk")
|
|
|
|
// .arg("{print $5}")
|
|
|
|
// .stdin(Stdio::from(grep.stdout.unwrap()))
|
|
|
|
// .stdout(Stdio::piped())
|
|
|
|
// .spawn().unwrap();
|
|
|
|
|
|
|
|
// // Collect the output
|
|
|
|
// let output = awk.wait_with_output().unwrap();
|
|
|
|
// let output = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
// println!("output: {}",output);
|
|
|
|
// let mut iter = output.split("(");
|
|
|
|
// iter.next();
|
|
|
|
// let output = iter.next().unwrap().split(")").collect::<Vec<&str>>()[0].to_string();
|
|
|
|
// println!("output: {}",output);
|
|
|
|
// output
|
|
|
|
// }
|
|
|
|
|
|
|
|
fn get_ssh_clinet() -> String {
|
|
|
|
// 获取特定环境变量的值
|
|
|
|
let client = env::var("SSH_CLIENT").expect("only support auto get ssh connection");
|
|
|
|
let client = client.split(' ').next().unwrap();
|
|
|
|
client.to_string()
|
|
|
|
}
|
|
|
|
|
2024-08-07 08:37:14 +08:00
|
|
|
fn param_parser() -> (String, String, u32) {
|
2024-08-06 08:38:35 +08:00
|
|
|
let matches = App::new("lws_client")
|
2024-08-07 08:37:14 +08:00
|
|
|
.version("1.0")
|
|
|
|
.author("Ekko.bao")
|
|
|
|
.about("linux&windows shared filesystem")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("s")
|
|
|
|
.short('s')
|
|
|
|
.long("server")
|
|
|
|
.takes_value(true)
|
|
|
|
.min_values(1)
|
|
|
|
.max_values(1)
|
|
|
|
.value_name("server")
|
|
|
|
.help(
|
|
|
|
format!(
|
|
|
|
"server addr: [ip[:port]], default is [sshclient:{}]",
|
|
|
|
DEFAULT_PORT
|
|
|
|
)
|
|
|
|
.as_str(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("m")
|
|
|
|
.short('m')
|
|
|
|
.long("mount")
|
|
|
|
.takes_value(true)
|
|
|
|
.min_values(1)
|
|
|
|
.max_values(1)
|
|
|
|
.value_name("mount point")
|
|
|
|
.required(true)
|
|
|
|
.help("mount point, eg: ~/mnt"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("c")
|
|
|
|
.short('c')
|
|
|
|
.long("cache")
|
|
|
|
.takes_value(true)
|
|
|
|
.min_values(1)
|
|
|
|
.max_values(1)
|
|
|
|
.value_name("cache invalid time")
|
|
|
|
.help("file info cache invalid time, unit: ms, defualt is 10_000ms"),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
let (ip, port) = match matches.value_of("s") {
|
2024-08-06 08:38:35 +08:00
|
|
|
Some(server) => {
|
|
|
|
let split = server.split(":").collect::<Vec<&str>>();
|
|
|
|
if split.len() == 2 {
|
2024-08-07 08:37:14 +08:00
|
|
|
(split[0].to_string(), split[1].to_string())
|
2024-08-06 08:38:35 +08:00
|
|
|
} else {
|
2024-08-07 08:37:14 +08:00
|
|
|
(split[0].to_string(), DEFAULT_PORT.to_string())
|
2024-08-06 08:38:35 +08:00
|
|
|
}
|
|
|
|
}
|
2024-08-07 08:37:14 +08:00
|
|
|
None => (String::new(), String::new()),
|
|
|
|
};
|
|
|
|
let ip = if ip.len() == 0 { get_ssh_clinet() } else { ip };
|
|
|
|
let port = if port.len() == 0 {
|
|
|
|
DEFAULT_PORT.to_string()
|
|
|
|
} else {
|
|
|
|
port
|
2024-08-06 08:38:35 +08:00
|
|
|
};
|
2024-08-07 08:37:14 +08:00
|
|
|
let server = format!("{}:{}", ip, port);
|
2024-08-06 08:38:35 +08:00
|
|
|
log::info!("args server: [{}]", server);
|
|
|
|
let mount_point = matches.value_of("m").unwrap().to_string();
|
|
|
|
log::info!("args mount_point: [{}]", mount_point);
|
2024-08-07 08:37:14 +08:00
|
|
|
let cache_life = match matches.value_of("c") {
|
|
|
|
Some(cache_life) => cache_life.parse().unwrap(),
|
|
|
|
None => DEFAULT_CACHE_LIFE,
|
|
|
|
};
|
|
|
|
log::info!("args cache invalid time: [{}ms]", cache_life);
|
|
|
|
(server, mount_point, cache_life)
|
2024-08-06 08:38:35 +08:00
|
|
|
}
|
|
|
|
|
2024-07-24 13:19:40 +08:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2024-08-04 17:57:26 +08:00
|
|
|
env_logger::init();
|
2024-08-07 08:37:14 +08:00
|
|
|
let (server, mount_point, cache_life) = param_parser();
|
|
|
|
let lws_ins = match LwsVfsIns::new(&server, cache_life).await {
|
2024-07-24 13:19:40 +08:00
|
|
|
Ok(ins) => ins,
|
|
|
|
Err(e) => {
|
2024-08-04 17:57:26 +08:00
|
|
|
log::error!("Error creating lws server instance: {:?}", e);
|
2024-07-24 13:19:40 +08:00
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
};
|
2024-08-04 17:57:26 +08:00
|
|
|
log::info!("lws client instance created");
|
2024-07-24 13:19:40 +08:00
|
|
|
match lws_ins.hello().await {
|
|
|
|
Err(e) => {
|
2024-08-04 17:57:26 +08:00
|
|
|
log::error!("lws client instance hello err {:?}", e);
|
2024-07-24 13:19:40 +08:00
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2024-08-04 17:57:26 +08:00
|
|
|
log::info!("start mount process");
|
2024-08-07 08:37:14 +08:00
|
|
|
let handle = thread::spawn(move || match LwsVfsIns::mount(&mount_point, lws_ins) {
|
|
|
|
Ok(_) => Ok::<i32, String>(0),
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("mount err {:?}", e);
|
|
|
|
Ok::<i32, String>(-1)
|
2024-07-24 13:19:40 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
match handle.join() {
|
2024-08-07 08:37:14 +08:00
|
|
|
Ok(_) => Ok(()),
|
2024-07-24 13:19:40 +08:00
|
|
|
Err(e) => {
|
2024-08-04 17:57:26 +08:00
|
|
|
log::error!("mount thread start err {:?}", e);
|
2024-08-07 08:37:14 +08:00
|
|
|
Err(Box::new(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"mount fail",
|
|
|
|
)))
|
2024-07-24 13:19:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|