rayfish 0.1.4

P2P mesh VPN powered by iroh — connect peers by cryptographic identity, not IP address
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
//! Length-prefixed msgpack control protocol over QUIC bidirectional streams.
//!
//! Each message is encoded as a 4-byte big-endian length prefix followed by a msgpack body.
//! Control messages manage membership (join, approve, sync) and mesh topology (hello, reconnect).

use std::net::Ipv4Addr;

use anyhow::{Context, Result};
use iroh::endpoint::{RecvStream, SendStream};
use iroh::{EndpointId, SecretKey, Signature};
use serde::{Deserialize, Serialize};

use crate::membership::{ApprovedEntry, Member};

/// Certificate proving a device belongs to a user identity.
///
/// The user's private key signs the device's public key. Any peer can verify
/// the binding using only the user's public key.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeviceCert {
    pub user_identity: EndpointId,
    pub device_key: EndpointId,
    pub signature: Signature,
}

impl DeviceCert {
    pub fn create(user_secret: &SecretKey, device_pubkey: &EndpointId) -> Self {
        let signature = user_secret.sign(device_pubkey.as_bytes());
        Self {
            user_identity: user_secret.public(),
            device_key: *device_pubkey,
            signature,
        }
    }

    pub fn verify(&self) -> bool {
        self.user_identity
            .verify(self.device_key.as_bytes(), &self.signature)
            .is_ok()
    }
}

/// Messages for the device pairing protocol (ALPN `rayfish/pair/1`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PairMsg {
    Request {
        secret: [u8; 32],
        device_pubkey: EndpointId,
    },
    Response {
        cert: DeviceCert,
    },
}

/// Messages for the `ray connect` friend-request handshake (ALPN
/// `rayfish/connect/1`). The initiator (A) dials the recipient's (B) contact
/// key, sends `Request`, and polls until `Approved`. Approval is recipient-only:
/// only B acts, A just waits.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConnectMsg {
    /// A → B: request a direct connection. `from_endpoint` is A's transport id
    /// (the key B pre-approves into the minted network); `from_contact_id` is
    /// A's own contact key (for display/dedupe on B).
    Request {
        from_contact_id: EndpointId,
        from_endpoint: EndpointId,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        hostname: Option<String>,
    },
    /// B → A: queued, not yet approved. A retries with backoff.
    Pending,
    /// B → A: approved. Carries the minted 2-peer network's room id and B as the
    /// pinned coordinator, so A joins it like an invite-pinned join.
    Approved {
        room_id: EndpointId,
        coordinator: EndpointId,
    },
    /// B → A: request rejected.
    Denied { reason: String },
}

/// Control messages exchanged between peers over QUIC bidirectional streams.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ControlMsg {
    /// Sent by a joining peer as the first message on an initial (non-reconnect)
    /// join. Carries an optional invite secret (for invite-gated admission) and
    /// the joiner's desired hostname/device cert. The coordinator branches on the
    /// secret and the network's access mode to admit, gate, or deny.
    JoinRequest {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        invite_secret: Option<Vec<u8>>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        hostname: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        device_cert: Option<DeviceCert>,
    },
    /// Coordinator response telling the joiner it has been queued for live
    /// approval (closed network, no invite). The joiner retries until accepted.
    JoinPending,
    JoinApproved {
        your_ip: Ipv4Addr,
        members: Vec<Member>,
    },
    JoinDenied {
        reason: String,
    },
    /// Notify connected members that the roster/blob changed. Payload-free: it
    /// is a *trigger only*. Receivers reconverge from the network-key-signed
    /// pkarr record, never from any peer-supplied membership data.
    MemberSync,
    MeshHello {
        identity: EndpointId,
        ip: Ipv4Addr,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        hostname: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        device_cert: Option<DeviceCert>,
    },
    MemberApproved {
        identity: EndpointId,
        ip: Ipv4Addr,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        hostname: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        device_cert: Option<DeviceCert>,
    },
    Welcome {
        members: Vec<Member>,
        approved: Vec<ApprovedEntry>,
    },
    /// Notify connected members that the signed group blob changed. Payload-free:
    /// a *trigger only*. Receivers reconverge from the network-key-signed pkarr
    /// record, never from any peer-supplied hash.
    BlobUpdated,
    /// Coordinator grants the per-network secret key to another member, making it
    /// a co-coordinator (can publish the signed blob / suggest firewall rules).
    /// Sent over the network's authenticated mesh ALPN, so only the targeted peer
    /// receives it. The recipient stores the key and spawns a publisher.
    AdminGrant {
        network_pubkey: EndpointId,
        /// 32-byte per-network secret key (`SecretKey::to_bytes`).
        secret_key: [u8; 32],
    },
    FileOffer {
        from: EndpointId,
        filename: String,
        size: u64,
        mime_type: String,
        blob_hash: blake3::Hash,
    },
    /// Coordinator → coordinators: share a minted single-use invite's hash so any
    /// coordinator can redeem it. Carries the hash only, never the secret.
    InviteShare {
        id: String,
        secret_hash: Vec<u8>,
        expires: u64,
    },
    /// Coordinator → coordinators: a shared single-use invite was redeemed; burn it.
    InviteUsed {
        secret_hash: Vec<u8>,
    },
    /// Active liveness probe for `ray ping`. The receiver echoes back a `Pong`
    /// carrying the same nonce over a fresh stream (the control readers drop the
    /// stream's send half, so the reply cannot ride the request stream). The
    /// pinging side correlates by nonce to measure round-trip time.
    Ping {
        nonce: u64,
    },
    /// Echo reply to a `Ping`, carrying the originating nonce.
    Pong {
        nonce: u64,
    },
}

pub fn encode_msg(msg: &ControlMsg) -> Vec<u8> {
    let body = rmp_serde::to_vec_named(msg).expect("serialize control message");
    let len = (body.len() as u32).to_be_bytes();
    [len.as_slice(), &body].concat()
}

#[cfg(test)]
fn decode_msg(data: &[u8]) -> Result<ControlMsg> {
    anyhow::ensure!(data.len() >= 4, "message too short");
    let len = u32::from_be_bytes(data[..4].try_into().unwrap()) as usize;
    anyhow::ensure!(data.len() >= 4 + len, "incomplete message");
    rmp_serde::from_slice(&data[4..4 + len]).context("invalid control message")
}

pub async fn send_msg(stream: &mut SendStream, msg: &ControlMsg) -> Result<()> {
    let data = encode_msg(msg);
    stream
        .write_all(&data)
        .await
        .context("send control message")?;
    // Finish the stream so the FIN flushes the message. The protocol sends
    // exactly one message per bidirectional stream (the reader does
    // `accept_bi → recv_msg` in a loop), so finishing here is always correct.
    // Without it, dropping the `SendStream` resets it (RESET_STREAM) and the
    // peer loses any data not yet acknowledged — e.g. roster broadcasts sent
    // over a persistent connection. (Delivery before a *connection* drop still
    // needs the caller to wait on `conn.closed()`.)
    let _ = stream.finish();
    Ok(())
}

pub async fn recv_msg(stream: &mut RecvStream) -> Result<ControlMsg> {
    recv_framed(stream).await
}

/// Send any serializable message as a length-prefixed msgpack frame, then finish
/// the stream (same one-message-per-stream contract as [`send_msg`]). Used by
/// the `ray connect` handshake (`ConnectMsg`).
pub async fn send_framed<T: Serialize>(stream: &mut SendStream, msg: &T) -> Result<()> {
    let body = rmp_serde::to_vec_named(msg).context("serialize framed message")?;
    let len = (body.len() as u32).to_be_bytes();
    stream
        .write_all(&[len.as_slice(), &body].concat())
        .await
        .context("send framed message")?;
    let _ = stream.finish();
    Ok(())
}

/// Read a length-prefixed msgpack frame into any deserializable type.
pub async fn recv_framed<T: serde::de::DeserializeOwned>(stream: &mut RecvStream) -> Result<T> {
    let mut len_buf = [0u8; 4];
    stream
        .read_exact(&mut len_buf)
        .await
        .context("read message length")?;
    let len = u32::from_be_bytes(len_buf) as usize;
    anyhow::ensure!(len <= 65536, "framed message too large");
    let mut body = vec![0u8; len];
    stream
        .read_exact(&mut body)
        .await
        .context("read message body")?;
    rmp_serde::from_slice(&body).context("decode framed message")
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_id(seed: u8) -> EndpointId {
        let mut key_bytes = [0u8; 32];
        key_bytes[0] = seed;
        iroh::SecretKey::from(key_bytes).public()
    }

    #[test]
    fn test_roundtrip_join_approved() {
        let msg = ControlMsg::JoinApproved {
            your_ip: Ipv4Addr::new(100, 64, 0, 3),
            members: vec![Member {
                identity: test_id(1),
                ip: Ipv4Addr::new(100, 64, 0, 2),
                is_coordinator: true,
                hostname: None,
                user_identity: None,
                device_cert: None,
                collision_index: 0,
            }],
        };
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn test_roundtrip_mesh_hello() {
        let msg = ControlMsg::MeshHello {
            identity: test_id(1),
            ip: Ipv4Addr::new(100, 64, 0, 4),
            hostname: None,
            device_cert: None,
        };
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn test_roundtrip_join_request() {
        let msg = ControlMsg::JoinRequest {
            invite_secret: Some(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]),
            hostname: Some("alice".to_string()),
            device_cert: None,
        };
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn test_roundtrip_join_request_no_invite() {
        let msg = ControlMsg::JoinRequest {
            invite_secret: None,
            hostname: None,
            device_cert: None,
        };
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn test_roundtrip_join_pending() {
        let msg = ControlMsg::JoinPending;
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn test_roundtrip_connect_msg() {
        let msgs = vec![
            ConnectMsg::Request {
                from_contact_id: test_id(1),
                from_endpoint: test_id(2),
                hostname: Some("dario".to_string()),
            },
            ConnectMsg::Pending,
            ConnectMsg::Approved {
                room_id: test_id(3),
                coordinator: test_id(4),
            },
            ConnectMsg::Denied {
                reason: "no".to_string(),
            },
        ];
        for msg in msgs {
            let body = rmp_serde::to_vec_named(&msg).unwrap();
            let decoded: ConnectMsg = rmp_serde::from_slice(&body).unwrap();
            assert_eq!(msg, decoded);
        }
    }

    #[test]
    fn test_roundtrip_join_denied() {
        let msg = ControlMsg::JoinDenied {
            reason: "not authorized".to_string(),
        };
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn test_roundtrip_member_sync() {
        let msg = ControlMsg::MemberSync;
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn test_roundtrip_member_approved() {
        let msg = ControlMsg::MemberApproved {
            identity: test_id(1),
            ip: Ipv4Addr::new(100, 64, 12, 34),
            hostname: None,
            device_cert: None,
        };
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn test_roundtrip_welcome() {
        use crate::membership::ApprovedEntry;
        let msg = ControlMsg::Welcome {
            members: vec![Member {
                identity: test_id(1),
                ip: Ipv4Addr::new(100, 64, 0, 2),
                is_coordinator: true,
                hostname: None,
                user_identity: None,
                device_cert: None,
                collision_index: 0,
            }],
            approved: vec![ApprovedEntry {
                identity: test_id(2),
                ip: Ipv4Addr::new(100, 64, 0, 5),
                hostname: None,
                user_identity: None,
                device_cert: None,
                collision_index: 0,
            }],
        };
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn test_roundtrip_file_offer() {
        let msg = ControlMsg::FileOffer {
            from: test_id(1),
            filename: "report.pdf".to_string(),
            size: 1_048_576,
            mime_type: "application/pdf".to_string(),
            blob_hash: blake3::hash(b"file contents"),
        };
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn test_roundtrip_blob_updated() {
        let msg = ControlMsg::BlobUpdated;
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn test_roundtrip_admin_grant() {
        let key = test_key(7);
        let msg = ControlMsg::AdminGrant {
            network_pubkey: test_id(1),
            secret_key: key.to_bytes(),
        };
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
        if let ControlMsg::AdminGrant { secret_key, .. } = decoded {
            assert_eq!(SecretKey::from(secret_key).public(), key.public());
        }
    }

    fn test_key(seed: u8) -> SecretKey {
        let mut key_bytes = [0u8; 32];
        key_bytes[0] = seed;
        SecretKey::from(key_bytes)
    }

    #[test]
    fn test_device_cert_sign_verify() {
        let user_key = test_key(1);
        let device_key = test_key(2);
        let cert = DeviceCert::create(&user_key, &device_key.public());
        assert!(cert.verify());
        assert_eq!(cert.user_identity, user_key.public());
        assert_eq!(cert.device_key, device_key.public());
    }

    #[test]
    fn test_device_cert_rejects_wrong_signer() {
        let user_key = test_key(1);
        let device_key = test_key(2);
        let wrong_key = test_key(3);
        let mut cert = DeviceCert::create(&user_key, &device_key.public());
        cert.user_identity = wrong_key.public();
        assert!(!cert.verify());
    }

    #[test]
    fn test_roundtrip_mesh_hello_with_cert() {
        let user_key = test_key(1);
        let device_key = test_key(2);
        let cert = DeviceCert::create(&user_key, &device_key.public());
        let msg = ControlMsg::MeshHello {
            identity: device_key.public(),
            ip: Ipv4Addr::new(100, 64, 0, 5),
            hostname: Some("alice".to_string()),
            device_cert: Some(cert),
        };
        let bytes = encode_msg(&msg);
        let decoded = decode_msg(&bytes).unwrap();
        assert_eq!(msg, decoded);
        if let ControlMsg::MeshHello {
            device_cert: Some(c),
            ..
        } = &decoded
        {
            assert!(c.verify());
        } else {
            panic!("expected MeshHello with cert");
        }
    }

    #[test]
    fn test_roundtrip_invite_share_and_used() {
        for msg in [
            ControlMsg::InviteShare {
                id: "ab3f".into(),
                secret_hash: vec![1, 2, 3],
                expires: 42,
            },
            ControlMsg::InviteUsed {
                secret_hash: vec![4, 5, 6],
            },
        ] {
            let bytes = encode_msg(&msg);
            assert_eq!(decode_msg(&bytes).unwrap(), msg);
        }
    }

    #[test]
    fn test_roundtrip_ping_pong() {
        for msg in [
            ControlMsg::Ping { nonce: 0 },
            ControlMsg::Ping {
                nonce: u64::MAX,
            },
            ControlMsg::Pong {
                nonce: 0x0123_4567_89ab_cdef,
            },
        ] {
            let bytes = encode_msg(&msg);
            assert_eq!(decode_msg(&bytes).unwrap(), msg);
        }
    }
}