Compare commits

..

No commits in common. "b71d4c16a1411572b0fd41a2dc2cb8699dd68416" and "1a775622cfd0606cf0684199167236431b88ca21" have entirely different histories.

4 changed files with 138 additions and 109 deletions

57
Cargo.lock generated
View File

@ -345,22 +345,36 @@ checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
[[package]] [[package]]
name = "fltk" name = "fltk"
version = "1.2.7" version = "1.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0953c34fd5b0c6ac1b615fdcda8845c81ed364165b115ddac46ae8953d456640" checksum = "471fefc1fc3e019d314d542637f5bf48bd72a678f5e9052167b9ac7bea7b0a38"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"fltk-derive",
"fltk-sys", "fltk-sys",
"paste", "lazy_static",
"objc",
"raw-window-handle",
]
[[package]]
name = "fltk-derive"
version = "1.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712f09c29760a17e14a2f0d08a36b78bab82d5fbb3b2c3e17f3420f239351364"
dependencies = [
"quote",
"syn",
] ]
[[package]] [[package]]
name = "fltk-sys" name = "fltk-sys"
version = "1.2.7" version = "1.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2df700440c014123cc1644fbef7d33dcdda18cdd80373a7552a9d5cec47ca63" checksum = "5cc5eabad4839df806d102049c44586233743cf6607e2d2829155bb9fe45775e"
dependencies = [ dependencies = [
"cmake", "cmake",
"libc",
] ]
[[package]] [[package]]
@ -760,6 +774,15 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "malloc_buf"
version = "0.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "maplit" name = "maplit"
version = "1.0.2" version = "1.0.2"
@ -901,6 +924,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "objc"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1"
dependencies = [
"malloc_buf",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.8.0" version = "1.8.0"
@ -977,12 +1009,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "paste"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58"
[[package]] [[package]]
name = "pem" name = "pem"
version = "0.8.3" version = "0.8.3"
@ -1446,6 +1472,15 @@ dependencies = [
"rand_core", "rand_core",
] ]
[[package]]
name = "raw-window-handle"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a441a7a6c80ad6473bd4b74ec1c9a4c951794285bf941c2126f607c72e48211"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "rcgen" name = "rcgen"
version = "0.8.13" version = "0.8.13"

View File

@ -9,7 +9,7 @@ license = "AGPL-3.0"
[dependencies] [dependencies]
anyhow = "1.0.38" anyhow = "1.0.38"
fltk = "1.2.7" fltk = "1.1.1"
quic_demo = { path = "../quic_demo" } quic_demo = { path = "../quic_demo" }
quinn = "0.7.2" quinn = "0.7.2"
reqwest = "0.11.4" reqwest = "0.11.4"

View File

@ -1,13 +1,10 @@
use std::{ use std::str::FromStr;
str::FromStr,
};
use fltk::{ use fltk::{
app, app,
button::Button, button::Button,
enums::CallbackTrigger, enums::CallbackTrigger,
frame::Frame, frame::Frame,
group::Flex,
input::*, input::*,
prelude::*, prelude::*,
window::Window window::Window
@ -37,27 +34,13 @@ struct Opt {
enum Message { enum Message {
OpenPort (usize), OpenPort (usize),
ClosePort (usize), ClosePort (usize),
AddPort,
} }
struct GuiClient <'a> { struct GuiClient <'a> {
rt: &'a Runtime, rt: &'a Runtime,
frame_status: Frame, frame_status: Frame,
ports: Vec <Port>, forwarding_instances: Vec <Option <ForwardingInstance>>,
} gui_ports: Vec <GuiPort>,
struct Port {
gui: GuiPort,
forwarding_instance: Option <ForwardingInstance>,
}
struct GuiPort {
row: fltk::group::Flex,
input_client_port: Input,
input_server_id: Input,
input_server_port: Input,
but_open: Button,
but_close: Button,
} }
impl GuiClient <'_> { impl GuiClient <'_> {
@ -67,7 +50,7 @@ impl GuiClient <'_> {
port_idx: usize, port_idx: usize,
) -> anyhow::Result <()> ) -> anyhow::Result <()>
{ {
let params = self.ports [port_idx].gui.get_params ()?; let params = self.gui_ports [port_idx].get_params ()?;
let _guard = self.rt.enter (); let _guard = self.rt.enter ();
let forwarding_instance = self.rt.block_on (ForwardingInstance::new ( let forwarding_instance = self.rt.block_on (ForwardingInstance::new (
@ -75,16 +58,16 @@ impl GuiClient <'_> {
params, params,
))?; ))?;
self.ports [port_idx].forwarding_instance.replace (forwarding_instance); self.forwarding_instances [port_idx].replace (forwarding_instance);
self.ports [port_idx].gui.set_forwarding (true); self.gui_ports [port_idx].set_forwarding (true);
self.sync_status (); self.sync_status ();
Ok (()) Ok (())
} }
pub fn close_port (&mut self, port_idx: usize) -> anyhow::Result <()> { pub fn close_port (&mut self, port_idx: usize) -> anyhow::Result <()> {
if let Some (old_instance) = self.ports [port_idx].forwarding_instance.take () { if let Some (old_instance) = self.forwarding_instances [port_idx].take () {
self.rt.block_on (async { self.rt.block_on (async {
old_instance.close () old_instance.close ()
.await .await
@ -94,40 +77,23 @@ impl GuiClient <'_> {
})?; })?;
} }
self.ports [port_idx].gui.set_forwarding (false); self.gui_ports [port_idx].set_forwarding (false);
self.sync_status (); self.sync_status ();
Ok (()) Ok (())
} }
fn open_ports (&self) -> usize { fn open_ports (&self) -> usize {
self.ports.iter () self.forwarding_instances.iter ()
.map (|x| if x.forwarding_instance.is_some () { 1 } else { 0 }) .map (|x| if x.is_some () { 1 } else { 0 })
.sum () .sum ()
} }
pub fn sync_status (&mut self) { fn sync_status (&mut self) {
let open_ports = self.open_ports (); let open_ports = self.open_ports ();
self.frame_status.set_label (&format! ("Forwarding {} ports", open_ports)); self.frame_status.set_label (&format! ("Forwarding {} ports", open_ports));
} }
pub fn add_port (
&mut self,
ports_col: &mut Flex,
fltk_tx: fltk::app::Sender <Message>
) {
let mut gui = GuiPort::new (fltk_tx, self.ports.len ());
ports_col.add (&gui.row);
ports_col.set_size (&mut gui.row, 30);
let port = Port {
gui,
forwarding_instance: None,
};
self.ports.push (port);
}
} }
fn main () -> anyhow::Result <()> { fn main () -> anyhow::Result <()> {
@ -142,47 +108,66 @@ fn main () -> anyhow::Result <()> {
let window_title = opt.window_title.clone ().unwrap_or_else (|| "PTTH client proxy".to_string ()); let window_title = opt.window_title.clone ().unwrap_or_else (|| "PTTH client proxy".to_string ());
let mut wind = Window::new (100, 100, 800, 600, None) let mut wind = Window::new (100, 100, 800, 600, None)
.with_label (&window_title); .with_label (&window_title);
wind.make_resizable (true);
let mut col = Flex::default ().column ().size_of_parent (); let margin = 10;
let h = 30;
let mut x = margin;
let mut y = margin;
let mut frame_status = Frame::default (); let frame_status = Frame::new (x, y, 800 - 20, h, "Forwarding 0 ports");
col.set_size (&mut frame_status, 30);
y += h + margin;
x = margin;
{ {
let mut row = Flex::default ().row (); let w = 80;
Frame::new (x, y, w, h, "Local port");
x += w + margin;
let mut l = Frame::default ().with_label ("Local port"); let w = 120;
row.set_size (&mut l, 80); Frame::new (x, y, w, h, "Server ID");
let mut l = Frame::default ().with_label ("Server ID"); x += w + margin;
row.set_size (&mut l, 120);
let mut l = Frame::default ().with_label ("Server port");
row.set_size (&mut l, 80);
row.end ();
col.set_size (&mut row, 30); let w = 80;
Frame::new (x, y, w, h, "Server port");
// x += w + margin;
} }
let mut ports_col = Flex::default ().column (); y += h + margin;
ports_col.end (); x = margin;
let mut but_add_port = Button::default ().with_label ("+"); let gui_port_0 = GuiPort::new (fltk_tx, &mut x, y, 0);
but_add_port.set_trigger (CallbackTrigger::Release); y += h + margin;
but_add_port.emit (fltk_tx, Message::AddPort); x = margin;
col.set_size (&mut but_add_port, 30);
col.end (); let gui_port_1 = GuiPort::new (fltk_tx, &mut x, y, 1);
y += h + margin;
x = margin;
let gui_port_2 = GuiPort::new (fltk_tx, &mut x, y, 2);
// y += h + margin;
// x = margin;
let gui_ports = vec! [
gui_port_0,
gui_port_1,
gui_port_2,
];
let forwarding_instances = vec! [
None,
None,
None,
];
let mut gui_client = GuiClient { let mut gui_client = GuiClient {
rt: &rt, rt: &rt,
frame_status, frame_status,
ports: Default::default (), forwarding_instances,
gui_ports,
}; };
gui_client.add_port (&mut ports_col, fltk_tx); // y += h + margin;
ports_col.recalc ();
gui_client.sync_status ();
wind.end (); wind.end ();
wind.show (); wind.show ();
@ -215,19 +200,14 @@ fn main () -> anyhow::Result <()> {
while app.wait () { while app.wait () {
match fltk_rx.recv () { match fltk_rx.recv () {
Some (Message::OpenPort (port_idx)) => { Some (Message::OpenPort (port_idx)) => {
if let Err (e) = gui_client.open_port (connection_p2_p3.clone (), port_idx) match gui_client.open_port (connection_p2_p3.clone (), port_idx) {
{ Err (e) => error! ("{:?}", e),
error! ("{:?}", e); _ => (),
} };
}, },
Some (Message::ClosePort (port_idx)) => { Some (Message::ClosePort (port_idx)) => {
gui_client.close_port (port_idx)?; gui_client.close_port (port_idx)?;
}, },
Some (Message::AddPort) => {
gui_client.add_port (&mut ports_col, fltk_tx);
ports_col.recalc ();
ports_col.redraw ();
},
None => (), None => (),
} }
} }
@ -244,22 +224,38 @@ fn set_active <W: WidgetExt> (w: &mut W, b: bool) {
} }
} }
struct GuiPort {
input_client_port: Input,
input_server_id: Input,
input_server_port: Input,
but_open: Button,
but_close: Button,
}
impl GuiPort { impl GuiPort {
fn new (fltk_tx: fltk::app::Sender <Message>, port_idx: usize) -> Self { fn new (fltk_tx: fltk::app::Sender <Message>, x: &mut i32, y: i32, port_idx: usize) -> Self {
let margin = 10;
let h = 30;
let mut row = Flex::default ().row (); let w = 80;
let mut input_client_port = Input::new (*x, y, w, h, "");
*x += w + margin;
let mut input_client_port = Input::default (); let w = 120;
let mut input_server_id = Input::default (); let mut input_server_id = Input::new (*x, y, w, h, "");
let mut input_server_port = Input::default (); *x += w + margin;
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); let w = 80;
row.set_size (&mut input_server_id, 120); let mut input_server_port = Input::new (*x, y, w, h, "");
row.set_size (&mut input_server_port, 80); *x += w + margin;
row.set_size (&mut but_open, 80);
row.set_size (&mut but_close, 80); let w = 80;
let mut but_open = Button::new (*x, y, w, h, "Open");
*x += w + margin;
let w = 80;
let mut but_close = Button::new (*x, y, w, h, "Close");
// *x += w + margin;
input_client_port.set_value ("5901"); input_client_port.set_value ("5901");
input_server_id.set_value ("bogus_server"); input_server_id.set_value ("bogus_server");
@ -273,10 +269,7 @@ impl GuiPort {
set_active (&mut but_open, true); set_active (&mut but_open, true);
set_active (&mut but_close, false); set_active (&mut but_close, false);
row.end ();
Self { Self {
row,
input_client_port, input_client_port,
input_server_id, input_server_id,
input_server_port, input_server_port,

View File

@ -36,8 +36,9 @@ impl ForwardingInstance {
} }
pub async fn close (self) -> anyhow::Result <()> { pub async fn close (self) -> anyhow::Result <()> {
if self.shutdown_flag.send (false).is_err () { match self.shutdown_flag.send (false) {
warn! ("Trying to gracefully shutdown forwarding task but it appears to already be shut down"); Err (_) => warn! ("Trying to gracefully shutdown forwarding task but it appears to already be shut down"),
_ => (),
} }
self.task.await self.task.await
.context ("awaiting ForwardingInstance task")? .context ("awaiting ForwardingInstance task")?
@ -63,9 +64,9 @@ pub async fn forward_port (
) -> anyhow::Result <()> ) -> anyhow::Result <()>
{ {
let ForwardingParams { let ForwardingParams {
client_tcp_port,
server_id, server_id,
server_tcp_port, server_tcp_port,
..
} = params; } = params;
while *shutdown_flag_rx.borrow () { while *shutdown_flag_rx.borrow () {