Skip to content

Commit

Permalink
need smart contract function
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranay Tulugu committed Aug 21, 2023
1 parent 23d29a9 commit 8768f04
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 84 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions althea_types/src/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,6 @@ pub struct OperatorExitCheckinMessage {
pub pass: String,
/// This is to keep track of the rita exit uptime for debugging purposes
pub exit_uptime: Duration,
/// A list of registered wg keys that ops can use to display routers to be registered
pub registered_keys: Option<Vec<WgKey>>,
/// Number of users online
pub users_online: Option<u32>,
}
Expand Down
113 changes: 106 additions & 7 deletions rita_exit/src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! This module contains all the tools and functions that integrate with the clients database
//! for the exit, which is most exit logic in general. Keep in mind database connections are remote
//! and therefore synchronous database requests are quite expensive (on the order of tens of milliseconds)
use crate::database::geoip::get_country;
use crate::database::geoip::get_gateway_ip_bulk;
use crate::database::geoip::get_gateway_ip_single;
use crate::database::geoip::verify_ip;
use crate::database::struct_tools::display_hashset;
use crate::database::struct_tools::get_client_internal_ip;
use crate::database::struct_tools::get_client_ipv6;
use crate::database::struct_tools::to_exit_client;
Expand All @@ -25,6 +25,7 @@ use settings::get_rita_exit;
use std::collections::HashMap;
use std::collections::HashSet;
use std::net::IpAddr;
use std::time::Duration;
use std::time::Instant;
use std::time::SystemTime;

Expand All @@ -35,6 +36,20 @@ pub mod struct_tools;
/// one day in seconds
pub const ONE_DAY: i64 = 86400;

/// Default url exits use to request a client registration
pub const DEFAULT_REGISTER_URL: &str = "https://operator.althea.net:8080/register_router";

/// Timeout when requesting client registration
pub const CLIENT_REGISTER_TIMEOUT: Duration = Duration::from_secs(5);

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExitSignupReturn {
RegistrationOk,
PendingRegistration,
BadPhoneNumber,
InternalServerError { e: String },
}

pub fn get_exit_info() -> ExitDetails {
let exit_settings = get_rita_exit();
ExitDetails {
Expand All @@ -45,13 +60,19 @@ pub fn get_exit_info() -> ExitDetails {
netmask: exit_settings.exit_network.netmask,
description: exit_settings.description,
verif_mode: match exit_settings.verif_settings {
Some(ExitVerifSettings::Email(_mailer_settings)) => ExitVerifMode::Email,
Some(ExitVerifSettings::Phone(_phone_settings)) => ExitVerifMode::Phone,
None => ExitVerifMode::Off,
},
}
}

fn get_client_register_url() -> String {
match get_rita_exit().client_register_url {
Some(url) => url,
_ => DEFAULT_REGISTER_URL.to_string(),
}
}

/// Handles a new client registration api call. Performs a geoip lookup
/// on their registration ip to make sure that they are coming from a valid gateway
/// ip and then sends out an email of phone message
Expand All @@ -67,15 +88,93 @@ pub async fn signup_client(
let verify_status = verify_ip(gateway_ip)?;
info!("verified the ip country {:?}", client);

let user_country = get_country(gateway_ip)?;
info!("got the country {:?}", client);
// Is client requesting from a valid country? If so send registration request to ops
if !verify_status {
return Ok(ExitState::Denied {
message: format!(
"This exit only accepts connections from {}",
display_hashset(&exit_settings.allowed_countries),
),
});
}

// Forward request to ops and send result to client accordingly
let exit_client = to_exit_client(client.global);
if let Ok(exit_client) = exit_client {
match forward_client_signup_request(client).await {
registered => {
return Ok(ExitState::Registered {
our_details: ExitClientDetails {
client_internal_ip: get_client_internal_ip(),
internet_ipv6_subnet: get_client_ipv6(client.global),
},
general_details: get_exit_info(),
message: "Registration OK".to_string(),
})
}

pending => {
return Ok(ExitState::Pending {
general_details: get_exit_info(),
message: "awaiting email verification".to_string(),
email_code: None,
phone_code: None,
})
}
}
} else {
return Ok(ExitState::Denied {
message: format!("Error parsing client details with {:?}", exit_client,),
});
}
}

pub async fn forward_client_signup_request(exit_client: ExitClientIdentity) -> ExitSignupReturn {
let url: &str;
if cfg!(feature = "dev_env") {
url = "http://0.0.0.0:8080/register_router";
} else if cfg!(feature = "operator_debug") {
url = "http://192.168.10.2:8080/register_router";
} else {
url = &get_client_register_url();
}

info!(
"Doing database work for {:?} in country {} with verify_status {}",
client, user_country, verify_status
"About to request client {} registration with {}",
exit_client.global, url
);

forward_client_signup_request(client)
let client = awc::Client::default();
let response = client
.post(url)
.timeout(CLIENT_REGISTER_TIMEOUT)
.send_json(&exit_client)
.await;

let response = match response {
Ok(mut response) => {
trace!("Response is {:?}", response.status());
trace!("Response is {:?}", response.headers());
response.json().await
}
Err(e) => {
error!("Failed to perform client registration with {:?}", e);
return ExitSignupReturn::InternalServerError {
e: format!("Unable to contact registration server: {}", e),
};
}
};

let response: ExitSignupReturn = match response {
Ok(a) => a,
Err(e) => {
error!("Failed to decode registration request {:?}", e);
return ExitSignupReturn::InternalServerError {
e: format!("Failed to decode registration request {:?}", e),
};
}
};
response
}

/// Gets the status of a client and updates it in the database
Expand Down
Loading

0 comments on commit 8768f04

Please sign in to comment.