Make port in --server argument optional

This commit is contained in:
Jonas Herzig 2019-02-05 13:48:14 +01:00
parent 8950e30690
commit f9e85618c8

View file

@ -66,7 +66,7 @@ fn main() {
.add_option( .add_option(
&["--server"], &["--server"],
Store, Store,
"Hostname and port of upstream mumble server", "Hostname and (optionally) port of the upstream Mumble server",
) )
.required(); .required();
ap.refer(&mut accept_invalid_certs).add_option( ap.refer(&mut accept_invalid_certs).add_option(
@ -78,13 +78,19 @@ fn main() {
ap.parse_args_or_exit(); ap.parse_args_or_exit();
} }
// Try parsing as raw IPv6 address first
let (upstream_host, upstream_port) = match upstream.parse::<Ipv6Addr>() {
Ok(_) => (upstream.as_ref(), 64738),
Err(_) => {
// Otherwise split off port from end
let mut upstream_parts = upstream.rsplitn(2, ':'); let mut upstream_parts = upstream.rsplitn(2, ':');
let upstream_port: u16 = upstream_parts let right = upstream_parts.next().expect("Empty upstream address");
.next() match upstream_parts.next() {
.expect("Missing upstream port") Some(host) => (host, right.parse().expect("Failed to parse upstream port")),
.parse() None => (right, 64738),
.expect("Failed to parse upstream port"); }
let upstream_host = upstream_parts.next().expect("Missing upstream host name"); }
};
let upstream_host = Box::leak(Box::new(upstream_host.to_owned())).as_str(); let upstream_host = Box::leak(Box::new(upstream_host.to_owned())).as_str();
let upstream_addr = (upstream_host, upstream_port) let upstream_addr = (upstream_host, upstream_port)
.to_socket_addrs() .to_socket_addrs()