turn_rs/router/
mod.rs

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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
pub mod channels;
pub mod interfaces;
pub mod nodes;
pub mod nonces;
pub mod ports;

#[rustfmt::skip]
use crate::Observer;
use self::{
    channels::Channels,
    interfaces::{Interface, Interfaces},
    nodes::Nodes,
    nonces::Nonces,
    ports::Ports,
};

use std::{net::SocketAddr, sync::Arc, thread, time::Duration};

/// Router State Tree.
///
/// this state management example maintains the status of all
/// nodes in the current service and adds a node grouping model.
/// it is necessary to specify a group for each node.
///
/// The state between groups is isolated. However,
/// it should be noted that the node key only supports
/// long-term valid passwords,does not support short-term
/// valid passwords.
pub struct Router {
    realm: String,
    observer: Arc<dyn Observer>,
    ports: Ports,
    nonces: Nonces,
    nodes: Nodes,
    channels: Channels,
    interfaces: Interfaces,
}

impl Router {
    /// create a router.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    /// impl Observer for ObserverTest {}
    ///
    /// Router::new("test".to_string(), Arc::new(ObserverTest));
    /// ```
    pub fn new(realm: String, observer: Arc<dyn Observer>) -> Arc<Self> {
        let this = Arc::new(Self {
            interfaces: Interfaces::default(),
            channels: Channels::default(),
            nonces: Nonces::default(),
            ports: Ports::default(),
            nodes: Nodes::default(),
            observer,
            realm,
        });

        let this_ = Arc::downgrade(&this);
        thread::spawn(move || {
            while let Some(this) = this_.upgrade() {
                thread::sleep(Duration::from_secs(60));
                this.nodes.get_deaths().iter().for_each(|a| {
                    this.remove(a);
                });

                this.channels.get_deaths().iter().for_each(|c| {
                    this.channels.remove(*c);
                });
            }
        });

        this
    }

    /// get router capacity.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    /// impl Observer for ObserverTest {}
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// assert_eq!(router.capacity(), 16383);
    /// ```
    pub fn capacity(&self) -> usize {
        self.ports.capacity()
    }

    /// get router allocate size.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    /// impl Observer for ObserverTest {}
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// assert_eq!(router.len(), 0);
    /// ```
    pub fn len(&self) -> usize {
        self.ports.len()
    }

    /// get router allocate size is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    /// impl Observer for ObserverTest {}
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// assert_eq!(router.is_empty(), true);
    /// ```
    pub fn is_empty(&self) -> bool {
        self.ports.len() == 0
    }

    /// get addr interface.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    ///
    /// let interface = router.get_interface(&addr).unwrap();
    /// assert_eq!(interface.addr, addr);
    /// ```
    pub fn get_interface(&self, addr: &SocketAddr) -> Option<Arc<Interface>> {
        self.interfaces.get_ref(addr)
    }

    /// get user list.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    ///
    /// let users = router.get_users(0, 10);
    /// assert_eq!(users.as_slice(), &[("test".to_string(), vec![addr])]);
    /// ```
    pub fn get_users(&self, skip: usize, limit: usize) -> Vec<(String, Vec<SocketAddr>)> {
        self.nodes.get_users(skip, limit)
    }

    /// get node.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    ///
    /// let node = router.get_node(&addr).unwrap();
    /// assert_eq!(node.username.as_str(), "test");
    /// assert_eq!(node.password.as_str(), "test");
    /// assert_eq!(node.secret.as_slice(), &secret);
    /// assert_eq!(node.channels, vec![]);
    /// assert_eq!(node.ports, vec![]);
    /// ```
    pub fn get_node(&self, addr: &SocketAddr) -> Option<nodes::Node> {
        self.nodes.get_node(addr)
    }

    /// get node bound list.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    ///
    /// let ret = router.get_node_addrs("test");
    /// assert_eq!(ret, vec![addr]);
    /// ```
    pub fn get_node_addrs(&self, username: &str) -> Vec<SocketAddr> {
        self.nodes.get_addrs(username)
    }

    /// get the nonce of the node SocketAddr.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    ///
    /// let nonce = router.get_nonce(&addr);
    /// assert_eq!(nonce.len(), 16);
    /// ```
    pub fn get_nonce(&self, addr: &SocketAddr) -> Arc<String> {
        self.nonces.get(addr)
    }

    /// get the password of the node SocketAddr.
    ///
    /// require remote control service to distribute keys.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    /// ```
    pub fn get_key_block(
        &self,
        addr: &SocketAddr,
        interface: &SocketAddr,
        external: &SocketAddr,
        username: &str,
    ) -> Option<Arc<[u8; 16]>> {
        let key = self.nodes.get_secret(addr);
        if key.is_some() {
            return key;
        }

        let pwd = self.observer.get_password_blocking(addr, username)?;
        let key = self.nodes.insert(addr, &self.realm, username, &pwd)?;
        self.interfaces.insert(*addr, *interface, *external);
        Some(key)
    }

    /// get the password of the node SocketAddr.
    ///
    /// require remote control service to distribute keys.
    pub async fn get_key(
        &self,
        addr: &SocketAddr,
        interface: &SocketAddr,
        external: &SocketAddr,
        username: &str,
    ) -> Option<Arc<[u8; 16]>> {
        let key = self.nodes.get_secret(addr);
        if key.is_some() {
            return key;
        }

        let pwd = self.observer.get_password(addr, username).await?;
        let key = self.nodes.insert(addr, &self.realm, username, &pwd)?;
        self.interfaces.insert(*addr, *interface, *external);
        Some(key)
    }

    /// obtain the peer address bound to the current
    /// node according to the channel number.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    ///
    /// let port = router.alloc_port(&addr).unwrap();
    /// assert!(router.bind_port(&addr, port).is_some());
    /// assert_eq!(router.get_port_bound(port), Some(addr));
    /// ```
    pub fn get_channel_bound(&self, addr: &SocketAddr, channel: u16) -> Option<SocketAddr> {
        self.channels.get_bound(addr, channel)
    }

    /// obtain the peer address bound to the current
    /// node according to the port number.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    ///
    /// let port = router.alloc_port(&addr).unwrap();
    /// assert!(router.bind_port(&addr, port).is_some());
    /// assert_eq!(router.get_port_bound(port), Some(addr));
    /// ```
    pub fn get_port_bound(&self, port: u16) -> Option<SocketAddr> {
        self.ports.get(port)
    }

    /// get node the port.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let peer = "127.0.0.1:8081".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    ///
    /// let port = router.alloc_port(&addr).unwrap();
    /// assert!(router.bind_port(&addr, port).is_some());
    /// assert!(router.bind_port(&peer, port).is_some());
    /// assert_eq!(router.get_bound_port(&addr, &peer), Some(port));
    /// ```
    pub fn get_bound_port(&self, addr: &SocketAddr, peer: &SocketAddr) -> Option<u16> {
        self.ports.get_bound(addr, peer)
    }

    /// alloc a port from State.
    ///
    /// In all cases, the server SHOULD only allocate ports from the range
    /// 49152 - 65535 (the Dynamic and/or Private Port range [PORT-NUMBERS]),
    /// unless the TURN server application knows, through some means not
    /// specified here, that other applications running on the same host as
    /// the TURN server application will not be impacted by allocating ports
    /// outside this range.  This condition can often be satisfied by running
    /// the TURN server application on a dedicated machine and/or by
    /// arranging that any other applications on the machine allocate ports
    /// before the TURN server application starts.  In any case, the TURN
    /// server SHOULD NOT allocate ports in the range 0 - 1023 (the Well-
    /// Known Port range) to discourage clients from using TURN to run
    /// standard services.
    ///
    ///   NOTE: The use of randomized port assignments to avoid certain
    ///   types of attacks is described in [RFC6056].  It is RECOMMENDED
    ///   that a TURN server implement a randomized port assignment
    ///   algorithm from [RFC6056].  This is especially applicable to
    ///   servers that choose to pre-allocate a number of ports from the
    ///   underlying OS and then later assign them to allocations; for
    ///   example, a server may choose this technique to implement the
    ///   EVEN-PORT attribute.
    ///
    /// The server determines the initial value of the time-to-expiry field
    /// as follows.  If the request contains a LIFETIME attribute, then the
    /// server computes the minimum of the client's proposed lifetime and the
    /// server's maximum allowed lifetime.  If this computed value is greater
    /// than the default lifetime, then the server uses the computed lifetime
    /// as the initial value of the time-to-expiry field.  Otherwise, the
    /// server uses the default lifetime.  It is RECOMMENDED that the server
    /// use a maximum allowed lifetime value of no more than 3600 seconds (1
    /// hour).  Servers that implement allocation quotas or charge users for
    /// allocations in some way may wish to use a smaller maximum allowed
    /// lifetime (perhaps as small as the default lifetime) to more quickly
    /// remove orphaned allocations (that is, allocations where the
    /// corresponding client has crashed or terminated, or the client
    /// connection has been lost for some reason).  Also, note that the time-
    /// to-expiry is recomputed with each successful Refresh request, and
    /// thus, the value computed here applies only until the first refresh.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    /// assert!(router.alloc_port(&addr).is_some());
    /// ```
    pub fn alloc_port(&self, addr: &SocketAddr) -> Option<u16> {
        let port = self.ports.alloc(addr)?;
        self.nodes.push_port(addr, port);
        Some(port)
    }

    /// bind port for State.
    ///
    /// A server need not do anything special to implement
    /// idempotency of CreatePermission requests over UDP using the
    /// "stateless stack approach".  Retransmitted CreatePermission
    /// requests will simply refresh the permissions.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    ///
    /// let port = router.alloc_port(&addr).unwrap();
    /// assert!(router.bind_port(&addr, port).is_some());
    /// ```
    pub fn bind_port(&self, addr: &SocketAddr, port: u16) -> Option<()> {
        self.ports.bound(addr, port)
    }

    /// bind channel number for State.
    ///
    /// A server need not do anything special to implement
    /// idempotency of ChannelBind requests over UDP using the
    /// "stateless stack approach".  Retransmitted ChannelBind requests
    /// will simply refresh the channel binding and the corresponding
    /// permission.  Furthermore, the client must wait 5 minutes before
    /// binding a previously bound channel number or peer address to a
    /// different channel, eliminating the possibility that the
    /// transaction would initially fail but succeed on a
    /// retransmission.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    ///
    /// let port = router.alloc_port(&addr).unwrap();
    /// assert!(router.bind_channel(&addr, port, 0x4000).is_some());
    /// ```
    pub fn bind_channel(&self, addr: &SocketAddr, port: u16, channel: u16) -> Option<()> {
        let source = self.ports.get(port)?;
        self.channels.insert(addr, channel, &source)?;
        self.nodes.push_channel(addr, channel)?;
        Some(())
    }

    /// refresh node lifetime.
    ///
    /// The server computes a value called the "desired lifetime" as follows:
    /// if the request contains a LIFETIME attribute and the attribute value
    /// is zero, then the "desired lifetime" is zero.  Otherwise, if the
    /// request contains a LIFETIME attribute, then the server computes the
    /// minimum of the client's requested lifetime and the server's maximum
    /// allowed lifetime.  If this computed value is greater than the default
    /// lifetime, then the "desired lifetime" is the computed value.
    /// Otherwise, the "desired lifetime" is the default lifetime.
    ///
    /// Subsequent processing depends on the "desired lifetime" value:
    ///
    /// * If the "desired lifetime" is zero, then the request succeeds and the
    ///   allocation is deleted.
    ///
    /// * If the "desired lifetime" is non-zero, then the request succeeds and
    ///   the allocation's time-to-expiry is set to the "desired lifetime".
    ///
    /// If the request succeeds, then the server sends a success response
    /// containing:
    ///
    /// * A LIFETIME attribute containing the current value of the time-to-
    ///   expiry timer.
    ///
    /// NOTE: A server need not do anything special to implement
    /// idempotency of Refresh requests over UDP using the "stateless
    /// stack approach".  Retransmitted Refresh requests with a non-
    /// zero "desired lifetime" will simply refresh the allocation.  A
    /// retransmitted Refresh request with a zero "desired lifetime"
    /// will cause a 437 (Allocation Mismatch) response if the
    /// allocation has already been deleted, but the client will treat
    /// this as equivalent to a success response (see below).
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    /// router.refresh(&addr, 0);
    ///
    /// assert!(router.get_node(&addr).is_none());
    /// ```
    pub fn refresh(&self, addr: &SocketAddr, delay: u32) {
        if delay > 0 {
            self.nodes.set_lifetime(addr, delay);
        } else {
            self.remove(addr);
        }
    }

    /// remove a node.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    /// assert!(router.remove(&addr).is_some());
    /// assert!(router.get_node(&addr).is_none());
    /// ```
    pub fn remove(&self, addr: &SocketAddr) -> Option<()> {
        let node = self.nodes.remove(addr)?;
        self.ports.remove(addr, &node.ports);
        for c in node.channels {
            self.channels.remove(c);
        }

        self.nonces.remove(addr);
        self.interfaces.remove(addr);
        self.observer.abort(addr, &node.username);
        Some(())
    }

    /// remove a node from username.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::net::SocketAddr;
    /// use std::sync::Arc;
    /// use turn_rs::router::*;
    /// use turn_rs::*;
    ///
    /// struct ObserverTest;
    ///
    /// impl Observer for ObserverTest {
    ///     fn get_password_blocking(
    ///         &self,
    ///         _: &SocketAddr,
    ///         _: &str,
    ///     ) -> Option<String> {
    ///         Some("test".to_string())
    ///     }
    /// }
    ///
    /// let addr = "127.0.0.1:8080".parse::<SocketAddr>().unwrap();
    /// let secret = [
    ///     174, 238, 187, 253, 117, 209, 73, 157, 36, 56, 143, 91, 155, 16, 224,
    ///     239,
    /// ];
    ///
    /// let router = Router::new("test".to_string(), Arc::new(ObserverTest));
    /// let key = router.get_key_block(&addr, &addr, &addr, "test").unwrap();
    ///
    /// assert_eq!(key.as_slice(), &secret);
    /// router.remove_from_user("test");
    ///
    /// assert!(router.get_node(&addr).is_none());
    /// ```
    pub fn remove_from_user(&self, u: &str) {
        for addr in self.nodes.get_addrs(u) {
            self.remove(&addr);
        }
    }
}