2021-03-29 15:59:14 +08:00
|
|
|
use crate::client::*;
|
|
|
|
use hbb_common::{
|
|
|
|
config::PeerConfig,
|
2022-12-29 23:27:17 +08:00
|
|
|
config::READ_TIMEOUT,
|
|
|
|
futures::{SinkExt, StreamExt},
|
2021-03-29 15:59:14 +08:00
|
|
|
log,
|
|
|
|
message_proto::*,
|
|
|
|
protobuf::Message as _,
|
2022-12-29 11:18:03 +08:00
|
|
|
rendezvous_proto::ConnType,
|
2021-03-29 15:59:14 +08:00
|
|
|
tokio::{self, sync::mpsc},
|
|
|
|
Stream,
|
|
|
|
};
|
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Session {
|
|
|
|
id: String,
|
|
|
|
lc: Arc<RwLock<LoginConfigHandler>>,
|
|
|
|
sender: mpsc::UnboundedSender<Data>,
|
|
|
|
password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Session {
|
|
|
|
pub fn new(id: &str, sender: mpsc::UnboundedSender<Data>) -> Self {
|
|
|
|
let mut password = "".to_owned();
|
|
|
|
if PeerConfig::load(id).password.is_empty() {
|
2022-06-07 11:25:34 +08:00
|
|
|
password = rpassword::prompt_password("Enter password: ").unwrap();
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
let session = Self {
|
|
|
|
id: id.to_owned(),
|
|
|
|
sender,
|
|
|
|
password,
|
|
|
|
lc: Default::default(),
|
|
|
|
};
|
|
|
|
session
|
|
|
|
.lc
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
2023-02-10 17:09:31 +08:00
|
|
|
.initialize(id.to_owned(), ConnType::PORT_FORWARD, None);
|
2021-03-29 15:59:14 +08:00
|
|
|
session
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Interface for Session {
|
2022-12-29 00:02:31 +08:00
|
|
|
fn get_login_config_handler(&self) -> Arc<RwLock<LoginConfigHandler>> {
|
|
|
|
return self.lc.clone();
|
|
|
|
}
|
2022-12-29 11:18:03 +08:00
|
|
|
|
2022-12-29 00:02:31 +08:00
|
|
|
fn msgbox(&self, msgtype: &str, title: &str, text: &str, link: &str) {
|
2023-04-01 09:39:34 +08:00
|
|
|
match msgtype {
|
|
|
|
"input-password" => {
|
|
|
|
self.sender
|
|
|
|
.send(Data::Login((self.password.clone(), true)))
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
"re-input-password" => {
|
|
|
|
log::error!("{}: {}", title, text);
|
|
|
|
let password = rpassword::prompt_password("Enter password: ").unwrap();
|
|
|
|
let login_data = Data::Login((password, true));
|
|
|
|
self.sender.send(login_data).ok();
|
|
|
|
}
|
|
|
|
msg if msg.contains("error") => {
|
|
|
|
log::error!("{}: {}: {}", msgtype, title, text);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
log::info!("{}: {}: {}", msgtype, title, text);
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_login_error(&mut self, err: &str) -> bool {
|
2022-12-27 11:42:48 +08:00
|
|
|
handle_login_error(self.lc.clone(), err, self)
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_peer_info(&mut self, pi: PeerInfo) {
|
2022-12-29 00:02:31 +08:00
|
|
|
self.lc.write().unwrap().handle_peer_info(&pi);
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
|
2022-12-29 00:02:31 +08:00
|
|
|
async fn handle_hash(&mut self, pass: &str, hash: Hash, peer: &mut Stream) {
|
2023-01-10 14:41:25 +08:00
|
|
|
log::info!(
|
2023-01-10 15:01:46 +08:00
|
|
|
"password={}",
|
2023-01-10 14:41:25 +08:00
|
|
|
hbb_common::password_security::temporary_password()
|
|
|
|
);
|
2022-12-29 00:02:31 +08:00
|
|
|
handle_hash(self.lc.clone(), &pass, hash, self, peer).await;
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
|
2023-03-22 17:01:11 +08:00
|
|
|
async fn handle_login_from_ui(&mut self, os_username: String, os_password: String, password: String, remember: bool, peer: &mut Stream) {
|
|
|
|
handle_login_from_ui(self.lc.clone(), os_username, os_password, password, remember, peer).await;
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_test_delay(&mut self, t: TestDelay, peer: &mut Stream) {
|
|
|
|
handle_test_delay(t, peer).await;
|
|
|
|
}
|
2022-06-07 11:25:34 +08:00
|
|
|
|
|
|
|
fn send(&self, data: Data) {
|
|
|
|
self.sender.send(data).ok();
|
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
|
2022-12-29 11:18:03 +08:00
|
|
|
#[tokio::main(flavor = "current_thread")]
|
2022-12-29 23:27:17 +08:00
|
|
|
pub async fn connect_test(id: &str, key: String, token: String) {
|
2022-12-29 11:18:03 +08:00
|
|
|
let (sender, mut receiver) = mpsc::unbounded_channel::<Data>();
|
|
|
|
let handler = Session::new(&id, sender);
|
2022-12-29 23:27:17 +08:00
|
|
|
match crate::client::Client::start(id, &key, &token, ConnType::PORT_FORWARD, handler).await {
|
|
|
|
Err(err) => {
|
|
|
|
log::error!("Failed to connect {}: {}", &id, err);
|
|
|
|
}
|
|
|
|
Ok((mut stream, direct)) => {
|
|
|
|
log::info!("direct: {}", direct);
|
|
|
|
// rpassword::prompt_password("Input anything to exit").ok();
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
res = hbb_common::timeout(READ_TIMEOUT, stream.next()) => match res {
|
|
|
|
Err(_) => {
|
|
|
|
log::error!("Timeout");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(Some(Ok(bytes))) => {
|
|
|
|
let msg_in = Message::parse_from_bytes(&bytes).unwrap();
|
|
|
|
match msg_in.union {
|
|
|
|
Some(message::Union::Hash(hash)) => {
|
|
|
|
log::info!("Got hash");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-29 11:18:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 19:42:51 +08:00
|
|
|
#[tokio::main(flavor = "current_thread")]
|
2022-05-12 17:35:25 +08:00
|
|
|
pub async fn start_one_port_forward(
|
|
|
|
id: String,
|
|
|
|
port: i32,
|
|
|
|
remote_host: String,
|
|
|
|
remote_port: i32,
|
|
|
|
key: String,
|
2022-06-07 11:25:34 +08:00
|
|
|
token: String,
|
2022-05-12 17:35:25 +08:00
|
|
|
) {
|
2021-03-29 15:59:14 +08:00
|
|
|
crate::common::test_rendezvous_server();
|
|
|
|
crate::common::test_nat_type();
|
|
|
|
let (sender, mut receiver) = mpsc::unbounded_channel::<Data>();
|
|
|
|
let handler = Session::new(&id, sender);
|
2022-12-29 00:02:31 +08:00
|
|
|
if let Err(err) = crate::port_forward::listen(
|
|
|
|
handler.id.clone(),
|
|
|
|
handler.password.clone(),
|
|
|
|
port,
|
|
|
|
handler.clone(),
|
|
|
|
receiver,
|
|
|
|
&key,
|
|
|
|
&token,
|
|
|
|
handler.lc.clone(),
|
|
|
|
remote_host,
|
|
|
|
remote_port,
|
|
|
|
)
|
|
|
|
.await
|
2021-03-29 15:59:14 +08:00
|
|
|
{
|
|
|
|
log::error!("Failed to listen on {}: {}", port, err);
|
|
|
|
}
|
|
|
|
log::info!("port forward (:{}) exit", port);
|
|
|
|
}
|