2021-07-19 01:56:42 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2021-07-19 00:01:46 +00:00
|
|
|
use fltk::{
|
|
|
|
app,
|
|
|
|
button::Button,
|
|
|
|
enums::CallbackTrigger,
|
|
|
|
frame::Frame,
|
|
|
|
input::*,
|
|
|
|
prelude::*,
|
|
|
|
window::Window
|
|
|
|
};
|
2021-07-19 00:25:25 +00:00
|
|
|
use structopt::StructOpt;
|
2021-10-10 17:56:13 +00:00
|
|
|
use tokio::runtime::Runtime;
|
2021-07-19 00:25:25 +00:00
|
|
|
|
2021-07-21 23:28:42 +00:00
|
|
|
use quic_demo::{
|
|
|
|
client_proxy::*,
|
|
|
|
prelude::*,
|
|
|
|
protocol::PeerId,
|
|
|
|
};
|
2021-07-19 00:25:25 +00:00
|
|
|
|
|
|
|
#[derive (Debug, StructOpt)]
|
|
|
|
struct Opt {
|
2021-07-19 02:28:18 +00:00
|
|
|
#[structopt (long)]
|
|
|
|
window_title: Option <String>,
|
2021-07-19 00:25:25 +00:00
|
|
|
#[structopt (long)]
|
|
|
|
relay_addr: Option <String>,
|
|
|
|
#[structopt (long)]
|
|
|
|
client_id: Option <PeerId>,
|
2021-10-01 17:19:23 +00:00
|
|
|
#[structopt (long)]
|
|
|
|
cert_url: Option <String>,
|
2021-07-19 00:25:25 +00:00
|
|
|
}
|
2021-07-19 00:01:46 +00:00
|
|
|
|
|
|
|
#[derive (Clone, Copy)]
|
|
|
|
enum Message {
|
2021-07-19 02:37:09 +00:00
|
|
|
OpenPort (usize),
|
|
|
|
ClosePort (usize),
|
2021-07-19 00:01:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:56:13 +00:00
|
|
|
struct GuiClient <'a> {
|
|
|
|
rt: &'a Runtime,
|
|
|
|
frame_status: Frame,
|
2021-10-10 18:31:52 +00:00
|
|
|
ports: Vec <Port>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Port {
|
|
|
|
gui: GuiPort,
|
|
|
|
forwarding_instance: Option <ForwardingInstance>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GuiPort {
|
|
|
|
input_client_port: Input,
|
|
|
|
input_server_id: Input,
|
|
|
|
input_server_port: Input,
|
|
|
|
but_open: Button,
|
|
|
|
but_close: Button,
|
2021-10-10 17:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GuiClient <'_> {
|
|
|
|
pub fn open_port (
|
|
|
|
&mut self,
|
|
|
|
connection_p2_p3: quinn::Connection,
|
|
|
|
port_idx: usize,
|
|
|
|
) -> anyhow::Result <()>
|
|
|
|
{
|
2021-10-10 18:31:52 +00:00
|
|
|
let params = self.ports [port_idx].gui.get_params ()?;
|
2021-10-10 17:56:13 +00:00
|
|
|
|
|
|
|
let _guard = self.rt.enter ();
|
2021-10-10 18:17:50 +00:00
|
|
|
let forwarding_instance = self.rt.block_on (ForwardingInstance::new (
|
2021-10-10 17:56:13 +00:00
|
|
|
connection_p2_p3,
|
|
|
|
params,
|
2021-10-10 18:17:50 +00:00
|
|
|
))?;
|
|
|
|
|
2021-10-10 18:31:52 +00:00
|
|
|
self.ports [port_idx].forwarding_instance.replace (forwarding_instance);
|
2021-10-10 17:56:13 +00:00
|
|
|
|
2021-10-10 18:31:52 +00:00
|
|
|
self.ports [port_idx].gui.set_forwarding (true);
|
2021-10-10 18:00:20 +00:00
|
|
|
self.sync_status ();
|
2021-10-10 17:56:13 +00:00
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close_port (&mut self, port_idx: usize) -> anyhow::Result <()> {
|
2021-10-10 18:31:52 +00:00
|
|
|
if let Some (old_instance) = self.ports [port_idx].forwarding_instance.take () {
|
2021-10-10 18:08:25 +00:00
|
|
|
self.rt.block_on (async {
|
|
|
|
old_instance.close ()
|
|
|
|
.await
|
|
|
|
.context ("closing ForwardingInstance")?;
|
|
|
|
|
|
|
|
Ok::<_, anyhow::Error> (())
|
|
|
|
})?;
|
2021-10-10 17:56:13 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 18:31:52 +00:00
|
|
|
self.ports [port_idx].gui.set_forwarding (false);
|
2021-10-10 18:00:20 +00:00
|
|
|
self.sync_status ();
|
2021-10-10 17:56:13 +00:00
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
2021-10-10 18:00:20 +00:00
|
|
|
|
|
|
|
fn open_ports (&self) -> usize {
|
2021-10-10 18:31:52 +00:00
|
|
|
self.ports.iter ()
|
|
|
|
.map (|x| if x.forwarding_instance.is_some () { 1 } else { 0 })
|
2021-10-10 18:00:20 +00:00
|
|
|
.sum ()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sync_status (&mut self) {
|
|
|
|
let open_ports = self.open_ports ();
|
|
|
|
|
|
|
|
self.frame_status.set_label (&format! ("Forwarding {} ports", open_ports));
|
|
|
|
}
|
2021-10-10 17:56:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 00:01:46 +00:00
|
|
|
fn main () -> anyhow::Result <()> {
|
|
|
|
tracing_subscriber::fmt::init ();
|
2021-10-10 17:56:13 +00:00
|
|
|
let rt = Runtime::new ()?;
|
2021-07-19 00:01:46 +00:00
|
|
|
|
2021-07-19 00:25:25 +00:00
|
|
|
let opt = Opt::from_args ();
|
|
|
|
|
2021-07-19 00:01:46 +00:00
|
|
|
let (fltk_tx, fltk_rx) = app::channel::<Message> ();
|
|
|
|
|
2021-07-19 02:28:18 +00:00
|
|
|
let app = app::App::default ();
|
|
|
|
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)
|
|
|
|
.with_label (&window_title);
|
2021-07-19 00:01:46 +00:00
|
|
|
|
|
|
|
let margin = 10;
|
|
|
|
let h = 30;
|
|
|
|
let mut x = margin;
|
|
|
|
let mut y = margin;
|
|
|
|
|
2021-10-10 17:56:13 +00:00
|
|
|
let frame_status = Frame::new (x, y, 800 - 20, h, "Forwarding 0 ports");
|
2021-07-19 00:01:46 +00:00
|
|
|
|
|
|
|
y += h + margin;
|
2021-07-19 01:46:06 +00:00
|
|
|
x = margin;
|
2021-07-19 00:01:46 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let w = 80;
|
2021-07-19 02:37:09 +00:00
|
|
|
Frame::new (x, y, w, h, "Local port");
|
2021-07-19 00:01:46 +00:00
|
|
|
x += w + margin;
|
|
|
|
|
|
|
|
let w = 120;
|
2021-07-19 02:37:09 +00:00
|
|
|
Frame::new (x, y, w, h, "Server ID");
|
2021-07-19 00:01:46 +00:00
|
|
|
x += w + margin;
|
|
|
|
|
|
|
|
let w = 80;
|
2021-07-19 02:37:09 +00:00
|
|
|
Frame::new (x, y, w, h, "Server port");
|
2021-07-19 00:25:25 +00:00
|
|
|
// x += w + margin;
|
2021-07-19 00:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
y += h + margin;
|
2021-07-19 01:46:06 +00:00
|
|
|
x = margin;
|
2021-07-19 00:01:46 +00:00
|
|
|
|
2021-07-19 02:37:09 +00:00
|
|
|
let gui_port_0 = GuiPort::new (fltk_tx, &mut x, y, 0);
|
|
|
|
y += h + margin;
|
|
|
|
x = margin;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-10-10 18:31:52 +00:00
|
|
|
let ports = vec! [
|
|
|
|
Port {
|
|
|
|
gui: gui_port_0,
|
|
|
|
forwarding_instance: None,
|
|
|
|
},
|
|
|
|
Port {
|
|
|
|
gui: gui_port_1,
|
|
|
|
forwarding_instance: None,
|
|
|
|
},
|
|
|
|
Port {
|
|
|
|
gui: gui_port_2,
|
|
|
|
forwarding_instance: None,
|
|
|
|
},
|
2021-07-19 02:37:09 +00:00
|
|
|
];
|
2021-07-19 02:11:13 +00:00
|
|
|
|
2021-10-10 17:56:13 +00:00
|
|
|
let mut gui_client = GuiClient {
|
|
|
|
rt: &rt,
|
|
|
|
frame_status,
|
2021-10-10 18:31:52 +00:00
|
|
|
ports,
|
2021-10-10 17:56:13 +00:00
|
|
|
};
|
|
|
|
|
2021-07-19 00:25:25 +00:00
|
|
|
// y += h + margin;
|
2021-07-19 00:01:46 +00:00
|
|
|
|
|
|
|
wind.end ();
|
|
|
|
wind.show ();
|
|
|
|
|
2021-07-19 00:25:25 +00:00
|
|
|
let connection_p2_p3 = rt.block_on (async move {
|
2021-10-01 17:19:23 +00:00
|
|
|
let server_cert = match opt.cert_url.as_ref () {
|
|
|
|
Some (url) => reqwest::get (url).await?.bytes ().await?,
|
2021-10-10 17:39:51 +00:00
|
|
|
None => tokio::fs::read ("ptth_quic_output/quic_server.crt").await?.into (),
|
2021-10-01 17:19:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let relay_addr = opt.relay_addr
|
|
|
|
.unwrap_or_else (|| String::from ("127.0.0.1:30380"))
|
|
|
|
.parse ()
|
|
|
|
.context ("relay_addr should be like 127.0.0.1:30380")?;
|
2021-07-19 00:25:25 +00:00
|
|
|
let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&server_cert])?;
|
|
|
|
|
|
|
|
trace! ("Connecting to relay server");
|
|
|
|
|
|
|
|
let client_id = opt.client_id.unwrap_or_else (|| "bogus_client".to_string ());
|
|
|
|
|
|
|
|
let quinn::NewConnection {
|
|
|
|
connection,
|
|
|
|
..
|
2021-10-01 17:19:23 +00:00
|
|
|
} = protocol::p2_connect_to_p3 (&endpoint, &relay_addr, &client_id).await
|
|
|
|
.context ("P2 can't connect to P3")?;
|
2021-07-19 00:25:25 +00:00
|
|
|
|
|
|
|
Ok::<_, anyhow::Error> (connection)
|
|
|
|
})?;
|
2021-07-19 00:01:46 +00:00
|
|
|
|
|
|
|
while app.wait () {
|
|
|
|
match fltk_rx.recv () {
|
2021-07-19 02:37:09 +00:00
|
|
|
Some (Message::OpenPort (port_idx)) => {
|
2021-10-10 17:56:13 +00:00
|
|
|
match gui_client.open_port (connection_p2_p3.clone (), port_idx) {
|
|
|
|
Err (e) => error! ("{:?}", e),
|
|
|
|
_ => (),
|
|
|
|
};
|
2021-07-19 00:01:46 +00:00
|
|
|
},
|
2021-07-19 02:37:09 +00:00
|
|
|
Some (Message::ClosePort (port_idx)) => {
|
2021-10-10 17:56:13 +00:00
|
|
|
gui_client.close_port (port_idx)?;
|
2021-07-19 00:01:46 +00:00
|
|
|
},
|
|
|
|
None => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_active <W: WidgetExt> (w: &mut W, b: bool) {
|
|
|
|
if b {
|
|
|
|
w.activate ();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
w.deactivate ();
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 00:25:25 +00:00
|
|
|
|
2021-07-19 02:11:13 +00:00
|
|
|
impl GuiPort {
|
2021-07-19 02:37:09 +00:00
|
|
|
fn new (fltk_tx: fltk::app::Sender <Message>, x: &mut i32, y: i32, port_idx: usize) -> Self {
|
2021-07-19 02:28:18 +00:00
|
|
|
let margin = 10;
|
|
|
|
let h = 30;
|
|
|
|
|
|
|
|
let w = 80;
|
|
|
|
let mut input_client_port = Input::new (*x, y, w, h, "");
|
|
|
|
*x += w + margin;
|
|
|
|
|
|
|
|
let w = 120;
|
|
|
|
let mut input_server_id = Input::new (*x, y, w, h, "");
|
|
|
|
*x += w + margin;
|
|
|
|
|
|
|
|
let w = 80;
|
|
|
|
let mut input_server_port = Input::new (*x, y, w, h, "");
|
|
|
|
*x += w + margin;
|
|
|
|
|
|
|
|
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_server_id.set_value ("bogus_server");
|
|
|
|
input_server_port.set_value ("5900");
|
|
|
|
|
2021-10-10 18:17:50 +00:00
|
|
|
but_open.set_trigger (CallbackTrigger::Release);
|
2021-07-19 02:37:09 +00:00
|
|
|
but_open.emit (fltk_tx, Message::OpenPort (port_idx));
|
2021-10-10 18:17:50 +00:00
|
|
|
but_close.set_trigger (CallbackTrigger::Release);
|
2021-07-19 02:37:09 +00:00
|
|
|
but_close.emit (fltk_tx, Message::ClosePort (port_idx));
|
2021-07-19 02:28:18 +00:00
|
|
|
|
|
|
|
set_active (&mut but_open, true);
|
|
|
|
set_active (&mut but_close, false);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
input_client_port,
|
|
|
|
input_server_id,
|
|
|
|
input_server_port,
|
|
|
|
but_open,
|
|
|
|
but_close,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 02:11:13 +00:00
|
|
|
fn get_params (&self) -> anyhow::Result <ForwardingParams>
|
|
|
|
{
|
|
|
|
let client_tcp_port = u16::from_str (&self.input_client_port.value ())?;
|
|
|
|
let server_id = self.input_server_id.value ();
|
|
|
|
let server_tcp_port = u16::from_str (&self.input_server_port.value ())?;
|
|
|
|
|
|
|
|
Ok (ForwardingParams {
|
|
|
|
client_tcp_port,
|
|
|
|
server_id,
|
|
|
|
server_tcp_port,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_forwarding (&mut self, x: bool) {
|
|
|
|
set_active (&mut self.input_client_port, !x);
|
|
|
|
set_active (&mut self.input_server_id, !x);
|
|
|
|
set_active (&mut self.input_server_port, !x);
|
|
|
|
set_active (&mut self.but_open, !x);
|
|
|
|
set_active (&mut self.but_close, x);
|
|
|
|
|
|
|
|
self.but_open.set (x);
|
|
|
|
self.but_close.set (!x);
|
|
|
|
}
|
|
|
|
}
|