refactor(webrdp): js bridge refactor

This commit is contained in:
qwqVictor 2023-08-29 22:52:42 +08:00
parent 552c52b4ba
commit 130d32cc8c
3 changed files with 12 additions and 46 deletions

View File

@ -4,23 +4,17 @@ mod rdp_ws;
mod utils; mod utils;
use rdp_ws::Rdp; use rdp_ws::Rdp;
use tracing::warn;
use tracing_wasm::WASMLayerConfigBuilder; use tracing_wasm::WASMLayerConfigBuilder;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local; use wasm_bindgen_futures::spawn_local;
#[wasm_bindgen] #[wasm_bindgen]
extern "C" { extern "C" {
fn alert(s: &str);
pub fn setClipBoard(s: String); pub fn setClipBoard(s: String);
pub fn getClipBoard() -> String; pub fn getClipBoard() -> String;
fn prompt(msg: &str) -> String; fn wrongCredential();
} pub fn disconnectedAndRefresh(msg: Option<&str>);
fn jsExportedCredentials(key: &str) -> String;
fn read_credentials(user: &mut String, password: &mut String, domain: &mut String) {
*user = prompt("User:");
*password = prompt("Password:");
*domain = prompt("Domain:");
} }
fn start_websocket() -> Result<(), JsValue> { fn start_websocket() -> Result<(), JsValue> {
@ -41,15 +35,12 @@ fn start_websocket() -> Result<(), JsValue> {
); );
spawn_local(async move { spawn_local(async move {
let mut username = String::new(); let username = jsExportedCredentials("user");
let mut password = String::new(); let password = jsExportedCredentials("password");
let mut domain = String::new(); let domain = jsExportedCredentials("domain");
read_credentials(&mut username, &mut password, &mut domain);
let mut rdp = Rdp::new(&url, &username, &password, &domain); let mut rdp = Rdp::new(&url, &username, &password, &domain);
while !rdp.start().await { if !rdp.start().await {
warn!("Wrong credientials"); wrongCredential();
read_credentials(&mut username, &mut password, &mut domain);
rdp = Rdp::new(&url, &username, &password, &domain);
} }
rdp.main_loop().await rdp.main_loop().await
}); });

View File

@ -9,13 +9,12 @@ use rdp::{
}; };
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tracing::{info, warn}; use tracing::{info, warn};
use web_sys::Element; use crate::disconnectedAndRefresh;
const RDP_HOSTNAME: &str = "webrdp"; const RDP_HOSTNAME: &str = "webrdp";
pub struct Rdp { pub struct Rdp {
url: String, url: String,
status_bar: Element,
username: String, username: String,
password: String, password: String,
domain: String, domain: String,
@ -25,12 +24,6 @@ pub struct Rdp {
impl Rdp { impl Rdp {
pub fn new(url: &str, username: &str, password: &str, domain: &str) -> Self { pub fn new(url: &str, username: &str, password: &str, domain: &str) -> Self {
let status_bar = web_sys::window()
.unwrap()
.document()
.unwrap()
.get_element_by_id("rdp_status")
.unwrap();
let body = web_sys::window() let body = web_sys::window()
.unwrap() .unwrap()
.document() .document()
@ -42,7 +35,6 @@ impl Rdp {
let width = body.client_width() as u16; let width = body.client_width() as u16;
Self { Self {
url: url.to_owned(), url: url.to_owned(),
status_bar,
username: username.to_owned(), username: username.to_owned(),
password: password.to_owned(), password: password.to_owned(),
domain: domain.to_owned(), domain: domain.to_owned(),
@ -51,18 +43,6 @@ impl Rdp {
} }
} }
pub fn set_user(&mut self, username: &str) {
self.username = username.to_owned();
}
pub fn set_password(&mut self, password: &str) {
self.password = password.to_owned();
}
pub fn set_domain(&mut self, domain: &str) {
self.domain = domain.to_owned();
}
pub async fn start(&mut self) -> bool { pub async fn start(&mut self) -> bool {
let ws_stream = WsSecureBio::new(&self.url).await; let ws_stream = WsSecureBio::new(&self.url).await;
@ -127,6 +107,6 @@ impl Rdp {
} }
fn disconnect_with_msg(&self, msg: &str) { fn disconnect_with_msg(&self, msg: &str) {
self.status_bar.set_text_content(Some(msg)); disconnectedAndRefresh(Some(msg));
} }
} }

View File

@ -6,6 +6,7 @@ use tracing::{info, trace};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
use ws_stream_wasm::*; use ws_stream_wasm::*;
use crate::disconnectedAndRefresh;
pub type WsStream = IoStream<WsStreamIo, Vec<u8>>; pub type WsStream = IoStream<WsStreamIo, Vec<u8>>;
@ -44,13 +45,7 @@ impl WsSecureBio {
let onclose_callback = Closure::<dyn FnMut()>::new(move || { let onclose_callback = Closure::<dyn FnMut()>::new(move || {
info!("socket close"); info!("socket close");
let status_bar = web_sys::window() disconnectedAndRefresh(None);
.unwrap()
.document()
.unwrap()
.get_element_by_id("rdp_status")
.unwrap();
status_bar.set_text_content(Some("Server Disconnected"));
panic!("Closed"); panic!("Closed");
}); });