Skip to content

Commit

Permalink
connect: add tauri methods to get and set provider (nymtech#1406)
Browse files Browse the repository at this point in the history
  • Loading branch information
octol authored Jun 27, 2022
1 parent 97f77c4 commit 479cc20
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
7 changes: 6 additions & 1 deletion nym-connect/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions nym-connect/src-tauri/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub static SOCKS5_CONFIG_ID: Lazy<String> = Lazy::new(|| {
});

// TODO: make this configurable from the UI
// TODO: once we can set this is the UI, consider just removing it, and put in guards to halt if
// user hasn't chosen the provider
pub static PROVIDER_ADDRESS: &str = "EWa8DgePKfuWSjqPo6NEdavBK6gpnK4TKb2npi2HWuC2.6PGVT9y83UMGbFrPKDnCvTP2jJjpXYpD87ZpiRsLo1YR@CgQrYP8etksSBf4nALNqp93SHPpgFwEUyTsjBNNLj5WM";

const DEFAULT_ETH_ENDPOINT: &str = "https://rinkeby.infura.io/v3/00000000000000000000000000000000";
Expand Down Expand Up @@ -50,9 +52,10 @@ impl Config {
self.socks5.get_base_mut()
}

pub async fn init() {
pub async fn init(service_provider: Option<&String>) {
let service_provider = service_provider.map_or(PROVIDER_ADDRESS, String::as_str);
info!("Initialising...");
init_socks5(PROVIDER_ADDRESS, None).await;
init_socks5(service_provider, None).await;
info!("Configuration saved 🚀");
}
}
Expand Down
2 changes: 2 additions & 0 deletions nym-connect/src-tauri/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum BackendError {
CouldNotConnect,
#[error("Could not disconnect")]
CouldNotDisconnect,
#[error("No serverice provider set")]
NoServiceProviderSet,
}

impl Serialize for BackendError {
Expand Down
2 changes: 2 additions & 0 deletions nym-connect/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ fn main() {
tauri::Builder::default()
.manage(Arc::new(RwLock::new(State::new())))
.invoke_handler(tauri::generate_handler![
crate::operations::connection::connect::get_service_provider,
crate::operations::connection::connect::set_service_provider,
crate::operations::connection::connect::start_connecting,
crate::operations::connection::disconnect::start_disconnecting,
crate::operations::window::hide_window,
Expand Down
21 changes: 21 additions & 0 deletions nym-connect/src-tauri/src/operations/connection/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@ pub async fn start_connecting(
address: "Test".to_string(),
})
}

#[tauri::command]
pub async fn get_service_provider(
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<String, BackendError> {
let guard = state.read().await;
guard
.get_service_provider()
.clone()
.ok_or(BackendError::NoServiceProviderSet)
}

#[tauri::command]
pub async fn set_service_provider(
service_provider: String,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<(), BackendError> {
let mut guard = state.write().await;
guard.set_service_provider(service_provider);
Ok(())
}
16 changes: 13 additions & 3 deletions nym-connect/src-tauri/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ use tauri::Manager;

pub struct State {
status: ConnectionStatusKind,
service_provider: Option<String>,
socks5_client_sender: Option<Socks5ControlMessageSender>,
}

impl State {
pub fn new() -> Self {
State {
status: ConnectionStatusKind::Disconnected,
service_provider: None,
socks5_client_sender: None,
}
}
Expand All @@ -42,8 +44,16 @@ impl State {
.unwrap();
}

pub async fn init_config() {
crate::config::Config::init().await;
pub fn get_service_provider(&self) -> &Option<String> {
&self.service_provider
}

pub fn set_service_provider(&mut self, provider: String) {
self.service_provider = Some(provider);
}

pub async fn init_config(&self) {
crate::config::Config::init(self.service_provider.as_ref()).await;
}

pub async fn start_connecting(&mut self, window: &tauri::Window<tauri::Wry>) {
Expand All @@ -52,7 +62,7 @@ impl State {
self.status = ConnectionStatusKind::Connecting;

// Setup configuration by writing to file
Self::init_config().await;
self.init_config().await;

// Kick of the main task and get the channel for controlling it
let sender = start_nym_socks5_client();
Expand Down

0 comments on commit 479cc20

Please sign in to comment.