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
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
// After a roster reload replaces the runtime, give the fresh authenticated
// carrier enough time to finish instead of repeatedly aborting and restarting
// its connection inside the 15-second public-UI join deadline.
pub(crate) const JOIN_ROSTER_DELIVERY_TIMEOUT: Duration = Duration::from_secs(12);

fn roster_control_frame(signed_roster: SignedRoster) -> Result<FipsControlFrame> {
    Ok(FipsControlFrame::Roster {
        network_id: signed_roster.network_id()?,
        roster: signed_roster.roster()?,
        signed_roster: Some(Box::new(signed_roster)),
    })
}

impl FipsPrivateMeshRuntime {
    pub(crate) async fn ping_peers(&self, network_id: &str, now: u64) -> Result<usize> {
        let participants = self.ping_due_participants(now)?;
        self.ping_participants(network_id, now, participants).await
    }

    /// A manual join starts before its administrator has a usable return path.
    /// Retry every daemon heartbeat while identity confirmation is pending;
    /// the ordinary offline-peer cadence would otherwise suppress the first
    /// probe sent after routed discovery becomes usable.
    pub(crate) async fn ping_pending_join_peers(
        &self,
        network_id: &str,
        now: u64,
    ) -> Result<usize> {
        let participants = self.mesh.load().peer_pubkeys();
        self.ping_participants(network_id, now, participants).await
    }

    async fn ping_participants(
        &self,
        network_id: &str,
        now: u64,
        participants: Vec<String>,
    ) -> Result<usize> {
        let frame = FipsControlFrame::Ping {
            network_id: network_id.to_string(),
            sent_at: now,
        };
        let mut sent = 0usize;
        for participant in participants {
            self.note_ping_attempt(&participant, now)?;
            if self.send_probe_frame(&participant, &frame).await.is_ok() {
                sent += 1;
            }
        }
        Ok(sent)
    }

    pub(crate) fn enqueue_join_request(
        &self,
        control: &FipsControlTcpSender,
        participant: &str,
        requested_at: u64,
        request: MeshJoinRequest,
    ) -> Result<()> {
        self.enqueue_stateful_control_frame(
            control,
            participant,
            &FipsControlFrame::JoinRequest {
                requested_at,
                request,
            },
        )
    }

    pub(crate) fn enqueue_roster(
        &self,
        control: &FipsControlTcpSender,
        participant: &str,
        signed_roster: SignedRoster,
    ) -> Result<()> {
        self.enqueue_stateful_control_frame(
            control,
            participant,
            &roster_control_frame(signed_roster)?,
        )
    }

    pub(crate) fn roster_delivery(
        self: &Arc<Self>,
        control: FipsControlTcpSender,
        participant: String,
        signed_roster: SignedRoster,
    ) -> Result<FipsRosterDelivery> {
        let destination = control_frame_destination_peer(
            &self.mesh.load(),
            &self.peer_identities.load(),
            &participant,
        )?;
        let frame = roster_control_frame(signed_roster)?;
        let runtime = Arc::clone(self);
        Ok(Box::pin(async move {
            let sent_len = control.send(destination, &frame).await?;
            runtime.note_tx(
                Some(&participant),
                participant_pubkey_bytes(&participant).as_ref(),
                sent_len,
            )
        }))
    }

    pub(crate) fn join_roster_delivery(
        self: &Arc<Self>,
        control: FipsControlTcpSender,
        participant: String,
        join_roster: JoinRosterControl,
    ) -> Result<FipsRosterDelivery> {
        let participant_key = participant_pubkey_bytes(&participant);
        let destination = {
            let mesh = self.mesh.load();
            let peer_identities = self.peer_identities.load();
            control_frame_destination_peer(&mesh, &peer_identities, &participant)?
        };
        let runtime = Arc::clone(self);
        Ok(Box::pin(async move {
            let sent_len = send_join_roster_with_receipt(
                &control,
                destination,
                &join_roster,
                JOIN_ROSTER_DELIVERY_TIMEOUT,
            )
            .await
            .with_context(|| {
                format!("failed to deliver and apply FIPS-TCP join roster to {participant}")
            })?;
            runtime.note_join_roster_receipt(&participant)?;
            runtime.note_tx(Some(&participant), participant_key.as_ref(), sent_len)
        }))
    }

    /// A matching application receipt is authenticated inbound control
    /// traffic from the newly rostered participant. Surface that proven
    /// liveness immediately instead of waiting for the next periodic ping.
    pub(crate) fn note_join_roster_receipt(&self, participant: &str) -> Result<()> {
        self.note_control_rx(participant, 0, unix_timestamp())
    }

    pub(crate) async fn send_join_roster_ack(
        &self,
        control: &FipsControlTcpRuntime,
        participant: &str,
        roster_event_id: String,
    ) -> Result<()> {
        self.send_stateful_control_frame(
            control,
            participant,
            &FipsControlFrame::JoinRosterAck { roster_event_id },
        )
        .await
    }

    pub(crate) fn enqueue_capabilities(
        &self,
        control: &FipsControlTcpSender,
        participant: &str,
        network_id: &str,
        capabilities: PeerCapabilities,
    ) -> Result<()> {
        self.enqueue_stateful_control_frame(
            control,
            participant,
            &FipsControlFrame::Capabilities {
                network_id: network_id.to_string(),
                capabilities,
            },
        )
    }

    #[cfg(feature = "paid-exit")]
    pub(crate) async fn send_paid_route_session_open(
        &self,
        control: &FipsControlTcpRuntime,
        seller: &str,
        open: PaidRouteSessionOpen,
    ) -> Result<()> {
        self.send_stateful_control_frame(
            control,
            seller,
            &FipsControlFrame::PaidRouteSessionOpen { open },
        )
        .await
    }

    #[cfg(feature = "paid-exit")]
    pub(crate) async fn send_paid_route_session_open_ack(
        &self,
        control: &FipsControlTcpRuntime,
        buyer: &str,
        lease_id: String,
    ) -> Result<()> {
        self.send_stateful_control_frame(
            control,
            buyer,
            &FipsControlFrame::PaidRouteSessionOpenAck { lease_id },
        )
        .await
    }

    #[cfg(feature = "paid-exit")]
    pub(crate) fn enqueue_paid_route_payment(
        &self,
        control: &FipsControlTcpSender,
        seller: &str,
        id: String,
        envelope: StreamingRoutePaymentEnvelope,
    ) -> Result<()> {
        self.enqueue_stateful_control_frame(
            control,
            seller,
            &FipsControlFrame::PaidRoutePayment { id, envelope },
        )
    }

    #[cfg(feature = "paid-exit")]
    pub(crate) async fn send_paid_route_payment_ack(
        &self,
        control: &FipsControlTcpRuntime,
        buyer: &str,
        id: String,
    ) -> Result<()> {
        self.send_stateful_control_frame(
            control,
            buyer,
            &FipsControlFrame::PaidRoutePaymentAck { id },
        )
        .await
    }

    async fn send_stateful_control_frame(
        &self,
        control: &FipsControlTcpRuntime,
        participant: &str,
        frame: &FipsControlFrame,
    ) -> Result<()> {
        let participant_key = participant_pubkey_bytes(participant);
        let destination = {
            let mesh = self.mesh.load();
            let peer_identities = self.peer_identities.load();
            control_frame_destination_peer(&mesh, &peer_identities, participant)?
        };
        let sent_len = control
            .send(destination, frame)
            .await
            .with_context(|| format!("failed to send FIPS-TCP control frame to {participant}"))?;
        self.note_tx(Some(participant), participant_key.as_ref(), sent_len)?;
        Ok(())
    }

    fn enqueue_stateful_control_frame(
        &self,
        control: &FipsControlTcpSender,
        participant: &str,
        frame: &FipsControlFrame,
    ) -> Result<()> {
        let participant_key = participant_pubkey_bytes(participant);
        let destination = {
            let mesh = self.mesh.load();
            let peer_identities = self.peer_identities.load();
            control_frame_destination_peer(&mesh, &peer_identities, participant)?
        };
        let queued_len = control
            .enqueue(destination, frame)
            .with_context(|| format!("failed to queue FIPS-TCP control frame to {participant}"))?;
        self.note_tx(Some(participant), participant_key.as_ref(), queued_len)
    }

    async fn send_probe_frame(&self, participant: &str, frame: &FipsControlFrame) -> Result<()> {
        if !matches!(
            frame,
            FipsControlFrame::Ping { .. } | FipsControlFrame::Pong { .. }
        ) {
            return Err(anyhow!("stateful control frames require FIPS-TCP"));
        }
        let participant_key = participant_pubkey_bytes(participant);
        let destination = {
            let mesh = self.mesh.load();
            let peer_identities = self.peer_identities.load();
            control_frame_destination_peer(&mesh, &peer_identities, participant)?
        };
        let encoded = encode_fips_control_frame(frame)?;
        let sent_len = encoded.len();
        self.endpoint
            .send_batch_to_peer(destination, vec![encoded])
            .await
            .with_context(|| format!("failed to send FIPS probe to {participant}"))?;
        self.note_tx(Some(participant), participant_key.as_ref(), sent_len)
    }

    fn note_tx(
        &self,
        participant: Option<&str>,
        participant_key: Option<&ParticipantPubkeyBytes>,
        len: usize,
    ) -> Result<()> {
        // Hot path. Dataplane callers pass the already-parsed participant key
        // from FipsMeshRuntime, avoiding per-packet pubkey parsing or
        // string-key hashing for configured peers.
        let parsed_participant_key = participant_key
            .is_none()
            .then(|| participant.and_then(participant_pubkey_bytes))
            .flatten();
        let participant_key = participant_key.or(parsed_participant_key.as_ref());
        let peer_activity = self.peer_activity.load();
        if let Some(activity) = participant_key.and_then(|key| peer_activity.get(key)) {
            activity.note_tx(len);
            return Ok(());
        }
        drop(peer_activity);
        let participant = participant
            .map(str::to_owned)
            .or_else(|| participant_key.map(hex::encode))
            .ok_or_else(|| anyhow!("missing FIPS participant identity for tx accounting"))?;
        let mut presence = self
            .presence
            .write()
            .map_err(|_| anyhow!("FIPS mesh presence lock poisoned"))?;
        if let Some(entry) = presence.get_mut(&participant) {
            entry.tx_bytes = entry.tx_bytes.saturating_add(len as u64);
        } else {
            let entry = FipsPeerPresence {
                tx_bytes: len as u64,
                ..Default::default()
            };
            presence.insert(participant, entry);
        }
        Ok(())
    }

    fn note_ping_attempt(&self, participant: &str, now: u64) -> Result<()> {
        let mut presence = self
            .presence
            .write()
            .map_err(|_| anyhow!("FIPS mesh presence lock poisoned"))?;
        if let Some(entry) = presence.get_mut(participant) {
            entry.last_ping_sent_at = Some(now);
            entry.last_ping_started_at = Some(Instant::now());
        } else {
            let entry = FipsPeerPresence {
                last_ping_sent_at: Some(now),
                last_ping_started_at: Some(Instant::now()),
                ..Default::default()
            };
            presence.insert(participant.to_string(), entry);
        }
        Ok(())
    }

    fn note_pong(&self, participant: &str, sent_at: u64) -> Result<()> {
        let mut presence = self
            .presence
            .write()
            .map_err(|_| anyhow!("FIPS mesh presence lock poisoned"))?;
        let Some(entry) = presence.get_mut(participant) else {
            return Ok(());
        };
        if entry.last_ping_sent_at == Some(sent_at)
            && let Some(started_at) = entry.last_ping_started_at.take()
        {
            let elapsed_ms = started_at.elapsed().as_millis();
            if elapsed_ms <= FIPS_CONTROL_RTT_MAX_ACCEPT_MS {
                entry.rtt_ms = Some(elapsed_ms.min(u128::from(u64::MAX)) as u64);
            } else {
                entry.last_ping_sent_at = None;
            }
        }
        Ok(())
    }

    fn note_control_rx(&self, participant: &str, len: usize, now: u64) -> Result<()> {
        self.note_rx(participant, None, len, now, FipsPeerRxKind::Control)
    }

    fn note_data_rx(
        &self,
        participant: &str,
        participant_key: Option<&ParticipantPubkeyBytes>,
        len: usize,
        now: u64,
    ) -> Result<()> {
        self.note_rx(participant, participant_key, len, now, FipsPeerRxKind::Data)
    }

    fn note_rx(
        &self,
        participant: &str,
        participant_key: Option<&ParticipantPubkeyBytes>,
        len: usize,
        now: u64,
        kind: FipsPeerRxKind,
    ) -> Result<()> {
        let parsed_participant_key = participant_key
            .is_none()
            .then(|| participant_pubkey_bytes(participant))
            .flatten();
        let participant_key = participant_key.or(parsed_participant_key.as_ref());
        let peer_activity = self.peer_activity.load();
        if let Some(activity) = participant_key.and_then(|key| peer_activity.get(key)) {
            activity.note_rx(len, now, kind);
            return Ok(());
        }
        drop(peer_activity);
        let mut presence = self
            .presence
            .write()
            .map_err(|_| anyhow!("FIPS mesh presence lock poisoned"))?;
        if let Some(entry) = presence.get_mut(participant) {
            entry.last_seen_at = Some(now);
            match kind {
                FipsPeerRxKind::Control => entry.last_control_seen_at = Some(now),
                FipsPeerRxKind::Data => entry.last_data_seen_at = Some(now),
            }
            entry.rx_bytes = entry.rx_bytes.saturating_add(len as u64);
            entry.error = None;
        } else {
            let mut entry = FipsPeerPresence {
                last_seen_at: Some(now),
                rx_bytes: len as u64,
                error: None,
                ..Default::default()
            };
            match kind {
                FipsPeerRxKind::Control => entry.last_control_seen_at = Some(now),
                FipsPeerRxKind::Data => entry.last_data_seen_at = Some(now),
            }
            presence.insert(participant.to_string(), entry);
        }
        Ok(())
    }

    #[cfg(feature = "paid-exit")]
    pub(crate) fn set_paid_route_accounting_peers(
        &self,
        participants: Vec<FipsPaidRouteAccountingPeer>,
    ) -> Result<()> {
        let mut accounting = self
            .paid_route_accounting
            .lock()
            .map_err(|_| anyhow!("FIPS paid route accounting lock poisoned"))?;
        accounting.replace_peers(participants);
        Ok(())
    }

    #[cfg(feature = "paid-exit")]
    pub(crate) fn drain_paid_route_usage(&self, participant: &str) -> Result<PaidRouteUsage> {
        let mut accounting = self
            .paid_route_accounting
            .lock()
            .map_err(|_| anyhow!("FIPS paid route accounting lock poisoned"))?;
        Ok(accounting.drain(participant))
    }

    #[cfg(feature = "paid-exit")]
    fn note_paid_route_outbound_packet(
        &self,
        participant: Option<&str>,
        participant_key: Option<&ParticipantPubkeyBytes>,
        packet: &[u8],
    ) -> Result<()> {
        let mut accounting = self
            .paid_route_accounting
            .lock()
            .map_err(|_| anyhow!("FIPS paid route accounting lock poisoned"))?;
        accounting.record_outbound(participant, participant_key, packet);
        Ok(())
    }

    #[cfg(feature = "paid-exit")]
    fn note_paid_route_inbound_packet(
        &self,
        participant: Option<&str>,
        participant_key: Option<&ParticipantPubkeyBytes>,
        packet: &[u8],
    ) -> Result<()> {
        let mut accounting = self
            .paid_route_accounting
            .lock()
            .map_err(|_| anyhow!("FIPS paid route accounting lock poisoned"))?;
        accounting.record_inbound(participant, participant_key, packet);
        Ok(())
    }

    #[cfg(all(
        feature = "paid-exit",
        any(target_os = "linux", target_os = "macos", target_os = "windows")
    ))]
    fn note_paid_route_inbound_batch(
        &self,
        mesh: &FipsMeshRuntime,
        packets: &DirectTunWriteBatch,
    ) -> Result<()> {
        if packets.is_empty() {
            return Ok(());
        }
        let mut accounting = self
            .paid_route_accounting
            .lock()
            .map_err(|_| anyhow!("FIPS paid route accounting lock poisoned"))?;
        for run in &packets.runs {
            let admitter = direct_run_admitter(mesh, run)?;
            for packet in run.packet_slices() {
                accounting.record_inbound(None, admitter.source_pubkey_bytes(), packet);
            }
        }
        Ok(())
    }

    /// Hand the latest peer roster to fips without restarting the endpoint.
    ///
    /// The wrapper translates nvpn's intermediate hint shape
    /// ([`FipsEndpointPeerTransportConfig`]) into `fips_endpoint::PeerConfig`
    /// (carrying `seen_at_ms` per address) and calls
    /// [`fips_endpoint::FipsEndpoint::update_peers`]. fips diffs new vs old,
    /// initiates connections for fresh npubs, drops retry entries for
    /// removed ones, and refreshes address hints in place for the rest.
    pub(crate) async fn update_peers(
        &self,
        endpoint_peers: &[FipsEndpointPeerTransportConfig],
    ) -> Result<fips_endpoint::UpdatePeersOutcome> {
        let peers: Vec<FipsPeerConfig> = endpoint_peers
            .iter()
            .map(|peer| FipsPeerConfig {
                npub: peer.npub.clone(),
                alias: None,
                addresses: peer
                    .addresses
                    .iter()
                    .flat_map(fips_peer_addresses_from_hint)
                    .collect(),
                connect_policy: if peer.connect_on_start {
                    ConnectPolicy::AutoConnect
                } else {
                    ConnectPolicy::Manual
                },
                auto_reconnect: peer.auto_reconnect,
                discovery_fallback_transit: peer.discovery_fallback_transit,
            })
            .collect();
        self.endpoint
            .update_peers(peers)
            .await
            .context("fips: update_peers rejected by endpoint")
    }

    pub(crate) async fn refresh_peer_paths(
        &self,
        endpoint_peers: &[FipsEndpointPeerTransportConfig],
    ) -> Result<usize> {
        let peers = endpoint_peers
            .iter()
            .map(|peer| {
                PeerIdentity::from_npub(&peer.npub)
                    .with_context(|| format!("invalid FIPS endpoint peer npub {}", peer.npub))
            })
            .collect::<Result<Vec<_>>>()?;
        self.endpoint
            .refresh_peer_paths(peers)
            .await
            .context("fips: refresh_peer_paths rejected by endpoint")
    }

    pub(crate) async fn rebind_network_transports(
        &self,
        bind_interface: Option<String>,
    ) -> Result<usize> {
        self.endpoint
            .rebind_network_transports(bind_interface)
            .await
            .context("fips: network transport rebind rejected by endpoint")
    }
}