allow `0` to mean "The OS should pick an available TCP port for us to listen to"

main
_ 2021-10-10 20:42:17 +00:00
parent b71d4c16a1
commit 34873dff43
2 changed files with 35 additions and 17 deletions

View File

@ -51,6 +51,27 @@ struct Port {
forwarding_instance: Option <ForwardingInstance>,
}
impl Port {
pub fn open_port (&mut self, rt: &Runtime, connection_p2_p3: quinn::Connection)
-> anyhow::Result <()>
{
let params = self.gui.get_params ()?;
let _guard = rt.enter ();
let forwarding_instance = rt.block_on (ForwardingInstance::new (
connection_p2_p3,
params,
))?;
self.gui.input_client_port.set_value (&forwarding_instance.local_port ().to_string ());
self.forwarding_instance.replace (forwarding_instance);
self.gui.set_forwarding (true);
Ok (())
}
}
struct GuiPort {
row: fltk::group::Flex,
input_client_port: Input,
@ -67,17 +88,7 @@ impl GuiClient <'_> {
port_idx: usize,
) -> anyhow::Result <()>
{
let params = self.ports [port_idx].gui.get_params ()?;
let _guard = self.rt.enter ();
let forwarding_instance = self.rt.block_on (ForwardingInstance::new (
connection_p2_p3,
params,
))?;
self.ports [port_idx].forwarding_instance.replace (forwarding_instance);
self.ports [port_idx].gui.set_forwarding (true);
self.ports [port_idx].open_port (&self.rt, connection_p2_p3)?;
self.sync_status ();
Ok (())
@ -152,12 +163,12 @@ fn main () -> anyhow::Result <()> {
{
let mut row = Flex::default ().row ();
let mut l = Frame::default ().with_label ("Local port");
row.set_size (&mut l, 80);
let mut l = Frame::default ().with_label ("Server ID");
row.set_size (&mut l, 120);
let mut l = Frame::default ().with_label ("Server port");
row.set_size (&mut l, 80);
let mut l = Frame::default ().with_label ("Local port");
row.set_size (&mut l, 80);
row.end ();
col.set_size (&mut row, 30);
@ -249,19 +260,19 @@ impl GuiPort {
let mut row = Flex::default ().row ();
let mut input_client_port = Input::default ();
let mut input_server_id = Input::default ();
let mut input_server_port = Input::default ();
let mut input_client_port = Input::default ();
let mut but_open = Button::default ().with_label ("Open");
let mut but_close = Button::default ().with_label ("Close");
row.set_size (&mut input_client_port, 80);
row.set_size (&mut input_server_id, 120);
row.set_size (&mut input_server_port, 80);
row.set_size (&mut input_client_port, 80);
row.set_size (&mut but_open, 80);
row.set_size (&mut but_close, 80);
input_client_port.set_value ("5901");
input_client_port.set_value ("0");
input_server_id.set_value ("bogus_server");
input_server_port.set_value ("5900");

View File

@ -9,6 +9,7 @@ use crate::prelude::*;
pub struct ForwardingInstance {
task: JoinHandle <anyhow::Result <()>>,
shutdown_flag: watch::Sender <bool>,
local_port: u16,
}
impl ForwardingInstance {
@ -20,7 +21,8 @@ impl ForwardingInstance {
let (shutdown_flag, shutdown_flag_rx) = tokio::sync::watch::channel (true);
let listener = TcpListener::bind (("127.0.0.1", params.client_tcp_port)).await?;
trace! ("Accepting local TCP connections from P1 on {}", params.client_tcp_port);
let local_port = listener.local_addr ()?.port ();
trace! ("Accepting local TCP connections from P1 on {}", local_port);
let task = tokio::spawn (forward_port (
listener,
@ -32,6 +34,7 @@ impl ForwardingInstance {
Ok (Self {
task,
shutdown_flag,
local_port,
})
}
@ -44,6 +47,10 @@ impl ForwardingInstance {
.context ("inside ForwardingInstance task")?;
Ok (())
}
pub fn local_port (&self) -> u16 {
self.local_port
}
}
pub struct ForwardingParams {