Skip to content

Commit

Permalink
feat(rust): rename the port field into address and support change the…
Browse files Browse the repository at this point in the history
… address and the port

The changes include:
* Replace the Port field name to Address
* Support the formats Host:Port, IP:Port and Port (defaulting to 127.0.0.1 aka `localhost`)
* Adapt the app back-end to parse the address correctly.

- Closes: #5685
  • Loading branch information
0x61nas authored and adrianbenavides committed Sep 8, 2023
1 parent 4aece9e commit b7253f5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions implementations/rust/ockam/ockam_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ockam = { path = "../ockam", version = "^0.91.0", features = ["software_vault"]
ockam_api = { path = "../ockam_api", version = "0.34.0", features = ["std", "authenticators"] }
ockam_core = { path = "../ockam_core", version = "^0.84.0" }
ockam_multiaddr = { path = "../ockam_multiaddr", version = "0.25.0", features = ["cbor", "serde"] }
ockam_transport_tcp = { path = "../ockam_transport_tcp", version = "^0.85.0" }
percent-encoding = "2.3.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use miette::{IntoDiagnostic, WrapErr};
use ockam_api::address::extract_address_value;
use std::net::SocketAddr;
use ockam_transport_tcp::resolve_peer;
use tauri::{AppHandle, Manager, Wry};
use tracing::{debug, error, info};

Expand All @@ -9,16 +9,19 @@ use crate::app::AppState;
use crate::error::Error;
use crate::invitations::commands::create_service_invitation;

/// The default host to use when creating a TCP outlet if the user doesn't specify one.
const DEFAULT_HOST: &str = "localhost";

/// Create a TCP outlet within the default node.
#[tauri::command]
pub async fn tcp_outlet_create(
app: AppHandle<Wry>,
service: String,
port: String,
address: String,
email: String,
) -> Result<(), String> {
let email = if email.is_empty() { None } else { Some(email) };
tcp_outlet_create_impl(app, service, port, email)
tcp_outlet_create_impl(app, service, address, email)
.await
.map_err(|e| {
error!("{:?}", e);
Expand All @@ -30,15 +33,19 @@ pub async fn tcp_outlet_create(
async fn tcp_outlet_create_impl(
app: AppHandle<Wry>,
service: String,
port: String,
address: String,
email: Option<String>,
) -> crate::Result<()> {
debug!(%service, %port, "Creating an outlet");
debug!(%service, %address, "Creating an outlet");
let app_state = app.state::<AppState>();
let socket_addr: SocketAddr = format!("127.0.0.1:{port}")
.parse()
let addr = if let Some((host, port)) = address.split_once(':') {
format!("{host}:{port}")
} else {
format!("{DEFAULT_HOST}:{address}")
};
let socket_addr = resolve_peer(addr)
.into_diagnostic()
.wrap_err("Invalid port")?;
.wrap_err("Invalid address. The expected formats are 'host:port', 'ip:port' or 'port'")?;
let worker_addr = extract_address_value(&service).wrap_err("Invalid service address")?;
let node_manager_worker = app_state.node_manager_worker().await;
let mut node_manager = node_manager_worker.inner().write().await;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import { invoke } from "@tauri-apps/api/tauri";
let service = "";
let port = "10000";
let address = "localhost:10000";
let email = "";
let error_message = "";
async function submit() {
error_message = "";
await invoke("plugin:shared_service|tcp_outlet_create", {
service: service,
port: port,
address: address,
email: email,
})
.then(close)
Expand Down Expand Up @@ -47,15 +47,15 @@
</div>
<div class="flex items-start">
<div class="flex-1">
<div class="font-bold">Port</div>
<p class="text-sm text-gray-500">Choose a port for the service</p>
<div class="font-bold">Address</div>
<p class="text-sm text-gray-500">Choose an address for the service</p>
</div>
<div class="flex-1">
<input
type="text"
class="w-full border-none bg-transparent px-4 text-right text-base focus:outline-none"
placeholder={port}
bind:value={port}
placeholder={address}
bind:value={address}
/>
</div>
</div>
Expand Down

0 comments on commit b7253f5

Please sign in to comment.