aivpn-server 0.4.0

AIVPN - AI-powered VPN designed for censorship circumvention
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Integration tests for the Management HTTP API (Unix socket).
//!
//! Each test:
//!   1. Creates a real `ClientDatabase` in a temp directory.
//!   2. Spawns `management_api::serve()` on a unique temp Unix socket.
//!   3. Sends HTTP/1.1 requests over the Unix socket using hyper + tokio.
//!   4. Asserts on status codes and JSON response bodies.
//!
//! Guard: all tests are under `#[cfg(feature = "management-api")]` so they
//! are only compiled and executed when the feature is active.

#![cfg(feature = "management-api")]

use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;

use http_body_util::{BodyExt, Full};
use hyper::body::Bytes;
use hyper::{Method, Request, StatusCode};
use hyper_util::rt::TokioIo;
use serde_json::Value;
use tokio::net::UnixStream;
use tokio::time::sleep;

use aivpn_common::network_config::VpnNetworkConfig;
use aivpn_server::client_db::ClientDatabase;
use aivpn_server::management_api;

// ── Helpers ──────────────────────────────────────────────────────────────────

/// Global counter to give each test run a unique socket path even when
/// all 12 tests execute concurrently in the same process.
static SOCKET_COUNTER: AtomicU32 = AtomicU32::new(0);

/// Default VPN network config suitable for tests (10.99.0.0/24).
fn test_network_config() -> VpnNetworkConfig {
    VpnNetworkConfig {
        server_vpn_ip: "10.99.0.1".parse().unwrap(),
        prefix_len: 24,
        mtu: 1400,
    }
}

/// Create a temporary directory and a `ClientDatabase` inside it.
fn make_temp_db(test_name: &str) -> (tempfile::TempDir, Arc<ClientDatabase>) {
    let dir = tempfile::Builder::new()
        .prefix(&format!("aivpn_api_test_{}_", test_name))
        .tempdir()
        .expect("tempdir");
    let db_path = dir.path().join("clients.json");
    let db = ClientDatabase::load(&db_path, test_network_config()).expect("load db");
    (dir, Arc::new(db))
}

/// Return a unique Unix socket path. Uniqueness is guaranteed by combining
/// the process PID with an atomic counter so concurrent tests never share a path.
fn unique_socket_path() -> String {
    let n = SOCKET_COUNTER.fetch_add(1, Ordering::Relaxed);
    format!("/tmp/aivpn_mgmt_test_{}_{}.sock", std::process::id(), n)
}

/// Spawn the management API server and wait until the socket file appears.
async fn spawn_server(db: Arc<ClientDatabase>, socket_path: String) {
    spawn_server_with_key(db, socket_path, None, None).await;
}

/// Spawn the management API server with optional server key and address.
async fn spawn_server_with_key(
    db: Arc<ClientDatabase>,
    socket_path: String,
    server_pub_key: Option<[u8; 32]>,
    server_addr: Option<String>,
) {
    let db_clone = db.clone();
    let path_clone = socket_path.clone();
    tokio::spawn(async move {
        management_api::serve(
            Some(db_clone),
            Some(path_clone),
            server_pub_key,
            server_addr,
        )
        .await;
    });

    // Wait until the socket exists (up to 2 s)
    for _ in 0..40 {
        if std::path::Path::new(&socket_path).exists() {
            return;
        }
        sleep(Duration::from_millis(50)).await;
    }
    panic!("Management API socket did not appear: {}", socket_path);
}

/// Open a persistent hyper HTTP/1.1 connection over a Unix socket.
async fn connect(socket_path: &str) -> hyper::client::conn::http1::SendRequest<Full<Bytes>> {
    let stream = UnixStream::connect(socket_path)
        .await
        .expect("connect to unix socket");
    let io = TokioIo::new(stream);
    let (sender, conn) = hyper::client::conn::http1::handshake(io)
        .await
        .expect("http1 handshake");
    tokio::spawn(conn);
    sender
}

/// Send a single request and return (status, body-as-Value).
async fn send(
    sender: &mut hyper::client::conn::http1::SendRequest<Full<Bytes>>,
    method: Method,
    path: &str,
    body: Option<&str>,
) -> (StatusCode, Value) {
    let body_bytes = body.map(|s| Bytes::from(s.to_owned())).unwrap_or_default();

    let req = Request::builder()
        .method(method)
        .uri(path)
        .header("Host", "localhost")
        .header("Content-Type", "application/json")
        .header("Content-Length", body_bytes.len().to_string())
        .body(Full::new(body_bytes))
        .expect("build request");

    let res = sender.send_request(req).await.expect("send request");
    let status = res.status();
    let bytes = res
        .into_body()
        .collect()
        .await
        .expect("collect body")
        .to_bytes();
    let json: Value = serde_json::from_slice(&bytes).unwrap_or(Value::Null);
    (status, json)
}

// ── Tests ─────────────────────────────────────────────────────────────────────

/// GET /api/v1/status returns 200 with `version` and `uptime_secs` fields.
#[tokio::test]
async fn test_get_status_returns_200() {
    let (_dir, db) = make_temp_db("status");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    let mut sender = connect(&sock).await;
    let (status, body) = send(&mut sender, Method::GET, "/api/v1/status", None).await;

    assert_eq!(status, StatusCode::OK);
    assert!(
        body["version"].is_string(),
        "version must be a string: {:?}",
        body
    );
    assert!(
        body["uptime_secs"].is_number(),
        "uptime_secs must be a number: {:?}",
        body
    );
}

/// GET /api/v1/clients on an empty database returns 200 and an empty array.
#[tokio::test]
async fn test_list_clients_empty() {
    let (_dir, db) = make_temp_db("list_empty");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    let mut sender = connect(&sock).await;
    let (status, body) = send(&mut sender, Method::GET, "/api/v1/clients", None).await;

    assert_eq!(status, StatusCode::OK);
    assert!(body.is_array(), "body must be an array: {:?}", body);
    assert_eq!(body.as_array().unwrap().len(), 0);
}

/// POST /api/v1/clients creates a client and returns 201 with expected fields.
/// Verifies that `psk` is NOT present in the response.
#[tokio::test]
async fn test_add_client_returns_201_without_psk() {
    let (_dir, db) = make_temp_db("add_client");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    let mut sender = connect(&sock).await;
    let payload = r#"{"name": "alice"}"#;
    let (status, body) = send(&mut sender, Method::POST, "/api/v1/clients", Some(payload)).await;

    assert_eq!(
        status,
        StatusCode::CREATED,
        "expected 201, got {:?}: {:?}",
        status,
        body
    );

    // Required fields present
    assert!(body["id"].is_string(), "id must be string: {:?}", body);
    assert!(body["name"].is_string(), "name must be string: {:?}", body);
    assert!(
        body["vpn_ip"].is_string(),
        "vpn_ip must be string: {:?}",
        body
    );
    assert!(
        body["enabled"].is_boolean(),
        "enabled must be bool: {:?}",
        body
    );
    assert!(
        body["created_at"].is_string(),
        "created_at must be string: {:?}",
        body
    );

    // PSK must NOT be in the response
    assert!(
        body.get("psk").is_none(),
        "PSK must not appear in API response: {:?}",
        body
    );

    // Name matches what we sent
    assert_eq!(body["name"], "alice");
    assert_eq!(body["enabled"], true);
}

/// POST /api/v1/clients with a duplicate name returns 409 Conflict.
#[tokio::test]
async fn test_add_client_duplicate_returns_409() {
    let (_dir, db) = make_temp_db("add_dup");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    let payload = r#"{"name": "bob"}"#;

    // First creation — should succeed
    let mut sender = connect(&sock).await;
    let (s1, _) = send(&mut sender, Method::POST, "/api/v1/clients", Some(payload)).await;
    assert_eq!(s1, StatusCode::CREATED);

    // Second creation with same name — open a new connection
    let mut sender2 = connect(&sock).await;
    let (s2, body) = send(&mut sender2, Method::POST, "/api/v1/clients", Some(payload)).await;
    assert_eq!(
        s2,
        StatusCode::CONFLICT,
        "duplicate name must return 409: {:?}",
        body
    );
    assert!(
        body["error"].is_string(),
        "error field expected: {:?}",
        body
    );
}

/// GET /api/v1/clients/:id returns 200 with the correct client, without PSK.
#[tokio::test]
async fn test_get_client_by_id() {
    let (_dir, db) = make_temp_db("get_by_id");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    // Create a client
    let mut sender = connect(&sock).await;
    let (_, created) = send(
        &mut sender,
        Method::POST,
        "/api/v1/clients",
        Some(r#"{"name":"charlie"}"#),
    )
    .await;
    let id = created["id"].as_str().unwrap().to_string();

    // Fetch by ID
    let mut sender2 = connect(&sock).await;
    let path = format!("/api/v1/clients/{}", id);
    let (status, body) = send(&mut sender2, Method::GET, &path, None).await;

    assert_eq!(status, StatusCode::OK);
    assert_eq!(body["id"], id.as_str());
    assert_eq!(body["name"], "charlie");
    assert!(body.get("psk").is_none(), "PSK must not appear: {:?}", body);
}

/// GET /api/v1/clients/:id for a non-existent ID returns 404.
#[tokio::test]
async fn test_get_client_not_found_returns_404() {
    let (_dir, db) = make_temp_db("get_404");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    let mut sender = connect(&sock).await;
    let (status, body) = send(
        &mut sender,
        Method::GET,
        "/api/v1/clients/nonexistent",
        None,
    )
    .await;

    assert_eq!(status, StatusCode::NOT_FOUND);
    assert!(
        body["error"].is_string(),
        "error field expected: {:?}",
        body
    );
}

/// DELETE /api/v1/clients/:id removes the client and returns 204 No Content.
#[tokio::test]
async fn test_delete_client_returns_204() {
    let (_dir, db) = make_temp_db("delete_client");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    // Create client
    let mut sender = connect(&sock).await;
    let (_, created) = send(
        &mut sender,
        Method::POST,
        "/api/v1/clients",
        Some(r#"{"name":"dave"}"#),
    )
    .await;
    let id = created["id"].as_str().unwrap().to_string();

    // Delete
    let mut sender2 = connect(&sock).await;
    let path = format!("/api/v1/clients/{}", id);
    let (status, _) = send(&mut sender2, Method::DELETE, &path, None).await;
    assert_eq!(status, StatusCode::NO_CONTENT, "DELETE must return 204");

    // Verify it's gone
    let mut sender3 = connect(&sock).await;
    let (status2, _) = send(&mut sender3, Method::GET, &path, None).await;
    assert_eq!(
        status2,
        StatusCode::NOT_FOUND,
        "deleted client must return 404"
    );
}

/// DELETE /api/v1/clients/:id for a non-existent ID returns 404.
#[tokio::test]
async fn test_delete_nonexistent_client_returns_404() {
    let (_dir, db) = make_temp_db("delete_404");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    let mut sender = connect(&sock).await;
    let (status, body) = send(
        &mut sender,
        Method::DELETE,
        "/api/v1/clients/no-such-id",
        None,
    )
    .await;

    assert_eq!(status, StatusCode::NOT_FOUND);
    assert!(
        body["error"].is_string(),
        "error field expected: {:?}",
        body
    );
}

/// POST /api/v1/reload returns 200 with `reloaded` boolean field.
#[tokio::test]
async fn test_reload_endpoint() {
    let (_dir, db) = make_temp_db("reload");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    let mut sender = connect(&sock).await;
    let (status, body) = send(&mut sender, Method::POST, "/api/v1/reload", None).await;

    assert_eq!(status, StatusCode::OK, "reload must return 200: {:?}", body);
    assert!(
        body["reloaded"].is_boolean(),
        "`reloaded` must be a bool: {:?}",
        body
    );
}

/// GET /api/v1/clients returns all clients added so far.
#[tokio::test]
async fn test_list_clients_after_add() {
    let (_dir, db) = make_temp_db("list_after_add");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    // Add two clients
    for name in &["eve", "frank"] {
        let mut s = connect(&sock).await;
        let payload = format!(r#"{{"name":"{}"}}"#, name);
        let (status, _) = send(&mut s, Method::POST, "/api/v1/clients", Some(&payload)).await;
        assert_eq!(status, StatusCode::CREATED);
    }

    // List
    let mut sender = connect(&sock).await;
    let (status, body) = send(&mut sender, Method::GET, "/api/v1/clients", None).await;
    assert_eq!(status, StatusCode::OK);
    let arr = body.as_array().expect("body must be array");
    assert_eq!(
        arr.len(),
        2,
        "expected 2 clients, got {}: {:?}",
        arr.len(),
        body
    );

    // PSK must not appear in any item
    for item in arr {
        assert!(
            item.get("psk").is_none(),
            "PSK must not appear in list: {:?}",
            item
        );
        assert!(item["id"].is_string());
        assert!(item["name"].is_string());
        assert!(item["vpn_ip"].is_string());
    }
}

/// Stats fields are present in the client response.
#[tokio::test]
async fn test_client_response_has_stats() {
    let (_dir, db) = make_temp_db("stats_fields");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    let mut sender = connect(&sock).await;
    let (_, created) = send(
        &mut sender,
        Method::POST,
        "/api/v1/clients",
        Some(r#"{"name":"grace"}"#),
    )
    .await;
    let id = created["id"].as_str().unwrap().to_string();

    let mut sender2 = connect(&sock).await;
    let path = format!("/api/v1/clients/{}", id);
    let (status, body) = send(&mut sender2, Method::GET, &path, None).await;

    assert_eq!(status, StatusCode::OK);
    let stats = &body["stats"];
    assert!(stats.is_object(), "stats must be an object: {:?}", body);
    assert!(stats["bytes_in"].is_number(), "bytes_in: {:?}", stats);
    assert!(stats["bytes_out"].is_number(), "bytes_out: {:?}", stats);
    assert!(
        stats["total_connections"].is_number(),
        "total_connections: {:?}",
        stats
    );
}

/// Confirm the PSK field is absent even when it is stored in the DB.
/// (Checks both single-client GET and list GET.)
#[tokio::test]
async fn test_psk_never_exposed() {
    let (_dir, db) = make_temp_db("psk_never");
    let sock = unique_socket_path();
    spawn_server(db, sock.clone()).await;

    let mut sender = connect(&sock).await;
    let (_, created) = send(
        &mut sender,
        Method::POST,
        "/api/v1/clients",
        Some(r#"{"name":"henry"}"#),
    )
    .await;
    let id = created["id"].as_str().unwrap().to_string();

    // Check PSK not in POST response
    assert!(
        created.get("psk").is_none(),
        "PSK in POST response: {:?}",
        created
    );

    // Check PSK not in GET single
    let mut s2 = connect(&sock).await;
    let path = format!("/api/v1/clients/{}", id);
    let (_, single) = send(&mut s2, Method::GET, &path, None).await;
    assert!(
        single.get("psk").is_none(),
        "PSK in GET single: {:?}",
        single
    );

    // Check PSK not in GET list
    let mut s3 = connect(&sock).await;
    let (_, list) = send(&mut s3, Method::GET, "/api/v1/clients", None).await;
    for item in list.as_array().unwrap() {
        assert!(item.get("psk").is_none(), "PSK in list item: {:?}", item);
    }
}

/// GET /api/v1/clients/:id/connection-key returns 503 when server is not
/// configured with --server-ip / --key-file.
#[tokio::test]
async fn test_connection_key_without_server_config_returns_503() {
    let (_dir, db) = make_temp_db("conn_key_503");
    let sock = unique_socket_path();
    // No server_pub_key, no server_addr
    spawn_server(db, sock.clone()).await;

    let mut sender = connect(&sock).await;
    let (_, created) = send(
        &mut sender,
        Method::POST,
        "/api/v1/clients",
        Some(r#"{"name":"ivan"}"#),
    )
    .await;
    let id = created["id"].as_str().unwrap().to_string();

    let mut s2 = connect(&sock).await;
    let path = format!("/api/v1/clients/{}/connection-key", id);
    let (status, body) = send(&mut s2, Method::GET, &path, None).await;

    assert_eq!(
        status,
        StatusCode::SERVICE_UNAVAILABLE,
        "expected 503: {:?}",
        body
    );
    assert!(
        body["error"].is_string(),
        "error field expected: {:?}",
        body
    );
}

/// GET /api/v1/clients/:id/connection-key returns 200 with a valid aivpn:// string
/// when the server is configured with a key and address.
#[tokio::test]
async fn test_connection_key_returns_aivpn_url() {
    let (_dir, db) = make_temp_db("conn_key_200");
    let sock = unique_socket_path();

    // Fake 32-byte server public key and a server address
    let pub_key = [0x42u8; 32];
    let server_addr = "1.2.3.4:4443".to_string();
    spawn_server_with_key(db, sock.clone(), Some(pub_key), Some(server_addr)).await;

    // Create client
    let mut sender = connect(&sock).await;
    let (_, created) = send(
        &mut sender,
        Method::POST,
        "/api/v1/clients",
        Some(r#"{"name":"judy"}"#),
    )
    .await;
    let id = created["id"].as_str().unwrap().to_string();

    // Get connection key
    let mut s2 = connect(&sock).await;
    let path = format!("/api/v1/clients/{}/connection-key", id);
    let (status, body) = send(&mut s2, Method::GET, &path, None).await;

    assert_eq!(status, StatusCode::OK, "expected 200: {:?}", body);
    let key = body["connection_key"]
        .as_str()
        .expect("connection_key must be a string");
    assert!(
        key.starts_with("aivpn://"),
        "key must start with aivpn://: {}",
        key
    );

    // connection-key for non-existent client must return 404
    let mut s3 = connect(&sock).await;
    let (status404, _) = send(
        &mut s3,
        Method::GET,
        "/api/v1/clients/no-such-id/connection-key",
        None,
    )
    .await;
    assert_eq!(status404, StatusCode::NOT_FOUND);
}