rebind-client 0.1.0

Rust client for the Rebind remote access WebSocket protocol
Documentation
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/// Integration tests for RebindClient using an in-process mock WebSocket server.
/// Tests run fully offline — no live relay required.

use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use futures_util::{SinkExt, StreamExt};
use serde_json::{json, Value};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::broadcast;
use tokio_tungstenite::{accept_async, tungstenite::Message};

use rebind_client::{RebindClient, RebindError};

// ── mock server ───────────────────────────────────────────────────────────────

#[derive(Clone)]
struct MockConfig {
    token: String,
    reply_delay_ms: u64,
    ping_returns_error: bool,
}

impl Default for MockConfig {
    fn default() -> Self {
        Self {
            token: String::new(),
            reply_delay_ms: 0,
            ping_returns_error: false,
        }
    }
}

struct MockServer {
    addr: SocketAddr,
    auth_count: Arc<Mutex<u32>>,
    subscribe_count: Arc<Mutex<u32>>,
    event_tx: broadcast::Sender<Value>,
    shutdown_tx: broadcast::Sender<()>,
}

impl MockServer {
    async fn start(cfg: MockConfig) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let auth_count = Arc::new(Mutex::new(0u32));
        let subscribe_count = Arc::new(Mutex::new(0u32));
        let (event_tx, _) = broadcast::channel(64);
        let (shutdown_tx, _) = broadcast::channel(1);

        let auth_count_srv = auth_count.clone();
        let subscribe_count_srv = subscribe_count.clone();
        let event_tx_srv = event_tx.clone();
        let mut shutdown_rx = shutdown_tx.subscribe();
        let cfg_srv = cfg;

        tokio::spawn(async move {
            loop {
                tokio::select! {
                    Ok((stream, _)) = listener.accept() => {
                        let auth = auth_count_srv.clone();
                        let subs = subscribe_count_srv.clone();
                        let ev_tx = event_tx_srv.clone();
                        let cfg = cfg_srv.clone();
                        tokio::spawn(handle_connection(stream, auth, subs, ev_tx, cfg));
                    }
                    _ = shutdown_rx.recv() => break,
                }
            }
        });

        Self { addr, auth_count, subscribe_count, event_tx, shutdown_tx }
    }

    fn url(&self) -> String {
        format!("ws://{}", self.addr)
    }

    fn auth_count(&self) -> u32 {
        *self.auth_count.lock().unwrap()
    }

    fn subscribe_count(&self) -> u32 {
        *self.subscribe_count.lock().unwrap()
    }

    fn push_mouse(&self, x: i32, y: i32) {
        let _ = self.event_tx.send(json!({ "t": "mouse", "x": x, "y": y }));
    }

    fn stop(&self) {
        let _ = self.shutdown_tx.send(());
    }
}

async fn handle_connection(
    stream: TcpStream,
    auth_count: Arc<Mutex<u32>>,
    subscribe_count: Arc<Mutex<u32>>,
    event_tx: broadcast::Sender<Value>,
    cfg: MockConfig,
) {
    let ws = accept_async(stream).await.unwrap();
    let (mut write, mut read) = ws.split();

    // send hello banner
    write
        .send(Message::Text(
            json!({
                "t": "hello",
                "protocol": "1.0.0",
                "auth_required": !cfg.token.is_empty()
            })
            .to_string()
            .into(),
        ))
        .await
        .unwrap();

    let mut subscriptions: HashMap<String, bool> = HashMap::new();
    let mut event_rx = event_tx.subscribe();
    let mut authed = cfg.token.is_empty();

    loop {
        tokio::select! {
            Some(Ok(Message::Text(raw))) = read.next() => {
                let Ok(req) = serde_json::from_str::<Value>(&raw) else { continue };
                let cmd = req["t"].as_str().unwrap_or("");

                if cfg.reply_delay_ms > 0 {
                    tokio::time::sleep(Duration::from_millis(cfg.reply_delay_ms)).await;
                }

                if !cfg.token.is_empty() && !authed && cmd != "auth" && cmd != "hello" {
                    send_err(&mut write, &req, "unauthenticated", "send auth first").await;
                    continue;
                }

                match cmd {
                    "hello" => send_reply(&mut write, &req, json!({ "protocol": "1.0.0" })).await,
                    "auth" => {
                        *auth_count.lock().unwrap() += 1;
                        if cfg.token.is_empty() || req["token"].as_str() == Some(&cfg.token) {
                            authed = true;
                            send_reply(&mut write, &req, json!({ "ok": true })).await;
                        } else {
                            send_err(&mut write, &req, "bad_token", "token does not match").await;
                        }
                    }
                    "ping" => {
                        if cfg.ping_returns_error {
                            send_err(&mut write, &req, "simulated", "ping error").await;
                        } else {
                            send_reply(&mut write, &req, json!({ "pong": true, "time_ms": 1000u64 })).await;
                        }
                    }
                    "screen.pixel" => {
                        let x = req["x"].as_i64().unwrap_or(0);
                        let y = req["y"].as_i64().unwrap_or(0);
                        if x < 0 || y < 0 {
                            send_err(&mut write, &req, "screen_error", "negative coordinates").await;
                        } else {
                            send_reply(&mut write, &req, json!({
                                "r": (x * y) & 0xFF,
                                "g": x & 0xFF,
                                "b": y & 0xFF,
                            })).await;
                        }
                    }
                    "screen.resolution" => send_reply(&mut write, &req, json!({ "width": 1920, "height": 1080 })).await,
                    "system.mouse" => send_reply(&mut write, &req, json!({ "x": 100, "y": 200 })).await,
                    "system.window" => send_reply(&mut write, &req, json!({
                        "window": { "title": "Mock Window", "process": "mock.exe", "x": 0, "y": 0, "width": 800, "height": 600 }
                    })).await,
                    "system.time" => send_reply(&mut write, &req, json!({ "time_ms": 1000u64 })).await,
                    "input.keys" => send_reply(&mut write, &req, json!({ "keys": [] })).await,
                    "input.is_down" => send_reply(&mut write, &req, json!({ "down": false })).await,
                    "input.modifiers" => send_reply(&mut write, &req, json!({
                        "modifiers": { "shift": false, "ctrl": false, "alt": false, "win": false }
                    })).await,
                    "clipboard.get" => send_reply(&mut write, &req, json!({ "text": "mock clipboard" })).await,
                    "clipboard.set" => send_reply(&mut write, &req, json!({ "ok": true })).await,
                    "window.list" => send_reply(&mut write, &req, json!({ "windows": [] })).await,
                    "window.find" => send_reply(&mut write, &req, json!({ "handle": null })).await,
                    "window.activate" | "window.move" => send_reply(&mut write, &req, json!({ "ok": true })).await,
                    "subscribe" => {
                        *subscribe_count.lock().unwrap() += 1;
                        if let Some(events) = req["events"].as_array() {
                            for e in events {
                                if let Some(s) = e.as_str() {
                                    subscriptions.insert(s.to_string(), true);
                                }
                            }
                        }
                        send_reply(&mut write, &req, json!({ "ok": true })).await;
                    }
                    "unsubscribe" => {
                        if let Some(events) = req["events"].as_array() {
                            for e in events {
                                if let Some(s) = e.as_str() {
                                    subscriptions.remove(s);
                                }
                            }
                        }
                        send_reply(&mut write, &req, json!({ "ok": true })).await;
                    }
                    // fire-and-forget HID — no reply
                    "hid.down" | "hid.up" | "hid.press" | "hid.type"
                    | "hid.move" | "hid.move_to" | "hid.scroll" => {}
                    _ => send_err(&mut write, &req, "unknown_command", &format!("unknown '{cmd}'")).await,
                }
            }
            Ok(event) = event_rx.recv() => {
                let t = event["t"].as_str().unwrap_or("");
                if subscriptions.contains_key(t) {
                    let _ = write.send(Message::Text(event.to_string().into())).await;
                }
            }
            else => break,
        }
    }
}

async fn send_reply<W>(write: &mut W, req: &Value, mut payload: Value)
where
    W: futures_util::Sink<Message, Error = tungstenite::Error> + Unpin,
{
    if req["id"].is_null() || req.get("id").is_none() {
        return;
    }
    payload["id"] = req["id"].clone();
    let _ = write.send(Message::Text(payload.to_string().into())).await;
}

async fn send_err<W>(write: &mut W, req: &Value, code: &str, message: &str)
where
    W: futures_util::Sink<Message, Error = tungstenite::Error> + Unpin,
{
    if req.get("id").is_none() {
        return;
    }
    let msg = json!({ "id": req["id"], "error": { "code": code, "message": message } });
    let _ = write.send(Message::Text(msg.to_string().into())).await;
}

// ── helpers ───────────────────────────────────────────────────────────────────

async fn wait_for<F>(pred: F, timeout_ms: u64)
where
    F: Fn() -> bool,
{
    let deadline = std::time::Instant::now() + Duration::from_millis(timeout_ms);
    while std::time::Instant::now() < deadline {
        if pred() {
            return;
        }
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    panic!("wait_for timed out after {timeout_ms}ms");
}

// ── tests ─────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn test_connect() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_auth_success() {
    let srv = MockServer::start(MockConfig {
        token: "secret".into(),
        ..Default::default()
    })
    .await;
    let client = RebindClient::connect_with_token(&srv.url(), "secret")
        .await
        .unwrap();
    assert_eq!(srv.auth_count(), 1);
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_auth_failure() {
    let srv = MockServer::start(MockConfig {
        token: "secret".into(),
        ..Default::default()
    })
    .await;
    let result = RebindClient::connect_with_token(&srv.url(), "wrong").await;
    assert!(matches!(result, Err(RebindError::Server { .. })));
    srv.stop();
}

#[tokio::test]
async fn test_ping() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    let ms = client.ping().await.unwrap();
    assert!(ms > 0);
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_screen_pixel() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    let px = client.screen_pixel(10, 20).await.unwrap();
    assert_eq!(px.r, ((10 * 20) & 0xFF) as u8);
    assert_eq!(px.g, 10);
    assert_eq!(px.b, 20);
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_screen_pixel_server_error() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    let result = client.screen_pixel(-1, -1).await;
    match result {
        Err(RebindError::Server { code, .. }) => assert_eq!(code, "screen_error"),
        other => panic!("expected Server error, got {other:?}"),
    }
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_screen_resolution() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    let res = client.screen_resolution().await.unwrap();
    assert_eq!(res.width, 1920);
    assert_eq!(res.height, 1080);
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_system_mouse() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    let (x, y) = client.system_mouse().await.unwrap();
    assert_eq!(x, 100);
    assert_eq!(y, 200);
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_system_window() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    let win = client.system_window().await.unwrap();
    assert_eq!(win.title, "Mock Window");
    assert_eq!(win.process, "mock.exe");
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_input_keys() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    let keys = client.input_keys().await.unwrap();
    assert!(keys.is_empty());
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_input_is_down() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    assert!(!client.input_is_down("A").await.unwrap());
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_clipboard_get() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    let text = client.clipboard_get().await.unwrap();
    assert_eq!(text, "mock clipboard");
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_clipboard_set() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    client.clipboard_set("hello").await.unwrap();
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_window_list() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    let windows = client.window_list(None).await.unwrap();
    assert!(windows.is_empty());
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_window_find_none() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    let handle = client.window_find("Nonexistent").await.unwrap();
    assert!(handle.is_none());
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_hid_methods_no_panic() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    client.hid_down("A");
    client.hid_up("A");
    client.hid_press("A", 20);
    client.hid_type("hello");
    client.hid_move(10, 20);
    client.hid_move_to(100, 200);
    client.hid_scroll(3);
    // give a tick for the sends to flush
    tokio::time::sleep(Duration::from_millis(10)).await;
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_timeout() {
    let srv = MockServer::start(MockConfig {
        reply_delay_ms: 500,
        ..Default::default()
    })
    .await;
    let client = RebindClient::connect_with_options(&srv.url(), "", 50)
        .await
        .unwrap();
    let result = client.ping().await;
    assert!(matches!(result, Err(RebindError::Timeout(_))));
    client.close().await;
    srv.stop();
}

#[tokio::test]
async fn test_concurrent_rpcs() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();

    let mut handles = vec![];
    // wrap client in Arc so we can share it across tasks
    let client = Arc::new(client);
    for i in 0i32..20 {
        let c = client.clone();
        handles.push(tokio::spawn(async move {
            c.screen_pixel(i, i * 2).await
        }));
    }
    for (i, h) in handles.into_iter().enumerate() {
        let px = h.await.unwrap().unwrap();
        assert_eq!(px.g, i as u8);
    }
    srv.stop();
}

#[tokio::test]
async fn test_mouse_events() {
    let srv = MockServer::start(MockConfig::default()).await;
    let client = RebindClient::connect(&srv.url()).await.unwrap();
    let mut rx = client.mouse_events().await.unwrap();

    wait_for(|| srv.subscribe_count() >= 1, 2000).await;

    srv.push_mouse(1, 2);
    srv.push_mouse(3, 4);
    srv.push_mouse(5, 6);

    let p1 = tokio::time::timeout(Duration::from_secs(2), rx.recv()).await.unwrap().unwrap();
    let p2 = tokio::time::timeout(Duration::from_secs(2), rx.recv()).await.unwrap().unwrap();
    let p3 = tokio::time::timeout(Duration::from_secs(2), rx.recv()).await.unwrap().unwrap();

    assert_eq!((p1.x, p1.y), (1, 2));
    assert_eq!((p2.x, p2.y), (3, 4));
    assert_eq!((p3.x, p3.y), (5, 6));

    client.close().await;
    srv.stop();
}