Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Allow --nat extip:your.host.here.org (#10830)
Browse files Browse the repository at this point in the history
* Allow --nat extip:your.host.here.org

Closes #10604

* Use split instead of Regex
  • Loading branch information
dvdplm authored and debris committed Jul 3, 2019
1 parent b4af8df commit 895574b
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::time::Duration;
use std::io::Read;
use std::net::SocketAddr;
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::PathBuf;
use std::collections::{HashSet, BTreeMap};
use std::iter::FromIterator;
Expand Down Expand Up @@ -725,9 +725,18 @@ impl Configuration {
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 public_address = if self.args.arg_nat.starts_with("extip:") {
let host = &self.args.arg_nat[6..];
let host = host.parse().map_err(|_| format!("Invalid host given with `--nat extip:{}`", host))?;
Some(SocketAddr::new(host, port))
let host = self.args.arg_nat[6..].split(':').next().expect("split has at least one part; qed");
let host = format!("{}:{}", 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 {
None
};
Expand Down Expand Up @@ -1844,6 +1853,33 @@ mod tests {
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]
fn should_expose_all_servers() {
// given
Expand Down

0 comments on commit 895574b

Please sign in to comment.