-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.rs
More file actions
322 lines (281 loc) · 11.8 KB
/
Copy pathclient.rs
File metadata and controls
322 lines (281 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use crate::{
error::WasmError,
hostcall::handle_host_calls,
http_client::GatewayHttpViaWSClient,
types::{SecurityWarningCallback, WasmCommandCompletion, WasmWinRmConfig},
JsSessionEvent, WasmPowerShellStream,
};
use futures::StreamExt;
use ironposh_async::RemoteAsyncPowershellClient;
use ironposh_client_core::{connector::WinRmConfig, powershell::PipelineHandle};
use js_sys::{Array, Function, Promise};
use std::convert::TryFrom;
use tracing::{error, info, warn};
use url::Url;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::{future_to_promise, spawn_local, JsFuture};
// Main PowerShell client
#[wasm_bindgen]
pub struct WasmPowerShellClient {
client: RemoteAsyncPowershellClient,
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "(js_host_call: JsHostCall) => Promise<any> | any")]
pub type HostCallHandler;
#[wasm_bindgen(typescript_type = "(session_event: JsSessionEvent) => void")]
pub type SessionEventHandler;
}
#[wasm_bindgen]
impl WasmPowerShellClient {
/// Check the security configuration and return any warnings.
/// Call this before connect() to inspect security status.
#[wasm_bindgen]
pub fn check_security(config: &WasmWinRmConfig) -> Array {
let warnings = config.check_security();
let arr = Array::new();
for warning in warnings {
let js_warning = serde_wasm_bindgen::to_value(&warning)
.expect("Failed to serialize SecurityWarning");
arr.push(&js_warning);
}
arr
}
/// Connect to a PowerShell session with security callback.
///
/// If security warnings are detected and `on_security_warning` is provided,
/// the callback will be invoked with the list of warnings. The callback must
/// return a Promise<boolean>:
/// - `true`: User accepts the risk, continue with connection
/// - `false`: User rejects, abort connection
///
/// If warnings exist but no callback is provided, connection will be rejected.
#[wasm_bindgen]
pub async fn connect_with_security_check(
config: WasmWinRmConfig,
host_call_handler: HostCallHandler,
session_event_handler: SessionEventHandler,
on_security_warning: Option<SecurityWarningCallback>,
) -> Result<Self, WasmError> {
// Check for security warnings
let warnings = config.check_security();
if !warnings.is_empty() {
// Log warnings
for warning in &warnings {
warn!(?warning, "security warning detected");
}
// If callback provided, ask user
if let Some(callback) = on_security_warning {
let callback_fn: Function = callback.unchecked_into();
// Convert warnings to JS array
let js_warnings = Array::new();
for warning in &warnings {
let js_warning = serde_wasm_bindgen::to_value(warning)
.expect("Failed to serialize SecurityWarning");
js_warnings.push(&js_warning);
}
// Call the callback and await the promise
let result = callback_fn
.call1(&JsValue::NULL, &js_warnings)
.map_err(|e| {
error!(?e, "failed to call security warning callback");
WasmError::Generic(format!(
"Failed to call security warning callback: {e:?}"
))
})?;
let promise = Promise::from(result);
let should_continue = JsFuture::from(promise).await.map_err(|e| {
error!(?e, "security warning callback promise rejected");
WasmError::Generic(format!("Security warning callback rejected: {e:?}"))
})?;
if !should_continue.as_bool().unwrap_or(false) {
info!("user rejected insecure connection");
return Err(WasmError::Generic(
"Connection rejected: user declined insecure connection".into(),
));
}
info!("user accepted insecure connection, proceeding");
} else {
// No callback provided, reject by default
error!("security warnings detected but no callback provided");
return Err(WasmError::Generic(format!(
"Connection rejected: security warnings detected ({warnings:?}). Provide on_security_warning callback to handle."
)));
}
}
// Proceed with connection
Self::connect_internal(config, host_call_handler, session_event_handler)
}
/// Connect to a PowerShell session (legacy method, no security callback).
/// Will reject if there are any security warnings.
#[wasm_bindgen]
pub fn connect(
config: WasmWinRmConfig,
host_call_handler: HostCallHandler,
session_event_handler: SessionEventHandler,
) -> Result<Self, WasmError> {
// Check for security warnings first
let warnings = config.check_security();
if !warnings.is_empty() {
error!(
?warnings,
"security warnings detected, use connect_with_security_check"
);
return Err(WasmError::Generic(format!(
"Connection rejected: security warnings detected ({warnings:?}). Use connect_with_security_check() with a callback to handle warnings."
)));
}
Self::connect_internal(config, host_call_handler, session_event_handler)
}
fn connect_internal(
config: WasmWinRmConfig,
host_call_handler: HostCallHandler,
session_event_handler: SessionEventHandler,
) -> Result<Self, WasmError> {
info!(
gateway_url = %config.gateway_url,
"connecting PowerShell client"
);
if !host_call_handler.is_function() || !session_event_handler.is_function() {
error!("host_call_handler or session_event_handler is not a function");
return Err(WasmError::InvalidArgument(
"host_call_handler and session_event_handler must be functions".into(),
));
}
let url = Url::parse(&config.gateway_url).map_err(|e| {
error!(
?e,
gateway_url = %config.gateway_url,
"failed to parse gateway URL"
);
WasmError::UrlParseError {
source: e,
target: config.gateway_url.clone(),
}
})?;
let http_client = GatewayHttpViaWSClient::new(url, config.gateway_token.clone());
let internal_config: WinRmConfig = config.into();
let (client, host_io, session_event_rx, task) =
RemoteAsyncPowershellClient::open_task(internal_config, http_client);
// Spawn session event handler task
spawn_local(async move {
let mut session_event_rx = session_event_rx;
let session_event_handler = session_event_handler.unchecked_into::<Function>();
while let Some(event) = session_event_rx.next().await {
let event: JsSessionEvent = event.into();
if let Err(e) = session_event_handler.call1(&JsValue::NULL, &event.into()) {
error!(?e, "failed to call session event handler");
}
}
info!("session event handler task exiting");
});
let (host_call_rx, submitter) = host_io.into_parts();
wasm_bindgen_futures::spawn_local(handle_host_calls(
host_call_rx,
submitter,
host_call_handler.unchecked_into(),
));
info!("spawning background task for PowerShell client");
// Spawn background task
wasm_bindgen_futures::spawn_local(async move {
#[expect(clippy::large_futures)]
if let Err(e) = task.await {
error!(?e, "background task failed");
web_sys::console::error_1(&format!("Background task failed: {e}").into());
}
});
info!("PowerShell client connected successfully");
Ok(Self { client })
}
#[wasm_bindgen]
pub async fn execute_command(
&mut self,
script: String,
) -> Result<WasmPowerShellStream, WasmError> {
info!(script_length = script.len(), "executing PowerShell command");
let stream = self.client.send_script(script).await.map_err(|e| {
error!(?e, "failed to send PowerShell script");
e
})?;
let (kill_tx, kill_rx) = futures::channel::oneshot::channel::<PipelineHandle>();
let mut client_clone = self.client.clone();
spawn_local(async move {
let Ok(pipeline_handle) = kill_rx.await else {
return;
};
let _ = client_clone
.kill_pipeline(pipeline_handle)
.await
.inspect_err(|e| {
error!(?e, "failed to kill PowerShell pipeline");
});
});
let stream = crate::stream::WasmPowerShellStream::new(stream, kill_tx);
info!("PowerShell command stream created successfully");
Ok(stream)
}
#[wasm_bindgen]
pub async fn tab_complete(
&mut self,
input_script: String,
cursor_column: u32,
) -> Result<WasmCommandCompletion, WasmError> {
use ironposh_client_core::connector::active_session::UserEvent;
fn escape_ps_single_quoted(input: &str) -> String {
input.replace('\'', "''")
}
let escaped = escape_ps_single_quoted(&input_script);
let script =
format!("TabExpansion2 -inputScript '{escaped}' -cursorColumn {cursor_column}");
info!(
cursor_column,
input_len = input_script.len(),
"tab_complete: sending TabExpansion2"
);
let stream = self.client.send_script_raw(script).await?;
let mut stream = stream.boxed();
let mut output: Option<ironposh_psrp::PsValue> = None;
let mut error_message: Option<String> = None;
while let Some(ev) = stream.next().await {
match ev {
UserEvent::PipelineOutput { output: out, .. } => {
if output.is_none() {
output = Some(out.data);
}
}
UserEvent::ErrorRecord { error_record, .. } => {
let concise = error_record.render_concise();
error_message = Some(concise.clone());
warn!(error_message = %concise, "tab_complete: error record");
}
UserEvent::PipelineFinished { .. } => break,
UserEvent::PipelineCreated { .. } => {}
}
}
let Some(ps_value) = output else {
return Err(WasmError::Generic(
error_message.unwrap_or_else(|| "TabExpansion2 returned no output".into()),
));
};
let completion = ironposh_psrp::CommandCompletion::try_from(&ps_value)
.map_err(|e| WasmError::Generic(e.to_string()))?;
info!(
?completion,
cursor_column,
replacement_index = completion.replacement_index,
replacement_length = completion.replacement_length,
matches = completion.completion_matches.len(),
"tab_complete: parsed completion"
);
Ok(WasmCommandCompletion::from(&completion))
}
// pub async fn next_host_call
#[wasm_bindgen]
pub fn disconnect(&self) -> Promise {
info!("disconnecting PowerShell client");
future_to_promise(async move {
info!("PowerShell client disconnected");
Ok(JsValue::NULL)
})
}
}