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
use std::fs;
use std::time::SystemTime;

use beserial::{Deserialize, Serialize};
use keys::{KeyPair, PublicKey};
use network_primitives::address::net_address::NetAddress;
use network_primitives::address::peer_address::{PeerAddress, PeerAddressType};
use network_primitives::address::PeerId;
use network_primitives::address::seed_list::SeedList;
use network_primitives::protocol::{Protocol, ProtocolFlags};
use network_primitives::services::Services;
use utils::time::systemtime_to_timestamp;
use network_primitives::address::{PeerUri};

use crate::error::Error;


// One or multiple seed nodes. Either a peer URI or a http(s) URL to a seed list
#[derive(Clone, Debug)]
pub enum Seed {
    Peer(PeerUri),
    List(SeedList)
}


#[derive(Clone, Debug)]
pub struct NetworkConfig {
    protocol_mask: ProtocolFlags,
    key_pair: Option<KeyPair>,
    peer_id: Option<PeerId>,
    services: Services,
    protocol_config: ProtocolConfig,
    user_agent: Option<String>,
    additional_seeds: Vec<Seed>,
}

impl NetworkConfig {
    pub fn new_ws_network_config(host: String, port: u16, reverse_proxy_config: Option<ReverseProxyConfig>) -> Self {
        Self {
            protocol_mask: ProtocolFlags::WS | ProtocolFlags::WSS,
            key_pair: None,
            peer_id: None,
            services: Services::full(),
            protocol_config: ProtocolConfig::Ws {
                host,
                port,
                reverse_proxy_config,
            },
            user_agent: None,
            additional_seeds: Vec::new()
        }
    }

    pub fn new_wss_network_config(host: String, port: u16, identity_file: String, identity_password: String) -> Self {
        Self {
            protocol_mask: ProtocolFlags::WS | ProtocolFlags::WSS,
            key_pair: None,
            peer_id: None,
            services: Services::full(),
            protocol_config: ProtocolConfig::Wss {
                host,
                port,
                identity_file,
                identity_password,
            },
            user_agent: None,
            additional_seeds: Vec::new()
        }
    }

    pub fn new_dumb_network_config() -> Self {
        Self {
            protocol_mask: ProtocolFlags::WS | ProtocolFlags::WSS, // TODO Browsers might not always support WS.
            key_pair: None,
            peer_id: None,
            services: Services::full(),
            protocol_config: ProtocolConfig::Dumb,
            user_agent: None,
            additional_seeds: Vec::new()
        }
    }

    pub fn init_persistent(&mut self, peer_key_store: &PeerKeyStore) -> Result<(), Error> {
        if self.key_pair.is_some() {
            return Ok(());
        }

        let key_pair = match peer_key_store.load_peer_key() {
            Err(Error::IoError(_)) => {
                let key_pair = KeyPair::generate();
                peer_key_store.save_peer_key(&key_pair)?;
                Ok(key_pair)
            },
            res => res,
        }?;

        self.peer_id = Some(PeerId::from(&key_pair.public));
        self.key_pair = Some(key_pair);
        Ok(())
    }

    pub fn init_volatile(&mut self) {
        let key_pair = KeyPair::generate();
        self.peer_id = Some(PeerId::from(&key_pair.public));
        self.key_pair = Some(key_pair);
    }

    pub fn protocol(&self) -> Protocol {
        Protocol::from(&self.protocol_config)
    }

    pub fn protocol_mask(&self) -> ProtocolFlags {
        self.protocol_mask
    }

    pub fn key_pair(&self) -> &KeyPair {
        &self.key_pair.as_ref().expect("NetworkConfig is uninitialized")
    }

    pub fn public_key(&self) -> &PublicKey {
        &self.key_pair.as_ref().expect("NetworkConfig is uninitialized").public
    }

    pub fn peer_id(&self) -> &PeerId {
        &self.peer_id.as_ref().expect("NetworkConfig is uninitialized")
    }

    pub fn services(&self) -> &Services {
        &self.services
    }

    pub fn set_services(&mut self, services: Services) {
        self.services = services;
    }

    pub fn can_connect(&self, protocol: Protocol) -> bool {
        self.protocol_mask.contains(ProtocolFlags::from(protocol))
    }

    pub fn user_agent(&self) -> &Option<String> {
        &self.user_agent
    }

    pub fn set_user_agent(&mut self, user_agent: String) {
        self.user_agent = Some(user_agent)
    }

    pub fn additional_seeds(&self) -> &Vec<Seed> {
        &self.additional_seeds
    }

    pub fn set_additional_seeds(&mut self, seeds: Vec<Seed>) {
        self.additional_seeds = seeds
    }

    pub fn protocol_config(&self) -> &ProtocolConfig {
        &self.protocol_config
    }

    pub fn peer_address(&self) -> PeerAddress {
        // TODO Check PeerAddress globally reachable.
        let mut addr = PeerAddress {
            ty: match self.protocol_config {
                ProtocolConfig::Rtc => PeerAddressType::Rtc,
                ProtocolConfig::Dumb => PeerAddressType::Dumb,
                ProtocolConfig::Ws {
                    ref host,
                    port,
                    ref reverse_proxy_config,
                    ..
                } => {
                    if let Some(reverse_proxy_config) = reverse_proxy_config.as_ref() {
                        if reverse_proxy_config.with_tls_termination {
                            PeerAddressType::Wss(host.clone(), reverse_proxy_config.port)
                        } else {
                            PeerAddressType::Ws(host.clone(), reverse_proxy_config.port)
                        }
                    } else {
                        PeerAddressType::Ws(host.clone(), port)
                    }
                },
                ProtocolConfig::Wss {
                    ref host,
                    port,
                    ..
                } => PeerAddressType::Wss(host.clone(), port),
            },
            services: self.services.provided,
            timestamp: systemtime_to_timestamp(SystemTime::now()),
            net_address: NetAddress::Unspecified,
            public_key: self.key_pair.as_ref().expect("NetworkConfig is uninitialized").public,
            distance: 0,
            signature: None,
            peer_id: self.peer_id.as_ref().expect("NetworkConfig is uninitialized").clone(),
        };
        if addr.protocol() == Protocol::Wss || addr.protocol() == Protocol::Ws {
            assert!(addr.is_globally_reachable(false), "PeerAddress not globally reachable.");
        }
        addr.signature = Some(self.key_pair.as_ref().expect("NetworkConfig is uninitialized").sign(&addr.get_signature_data()[..]));
        addr
    }

    pub fn is_initialized(&self) -> bool {
        self.key_pair.is_some() && self.peer_id.is_some()
    }
}

#[derive(Debug, Clone)]
pub struct ReverseProxyConfig {
    pub port: u16,
    pub address: NetAddress,
    pub header: String,
    pub with_tls_termination: bool,
}

#[derive(Debug, Clone)]
pub enum ProtocolConfig {
    Dumb,
    Ws {
        host: String,
        port: u16,
        reverse_proxy_config: Option<ReverseProxyConfig>,
    },
    Wss {
        host: String,
        port: u16,
        identity_file: String,
        identity_password: String,
    },
    Rtc,
}

impl From<&ProtocolConfig> for Protocol {
    fn from(config: &ProtocolConfig) -> Self {
        match config {
            ProtocolConfig::Dumb => Protocol::Dumb,
            ProtocolConfig::Rtc => Protocol::Rtc,
            ProtocolConfig::Ws { reverse_proxy_config, .. } => {
                match reverse_proxy_config {
                    Some(ReverseProxyConfig { with_tls_termination: true, .. }) => Protocol::Wss,
                    Some(ReverseProxyConfig { with_tls_termination: false, .. }) => Protocol::Ws,
                    _ => Protocol::Ws,
                }
            },
            ProtocolConfig::Wss { .. } => Protocol::Wss,
        }
    }
}

pub struct PeerKeyStore {
    path: String,
}

impl PeerKeyStore {
    pub fn new(path: String) -> Self {
        PeerKeyStore {
            path
        }
    }

    pub fn load_peer_key(&self) -> Result<KeyPair, Error> {
        match fs::read(&self.path) {
            Ok(data) => {
                Deserialize::deserialize_from_vec(&data).map_err(|_| Error::InvalidPeerKey)
            },
            Err(e) => Err(Error::IoError(e)),
        }
    }

    pub fn save_peer_key(&self, key_pair: &KeyPair) -> Result<(), Error> {
        Ok(fs::write(&self.path, key_pair.serialize_to_vec())?)
    }
}