2024-07-24 13:19:40 +08:00
|
|
|
use lws_client::LwsVfsIns;
|
|
|
|
use std::thread;
|
2024-08-06 08:38:35 +08:00
|
|
|
use clap::{Arg, App};
|
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;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn param_parser() -> (String, String) {
|
|
|
|
let matches = App::new("lws_client")
|
|
|
|
.version("1.0")
|
|
|
|
.author("Ekko.bao")
|
|
|
|
.about("linux&windows shared filesystem")
|
|
|
|
.arg(Arg::with_name("s")
|
|
|
|
.short('s')
|
|
|
|
.long("server")
|
|
|
|
.takes_value(true)
|
|
|
|
.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)
|
|
|
|
.value_name("mount point")
|
|
|
|
.required(true)
|
|
|
|
.help("mount point, eg: ~/mnt"))
|
|
|
|
.get_matches();
|
|
|
|
let server = match matches.value_of("s") {
|
|
|
|
Some(server) => {
|
|
|
|
let split = server.split(":").collect::<Vec<&str>>();
|
|
|
|
if split.len() == 2 {
|
|
|
|
server.to_string()
|
|
|
|
} else {
|
|
|
|
format!("{}:{}", split[0], DEFAULT_PORT)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
let ip = get_ssh_clinet();
|
|
|
|
format!("{}:{}", ip, DEFAULT_PORT)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
log::info!("args server: [{}]", server);
|
|
|
|
let mount_point = matches.value_of("m").unwrap().to_string();
|
|
|
|
log::info!("args mount_point: [{}]", mount_point);
|
|
|
|
(server, mount_point)
|
|
|
|
}
|
|
|
|
|
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-06 08:38:35 +08:00
|
|
|
let (server, mount_point) = param_parser();
|
|
|
|
let lws_ins = match LwsVfsIns::new(&server).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-07-24 13:19:40 +08:00
|
|
|
let handle = thread::spawn(move ||{
|
2024-08-06 08:38:35 +08:00
|
|
|
match LwsVfsIns::mount(&mount_point, lws_ins) {
|
2024-07-24 13:19:40 +08:00
|
|
|
Ok(_) => {
|
|
|
|
Ok::<i32, String>(0)
|
|
|
|
},
|
|
|
|
Err(e) => {
|
2024-08-04 17:57:26 +08:00
|
|
|
log::error!("mount err {:?}", e);
|
2024-07-24 13:19:40 +08:00
|
|
|
Ok::<i32, String>(-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
match handle.join() {
|
|
|
|
Ok(_) => {
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
Err(e) => {
|
2024-08-04 17:57:26 +08:00
|
|
|
log::error!("mount thread start err {:?}", e);
|
2024-07-24 13:19:40 +08:00
|
|
|
Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "mount fail")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|