2020-12-13 03:29:54 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
convert::TryFrom,
|
2021-04-29 02:52:43 +00:00
|
|
|
path::Path,
|
2021-04-03 04:19:33 +00:00
|
|
|
time::Instant,
|
2020-12-13 03:29:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use dashmap::DashMap;
|
|
|
|
use tokio::sync::{
|
|
|
|
Mutex,
|
|
|
|
RwLock,
|
|
|
|
oneshot,
|
|
|
|
watch,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
Body,
|
|
|
|
Config,
|
|
|
|
RelayError,
|
|
|
|
ShuttingDownError,
|
2021-04-29 02:52:43 +00:00
|
|
|
config::machine_editable,
|
2020-12-13 03:29:54 +00:00
|
|
|
};
|
|
|
|
|
2021-04-28 01:10:32 +00:00
|
|
|
use ptth_core::{
|
|
|
|
http_serde,
|
|
|
|
prelude::*,
|
|
|
|
};
|
2020-12-13 03:29:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Here's what we need to handle:
|
|
|
|
|
|
|
|
When a request comes in:
|
|
|
|
|
|
|
|
- Park the client in response_rendezvous
|
|
|
|
- Look up the server ID in request_rendezvous
|
|
|
|
- If a server is parked, unpark it and send the request
|
|
|
|
- Otherwise, queue the request
|
|
|
|
|
|
|
|
When a server comes to listen:
|
|
|
|
|
|
|
|
- Look up the server ID in request_rendezvous
|
|
|
|
- Either return all pending requests, or park the server
|
|
|
|
|
|
|
|
When a server comes to respond:
|
|
|
|
|
|
|
|
- Look up the parked client in response_rendezvous
|
|
|
|
- Unpark the client and begin streaming
|
|
|
|
|
|
|
|
So we need these lookups to be fast:
|
|
|
|
|
|
|
|
- Server IDs, where (1 server) or (0 or many clients)
|
|
|
|
can be parked
|
|
|
|
- Request IDs, where 1 client is parked
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub enum RequestRendezvous {
|
|
|
|
ParkedClients (Vec <http_serde::WrappedRequest>),
|
|
|
|
ParkedServer (oneshot::Sender <Result <http_serde::WrappedRequest, ShuttingDownError>>),
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResponseRendezvous = oneshot::Sender <Result <(http_serde::ResponseParts, Body), ShuttingDownError>>;
|
|
|
|
|
|
|
|
#[derive (Clone)]
|
|
|
|
pub struct ServerStatus {
|
|
|
|
pub last_seen: DateTime <Utc>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ServerStatus {
|
|
|
|
fn default () -> Self {
|
|
|
|
Self {
|
|
|
|
last_seen: Utc::now (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:07 +00:00
|
|
|
pub struct Relay {
|
2021-04-18 13:51:36 +00:00
|
|
|
pub (crate) config: RwLock <Config>,
|
2021-04-29 02:52:43 +00:00
|
|
|
pub (crate) me_config: RwLock <machine_editable::Config>,
|
2020-12-13 03:29:54 +00:00
|
|
|
|
2021-04-28 03:36:44 +00:00
|
|
|
/// The parked clients or parked server, keyed by server
|
|
|
|
|
2021-04-18 13:51:36 +00:00
|
|
|
pub (crate) request_rendezvous: Mutex <HashMap <String, RequestRendezvous>>,
|
|
|
|
pub (crate) server_status: Mutex <HashMap <String, ServerStatus>>,
|
2020-12-13 03:29:54 +00:00
|
|
|
|
2021-04-28 03:36:44 +00:00
|
|
|
/// The parked requests, keyed by request ID
|
|
|
|
|
2021-04-18 13:51:36 +00:00
|
|
|
pub (crate) response_rendezvous: RwLock <DashMap <String, ResponseRendezvous>>,
|
2020-12-13 03:29:54 +00:00
|
|
|
|
2021-04-18 13:51:36 +00:00
|
|
|
pub (crate) shutdown_watch_tx: watch::Sender <bool>,
|
|
|
|
pub (crate) shutdown_watch_rx: watch::Receiver <bool>,
|
2021-03-15 19:11:57 +00:00
|
|
|
|
2021-04-29 01:18:53 +00:00
|
|
|
/// List of recently rejected server names (Used to approve servers)
|
2021-04-18 13:51:36 +00:00
|
|
|
pub (crate) unregistered_servers: BoundedVec <RejectedServer>,
|
2021-04-03 04:19:33 +00:00
|
|
|
|
2021-04-29 01:18:53 +00:00
|
|
|
/// Memory backend for audit logging
|
2021-04-03 04:19:33 +00:00
|
|
|
// TODO: Add file / database / network server logging backend
|
2021-04-18 13:51:36 +00:00
|
|
|
pub (crate) audit_log: BoundedVec <AuditEvent>,
|
2021-04-03 04:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Clone)]
|
|
|
|
pub struct RejectedServer {
|
|
|
|
pub name: String,
|
|
|
|
pub tripcode: blake3::Hash,
|
|
|
|
pub seen: DateTime <Utc>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Clone, Debug)]
|
|
|
|
pub struct AuditEvent {
|
|
|
|
time_monotonic: Instant,
|
2021-04-29 01:18:53 +00:00
|
|
|
pub time_utc: DateTime <Utc>,
|
|
|
|
pub data: AuditData,
|
2021-04-03 04:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Clone, Debug)]
|
|
|
|
pub enum AuditData {
|
2021-05-02 21:18:07 +00:00
|
|
|
RegisterServer {
|
|
|
|
user: Option <String>,
|
2021-05-02 21:59:04 +00:00
|
|
|
server: crate::config::file::Server,
|
2021-05-02 21:18:07 +00:00
|
|
|
},
|
2021-04-03 04:19:33 +00:00
|
|
|
RelayStart,
|
|
|
|
WebClientGet {
|
2021-04-29 01:18:53 +00:00
|
|
|
user: Option <String>,
|
2021-04-03 04:19:33 +00:00
|
|
|
server_name: String,
|
2021-04-29 01:18:53 +00:00
|
|
|
uri: String,
|
2021-04-03 04:19:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AuditEvent {
|
|
|
|
pub fn new (data: AuditData) -> Self {
|
|
|
|
Self {
|
|
|
|
time_monotonic: Instant::now (),
|
|
|
|
time_utc: Utc::now (),
|
|
|
|
data,
|
|
|
|
}
|
|
|
|
}
|
2020-12-13 03:29:54 +00:00
|
|
|
}
|
|
|
|
|
2021-04-03 03:14:23 +00:00
|
|
|
pub struct BoundedVec <T: Clone> {
|
|
|
|
bound: usize,
|
|
|
|
v: RwLock <Vec <T>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <T: Clone> BoundedVec <T> {
|
2021-04-03 03:30:22 +00:00
|
|
|
pub fn new (bound: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
bound,
|
|
|
|
v: Default::default (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 03:14:23 +00:00
|
|
|
pub async fn to_vec (&self) -> Vec <T> {
|
|
|
|
let guard = self.v.read ().await;
|
|
|
|
(*guard).clone ()
|
|
|
|
}
|
|
|
|
|
2021-04-03 03:30:22 +00:00
|
|
|
// Not mut because we have a RwLock
|
|
|
|
// One of the only problems with Rust is that
|
|
|
|
// 'mut' doesn't _really_ -mean 'mutable' once you're
|
|
|
|
// multi-threading or using async
|
|
|
|
pub async fn push (&self, x: T) {
|
2021-04-03 03:14:23 +00:00
|
|
|
let mut guard = self.v.write ().await;
|
|
|
|
guard.push (x);
|
|
|
|
|
|
|
|
while guard.len () > self.bound {
|
|
|
|
guard.remove (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:07 +00:00
|
|
|
impl TryFrom <Config> for Relay {
|
2020-12-13 03:29:54 +00:00
|
|
|
type Error = RelayError;
|
|
|
|
|
|
|
|
fn try_from (config: Config) -> Result <Self, Self::Error> {
|
|
|
|
let (shutdown_watch_tx, shutdown_watch_rx) = watch::channel (false);
|
|
|
|
|
2021-04-29 02:52:43 +00:00
|
|
|
let me_config = machine_editable::Config::default ();
|
|
|
|
|
|
|
|
let me_config = match machine_editable::Config::from_file (Path::new ("data/ptth_relay_me_config.toml"))
|
|
|
|
{
|
|
|
|
Err (e) => {
|
2021-05-02 22:23:12 +00:00
|
|
|
warn! ("Can't load machine-editable config: {:?}", e);
|
2021-04-29 02:52:43 +00:00
|
|
|
me_config
|
|
|
|
},
|
|
|
|
Ok (x) => x,
|
|
|
|
};
|
|
|
|
|
2020-12-13 03:29:54 +00:00
|
|
|
Ok (Self {
|
|
|
|
config: config.into (),
|
2021-04-29 02:52:43 +00:00
|
|
|
me_config: me_config.into (),
|
2020-12-13 03:29:54 +00:00
|
|
|
request_rendezvous: Default::default (),
|
|
|
|
server_status: Default::default (),
|
|
|
|
response_rendezvous: Default::default (),
|
|
|
|
shutdown_watch_tx,
|
|
|
|
shutdown_watch_rx,
|
2021-04-03 03:30:22 +00:00
|
|
|
unregistered_servers: BoundedVec::new (20),
|
2021-04-03 04:19:33 +00:00
|
|
|
audit_log: BoundedVec::new (256),
|
2020-12-13 03:29:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:07 +00:00
|
|
|
impl Relay {
|
2021-04-18 13:51:36 +00:00
|
|
|
/// Returns a `Vec` of the names of currently connected servers
|
|
|
|
|
2020-12-13 03:29:54 +00:00
|
|
|
pub async fn list_servers (&self) -> Vec <String> {
|
|
|
|
self.request_rendezvous.lock ().await.iter ()
|
|
|
|
.map (|(k, _)| (*k).clone ())
|
|
|
|
.collect ()
|
|
|
|
}
|
2021-04-27 19:55:08 +00:00
|
|
|
|
2021-05-02 22:23:12 +00:00
|
|
|
pub async fn server_exists (&self, name: &str) -> bool {
|
|
|
|
{
|
|
|
|
let config = self.config.read ().await;
|
|
|
|
if config.servers.contains_key (name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let config = self.me_config.read ().await;
|
|
|
|
if config.servers.contains_key (name) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
2021-05-02 22:32:24 +00:00
|
|
|
#[must_use]
|
2021-04-27 19:55:08 +00:00
|
|
|
pub fn build () -> Builder {
|
|
|
|
Builder::default ()
|
|
|
|
}
|
2021-04-28 01:10:32 +00:00
|
|
|
|
|
|
|
pub async fn park_client (
|
|
|
|
&self,
|
|
|
|
server_name: &str,
|
|
|
|
req: http_serde::RequestParts,
|
|
|
|
req_id: &str,
|
|
|
|
) {
|
|
|
|
use RequestRendezvous::*;
|
|
|
|
|
|
|
|
let mut request_rendezvous = self.request_rendezvous.lock ().await;
|
|
|
|
|
|
|
|
let wrapped = http_serde::WrappedRequest {
|
|
|
|
id: req_id.to_string (),
|
|
|
|
req,
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_rendezvous = match request_rendezvous.remove (server_name) {
|
|
|
|
Some (ParkedClients (mut v)) => {
|
|
|
|
debug! ("Parking request {} ({} already queued)", req_id, v.len ());
|
|
|
|
v.push (wrapped);
|
|
|
|
ParkedClients (v)
|
|
|
|
},
|
|
|
|
Some (ParkedServer (s)) => {
|
|
|
|
// If sending to the server fails, queue it
|
|
|
|
|
|
|
|
match s.send (Ok (wrapped)) {
|
|
|
|
Ok (()) => {
|
|
|
|
// TODO: This can actually still fail, if the server
|
|
|
|
// disconnects right as we're sending this.
|
|
|
|
// Then what?
|
|
|
|
|
|
|
|
trace! (
|
|
|
|
"Sending request {} directly to server {}",
|
|
|
|
req_id,
|
|
|
|
server_name,
|
|
|
|
);
|
|
|
|
|
|
|
|
ParkedClients (vec! [])
|
|
|
|
},
|
|
|
|
Err (Ok (wrapped)) => {
|
|
|
|
debug! ("Parking request {}", req_id);
|
|
|
|
ParkedClients (vec! [wrapped])
|
|
|
|
},
|
|
|
|
Err (_) => unreachable! (),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
debug! ("Parking request {}", req_id);
|
|
|
|
ParkedClients (vec! [wrapped])
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
request_rendezvous.insert (server_name.to_string (), new_rendezvous);
|
|
|
|
}
|
2021-04-27 19:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Default)]
|
|
|
|
pub struct Builder {
|
|
|
|
config: Config,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Builder {
|
|
|
|
pub fn build (self) -> Result <Relay, RelayError> {
|
|
|
|
Relay::try_from (self.config)
|
|
|
|
}
|
|
|
|
|
2021-04-27 23:48:22 +00:00
|
|
|
pub fn address (mut self, addr: std::net::IpAddr) -> Self {
|
|
|
|
self.config.address = addr;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-27 20:05:27 +00:00
|
|
|
pub fn enable_scraper_api (mut self, b: bool) -> Self {
|
2021-04-27 19:55:08 +00:00
|
|
|
self.config.iso.enable_scraper_api = b;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-27 20:05:27 +00:00
|
|
|
pub fn port (mut self, port: u16) -> Self {
|
2021-04-27 19:55:08 +00:00
|
|
|
self.config.port = Some (port);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-27 20:05:27 +00:00
|
|
|
pub fn scraper_key (mut self, key: crate::key_validity::ScraperKey <crate::key_validity::Valid30Days>)
|
|
|
|
-> Self
|
2021-04-27 19:55:08 +00:00
|
|
|
{
|
|
|
|
self.config.scraper_keys.insert (key.hash.encode_base64 (), key);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-27 20:05:27 +00:00
|
|
|
pub fn server (mut self, server: crate::config::file::Server) -> Self {
|
2021-04-27 19:55:08 +00:00
|
|
|
self.config.servers.insert (server.name.clone (), server);
|
|
|
|
self
|
|
|
|
}
|
2020-12-13 03:29:54 +00:00
|
|
|
}
|