networking/
utils.rs

1use crate::encryption::PubKeyComp;
2pub use crate::{ArtificeConfig, ArtificeHostData, ArtificePeer};
3use crate::{Layer3Addr, Layer3SocketAddr};
4use num_bigint_dig::BigUint;
5use rand::distributions::Alphanumeric;
6use rand::{thread_rng, Rng};
7use rsa::RSAPrivateKey;
8use std::sync::mpsc::{channel, Receiver, Sender};
9
10use crate::error::NetworkError;
11use std::iter;
12use tokio::sync::mpsc::{
13    channel as tokio_channel, Receiver as AsyncReceiver, Sender as AsyncSender,
14};
15
16pub fn get_private_key() -> RSAPrivateKey {
17    let n = BigUint::from_bytes_be(&[
18        214, 82, 135, 64, 34, 118, 248, 217, 78, 42, 130, 198, 138, 28, 117, 66, 23, 74, 77, 139,
19        51, 187, 14, 166, 48, 66, 217, 18, 157, 202, 175, 238, 60, 29, 39, 85, 58, 33, 145, 25, 55,
20        214, 51, 72, 184, 142, 141, 183, 54, 186, 102, 59, 131, 86, 167, 220, 74, 65, 65, 43, 42,
21        104, 182, 108, 144, 96, 238, 52, 145, 126, 239, 25, 41, 16, 221, 86, 26, 27, 87, 100, 171,
22        205, 80, 216, 235, 202, 125, 204, 231, 254, 67, 77, 100, 154, 173, 137, 14, 154, 200, 188,
23        123, 41, 96, 177, 19, 105, 23, 230, 2, 249, 66, 147, 107, 152, 108, 37, 203, 68, 228, 119,
24        255, 64, 4, 53, 106, 145, 228, 191, 24, 199, 3, 232, 39, 170, 123, 227, 204, 68, 97, 216,
25        182, 169, 82, 144, 88, 184, 84, 127, 118, 168, 50, 192, 241, 111, 15, 32, 12, 130, 25, 74,
26        107, 187, 126, 154, 154, 194, 29, 56, 122, 3, 208, 65, 206, 216, 3, 22, 113, 227, 55, 55,
27        206, 184, 132, 85, 236, 144, 7, 146, 98, 220, 37, 208, 81, 82, 21, 166, 151, 67, 14, 73,
28        194, 84, 242, 234, 202, 111, 159, 5, 100, 93, 52, 120, 243, 86, 116, 123, 114, 52, 184, 74,
29        99, 96, 120, 124, 19, 53, 161, 84, 133, 163, 144, 88, 163, 17, 171, 101, 164, 223, 169, 15,
30        63, 80, 2, 207, 95, 248, 66, 244, 196, 207, 68, 49, 45, 25, 95, 255,
31    ]);
32    let e = BigUint::from_bytes_be(&[1, 0, 1]);
33    let d = BigUint::from_bytes_be(&[
34        11, 49, 174, 76, 196, 64, 16, 137, 81, 18, 217, 87, 195, 88, 239, 42, 239, 138, 122, 248,
35        93, 80, 84, 216, 139, 70, 194, 141, 189, 94, 27, 200, 70, 173, 131, 35, 105, 112, 221, 98,
36        66, 207, 86, 72, 99, 19, 87, 5, 141, 191, 56, 157, 189, 35, 102, 139, 19, 249, 202, 140,
37        149, 159, 120, 127, 189, 30, 194, 242, 77, 243, 34, 75, 223, 32, 57, 95, 72, 231, 202, 173,
38        192, 103, 109, 228, 150, 148, 49, 243, 228, 222, 27, 231, 203, 236, 100, 73, 247, 80, 80,
39        81, 191, 225, 14, 98, 209, 79, 184, 230, 211, 154, 239, 70, 65, 229, 105, 40, 164, 48, 73,
40        250, 150, 131, 98, 116, 227, 199, 16, 250, 109, 169, 223, 94, 194, 200, 235, 244, 81, 141,
41        113, 70, 119, 176, 15, 189, 108, 182, 211, 139, 9, 72, 167, 91, 225, 129, 247, 159, 121,
42        242, 5, 23, 91, 140, 221, 19, 184, 19, 24, 255, 255, 220, 67, 192, 150, 40, 159, 117, 191,
43        35, 175, 156, 230, 134, 246, 25, 89, 45, 105, 99, 44, 246, 35, 232, 239, 248, 1, 250, 42,
44        150, 86, 234, 244, 142, 204, 108, 149, 31, 236, 200, 217, 36, 169, 27, 254, 17, 99, 82, 80,
45        46, 228, 129, 124, 79, 28, 156, 82, 145, 111, 220, 16, 204, 203, 24, 220, 246, 241, 15,
46        206, 116, 152, 29, 56, 164, 237, 164, 101, 52, 139, 219, 66, 24, 55, 128, 116, 219, 189,
47        217,
48    ]);
49    let primes = vec![
50        BigUint::from_bytes_be(&[
51            218, 18, 255, 185, 216, 198, 202, 164, 130, 230, 249, 202, 68, 69, 5, 246, 219, 99,
52            170, 211, 253, 14, 210, 149, 41, 44, 110, 173, 245, 102, 19, 175, 3, 243, 129, 86, 166,
53            104, 105, 78, 11, 162, 161, 239, 8, 206, 97, 222, 183, 132, 115, 39, 49, 172, 164, 33,
54            43, 135, 173, 254, 37, 14, 185, 213, 129, 255, 31, 152, 100, 138, 247, 156, 59, 183,
55            46, 242, 184, 222, 208, 55, 159, 29, 5, 192, 123, 12, 186, 176, 32, 237, 151, 159, 190,
56            44, 144, 186, 37, 149, 107, 154, 19, 116, 16, 196, 53, 166, 113, 122, 192, 87, 122,
57            124, 252, 84, 221, 91, 56, 15, 55, 74, 92, 56, 82, 176, 104, 149, 87, 195,
58        ]),
59        BigUint::from_bytes_be(&[
60            251, 152, 125, 227, 130, 36, 160, 198, 157, 132, 251, 120, 127, 208, 105, 53, 3, 219,
61            28, 218, 154, 192, 227, 251, 141, 201, 136, 237, 210, 125, 215, 149, 134, 32, 240, 187,
62            116, 102, 200, 255, 174, 116, 14, 121, 133, 65, 96, 31, 211, 100, 81, 152, 87, 243,
63            210, 108, 0, 120, 144, 148, 74, 68, 183, 245, 111, 38, 138, 127, 60, 84, 53, 100, 203,
64            208, 82, 220, 91, 30, 137, 202, 143, 93, 123, 7, 206, 144, 171, 174, 124, 125, 114,
65            116, 226, 238, 225, 189, 243, 122, 47, 123, 185, 100, 250, 200, 25, 248, 70, 89, 234,
66            28, 113, 190, 180, 50, 253, 221, 215, 209, 8, 176, 82, 10, 248, 85, 73, 236, 79, 21,
67        ]),
68    ];
69    RSAPrivateKey::from_components(n, e, d, primes)
70}
71/// used in examples, and tests, generates ArtificePeer, and ArtificeConfig because private keys take a while to generate
72/// this method generates static data, so it should never be used in production environments
73pub fn test_config() -> (ArtificePeer, ArtificeConfig) {
74    let peer_addr: Layer3SocketAddr = Layer3SocketAddr::new(Layer3Addr::newv4(0, 0, 0, 0), 6464);
75    let host_addr: Layer3SocketAddr = Layer3SocketAddr::new(Layer3Addr::newv4(0, 0, 0, 0), 6464);
76    let private_key = get_private_key();
77    let pubkey = PubKeyComp::from(&private_key);
78    // poorly named, global is unique to each host, and peer hash is a pre-shared key
79    let host_hash = "f7Cgkll1EegEa5UyuUEADpYAXRXwrhbSB0FLLiYxHpBotzNrw9";
80    let peer_hash = "7VKkjONo1txtTAiR1vQWUTsGxh8jwQJips1ClMv9zv1CsOo3ZX";
81    let remote_hash = "73C0YnEJRpTd56wPwR8zHa3egpW8iM1ShCRAtutkcssenNkJ0T";
82    let peer = ArtificePeer::new(remote_hash, peer_hash, peer_addr, Some(pubkey));
83    let host_data = ArtificeHostData::new(&private_key, host_hash);
84    let config = ArtificeConfig::new(host_addr, host_data, false);
85    (peer, config)
86}
87/// used to generate things such as pair keys, and global peer hash, see ArtificePeer
88pub fn random_string(len: usize) -> String {
89    let mut rng = thread_rng();
90    iter::repeat(())
91        .map(|()| rng.sample(Alphanumeric))
92        .take(len)
93        .collect()
94}
95
96pub trait Query {
97    type Left;
98    type Right;
99    /// manually create using an existing sender and receiver
100    fn create(sender: Self::Left, receiver: Self::Right) -> Self;
101    /// split a query into its components
102    fn into_split(self) -> (Self::Left, Self::Right);
103    fn split(&mut self) -> (&mut Self::Left, &mut Self::Right);
104}
105
106pub fn async_channel<S, R>(len: usize) -> (AsyncQuery<R, S>, AsyncQuery<S, R>) {
107    let (l_sender, l_receiver): (AsyncSender<R>, AsyncReceiver<R>) = tokio_channel(len);
108    let (r_sender, r_receiver): (AsyncSender<S>, AsyncReceiver<S>) = tokio_channel(len);
109    (
110        AsyncQuery {
111            sender: l_sender,
112            receiver: r_receiver,
113        },
114        AsyncQuery {
115            sender: r_sender,
116            receiver: l_receiver,
117        },
118    )
119}
120#[derive(Debug)]
121pub struct AsyncQuery<S, R> {
122    sender: AsyncSender<S>,
123    receiver: AsyncReceiver<R>,
124}
125impl<S, R> AsyncQuery<S, R> {
126    pub async fn send(&mut self, data: S) -> Result<(), NetworkError> {
127        Ok(self.sender.send(data).await?)
128    }
129    pub async fn recv(&mut self) -> Option<R> {
130        Some(self.receiver.recv().await?)
131    }
132}
133impl<S, R> Query for AsyncQuery<S, R> {
134    type Left = AsyncSender<S>;
135    type Right = AsyncReceiver<R>;
136    fn create(sender: Self::Left, receiver: Self::Right) -> Self {
137        Self { sender, receiver }
138    }
139    fn into_split(self) -> (Self::Left, Self::Right) {
140        (self.sender, self.receiver)
141    }
142    fn split(&mut self) -> (&mut Self::Left, &mut Self::Right) {
143        (&mut self.sender, &mut self.receiver)
144    }
145}
146pub fn sync_channel<R, S>() -> (SyncQuery<R, S>, SyncQuery<S, R>) {
147    let (l_sender, l_receiver) = channel();
148    let (r_sender, r_receiver) = channel();
149    (
150        SyncQuery {
151            sender: l_sender,
152            receiver: r_receiver,
153        },
154        SyncQuery {
155            sender: r_sender,
156            receiver: l_receiver,
157        },
158    )
159}
160#[derive(Debug)]
161pub struct SyncQuery<S, R> {
162    sender: Sender<S>,
163    receiver: Receiver<R>,
164}
165impl<S, R> SyncQuery<S, R> {
166    pub fn send(&mut self, data: S) -> Result<(), NetworkError> {
167        Ok(self.sender.send(data)?)
168    }
169    pub fn recv(&mut self) -> Result<R, NetworkError> {
170        Ok(self.receiver.recv()?)
171    }
172}
173impl<S, R> Query for SyncQuery<S, R> {
174    type Left = Sender<S>;
175    type Right = Receiver<R>;
176    fn create(sender: Self::Left, receiver: Self::Right) -> Self {
177        Self { sender, receiver }
178    }
179    fn into_split(self) -> (Self::Left, Self::Right) {
180        (self.sender, self.receiver)
181    }
182    fn split(&mut self) -> (&mut Self::Left, &mut Self::Right) {
183        (&mut self.sender, &mut self.receiver)
184    }
185}
186
187// ================================================================
188//                          Tests
189// ================================================================
190/*#[test]
191fn random_string_test(){
192    use std::time::SystemTime;
193    let time1 = SystemTime::now();
194    let first_string = random_string(16);
195    assert!(time1.elapsed().unwrap().as_micros() < 400);
196}*/