Allow --nat extip:your.host.here.org (#10830)

* Allow --nat extip:your.host.here.org

Closes #10604

* Use split instead of Regex
This commit is contained in:
David 2019-07-03 09:44:32 +02:00 committed by Marek Kotewicz
parent b4af8df535
commit 895574b774

View File

@ -16,7 +16,7 @@
use std::time::Duration; use std::time::Duration;
use std::io::Read; use std::io::Read;
use std::net::SocketAddr; use std::net::{SocketAddr, ToSocketAddrs};
use std::path::PathBuf; use std::path::PathBuf;
use std::collections::{HashSet, BTreeMap}; use std::collections::{HashSet, BTreeMap};
use std::iter::FromIterator; use std::iter::FromIterator;
@ -725,9 +725,18 @@ impl Configuration {
let port = self.args.arg_ports_shift + self.args.arg_port; let port = self.args.arg_ports_shift + self.args.arg_port;
let listen_address = SocketAddr::new(self.interface(&self.args.arg_interface).parse().unwrap(), port); let listen_address = SocketAddr::new(self.interface(&self.args.arg_interface).parse().unwrap(), port);
let public_address = if self.args.arg_nat.starts_with("extip:") { let public_address = if self.args.arg_nat.starts_with("extip:") {
let host = &self.args.arg_nat[6..]; let host = self.args.arg_nat[6..].split(':').next().expect("split has at least one part; qed");
let host = host.parse().map_err(|_| format!("Invalid host given with `--nat extip:{}`", host))?; let host = format!("{}:{}", host, port);
Some(SocketAddr::new(host, port)) match host.to_socket_addrs() {
Ok(mut addr_iter) => {
if let Some(addr) = addr_iter.next() {
Some(addr)
} else {
return Err(format!("Invalid host given with `--nat extip:{}`", &self.args.arg_nat[6..]))
}
},
Err(_) => return Err(format!("Invalid host given with `--nat extip:{}`", &self.args.arg_nat[6..]))
}
} else { } else {
None None
}; };
@ -1844,6 +1853,33 @@ mod tests {
assert_eq!(conf1.ipfs_config().port, 5002); assert_eq!(conf1.ipfs_config().port, 5002);
} }
#[test]
fn should_resolve_external_nat_hosts() {
// Ip works
let conf = parse(&["parity", "--nat", "extip:1.1.1.1"]);
assert_eq!(conf.net_addresses().unwrap().1.unwrap().ip().to_string(), "1.1.1.1");
assert_eq!(conf.net_addresses().unwrap().1.unwrap().port(), 30303);
// Ip with port works, port is discarded
let conf = parse(&["parity", "--nat", "extip:192.168.1.1:123"]);
assert_eq!(conf.net_addresses().unwrap().1.unwrap().ip().to_string(), "192.168.1.1");
assert_eq!(conf.net_addresses().unwrap().1.unwrap().port(), 30303);
// Hostname works
let conf = parse(&["parity", "--nat", "extip:ethereum.org"]);
assert!(conf.net_addresses().unwrap().1.is_some());
assert_eq!(conf.net_addresses().unwrap().1.unwrap().port(), 30303);
// Hostname works, garbage at the end is discarded
let conf = parse(&["parity", "--nat", "extip:ethereum.org:whatever bla bla 123"]);
assert!(conf.net_addresses().unwrap().1.is_some());
assert_eq!(conf.net_addresses().unwrap().1.unwrap().port(), 30303);
// Garbage is error
let conf = parse(&["parity", "--nat", "extip:blabla"]);
assert!(conf.net_addresses().is_err());
}
#[test] #[test]
fn should_expose_all_servers() { fn should_expose_all_servers() {
// given // given