nvpn 4.1.13

CLI and daemon for Nostr VPN private mesh networks
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
use super::*;
use nostr_sdk::prelude::{Keys, ToBech32};
use nostr_vpn_core::config::InternetSource;
use nostr_vpn_core::paid_routes::{PaidRouteChannelTerms, PaidRouteIpSupport, PaidRoutePricing};

#[path = "mint_failover_tests.rs"]
mod mint_failover_tests;

#[test]
fn automatic_selection_uses_signed_seller_endpoint_for_a_routable_probe_session() {
    let seller = Keys::generate();
    let seller_pubkey = seller.public_key().to_hex();
    let now = unix_timestamp();
    let directory =
        std::env::temp_dir().join(format!("nvpn-auto-buyer-{}-{now}", std::process::id()));
    std::fs::create_dir_all(&directory).expect("create automatic buyer test directory");
    let config_path = directory.join("config.toml");
    let store_path = paid_route_store_file_path(&config_path);
    let mint = "https://mint.example";
    let offer_config = PaidExitConfig {
        enabled: true,
        pricing: PaidRoutePricing {
            price_msat_per_gb: 90_000,
        },
        channel: PaidRouteChannelTerms {
            accepted_mints: vec![mint.to_string()],
            max_channel_capacity_sat: 100,
            channel_expiry_secs: 600,
            free_probe_units: 1_048_576,
            ..PaidRouteChannelTerms::default()
        },
        ip_support: PaidRouteIpSupport {
            ipv4: true,
            ..PaidRouteIpSupport::default()
        },
        ..PaidExitConfig::default()
    };
    let seller_endpoint = "1.1.1.1:2122".to_string();
    let signed = nostr_vpn_core::paid_routes::signed_paid_exit_offer_from_config_with_receiver_and_fips_endpoints(
        "automatic",
        &seller,
        &offer_config,
        None,
        std::slice::from_ref(&seller_endpoint),
        None,
        now,
    )
    .expect("signed offer");
    let mut store = PaidRouteStore::default();
    store.upsert_wallet_mint(mint, "approved", Some(100_000), now);
    // The daemon receives the advert through pubsub. Opening the marketplace
    // or running `paid-exit discover` must not be required for Automatic.
    let cache_path = crate::control_pubsub_runtime::control_pubsub_store_file_path(&config_path);
    std::fs::write(
        cache_path,
        serde_json::to_vec(&json!({
            "version": 1, "events": [signed.event]
        }))
        .expect("encode received advert"),
    )
    .expect("persist received advert");
    update_paid_route_store(&store_path, |target| {
        *target = store;
        Ok(())
    })
    .expect("write store");

    let mut app = AppConfig::generated();
    app.set_internet_source(InternetSource::PaidAutomatic);
    app.fips_nostr_discovery_enabled = false;
    app.connect_to_non_roster_fips_peers = false;
    assert!(app.fips_peer_endpoints.is_empty());
    let mut automatic = PaidExitAutomaticBuyer::default();
    assert!(
        reconcile_automatic_paid_exit_selection(&mut automatic, &mut app, &config_path, now,)
            .expect("automatic selection")
    );

    assert_eq!(app.internet_source, InternetSource::PaidAutomatic);
    assert_eq!(
        app.public_paid_exit_node_pubkey_hex().as_deref(),
        Some(seller_pubkey.as_str())
    );
    let stored = load_paid_route_store(&store_path).expect("reloaded store");
    assert_eq!(stored.sessions.len(), 1);
    let session = stored.sessions.values().next().expect("probe session");
    assert_eq!(session.session.payment.paid_msat, 0);
    assert!(session.session.payment.cashu_spilman_payment.is_none());
    assert!(
        stored
            .buyer_session_allows_routing(&session.session.session_id, now)
            .expect("automatic probe route decision")
    );
    let saved = AppConfig::load(&config_path).expect("saved automatic route config");
    assert_eq!(saved.internet_source, InternetSource::PaidAutomatic);
    assert_eq!(
        saved.fips_peer_endpoint_hints(&seller_pubkey),
        vec![seller_endpoint.clone()],
        "automatic selection must dial the endpoint from the signed offer"
    );
    assert_eq!(
        saved.public_paid_exit_node_pubkey_hex().as_deref(),
        Some(seller_pubkey.as_str())
    );
    let network_id = saved.effective_network_id();
    let own_pubkey = saved.own_nostr_pubkey_hex().expect("buyer pubkey");
    let tunnel = crate::fips_private_mesh::FipsPrivateTunnelConfig::from_app(
        &saved,
        &network_id,
        "utun-test",
        Some(&own_pubkey),
        None,
        &[],
    )
    .expect("automatic paid exit tunnel config");
    let seller_peer = tunnel
        .peers
        .iter()
        .find(|peer| peer.participant_pubkey == seller_pubkey)
        .expect("selected seller tunnel peer");
    assert!(
        seller_peer
            .allowed_ips
            .iter()
            .any(|route| route == "0.0.0.0/0")
    );
    assert!(
        !crate::fips_private_mesh::effective_fips_route_targets(&tunnel, &[])
            .iter()
            .any(|route| route == "0.0.0.0/0"),
        "non-strict mode must retain the home route until the selected seller connects"
    );
    assert!(
        crate::fips_private_mesh::effective_fips_route_targets(
            &tunnel,
            &[test_peer_status(&seller_pubkey, now)],
        )
        .iter()
        .any(|route| route == "0.0.0.0/0"),
        "the automatic seller connection must activate the exit route"
    );
    assert!(!automatic.payments_allowed(&app, now));
    // A restarted buyer may already have selected this seller, but still need
    // to restore its signed endpoint before the tunnel can reconnect.
    app.set_fips_peer_endpoint_hints(&seller_pubkey, &[])
        .expect("clear seller endpoint");
    app.save(&config_path)
        .expect("save selection without endpoint");
    let mut recovered = PaidExitAutomaticBuyer::default();
    assert!(
        reconcile_automatic_paid_exit_selection(&mut recovered, &mut app, &config_path, now + 1,)
            .expect("recover automatic session")
    );
    assert_eq!(
        recovered
            .candidate
            .as_ref()
            .expect("recovered candidate")
            .session_id,
        session.session.session_id,
    );
    let saved = AppConfig::load(&config_path).expect("saved recovered endpoint");
    assert_eq!(
        saved.fips_peer_endpoint_hints(&seller_pubkey),
        vec![seller_endpoint]
    );
    assert_eq!(
        load_paid_route_store(&store_path).unwrap().sessions.len(),
        1
    );
    let _ = fs::remove_dir_all(directory);
}

#[test]
fn automatic_buyer_requires_probe_authenticated_seller_and_both_counter_directions() {
    let seller = Keys::generate();
    let seller_pubkey = seller.public_key().to_hex();
    let mut automatic = PaidExitAutomaticBuyer {
        candidate: Some(test_candidate(&seller_pubkey)),
        ..PaidExitAutomaticBuyer::default()
    };
    let mut app = AppConfig::generated();
    app.set_internet_source(InternetSource::PaidAutomatic);
    let now = 100;

    assert!(!automatic.payments_allowed(&app, now));
    let candidate = automatic.candidate.as_mut().expect("candidate");
    candidate.probe_succeeded = true;
    candidate.funded = true;
    candidate.observe_presence(&[test_peer_status("other", now)], now);
    candidate.observe_usage(
        &PaidRouteUsage {
            tx_bytes: 10,
            ..PaidRouteUsage::default()
        },
        now,
    );
    assert!(!automatic.payments_allowed(&app, now));

    automatic
        .candidate
        .as_mut()
        .expect("candidate")
        .observe_presence(&[test_peer_status(&seller_pubkey, now)], now);
    assert!(!automatic.payments_allowed(&app, now));
    automatic
        .candidate
        .as_mut()
        .expect("candidate")
        .observe_usage(
            &PaidRouteUsage {
                rx_bytes: 20,
                ..PaidRouteUsage::default()
            },
            now,
        );
    assert!(automatic.payments_allowed(&app, now));
    assert!(!automatic.payments_allowed(&app, now + PAID_EXIT_AUTO_HEALTH_TTL_SECS + 1));
}

#[test]
fn automatic_probe_waits_for_authenticated_seller_admission() {
    let seller = Keys::generate();
    let seller_pubkey = seller.public_key().to_hex();
    let mut candidate = test_candidate(&seller_pubkey);
    candidate.probe_started_at = None;
    let now = 100;

    candidate.observe_presence(&[test_peer_status(&seller_pubkey, now)], now);

    assert!(!candidate.ready_to_probe(false, now));
    assert!(candidate.ready_to_probe(true, now));
}

#[test]
fn funded_idle_provider_stays_selected_and_unanswered_traffic_gets_rechecked() {
    let seller = Keys::generate().public_key().to_hex();
    let mut candidate = test_candidate(&seller);
    candidate.funded = true;
    candidate.probe_succeeded = true;
    candidate.observe_presence(&[test_peer_status(&seller, 100)], 100);
    candidate.observe_usage(
        &PaidRouteUsage {
            tx_bytes: 10,
            rx_bytes: 20,
            ..Default::default()
        },
        100,
    );
    candidate.observe_presence(&[test_peer_status(&seller, 200)], 200);
    assert!(
        !candidate.should_failover(200),
        "healthy idle connection must stay selected"
    );
    assert!(!candidate.ready_to_probe(true, 200));
    candidate.observe_usage(
        &PaidRouteUsage {
            tx_bytes: 10,
            ..Default::default()
        },
        200,
    );
    candidate.observe_presence(&[test_peer_status(&seller, 261)], 261);
    assert!(
        candidate.ready_to_probe(true, 261),
        "check actual Internet delivery before switching provider"
    );
    assert!(!candidate.should_failover(261));
    candidate.observe_usage(
        &PaidRouteUsage {
            rx_bytes: 20,
            ..Default::default()
        },
        262,
    );
    assert!(!candidate.ready_to_probe(true, 262));
    assert!(
        candidate.should_failover(322),
        "a lost authenticated peer still needs recovery"
    );
}

#[test]
fn returning_provider_can_fund_before_probe_but_needs_fresh_health_to_stream_payments() {
    let seller = Keys::generate();
    let pubkey = seller.public_key().to_hex();
    let mut candidate = test_candidate(&pubkey);
    candidate.selection.previously_verified = true;
    candidate.probe_started_at = None;
    assert!(!candidate.ready_to_fund(100));
    candidate.observe_presence(&[test_peer_status(&pubkey, 100)], 100);
    assert!(candidate.ready_to_fund(100));
    assert!(!candidate.ready_to_probe(false, 100));
    assert!(!candidate.health_evidence_fresh(100));
    candidate.funding_attempted = true;
    candidate.funded = true;
    assert!(
        !candidate.ready_to_fund(100),
        "retry must reuse the funded channel"
    );
    assert!(candidate.ready_to_probe(true, 100));
    assert!(!candidate.health_evidence_fresh(100));
    candidate.probe_succeeded = true;
    candidate.observe_usage(
        &PaidRouteUsage {
            tx_bytes: 200,
            rx_bytes: 400,
            ..Default::default()
        },
        101,
    );
    assert!(candidate.health_evidence_fresh(101));
    let mut stranger = test_candidate(&pubkey);
    stranger.observe_presence(&[test_peer_status(&pubkey, 100)], 100);
    assert!(
        !stranger.ready_to_fund(100),
        "an unknown provider still needs a successful trial"
    );
}

#[test]
fn automatic_cancellation_never_overwrites_another_internet_mode() {
    let seller = Keys::generate();
    let seller_npub = seller.public_key().to_bech32().expect("seller npub");
    let seller_pubkey = seller.public_key().to_hex();
    let mut app = AppConfig::generated();
    app.select_public_paid_exit_node(&seller_npub)
        .expect("manual seller");
    let selected_before = app.exit_node.clone();
    let mut automatic = PaidExitAutomaticBuyer {
        candidate: Some(test_candidate(&seller_pubkey)),
        ..PaidExitAutomaticBuyer::default()
    };
    let generation = automatic.generation;
    assert!(automatic.continues_with_manual_provider(&app));
    let mut different = app.clone();
    different
        .select_public_paid_exit_node(&Keys::generate().public_key().to_hex())
        .unwrap();
    assert!(!automatic.continues_with_manual_provider(&different));
    for mode in [
        InternetSource::Direct,
        InternetSource::PrivateVpn,
        InternetSource::WireGuard,
    ] {
        different.set_internet_source(mode);
        assert!(!automatic.continues_with_manual_provider(&different));
    }

    automatic.cancel_if_disabled(&app);

    assert_eq!(app.internet_source, InternetSource::PaidManual);
    assert_eq!(app.exit_node, selected_before);
    assert!(automatic.candidate.is_none());
    assert_ne!(automatic.generation, generation);
    assert!(automatic.payments_allowed(&app, 100));
}

#[test]
fn automatic_failed_offer_is_retried_after_cooldown() {
    let mut automatic = PaidExitAutomaticBuyer::default();
    automatic.start_candidate(
        serde_json::from_value(json!({
            "offer_key": "offer",
            "mint_url": "https://mint.example",
            "channel_capacity_sat": 10,
        }))
        .expect("selection"),
        "seller".to_string(),
        "session".to_string(),
        false,
        100,
    );
    automatic.cancel_candidate(true, 120);

    assert!(automatic.rejected_offers.contains_key("offer"));
    automatic.expire_rejected_offers(120 + PAID_EXIT_AUTO_RETRY_COOLDOWN_SECS - 1);
    assert!(automatic.rejected_offers.contains_key("offer"));
    automatic.expire_rejected_offers(120 + PAID_EXIT_AUTO_RETRY_COOLDOWN_SECS);
    assert!(!automatic.rejected_offers.contains_key("offer"));
}

#[test]
fn automatic_candidate_keeps_same_offer_when_funded_capacity_becomes_authoritative() {
    let seller = Keys::generate();
    let mut candidate = test_candidate(&seller.public_key().to_hex());
    let funded = serde_json::from_value(json!({
        "offer_key": "offer",
        "mint_url": "https://mint.example",
        "channel_capacity_sat": 7,
    }))
    .expect("funded selection");

    candidate.reconcile_selection(funded);

    assert!(!candidate.failed);
    assert_eq!(candidate.selection.channel_capacity_sat, 7);

    let replacement = serde_json::from_value(json!({
        "offer_key": "replacement",
        "mint_url": "https://mint.example",
        "channel_capacity_sat": 7,
    }))
    .expect("replacement selection");
    candidate.reconcile_selection(replacement);
    assert!(candidate.failed);

    let mut candidate = test_candidate(&seller.public_key().to_hex());
    let changed_mint = serde_json::from_value(json!({
        "offer_key": "offer",
        "mint_url": "https://other-mint.example",
        "channel_capacity_sat": 7,
    }))
    .expect("changed mint selection");
    candidate.reconcile_selection(changed_mint);
    assert!(candidate.failed);
}

#[test]
fn automatic_probe_health_does_not_require_a_vendor_bandwidth_sample() {
    let measurement = PaidRouteProbeMeasurement {
        realized_exit_ip: Some("203.0.113.10".to_string()),
        observed_country_code: None,
        observed_asn: None,
        quality: Default::default(),
        samples: vec![PaidRouteProbeSample::success(
            "203.0.113.10".to_string(),
            12,
        )],
    };

    assert!(runtime::automatic_probe_observed_public_ip(&measurement));
    assert!(measurement.quality.down_bps.is_none());
    assert!(measurement.quality.up_bps.is_none());
}

fn test_candidate(seller_pubkey: &str) -> PaidExitAutomaticCandidate {
    PaidExitAutomaticCandidate {
        selection: serde_json::from_value(json!({
            "offer_key": "offer",
            "mint_url": "https://mint.example",
            "channel_capacity_sat": 10,
        }))
        .expect("selection"),
        seller_pubkey: seller_pubkey.to_string(),
        session_id: "session".to_string(),
        selected_at: 100,
        probe_started_at: Some(100),
        probe_succeeded: false,
        funding_attempted: false,
        funded: false,
        last_authenticated_at: None,
        last_tx_at: None,
        last_rx_at: None,
        unanswered_since: None,
        failed: false,
    }
}

#[test]
fn mint_outage_keeps_authenticated_provider_and_allows_payment_retry() {
    let seller = Keys::generate();
    let pubkey = seller.public_key().to_hex();
    let mut candidate = test_candidate(&pubkey);
    candidate.selection.previously_verified = true;
    candidate.probe_started_at = None;
    candidate.funding_attempted = true;
    for now in [131, 200, 500] {
        candidate.observe_presence(&[test_peer_status(&pubkey, now)], now);
        assert!(
            !candidate.should_failover(now),
            "mint outage is not a provider failure"
        );
        assert!(
            candidate.ready_to_fund(now),
            "retry the same session without another trial"
        );
        assert!(
            !candidate.ready_to_probe(true, now),
            "funding must not reuse trial admission"
        );
    }
    assert!(
        candidate.should_failover(561),
        "an unreachable provider must still fail over"
    );
}

fn test_peer_status(pubkey: &str, now: u64) -> MeshPeerStatus {
    MeshPeerStatus {
        pubkey: pubkey.to_string(),
        connected: true,
        endpoint_npub: String::new(),
        transport_addr: None,
        transport_type: None,
        srtt_ms: None,
        srtt_age_ms: None,
        link_packets_sent: 0,
        link_packets_recv: 0,
        link_bytes_sent: 0,
        link_bytes_recv: 0,
        rekey_in_progress: false,
        rekey_draining: false,
        current_k_bit: None,
        last_outbound_route: None,
        direct_probe_pending: false,
        direct_probe_after_ms: None,
        direct_probe_retry_count: 0,
        direct_probe_auto_reconnect: false,
        direct_probe_expires_at_ms: None,
        nostr_traversal_consecutive_failures: 0,
        nostr_traversal_in_cooldown: false,
        nostr_traversal_cooldown_until_ms: None,
        nostr_traversal_last_observed_skew_ms: None,
        last_seen_at: Some(now),
        last_control_seen_at: Some(now),
        last_data_seen_at: Some(now),
        tx_bytes: 0,
        rx_bytes: 0,
        error: None,
    }
}