sshuttle-gui/src/app.rs

173 lines
5.4 KiB
Rust

use egui::Window;
use crate::connect_ssh;
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
pub struct TemplateApp {
// Example stuff:
label: String,
deets: SshDeets,
// this how you opt-out of serialization of a member
#[serde(skip)]
value: f32,
}
#[derive(serde::Deserialize, serde::Serialize, Clone)]
#[serde(default)]
pub struct SshDeets {
pub dns: bool,
pub user: String,
pub ip: String,
pub port: String,
pub mask: String,
}
impl Default for SshDeets {
fn default() -> Self {
Self {
dns: true,
user: "No".to_owned(),
ip: "127.0.0.1".to_owned(),
port: "8080".to_owned(),
mask: "".to_owned(),
}
}
}
impl SshDeets {
pub fn new(self,
yeah: bool,
username: &str,
ip_addy: &str,
port_num: &str,
masking: String) -> Self
{
Self {
dns: yeah,
user: String::from(username),
ip: String::from(ip_addy),
port: String::from(port_num),
mask: masking,
}
}
pub fn concat(&self, connect: bool) -> Vec<String> {
let mut final_thing: Vec<String> = vec![];
if connect == false{
if self.dns == true {
final_thing.push(String::from("--dns"));
final_thing.push(String::from("-Nr"));
final_thing.push(format!("{}:{}", self.user, self.ip));
final_thing.push(String::from("-x"));
final_thing.push(self.mask.clone());
return final_thing;
}
else {
return vec![];
}
}else {
unimplemented!();
}
}
}
impl Default for TemplateApp {
fn default() -> Self {
Self {
// Example stuff:
deets: SshDeets::default(),
label: "Hello World!".to_owned(),
value: 2.7,
}
}
}
impl TemplateApp {
/// Called once before the first frame.
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
// This is also where you can customized the look at feel of egui using
// `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.
// Load previous app state (if any).
// Note that you must enable the `persistence` feature for this to work.
if let Some(storage) = cc.storage {
return eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
}
Default::default()
}
}
impl eframe::App for TemplateApp {
/// Called by the frame work to save state before shutdown.
fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, eframe::APP_KEY, self);
}
/// Called each time the UI needs repainting, which may be many times per second.
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
let Self { label, value, deets } = self;
// Examples of how to create different panels and windows.
// Pick whichever suits you.
// Tip: a good default choice is to just keep the `CentralPanel`.
// For inspiration and more examples, go to https://emilk.github.io/egui
#[cfg(not(target_arch = "wasm32"))] // no File->Quit on web pages!
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
// The top panel is often a good place for a menu bar:
egui::menu::bar(ui, |ui| {
ui.menu_button("File", |ui| {
if ui.button("Quit").clicked() {
_frame.close();
}
});
});
});
egui::CentralPanel::default().show(ctx, |ui| {
// The central panel the region left after adding TopPanel's and SidePanel's
let mut outout = String::new();
ui.label("put your linux ssh username, ip, and port (does not have to be root)");
ui.text_edit_singleline(&mut deets.user);
ui.text_edit_singleline(&mut deets.ip);
ui.text_edit_singleline(&mut deets.port);
ui.label("check if using --dns flag");
ui.checkbox(&mut deets.dns, "dns'd");
ui.label("include mask flags");
ui.text_edit_singleline(&mut deets.mask);
if ui.button("connect").clicked() {
connect_ssh(false, &*deets);
}
/* ui.heading("eframe template");
ui.hyperlink("https://github.com/emilk/eframe_template");
ui.add(egui::github_link_file!(
"https://github.com/emilk/eframe_template/blob/master/",
"Source code."
)); */
egui::SidePanel::right("side_panel").show(ctx, |ui| {
ui.add(egui::widgets::Label::new("This will be for stdout eventually".to_owned()));
});
egui::warn_if_debug_build(ui);
});
Window::new("info")
.anchor(egui::Align2::RIGHT_TOP, [-5.0,5.0])
.default_size([500.0,500.0])
.show(ctx, |ui| {
ui.label("Make sure you have ssh keys, no password connecting!");
});
}
}