Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add send_device_info #181

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ rumqttc = { version = "0.20.0" }
cuid = { version = "1.3.1" }
shadow-rs = "0.21.0"
dotenv = "0.15.0"
mac_address = "1.1.5"

[dev-dependencies]
rstest = { version = "0.17.0" }
Expand Down
25 changes: 25 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use crate::config::ServerConfig;
use crate::network::Network;
use dotenv::dotenv;
use handlers::Command;
use mac_address::get_mac_address;
use std::env;

mod config;
mod controllers;
Expand Down Expand Up @@ -180,6 +182,7 @@ async fn main() -> std::io::Result<()> {

// NOTE: hub initilize
hub_initilize(did.did_document.id.clone()).await;
send_device_info(did.did_document.id.clone()).await;

let sock_path = runtime_dir.clone().join("nodex.sock");

Expand Down Expand Up @@ -323,6 +326,28 @@ async fn hub_initilize(did: String) {
};
}

async fn send_device_info(did: String) {
const VERSION: &str = env!("CARGO_PKG_VERSION");
const OS: &str = env::consts::OS;
let mac_address: String = match get_mac_address() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MUST
Following this code, get_mac_address() returns the first MAC address.
https://github.com/repnop/mac_address/blob/master/src/lib.rs#L107C5-L107C5
So if the machine has multiple NICs, it may return a different address that doesn't connect to the Hub.
Is this the desired behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly getting the first one with the OS command when there are multiple NICs is a problem, but is there any other better way?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no simple solution, but, there may be some solutions like the following page.
https://qiita.com/saiton03/items/e23b308cc2b8e4ebd66d

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if I have one domain and multiple IP addresses?

Maybe this time, the purpose is not to know which network interface was used to make the connection, but to get the mac address to identify the terminal, so I think it is OK as long as it is unique.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if I have one domain and multiple IP addresses?

DNS will return one IP address randomly. This occurs, for example, with DNS round robin.
If the device has multiple NICs, and the server uses DNS round-robin, the MAC address may switched randomly.

Maybe this time, the purpose is not to know which network interface was used to make the connection, but to get the mac address to identify the terminal, so I think it is OK as long as it is unique.

OK, I understood.

Ok(Some(ma)) => ma.to_string(),
Ok(None) => String::from("No MAC address found."),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO
This code If no mac address registered, the above message is inserted into the Hub instead.
But, I think that if Agent has no mac address, Agent can't connect to Hub.
So I think if Agent can't find the MAC address, we should shutdown the Agent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the mac address is additional information since it is used for the log display screen. It is not a required field, so it is better to be able to communicate even if it cannot be obtained.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not a required field, so it is better to be able to communicate even if it cannot be obtained.

I think so too. But, I think that the device that has no MAC address can't connect to the Hub over IP.
Are there situations where the device can communicate via IP in the absence of a MAC address?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there would be no communication with devices that do not have a MAC address.

Copy link
Contributor

@rabe1028 rabe1028 Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there would be no communication with devices that do not have a MAC address.

I think so too.
So, I think the message, "No MAC address found." can't be sent to Hub.

Err(_) => String::from("No MAC address found."),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits

Suggested change
Ok(None) => String::from("No MAC address found."),
Err(_) => String::from("No MAC address found."),
_ => String::from("No MAC address found."),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, Thanks!
c0b716c

};

let hub = Hub::new();
match hub
.send_device_info(did, mac_address, VERSION.to_string(), OS.to_string())
.await
{
Ok(()) => (),
Err(e) => {
log::error!("{:?}", e);
panic!()
}
};
}

use env_logger::fmt::Color;
use log::Level;

Expand Down
50 changes: 48 additions & 2 deletions src/nodex/utils/hub_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use reqwest::{
};
use sha2::Sha256;

use crate::services::internal::didcomm_encrypted::DIDCommEncryptedService;
use serde_json::json;

type HmacSha256 = Hmac<Sha256>;

pub struct HubClientConfig {
Expand Down Expand Up @@ -82,8 +85,8 @@ impl HubClient {
}
}

pub async fn post(&self, _path: &str, body: &str) -> Result<reqwest::Response, NodeXError> {
let url = self.base_url.join(_path);
pub async fn post(&self, path: &str, body: &str) -> Result<reqwest::Response, NodeXError> {
let url = self.base_url.join(path);
let headers = self.auth_headers(body.to_string());
if let Err(e) = headers {
log::error!("{:?}", e);
Expand All @@ -105,6 +108,49 @@ impl HubClient {
}
}

pub async fn send_device_info(
&self,
path: &str,
to_did: &str,
mac_address: &str,
version: &str,
os: &str,
) -> Result<reqwest::Response, NodeXError> {
let message = json!({
"mac_address": mac_address,
"version": version,
"os": os,
});
let payload = match DIDCommEncryptedService::generate(to_did, &json!(message), None).await {
Ok(v) => v,
Err(e) => {
log::error!("{:?}", e);
return Err(NodeXError {});
}
};
let payload = match serde_json::to_string(&payload) {
Ok(v) => v,
Err(e) => {
log::error!("{:?}", e);
return Err(NodeXError {});
}
};
let url = self.base_url.join(path);
match self
.instance
.post(&url.unwrap().to_string())
.body(payload.to_string())
.send()
.await
{
Ok(v) => Ok(v),
Err(e) => {
log::error!("{:?}", e);
Err(NodeXError {})
}
}
}

#[allow(dead_code)]
pub async fn put(&self, _path: &str) -> Result<reqwest::Response, NodeXError> {
let url = self.base_url.join(_path);
Expand Down
35 changes: 35 additions & 0 deletions src/services/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ struct RegisterDeviceRequest {
project_id: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct SendDeviceInfoRequest {
device_did: String,
mac_address: String,
version: String,
os: String,
}

impl Hub {
pub fn new() -> Self {
let server_config = server_config();
Expand Down Expand Up @@ -59,4 +67,31 @@ impl Hub {
}
}
}

pub async fn send_device_info(
&self,
to_did: String,
mac_address: String,
version: String,
os: String,
) -> Result<(), NodeXError> {
let res = match self
.http_client
.send_device_info("/v1/device_info", &to_did, &mac_address, &version, &os)
.await
{
Ok(v) => v,
Err(e) => {
log::error!("{:?}", e);
return Err(NodeXError {});
}
};
match res.json::<EmptyResponse>().await {
Ok(_) => Ok(()),
Err(e) => {
log::error!("{:?}", e);
Err(NodeXError {})
}
}
}
}
Loading