-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
123 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use config::{Config, File}; | ||
use serde::Deserialize; | ||
use std::path::Path; | ||
|
||
/// Configuration for the proxy server | ||
/// | ||
/// This struct holds the configuration parameters needed to connect to proxy server. | ||
#[derive(Debug, Deserialize)] | ||
pub struct ProxyConfig { | ||
pub proxy_address: String, | ||
pub node_public_address: String, | ||
pub country: String, | ||
} | ||
|
||
impl ProxyConfig { | ||
/// Creates a new ProxyConfig instance from a configuration file | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `config_file_path` - Path to the configuration file. The file should be in a format | ||
/// supported by the `config` crate (e.g., TOML, JSON, YAML) and | ||
/// contain an "proxy_server" section with the required configuration | ||
/// parameters. | ||
/// | ||
/// # Returns | ||
/// | ||
/// Returns a new `ProxyConfig` instance populated with values from the config file. | ||
/// | ||
/// # Panics | ||
/// | ||
/// This method will panic if: | ||
/// * The configuration file cannot be read or parsed | ||
/// * The "proxy_server" section is missing from the configuration | ||
/// * The configuration format doesn't match the expected structure | ||
pub fn from_file_path<P: AsRef<Path>>(config_file_path: P) -> Self { | ||
let builder = Config::builder() | ||
.add_source(File::with_name(config_file_path.as_ref().to_str().unwrap())) | ||
.add_source( | ||
config::Environment::with_prefix("PROXY_SERVER") | ||
.keep_prefix(true) | ||
.separator("__"), | ||
); | ||
let config = builder | ||
.build() | ||
.expect("Failed to generate atoma-daemon configuration file"); | ||
config | ||
.get::<Self>("proxy_server") | ||
.expect("Failed to generate configuration instance") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use atoma_utils::constants::SIGNATURE; | ||
use config::ProxyConfig; | ||
use reqwest::Client; | ||
use serde_json::json; | ||
use sui_keys::keystore::FileBasedKeystore; | ||
|
||
use crate::server::utils::sign_response_body; | ||
|
||
pub mod config; | ||
|
||
/// Registers the node on the proxy server | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `config` - Proxy configuration | ||
/// * `node_small_id` - Small ID of the node | ||
/// * `keystore` - Keystore for signing the registration request | ||
/// * `address_index` - Index of the address to use for signing | ||
pub async fn register_on_proxy( | ||
config: &ProxyConfig, | ||
node_small_id: u64, | ||
keystore: &FileBasedKeystore, | ||
address_index: usize, | ||
) -> anyhow::Result<()> { | ||
let client = Client::new(); | ||
let url = format!("{}/node/registration", config.proxy_address); | ||
|
||
let body = json!({ | ||
"node_small_id": node_small_id, | ||
"public_address": config.node_public_address, | ||
"country": config.country, | ||
}); | ||
|
||
let (_, signature) = sign_response_body(&body, keystore, address_index)?; | ||
|
||
let res = client | ||
.post(&url) | ||
.header(SIGNATURE, signature) | ||
.json(&body) | ||
.send() | ||
.await?; | ||
|
||
if !res.status().is_success() { | ||
anyhow::bail!("Failed to register on proxy server: {}", res.status()); | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters