refactor(webrdp): js bridge refactor
This commit is contained in:
parent
552c52b4ba
commit
130d32cc8c
@ -4,23 +4,17 @@ mod rdp_ws;
|
||||
mod utils;
|
||||
|
||||
use rdp_ws::Rdp;
|
||||
use tracing::warn;
|
||||
use tracing_wasm::WASMLayerConfigBuilder;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen_futures::spawn_local;
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
fn alert(s: &str);
|
||||
pub fn setClipBoard(s: String);
|
||||
pub fn getClipBoard() -> String;
|
||||
fn prompt(msg: &str) -> String;
|
||||
}
|
||||
|
||||
fn read_credentials(user: &mut String, password: &mut String, domain: &mut String) {
|
||||
*user = prompt("User:");
|
||||
*password = prompt("Password:");
|
||||
*domain = prompt("Domain:");
|
||||
fn wrongCredential();
|
||||
pub fn disconnectedAndRefresh(msg: Option<&str>);
|
||||
fn jsExportedCredentials(key: &str) -> String;
|
||||
}
|
||||
|
||||
fn start_websocket() -> Result<(), JsValue> {
|
||||
@ -41,15 +35,12 @@ fn start_websocket() -> Result<(), JsValue> {
|
||||
);
|
||||
|
||||
spawn_local(async move {
|
||||
let mut username = String::new();
|
||||
let mut password = String::new();
|
||||
let mut domain = String::new();
|
||||
read_credentials(&mut username, &mut password, &mut domain);
|
||||
let username = jsExportedCredentials("user");
|
||||
let password = jsExportedCredentials("password");
|
||||
let domain = jsExportedCredentials("domain");
|
||||
let mut rdp = Rdp::new(&url, &username, &password, &domain);
|
||||
while !rdp.start().await {
|
||||
warn!("Wrong credientials");
|
||||
read_credentials(&mut username, &mut password, &mut domain);
|
||||
rdp = Rdp::new(&url, &username, &password, &domain);
|
||||
if !rdp.start().await {
|
||||
wrongCredential();
|
||||
}
|
||||
rdp.main_loop().await
|
||||
});
|
||||
|
@ -9,13 +9,12 @@ use rdp::{
|
||||
};
|
||||
use tokio::sync::mpsc;
|
||||
use tracing::{info, warn};
|
||||
use web_sys::Element;
|
||||
use crate::disconnectedAndRefresh;
|
||||
|
||||
const RDP_HOSTNAME: &str = "webrdp";
|
||||
|
||||
pub struct Rdp {
|
||||
url: String,
|
||||
status_bar: Element,
|
||||
username: String,
|
||||
password: String,
|
||||
domain: String,
|
||||
@ -25,12 +24,6 @@ pub struct Rdp {
|
||||
|
||||
impl Rdp {
|
||||
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()
|
||||
.unwrap()
|
||||
.document()
|
||||
@ -42,7 +35,6 @@ impl Rdp {
|
||||
let width = body.client_width() as u16;
|
||||
Self {
|
||||
url: url.to_owned(),
|
||||
status_bar,
|
||||
username: username.to_owned(),
|
||||
password: password.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 {
|
||||
let ws_stream = WsSecureBio::new(&self.url).await;
|
||||
|
||||
@ -127,6 +107,6 @@ impl Rdp {
|
||||
}
|
||||
|
||||
fn disconnect_with_msg(&self, msg: &str) {
|
||||
self.status_bar.set_text_content(Some(msg));
|
||||
disconnectedAndRefresh(Some(msg));
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ use tracing::{info, trace};
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::JsCast;
|
||||
use ws_stream_wasm::*;
|
||||
use crate::disconnectedAndRefresh;
|
||||
|
||||
pub type WsStream = IoStream<WsStreamIo, Vec<u8>>;
|
||||
|
||||
@ -44,13 +45,7 @@ impl WsSecureBio {
|
||||
|
||||
let onclose_callback = Closure::<dyn FnMut()>::new(move || {
|
||||
info!("socket close");
|
||||
let status_bar = web_sys::window()
|
||||
.unwrap()
|
||||
.document()
|
||||
.unwrap()
|
||||
.get_element_by_id("rdp_status")
|
||||
.unwrap();
|
||||
status_bar.set_text_content(Some("Server Disconnected"));
|
||||
disconnectedAndRefresh(None);
|
||||
panic!("Closed");
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user