-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmasq_node.rs
More file actions
492 lines (433 loc) · 16.9 KB
/
masq_node.rs
File metadata and controls
492 lines (433 loc) · 16.9 KB
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
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.
use crate::command::Command;
use base64::URL_SAFE_NO_PAD;
use masq_lib::blockchains::chains::Chain;
use masq_lib::constants::{
CENTRAL_DELIMITER, CHAIN_IDENTIFIER_DELIMITER, CURRENT_LOGFILE_NAME, HIGHEST_USABLE_PORT,
MASQ_URL_PREFIX,
};
use masq_lib::utils::to_string;
use node_lib::neighborhood::node_location::get_node_location;
use node_lib::sub_lib::cryptde::{CryptDE, PublicKey};
use node_lib::sub_lib::cryptde_null::CryptDENull;
use node_lib::sub_lib::neighborhood::{NodeDescriptor, RatePack};
use node_lib::sub_lib::node_addr::NodeAddr;
use node_lib::sub_lib::wallet::Wallet;
use regex::Regex;
use std::any::Any;
use std::env;
use std::ffi::OsStr;
use std::fmt;
use std::fs;
use std::net::IpAddr;
use std::net::SocketAddr;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
use std::thread;
use std::time::Duration;
use std::time::Instant;
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct NodeReference {
pub public_key: PublicKey,
pub node_addr_opt: Option<NodeAddr>,
pub chain: Chain,
}
impl FromStr for NodeReference {
type Err = String;
fn from_str(node_ref_str: &str) -> Result<Self, <Self as FromStr>::Err> {
let (chain, key, tail) = NodeDescriptor::parse_url(node_ref_str)?;
let public_key = Self::extract_public_key(key)?;
let (ip_addr_str, ports) = strip_ports(tail);
let ip_addr = Self::extract_ip_addr(ip_addr_str.as_str())?;
let port_list = Self::extract_port_list(ports.as_str())?;
Ok(NodeReference::new(public_key, ip_addr, port_list, chain))
}
}
fn strip_ports(tail_half: &str) -> (String, String) {
let idx_opt = tail_half.chars().rev().position(|char| char == ':');
if let Some(idx) = idx_opt {
let lenght = tail_half.len();
let reversed_idx = lenght - idx;
(
tail_half[..reversed_idx - 1].to_string(),
tail_half[reversed_idx..].to_string(),
)
} else {
(tail_half.to_string(), String::new())
}
}
impl From<&dyn MASQNode> for NodeReference {
fn from(masq_node: &dyn MASQNode) -> Self {
NodeReference {
public_key: masq_node.main_public_key().clone(),
node_addr_opt: Some(masq_node.node_addr()),
chain: masq_node.chain(),
}
}
}
impl fmt::Display for NodeReference {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let public_key_string = base64::encode_config(&self.public_key.as_slice(), URL_SAFE_NO_PAD);
let ip_addr_string = match &self.node_addr_opt {
Some(node_addr) => format!("{}", node_addr.ip_addr()),
None => String::new(),
};
let port_list_string = match &self.node_addr_opt {
Some(node_addr) => node_addr
.ports()
.iter()
.map(to_string)
.collect::<Vec<String>>()
.join(NodeAddr::PORTS_SEPARATOR),
None => String::new(),
};
write!(
f,
"{}{}{}{}{}{}:{}",
MASQ_URL_PREFIX,
self.chain.rec().literal_identifier,
CHAIN_IDENTIFIER_DELIMITER,
public_key_string,
CENTRAL_DELIMITER,
ip_addr_string,
port_list_string
)
.unwrap();
Ok(())
}
}
impl NodeReference {
pub fn new(
public_key: PublicKey,
ip_addr_opt: Option<IpAddr>,
ports: Vec<u16>,
chain: Chain,
) -> NodeReference {
match ip_addr_opt {
Some(ip_addr) => NodeReference {
public_key,
node_addr_opt: Some(NodeAddr::new(&ip_addr, &ports)),
chain,
},
None => NodeReference {
public_key,
node_addr_opt: None,
chain,
},
}
}
fn extract_public_key(slice: &str) -> Result<PublicKey, String> {
match base64::decode_config(slice,URL_SAFE_NO_PAD) {
Ok (data) => Ok (PublicKey::new (&data[..])),
Err (_) => Err (format!("The public key of a NodeReference must be represented as a valid Base64 string, not '{}'", slice))
}
}
fn extract_ip_addr(slice: &str) -> Result<Option<IpAddr>, String> {
if slice.is_empty() {
Ok(None)
} else {
match IpAddr::from_str(slice) {
Ok(ip_addr) => Ok(Some(ip_addr)),
Err(_) => Err(format!(
"The IP address of a NodeReference must be valid, not '{}'",
slice
)),
}
}
}
fn extract_port_list(slice: &str) -> Result<Vec<u16>, String> {
let port_list_numbers: Vec<i64> = if slice.is_empty() {
vec![]
} else {
String::from(slice)
.split(NodeAddr::PORTS_SEPARATOR)
.map(|x| x.parse::<i64>().unwrap_or(-1))
.collect()
};
if port_list_numbers.contains(&-1) {
return Err(format!(
"The port list must be a sequence of valid numbers separated by slashes, not '{}'",
slice
));
}
if let Some(x) = port_list_numbers
.iter()
.find(|x| x > &&(HIGHEST_USABLE_PORT as i64))
{
return Err(format!(
"Each port number must be {} or less, not '{}'",
HIGHEST_USABLE_PORT, x
));
}
Ok(port_list_numbers.into_iter().map(|x| x as u16).collect())
}
}
pub enum PortSelector {
First,
Last,
Index(usize),
}
pub trait MASQNode: Any {
// This is the name of the Docker container on which this MASQNode will run.
fn name(&self) -> &str;
// This is the NodeReference stated by the Node in the console. Its IP address won't be accurate if it's a zero-hop Node.
fn node_reference(&self) -> NodeReference;
// If this MASQNode has a main CryptDENull instead of a CryptDEReal, you can get it here.
fn main_cryptde_null(&self) -> Option<&CryptDENull>;
// If this MASQNode has an alias CryptDENull instead of a CryptDEReal, you can get it here.
fn alias_cryptde_null(&self) -> Option<&CryptDENull>;
// The CryptDE that can be used for signing for this Node, if any. (None if it's a MASQRealNode with a CryptDEReal.)
fn signing_cryptde(&self) -> Option<&dyn CryptDE>;
// A reference to this MASQNode's main public key.
fn main_public_key(&self) -> &PublicKey;
// A reference to this MASQNode's alias public key.
fn alias_public_key(&self) -> &PublicKey;
// This is the IP address of the container in which the Node is running.
fn ip_address(&self) -> IpAddr;
fn port_list(&self) -> Vec<u16>;
// This contains the IP address of the container in which the Node is running.
fn node_addr(&self) -> NodeAddr;
// This contains the IP address of the container in which the Node is running.
fn socket_addr(&self, port_selector: PortSelector) -> SocketAddr;
// This is the wallet address at which this Node expects to be paid.
fn earning_wallet(&self) -> Wallet;
// This is the wallet address from which this Node expects to pay bills, or None if the Node is earn-only.
fn consuming_wallet(&self) -> Option<Wallet>;
// The RatePack this Node will use to charge fees.
fn rate_pack(&self) -> RatePack;
fn chain(&self) -> Chain;
fn accepts_connections(&self) -> bool;
fn routes_data(&self) -> bool;
fn country_code_opt(&self) -> Option<String>;
}
pub struct MASQNodeUtils {}
impl MASQNodeUtils {
pub fn clean_up_existing_container(name: &str) {
let mut command = Command::new("docker", Command::strings(vec!["stop", "-t", "0", name]));
command.stdout_and_stderr(); // success, failure, don't care
let mut command = Command::new("docker", Command::strings(vec!["rm", "-f", name]));
command.stdout_and_stderr(); // success, failure, don't care
}
pub fn find_project_root() -> String {
let path_buf = Self::start_from(Path::new(&env::var("PWD").unwrap()));
path_buf.as_path().to_str().unwrap().to_string()
}
pub fn stop(name: &str) {
let mut command = Command::new("docker", Command::strings(vec!["stop", "-t", "0", name]));
command.stdout_or_stderr().unwrap();
}
pub fn socket_addr(
node_addr: &NodeAddr,
port_selector: PortSelector,
name: &str,
) -> SocketAddr {
let port_list = node_addr.ports();
if port_list.is_empty() {
panic!("{} has no clandestine ports; can't make SocketAddr", name)
}
let idx = match port_selector {
PortSelector::First => 0,
PortSelector::Last => port_list.len() - 1,
PortSelector::Index(i) => i,
};
SocketAddr::new(node_addr.ip_addr(), port_list[idx])
}
pub fn assert_node_wrote_log_containing(name: &str, pattern: &str, timeout: Duration) {
let time_limit = Instant::now() + timeout;
let mut entire_log = String::new();
while Instant::now() < time_limit {
entire_log = MASQNodeUtils::retrieve_logs(name);
let regex = Regex::new(pattern).unwrap();
if regex.is_match(&entire_log) {
return;
} else {
thread::sleep(Duration::from_millis(250))
}
}
panic!(
"After {:?}, this pattern\n\n{}\n\ndid not match anything in this log:\n\n{}",
timeout, pattern, entire_log
);
}
pub fn retrieve_logs(name: &str) -> String {
let mut command = Command::new(
"docker",
Command::strings(vec![
"exec",
"-t",
name,
"cat",
&format!("/node_root/home/{}", CURRENT_LOGFILE_NAME),
]),
);
command.stdout_and_stderr()
}
fn start_from(start: &Path) -> PathBuf {
if fs::read_dir(start)
.unwrap()
.filter(|entry| {
let file_name = match entry {
Ok(dir_entry) => dir_entry.file_name(),
Err(e) => panic!("Should never happen: {}", e),
};
file_name == OsStr::new("multinode_integration_tests")
|| file_name == OsStr::new("node")
})
.count()
== 2
{
PathBuf::from(start)
} else {
Self::start_from(start.parent().unwrap())
}
}
pub fn derive_country_code_opt(node_addr: &NodeAddr) -> Option<String> {
let country_code_opt = get_node_location(Some(node_addr.ip_addr()));
if let Some(cc) = country_code_opt {
Some(cc.country_code)
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use base64::{decode_config, STANDARD_NO_PAD};
use masq_lib::test_utils::utils::TEST_DEFAULT_MULTINODE_CHAIN;
#[test]
fn strip_ports_works_single_port() {
let tail = "1.2.3.4:4444";
let result = strip_ports(tail);
assert_eq!(result, ("1.2.3.4".to_string(), "4444".to_string()))
}
#[test]
fn strip_ports_works_multiple_ports() {
let tail = "4.3.2.9:4444/1212/11133";
let result = strip_ports(tail);
assert_eq!(
result,
("4.3.2.9".to_string(), "4444/1212/11133".to_string())
)
}
#[test]
fn node_reference_from_str_fails_if_there_are_not_two_fields_to_divide_to() {
let string = String::from("masq://two@fields@nope");
let result = NodeReference::from_str(string.as_str());
assert_eq! (result, Err (String::from ("Either '@' delimiter position or format of node address is wrong. Should be 'masq://<chain identifier>:<public key>@<node address>', not 'masq://two@fields@nope'\nNodeAddr should be expressed as '<IP address>:<port>/<port>/...', probably not as 'fields@nope'")));
}
#[test]
fn node_reference_from_str_fails_if_key_is_not_valid_base64() {
let string = String::from("masq://dev:;;;@12.34.56.78:1234/2345");
let result = NodeReference::from_str(string.as_str());
assert_eq! (result, Err (String::from ("The public key of a NodeReference must be represented as a valid Base64 string, not ';;;'")));
}
#[test]
fn node_reference_from_str_fails_if_ip_address_is_not_valid() {
let key = PublicKey::new(&b"Booga"[..]);
let string = format!("masq://dev:{}@blippy:1234/2345", key);
let result = NodeReference::from_str(string.as_str());
assert_eq!(
result,
Err(String::from(
"Either '@' delimiter position or format of node address is wrong. Should be 'masq://<chain identifier>:<public key>@<node address>', not 'masq://dev:Qm9vZ2E@blippy:1234/2345'\nNodeAddr should be expressed as '<IP address>:<port>/<port>/...', probably not as 'blippy:1234/2345'"
))
);
}
#[test]
fn node_reference_from_str_fails_if_a_port_number_is_not_valid() {
let key = PublicKey::new(&b"Booga"[..]);
let string = format!("masq://dev:{}@12.34.56.78:weeble/frud", key);
let result = NodeReference::from_str(string.as_str());
assert_eq! (result, Err (String::from("Either '@' delimiter position or format of node address is wrong. Should be 'masq://<chain identifier>:<public key>@<node address>', not 'masq://dev:Qm9vZ2E@12.34.56.78:weeble/frud'\nNodeAddr should be expressed as '<IP address>:<port>/<port>/...', probably not as '12.34.56.78:weeble/frud'")));
}
#[test]
fn node_reference_from_str_fails_if_a_port_number_is_too_big() {
let key = PublicKey::new(&b"Booga"[..]);
let string = format!("masq://dev:{}@12.34.56.78:1234/65536", key);
let result = NodeReference::from_str(string.as_str());
assert_eq!(
result,
Err(String::from(
"Each port number must be 65535 or less, not '65536'"
))
);
}
#[test]
fn node_reference_from_str_happy_path() {
let key = PublicKey::new(&b"Booga"[..]);
let string = format!("masq://dev:{}@12.34.56.78:1234/2345", key);
let result = NodeReference::from_str(string.as_str()).unwrap();
assert_eq!(result.public_key, key);
assert_eq!(
result.node_addr_opt,
Some(NodeAddr::new(
&IpAddr::from_str("12.34.56.78").unwrap(),
&[1234, 2345]
))
);
}
#[test]
fn node_reference_from_str_complains_about_slash_in_the_key() {
let result = NodeReference::from_str(
"masq://dev:abJ5XvhVbmVyGejkYUkmftF09pmGZGKg/PzRNnWQxFw@12.23.34.45:5678",
);
assert_eq!(
result,
Err(String::from(
"The public key of a NodeReference must be represented as a valid Base64 string, not 'abJ5XvhVbmVyGejkYUkmftF09pmGZGKg/PzRNnWQxFw'"
))
);
}
#[test]
fn node_reference_from_str_complains_about_plus_in_the_key() {
let result = NodeReference::from_str(
"masq://dev:abJ5XvhVbmVy+GejkYUmftF09pmGZGKgkPzRNnWQxFw@12.23.34.45:5678",
);
assert_eq!(
result,
Err(String::from(
"The public key of a NodeReference must be represented as a valid Base64 string, not 'abJ5XvhVbmVy+GejkYUmftF09pmGZGKgkPzRNnWQxFw'"
))
);
}
#[test]
fn node_reference_from_string_works_if_there_are_no_ports() {
let key = PublicKey::new(&b"Booga"[..]);
let string = format!("masq://dev:{}@12.34.56.78:", key);
let result = NodeReference::from_str(string.as_str()).unwrap();
assert_eq!(result.public_key, key);
assert_eq!(
result.node_addr_opt,
Some(NodeAddr::new(
&IpAddr::from_str("12.34.56.78").unwrap(),
&[]
))
);
}
#[test]
fn node_reference_can_display_itself() {
let chain = TEST_DEFAULT_MULTINODE_CHAIN;
let subject = NodeReference::new(
PublicKey::new(&b"Booga"[..]),
Some(IpAddr::from_str("12.34.56.78").unwrap()),
vec![1234, 5678],
chain,
);
let result = format!("{}", subject);
assert_eq!(
result,
String::from("masq://dev:Qm9vZ2E@12.34.56.78:1234/5678")
);
}
#[test]
fn display_works_with_base64_as_url_safe_no_pad() {
let public_key_badly_encoded = "abJ5XvhVbmVy+GejkYUmftF/9pmGZGKgkPzRNnWQxFw";
let decoded_key_bytes = decode_config(public_key_badly_encoded, STANDARD_NO_PAD).unwrap();
let decoded_key = PublicKey::new(&decoded_key_bytes);
let node_reference = NodeReference::new(decoded_key, None, vec![], Chain::Dev);
let str_form = node_reference.to_string();
assert!(str_form.contains("abJ5XvhVbmVy-GejkYUmftF_9pmGZGKgkPzRNnWQxFw"))
}
}