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
use crate::discovery::Discovery;
use anyhow::Result;
use iroh_net::defaults::{default_derp_map, TEST_REGION_ID};
use iroh_net::derp::{DerpMap, DerpMode};
use iroh_net::key::SecretKey;
use iroh_net::magic_endpoint::accept_conn;
use iroh_net::{MagicEndpoint, NodeAddr};
use pkarr::url::Url;
use pkarr::DEFAULT_PKARR_RELAY;
use std::time::Duration;

mod discovery;
mod protocol;

pub use crate::protocol::{
    NotificationHandler, Protocol, ProtocolHandler, ProtocolHandlerBuilder, RequestHandler,
    Subscription, SubscriptionHandler,
};
pub use quinn::{Connection, RecvStream, SendStream};
pub type PeerId = iroh_net::key::PublicKey;

pub struct EndpointBuilder {
    alpn: Vec<u8>,
    handler: Option<ProtocolHandler>,
    secret: Option<[u8; 32]>,
    port: u16,
    pkarr_relay: Option<Url>,
    derp_map: Option<DerpMap>,
    ttl: Option<u32>,
    enable_dht: bool,
    enable_mdns: bool,
    republish: bool,
}

impl EndpointBuilder {
    pub fn new(alpn: Vec<u8>) -> Self {
        Self {
            alpn,
            handler: None,
            secret: None,
            port: 0,
            pkarr_relay: None,
            derp_map: None,
            ttl: None,
            enable_dht: false,
            enable_mdns: false,
            republish: true,
        }
    }

    pub fn secret(&mut self, secret: [u8; 32]) -> &mut Self {
        self.secret = Some(secret);
        self
    }

    pub fn port(&mut self, port: u16) -> &mut Self {
        self.port = port;
        self
    }

    pub fn handler(&mut self, handler: ProtocolHandler) -> &mut Self {
        self.handler = Some(handler);
        self
    }

    pub fn relay(&mut self, relay: Url) -> &mut Self {
        self.pkarr_relay = Some(relay);
        self
    }

    pub fn localhost_relay(&mut self) -> &mut Self {
        self.relay("http://127.0.0.1:6881".parse().unwrap())
    }

    pub fn pkarr_relay(&mut self) -> &mut Self {
        self.relay(DEFAULT_PKARR_RELAY.parse().unwrap())
    }

    pub fn derp_map(&mut self, map: DerpMap) -> &mut Self {
        self.derp_map = Some(map);
        self
    }

    pub fn localhost_derp_map(&mut self) -> &mut Self {
        self.derp_map(DerpMap::from_url(
            "http://127.0.0.1:3340".parse().unwrap(),
            TEST_REGION_ID,
        ))
    }

    pub fn iroh_derp_map(&mut self) -> &mut Self {
        self.derp_map(default_derp_map())
    }

    pub fn enable_dht(&mut self) -> &mut Self {
        self.enable_dht = true;
        self
    }

    pub fn enable_mdns(&mut self) -> &mut Self {
        self.enable_mdns = true;
        self
    }

    pub fn republish(&mut self, republish: bool) -> &mut Self {
        self.republish = republish;
        self
    }

    pub fn ttl(&mut self, ttl: u32) -> &mut Self {
        self.ttl = Some(ttl);
        self
    }

    pub async fn build(self) -> Result<Endpoint> {
        let secret = self.secret.unwrap_or_else(|| {
            let mut secret = [0; 32];
            getrandom::getrandom(&mut secret).unwrap();
            secret
        });
        let ttl = self.ttl.unwrap_or(30);
        Endpoint::new(
            secret,
            self.alpn,
            self.port,
            self.pkarr_relay,
            self.republish,
            self.derp_map,
            self.handler,
            ttl,
            self.enable_dht,
            self.enable_mdns,
        )
        .await
    }
}

#[derive(Clone)]
pub struct Endpoint {
    alpn: Vec<u8>,
    endpoint: MagicEndpoint,
}

impl Endpoint {
    pub fn builder(alpn: Vec<u8>) -> EndpointBuilder {
        EndpointBuilder::new(alpn)
    }

    async fn new(
        secret: [u8; 32],
        alpn: Vec<u8>,
        port: u16,
        relay: Option<Url>,
        republish: bool,
        derp_map: Option<DerpMap>,
        handler: Option<ProtocolHandler>,
        ttl: u32,
        enable_dht: bool,
        enable_mdns: bool,
    ) -> Result<Self> {
        let discovery = Discovery::new(secret, relay, enable_dht, enable_mdns, ttl)?;
        let builder = MagicEndpoint::builder()
            .secret_key(SecretKey::from(secret))
            .alpns(vec![alpn.clone()])
            .discovery(Box::new(discovery));
        let builder = if let Some(derp_map) = derp_map {
            builder.derp_mode(DerpMode::Custom(derp_map))
        } else {
            builder.derp_mode(DerpMode::Disabled)
        };
        let endpoint = builder.bind(port).await?;
        if let Some(handler) = handler {
            tokio::spawn(server(endpoint.clone(), handler));
        }
        if republish {
            tokio::spawn(republisher(endpoint.clone(), ttl));
        }

        Ok(Self { alpn, endpoint })
    }

    pub fn peer_id(&self) -> PeerId {
        self.endpoint.node_id()
    }

    pub async fn addr(&self) -> Result<NodeAddr> {
        Ok(self.endpoint.my_addr().await?)
    }

    pub fn add_address(&self, address: NodeAddr) -> Result<()> {
        self.endpoint.add_node_addr(address)?;
        Ok(())
    }

    pub fn discovery(&self) -> &dyn iroh_net::magicsock::Discovery {
        self.endpoint.discovery().unwrap()
    }

    pub async fn connect(&self, peer_id: &PeerId) -> Result<Connection> {
        Ok(self
            .endpoint
            .connect_by_node_id(peer_id, &self.alpn)
            .await?)
    }

    pub async fn notify<P: Protocol>(&self, peer_id: &PeerId, msg: &P::Request) -> Result<()> {
        let mut conn = self.connect(peer_id).await?;
        crate::protocol::notify::<P>(&mut conn, msg).await
    }

    pub async fn request<P: Protocol>(
        &self,
        peer_id: &PeerId,
        msg: &P::Request,
    ) -> Result<P::Response> {
        let mut conn = self.connect(peer_id).await?;
        crate::protocol::request_response::<P>(&mut conn, msg).await
    }

    pub async fn subscribe<P: Protocol>(
        &self,
        peer_id: &PeerId,
        msg: &P::Request,
    ) -> Result<Subscription<P::Response>> {
        let mut conn = self.connect(peer_id).await?;
        crate::protocol::subscribe::<P>(&mut conn, msg).await
    }
}

async fn server(endpoint: MagicEndpoint, handler: ProtocolHandler) {
    loop {
        let Some(conn) = endpoint.accept().await else {
            tracing::info!("socket closed");
            break;
        };
        match accept_conn(conn).await {
            Ok((peer_id, _alpn, conn)) => {
                handler.handle(peer_id, conn);
            }
            Err(err) => {
                dbg!(err);
            }
        }
    }
}

async fn republisher(endpoint: MagicEndpoint, period: u32) {
    loop {
        tokio::time::sleep(Duration::from_secs(period as _)).await;
        let addr = match endpoint.my_addr().await {
            Ok(addr) => addr,
            Err(err) => {
                dbg!(err);
                continue;
            }
        };
        if addr.info.direct_addresses.is_empty() {
            continue;
        }
        if let Some(discovery) = endpoint.discovery() {
            tracing::debug!("republishing");
            discovery.publish(&addr.info);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::channel::{mpsc, oneshot};
    use futures::SinkExt;
    use serde::{Deserialize, Serialize};
    use std::sync::Mutex;
    use std::time::Duration;

    const ALPN: &[u8] = b"/analog/tss/1";

    #[derive(Debug, Deserialize, Serialize)]
    pub struct Ping(u16);

    #[derive(Debug, Deserialize, Serialize)]
    pub struct Pong(u16);

    pub struct PingPong;

    impl Protocol for PingPong {
        const ID: u16 = 0;
        const REQ_BUF: usize = 1024;
        const RES_BUF: usize = 1024;
        type Request = Ping;
        type Response = Pong;
    }

    impl RequestHandler<Self> for PingPong {
        fn request(
            &self,
            _peer_id: PeerId,
            request: <Self as Protocol>::Request,
            response: oneshot::Sender<<Self as Protocol>::Response>,
        ) -> Result<()> {
            response
                .send(Pong(request.0))
                .map_err(|_| anyhow::anyhow!("response channel closed"))?;
            Ok(())
        }
    }

    impl SubscriptionHandler<Self> for PingPong {
        fn subscribe(
            &self,
            _peer_id: PeerId,
            request: <Self as Protocol>::Request,
            mut response: mpsc::Sender<<Self as Protocol>::Response>,
        ) -> Result<()> {
            tokio::spawn(async move {
                response.send(Pong(request.0)).await.unwrap();
                response.send(Pong(request.0)).await.unwrap();
            });
            Ok(())
        }
    }

    pub struct Pinger(Mutex<Option<oneshot::Sender<Ping>>>);

    impl Pinger {
        pub fn new() -> (Self, oneshot::Receiver<Ping>) {
            let (tx, rx) = oneshot::channel();
            (Self(Mutex::new(Some(tx))), rx)
        }
    }

    impl NotificationHandler<PingPong> for Pinger {
        fn notify(&self, _peer_id: PeerId, request: Ping) -> Result<()> {
            self.0
                .lock()
                .unwrap()
                .take()
                .unwrap()
                .send(request)
                .unwrap();
            Ok(())
        }
    }

    async fn wait_for_publish(endpoint: &Endpoint) -> Result<NodeAddr> {
        loop {
            let addr = endpoint.addr().await?;
            if addr.info.direct_addresses.is_empty() {
                tracing::info!("waiting for publish");
                tokio::time::sleep(Duration::from_secs(1)).await;
                continue;
            }
            return Ok(addr);
        }
    }

    async fn wait_for_resolve(endpoint: &Endpoint, peer_id: &PeerId) -> Result<iroh_net::AddrInfo> {
        loop {
            let Ok(addr) = endpoint.discovery().resolve(peer_id).await else {
                tokio::time::sleep(Duration::from_secs(1)).await;
                continue;
            };
            if addr.direct_addresses.is_empty() {
                tracing::info!("waiting for resolve");
                tokio::time::sleep(Duration::from_secs(1)).await;
                continue;
            }
            return Ok(addr);
        }
    }

    #[tokio::test]
    async fn mdns() -> Result<()> {
        env_logger::try_init().ok();

        let mut builder = Endpoint::builder(ALPN.to_vec());
        builder.enable_mdns();
        let e1 = builder.build().await?;
        let p1 = e1.peer_id();

        let mut builder = Endpoint::builder(ALPN.to_vec());
        builder.enable_mdns();
        let e2 = builder.build().await?;

        let a1_2 = wait_for_resolve(&e2, &p1).await?;
        tracing::info!("resolved {:?}", a1_2);

        let a1 = e1.addr().await?;
        assert_eq!(a1.info, a1_2);
        Ok(())
    }

    #[tokio::test]
    async fn pkarr() -> Result<()> {
        env_logger::try_init().ok();

        let mut builder = Endpoint::builder(ALPN.to_vec());
        builder.enable_dht();
        let e1 = builder.build().await?;
        let p1 = e1.peer_id();

        let mut builder = Endpoint::builder(ALPN.to_vec());
        builder.enable_dht();
        let e2 = builder.build().await?;

        let a1_2 = wait_for_resolve(&e2, &p1).await?;
        tracing::info!("resolved {:?}", a1_2);

        let a1 = e1.addr().await?;
        assert_eq!(a1.info, a1_2);
        Ok(())
    }

    #[tokio::test]
    async fn notify() -> Result<()> {
        env_logger::try_init().ok();

        let (pinger, rx) = Pinger::new();
        let mut builder = ProtocolHandler::builder();
        builder.register_notification_handler(pinger);
        let handler = builder.build();

        let mut builder = Endpoint::builder(ALPN.to_vec());
        builder.handler(handler);
        let e1 = builder.build().await?;
        let p1 = e1.peer_id();

        let builder = Endpoint::builder(ALPN.to_vec());
        let e2 = builder.build().await?;

        let a1 = wait_for_publish(&e1).await?;

        e2.add_address(a1)?;
        e2.notify::<PingPong>(&p1, &Ping(42)).await?;
        let ping = rx.await?;
        assert_eq!(ping.0, 42);
        Ok(())
    }

    #[tokio::test]
    async fn request_response() -> Result<()> {
        env_logger::try_init().ok();

        let mut builder = ProtocolHandler::builder();
        builder.register_request_handler(PingPong);
        let handler = builder.build();

        let mut builder = Endpoint::builder(ALPN.to_vec());
        builder.handler(handler);
        let e1 = builder.build().await?;
        let p1 = e1.peer_id();

        let builder = Endpoint::builder(ALPN.to_vec());
        let e2 = builder.build().await?;

        let a1 = wait_for_publish(&e1).await?;

        e2.add_address(a1)?;
        let pong = e2.request::<PingPong>(&p1, &Ping(42)).await?;
        assert_eq!(pong.0, 42);
        Ok(())
    }

    #[tokio::test]
    async fn subscribe() -> Result<()> {
        env_logger::try_init().ok();

        let mut builder = ProtocolHandler::builder();
        builder.register_subscription_handler(PingPong);
        let handler = builder.build();

        let mut builder = Endpoint::builder(ALPN.to_vec());
        builder.handler(handler);
        let e1 = builder.build().await?;
        let p1 = e1.peer_id();

        let builder = Endpoint::builder(ALPN.to_vec());
        let e2 = builder.build().await?;

        let a1 = wait_for_publish(&e1).await?;

        e2.add_address(a1)?;
        let mut subscription = e2.subscribe::<PingPong>(&p1, &Ping(42)).await?;
        while let Some(pong) = subscription.next().await? {
            assert_eq!(pong.0, 42);
        }
        Ok(())
    }
}