Files
Jędrzej Stuczyński e5afd54ce0 Feature/node status api (#680)
* Basic storage stub

* New models for new node status api

* Route handling

* Mounting new routes

* Missing selective commit

* Moved network monitor related files to separate package

* Starting to see some sqlx action

* Schema updates

* Log statement upon finished migration

* Removed old diesel related imports

* Converted mixnode cache initialisation into a fairing

* Moved cache related functionalities to separate package

Also defined staging there

* Created run method for validator cache + removed unwrap

* Removed old node-status-api types and left bunch of todo placeholders in their place

* Fixed managing validatorcache

* Status reports are starting to get constructed

* Submitting some dummy results to the database

* Removing duplicate code for generating reports

* Removed statuses older than 48h

* Initial attempt at trying to obtain reports for all active nodes

* Removed duplicates from the full report

* Grabbing uptime history

* Updating historical uptimes of active nodes

* Updated sqlx-data.json

* Removed all placeholder foomp owner values

* Changed Layer serde behaviour for easier usage

* Extended validator api config

* Initial (seems working !) integration with network monitor

* Added database path configuration to config

* Using ValidatorCache in NetworkMonitor

* Flag indicating whether validator cache has been initialised

* Introduced a locla-only route for reward script to perform daily chores

* Flag to save config to a file

* Moved spawning of receiving future to run method rather than new

* Removed arguments that dont make sense to be configured via CLI

* Removed dead code from config file

* More dead code removal

* Added validator API to CI

* Corrected manifest-path arguments

* Constructing network monitor by passing config

* Combined validator API CI with the main CI file

* Using query_as for NodeStatus

* Checking if historical uptimes were already calculated on particular day

* Making id field NOT NULL

* More query_as! action

* Updated sqlx-data.json

* Removed unused chrono feature

* Renamed the migration file

* Changed default validator endpoint to point to local validator

* Removing unnecessary clone

* More appropriate naming

* Removed dead code

* Lock file updates

* Updated network monitor address in contract code

* Don't stage node status api if network monitor is disabled

* cargo fmt

* Updated all license notices to SPDX
2021-07-19 14:02:47 +01:00

89 lines
3.4 KiB
Rust

// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use super::SHUTDOWN_TIMEOUT;
use crate::connection_controller::{ConnectionMessage, ConnectionReceiver};
use futures::FutureExt;
use futures::StreamExt;
use log::*;
use socks5_requests::ConnectionId;
use std::{sync::Arc, time::Duration};
use tokio::io::AsyncWriteExt;
use tokio::select;
use tokio::{net::tcp::OwnedWriteHalf, sync::Notify, time::sleep, time::Instant};
const MIX_TTL: Duration = Duration::from_secs(5 * 60);
async fn deal_with_message(
connection_message: ConnectionMessage,
writer: &mut OwnedWriteHalf,
local_destination_address: &str,
remote_source_address: &str,
connection_id: ConnectionId,
) -> bool {
debug!(
target: &*format!("({}) socks5 outbound", connection_id),
"[{} bytes]\t{} → remote → mixnet → local → {} Remote closed: {}",
connection_message.payload.len(),
remote_source_address,
local_destination_address,
connection_message.socket_closed
);
if let Err(err) = writer.write_all(&connection_message.payload).await {
// the other half is probably going to blow up too (if not, this task also needs to notify the other one!!)
error!(target: &*format!("({}) socks5 outbound", connection_id), "failed to write response back to the socket - {}", err);
return true;
}
if connection_message.socket_closed {
debug!(target: &*format!("({}) socks5 outbound", connection_id),
"Remote socket got closed - closing the local socket too");
return true;
}
false
}
pub(super) async fn run_outbound(
mut writer: OwnedWriteHalf,
local_destination_address: String, // addresses are provided for better logging
remote_source_address: String,
mut mix_receiver: ConnectionReceiver,
connection_id: ConnectionId,
shutdown_notify: Arc<Notify>,
) -> (OwnedWriteHalf, ConnectionReceiver) {
let shutdown_future = shutdown_notify.notified().then(|_| sleep(SHUTDOWN_TIMEOUT));
tokio::pin!(shutdown_future);
let mut mix_timeout = Box::pin(sleep(MIX_TTL));
loop {
select! {
connection_message = &mut mix_receiver.next() => {
if let Some(connection_message) = connection_message {
if deal_with_message(connection_message, &mut writer, &local_destination_address, &remote_source_address, connection_id).await {
break;
}
mix_timeout.as_mut().reset(Instant::now() + MIX_TTL);
} else {
warn!("mix receiver is none so we already got removed somewhere. This isn't really a warning, but shouldn't happen to begin with, so please say if you see this message");
break;
}
}
_ = &mut mix_timeout => {
warn!("didn't get anything from the client on {} mixnet in {:?}. Shutting down the proxy.", connection_id, MIX_TTL);
// If they were online it's kinda their fault they didn't send any heartbeat messages.
break;
}
_ = &mut shutdown_future => {
debug!("closing outbound proxy after inbound was closed {:?} ago", SHUTDOWN_TIMEOUT);
break;
}
}
}
trace!("{} - outbound closed", connection_id);
shutdown_notify.notify_one();
(writer, mix_receiver)
}