Skip to content

Commit ec62e15

Browse files
committed
✨ provide sync method for web package
1 parent f2810d0 commit ec62e15

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

crates/desktop/src/daemon/api_controller.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ pub fn router(state: ServerState) -> axum::Router {
6464
)
6565
.merge(
6666
axum::Router::new()
67-
.route("/connect", get(get_control_status).post(request_control))
67+
.route(
68+
"/connect",
69+
get(get_control_status)
70+
.post(request_control)
71+
.put(update_website_info),
72+
)
6873
.route(
6974
"/version",
7075
get(|| async { Json(env!("CARGO_PKG_VERSION")) }),
@@ -405,6 +410,62 @@ async fn request_control(
405410
}
406411
}
407412

413+
async fn update_website_info(
414+
State(state): State<ServerState>, headers: HeaderMap,
415+
axum::Json(scope_data): axum::Json<ScopeData>,
416+
) -> Result<impl IntoResponse, (StatusCode, String)> {
417+
let req_scope = headers
418+
.get("Origin")
419+
.and_then(|h| h.to_str().ok())
420+
.unwrap_or_default()
421+
.to_owned();
422+
let mut scopes = state.scopes.write().await;
423+
if let Some(scope) = scopes.iter_mut().find(|s| s.host == req_scope) {
424+
scope.name = scope_data.name.clone();
425+
scope.features = scope_data.features.clone();
426+
let state_d = scope.state.clone();
427+
428+
match slint::invoke_from_event_loop(move || {
429+
let ui_handle = state.ui.upgrade().unwrap();
430+
let scope_bridge = ui_handle.global::<ScopeBridge>();
431+
let scopes = scope_bridge.get_scopes();
432+
let scopes = scopes.as_any().downcast_ref::<VecModel<Scope>>().unwrap();
433+
let mut index = 0;
434+
for i in scopes.iter() {
435+
if i.host == req_scope {
436+
break;
437+
}
438+
index += 1;
439+
}
440+
let scope = Scope {
441+
host: req_scope.clone().into(),
442+
name: scope_data.name.clone().into(),
443+
state: state_d.into(),
444+
features: scope_data.features.join(",").into(),
445+
};
446+
scopes.set_row_data(index, scope);
447+
}) {
448+
Ok(_) => {
449+
debug!("Updated scope in UI");
450+
}
451+
Err(e) => {
452+
debug!("Failed to update UI: {e}");
453+
return Err((
454+
StatusCode::INTERNAL_SERVER_ERROR,
455+
"failed to update UI".to_owned(),
456+
));
457+
}
458+
}
459+
460+
Ok(StatusCode::OK)
461+
} else {
462+
Err((
463+
StatusCode::FORBIDDEN,
464+
format!("Scope {} not found", req_scope),
465+
))
466+
}
467+
}
468+
408469
async fn popup_window(
409470
State(state): State<ServerState>,
410471
) -> Result<impl IntoResponse, (StatusCode, String)> {

packages/wsrx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@xdsec/wsrx",
3-
"version": "0.4.4",
3+
"version": "0.4.5",
44
"description": "Client for WebSocket Reflector X",
55
"license": "MIT",
66
"repository": "XDSEC/WebSocketReflectorX",

packages/wsrx/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Wsrx {
3333
}
3434
}
3535

36-
private async sync() {
36+
public async sync() {
3737
try {
3838
const resp = await fetch(`${this.options.api}/pool`, {
3939
method: "GET",
@@ -65,15 +65,15 @@ class Wsrx {
6565
}
6666
}
6767
}
68-
} catch (e) { }
68+
} catch (e) {}
6969
}
7070

7171
private async tick() {
7272
if (this.interval !== null) {
7373
clearInterval(this.interval);
7474
}
7575
this.interval = setInterval(async () => {
76-
const state = await this.check().catch(() => { });
76+
const state = await this.check().catch(() => {});
7777
if (state) this.setState(state);
7878
else this.setState(WsrxState.Invalid);
7979
if (this.state === WsrxState.Invalid) {

0 commit comments

Comments
 (0)