connect: allow user select gateway on backend (#1415)

* connect: allow user selected gateway

* rustfmt
This commit is contained in:
Jon Häggblad
2022-06-28 17:39:33 +02:00
committed by GitHub
parent bb0c3d251e
commit ea22a6f80f
5 changed files with 46 additions and 7 deletions
+3 -2
View File
@@ -55,10 +55,11 @@ impl Config {
self.socks5.get_base_mut()
}
pub async fn init(service_provider: Option<&String>) {
pub async fn init(service_provider: Option<&String>, chosen_gateway_id: Option<&String>) {
let service_provider = service_provider.map_or(PROVIDER_ADDRESS, String::as_str);
let chosen_gateway_id = chosen_gateway_id.map(String::as_str);
info!("Initialising...");
init_socks5(service_provider, None).await;
init_socks5(service_provider, chosen_gateway_id).await;
info!("Configuration saved 🚀");
}
+3 -1
View File
@@ -10,8 +10,10 @@ pub enum BackendError {
CouldNotConnect,
#[error("Could not disconnect")]
CouldNotDisconnect,
#[error("No serverice provider set")]
#[error("No service provider set")]
NoServiceProviderSet,
#[error("No gateway provider set")]
NoGatewaySet,
}
impl Serialize for BackendError {
+2
View File
@@ -35,7 +35,9 @@ fn main() {
.manage(Arc::new(RwLock::new(State::new())))
.invoke_handler(tauri::generate_handler![
crate::config::get_config_file_location,
crate::operations::connection::connect::get_gateway,
crate::operations::connection::connect::get_service_provider,
crate::operations::connection::connect::set_gateway,
crate::operations::connection::connect::set_service_provider,
crate::operations::connection::connect::start_connecting,
crate::operations::connection::disconnect::start_disconnecting,
@@ -39,3 +39,24 @@ pub async fn set_service_provider(
guard.set_service_provider(service_provider);
Ok(())
}
#[tauri::command]
pub async fn get_gateway(
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<String, BackendError> {
let guard = state.read().await;
guard
.get_gateway()
.clone()
.ok_or(BackendError::NoGatewaySet)
}
#[tauri::command]
pub async fn set_gateway(
gateway: String,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<(), BackendError> {
let mut guard = state.write().await;
guard.set_gateway(gateway);
Ok(())
}
+17 -4
View File
@@ -1,3 +1,4 @@
use client_core::config::GatewayEndpoint;
use futures::channel::mpsc;
use futures::SinkExt;
use log::info;
@@ -17,6 +18,7 @@ use tauri::Manager;
pub struct State {
status: ConnectionStatusKind,
service_provider: Option<String>,
gateway: Option<String>,
socks5_client_sender: Option<Socks5ControlMessageSender>,
}
@@ -25,6 +27,7 @@ impl State {
State {
status: ConnectionStatusKind::Disconnected,
service_provider: None,
gateway: None,
socks5_client_sender: None,
}
}
@@ -52,8 +55,16 @@ impl State {
self.service_provider = Some(provider);
}
pub fn get_gateway(&self) -> &Option<String> {
&self.gateway
}
pub fn set_gateway(&mut self, gateway: String) {
self.gateway = Some(gateway);
}
pub async fn init_config(&self) {
crate::config::Config::init(self.service_provider.as_ref()).await;
crate::config::Config::init(self.service_provider.as_ref(), self.gateway.as_ref()).await;
}
pub async fn start_connecting(&mut self, window: &tauri::Window<tauri::Wry>) {
@@ -65,7 +76,8 @@ impl State {
self.init_config().await;
// Kick of the main task and get the channel for controlling it
let sender = start_nym_socks5_client();
let (sender, used_gateway) = start_nym_socks5_client();
self.gateway = Some(used_gateway.gateway_id);
self.socks5_client_sender = Some(sender);
self.status = ConnectionStatusKind::Connected;
@@ -87,11 +99,12 @@ impl State {
}
}
fn start_nym_socks5_client() -> Socks5ControlMessageSender {
fn start_nym_socks5_client() -> (Socks5ControlMessageSender, GatewayEndpoint) {
let id: &str = SOCKS5_CONFIG_ID;
info!("Loading config from file");
let config = nym_socks5::client::config::Config::load_from_file(Some(id)).unwrap();
let used_gateway = config.get_base().get_gateway_endpoint().clone();
let mut socks5_client = Socks5NymClient::new(config);
info!("Starting socks5 client");
@@ -107,5 +120,5 @@ fn start_nym_socks5_client() -> Socks5ControlMessageSender {
});
});
sender
(sender, used_gateway)
}