relay/src/data/state.rs

122 lines
3.3 KiB
Rust
Raw Normal View History

use crate::{
config::{Config, UrlKind},
2020-03-23 22:17:53 +00:00
data::NodeCache,
db::Db,
2021-09-18 17:55:39 +00:00
error::Error,
requests::{Breakers, Requests},
};
2022-01-17 22:54:45 +00:00
use activitystreams::iri_string::types::IriString;
use actix_web::web;
use async_rwlock::RwLock;
2020-03-15 16:29:01 +00:00
use lru::LruCache;
2020-03-16 03:36:46 +00:00
use rand::thread_rng;
2021-08-01 20:12:06 +00:00
use rsa::{RsaPrivateKey, RsaPublicKey};
2021-02-10 04:05:06 +00:00
use std::sync::Arc;
2021-09-18 17:55:39 +00:00
use tracing::info;
2020-03-15 02:05:40 +00:00
#[derive(Clone)]
pub struct State {
2021-08-01 20:12:06 +00:00
pub(crate) public_key: RsaPublicKey,
private_key: RsaPrivateKey,
2022-01-17 22:54:45 +00:00
object_cache: Arc<RwLock<LruCache<IriString, IriString>>>,
node_cache: NodeCache,
breakers: Breakers,
2021-02-10 04:05:06 +00:00
pub(crate) db: Db,
2020-03-15 02:05:40 +00:00
}
2021-09-18 17:55:39 +00:00
impl std::fmt::Debug for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("State")
.field("node_cache", &self.node_cache)
.field("breakers", &self.breakers)
.field("db", &self.db)
.finish()
}
}
2020-03-16 03:36:46 +00:00
impl State {
2021-02-10 04:05:06 +00:00
pub(crate) fn node_cache(&self) -> NodeCache {
self.node_cache.clone()
}
2021-09-21 18:26:31 +00:00
pub(crate) fn requests(&self, config: &Config) -> Requests {
2020-03-18 04:35:20 +00:00
Requests::new(
2021-09-21 18:26:31 +00:00
config.generate_url(UrlKind::MainKey).to_string(),
self.private_key.clone(),
2021-09-21 18:26:31 +00:00
config.user_agent(),
self.breakers.clone(),
2020-03-18 04:35:20 +00:00
)
}
2021-09-21 19:32:25 +00:00
#[tracing::instrument(
name = "Get inboxes for other domains",
fields(
existing_inbox = existing_inbox.to_string().as_str(),
2022-01-17 22:54:45 +00:00
authority
2021-09-21 19:32:25 +00:00
)
)]
2021-02-10 04:05:06 +00:00
pub(crate) async fn inboxes_without(
&self,
2022-01-17 22:54:45 +00:00
existing_inbox: &IriString,
authority: &str,
) -> Result<Vec<IriString>, Error> {
2021-02-10 04:05:06 +00:00
Ok(self
.db
.inboxes()
.await?
2020-03-15 22:37:53 +00:00
.iter()
2021-02-10 04:05:06 +00:00
.filter_map(|inbox| {
2022-01-17 22:54:45 +00:00
if let Some(authority_str) = inbox.authority_str() {
if inbox != existing_inbox && authority_str != authority {
2021-02-10 04:05:06 +00:00
return Some(inbox.clone());
2020-03-15 22:37:53 +00:00
}
}
None
})
2021-02-10 04:05:06 +00:00
.collect())
2020-03-15 22:37:53 +00:00
}
2022-01-17 22:54:45 +00:00
pub(crate) async fn is_cached(&self, object_id: &IriString) -> bool {
2021-02-10 04:05:06 +00:00
self.object_cache.read().await.contains(object_id)
2020-03-15 17:49:27 +00:00
}
2022-01-17 22:54:45 +00:00
pub(crate) async fn cache(&self, object_id: IriString, actor_id: IriString) {
2021-02-10 04:05:06 +00:00
self.object_cache.write().await.put(object_id, actor_id);
2020-03-15 17:49:27 +00:00
}
2021-09-18 17:55:39 +00:00
#[tracing::instrument(name = "Building state")]
2021-09-21 18:26:31 +00:00
pub(crate) async fn build(db: Db) -> Result<Self, Error> {
2021-02-10 04:05:06 +00:00
let private_key = if let Ok(Some(key)) = db.private_key().await {
2021-09-18 17:55:39 +00:00
info!("Using existing key");
2021-02-10 04:05:06 +00:00
key
} else {
info!("Generating new keys");
let key = web::block(move || {
let mut rng = thread_rng();
2021-08-01 20:12:06 +00:00
RsaPrivateKey::new(&mut rng, 4096)
2021-02-10 04:05:06 +00:00
})
2021-02-11 00:00:11 +00:00
.await??;
2021-02-10 04:05:06 +00:00
db.update_private_key(&key).await?;
2021-02-10 04:05:06 +00:00
key
};
let public_key = private_key.to_public_key();
2020-03-15 02:05:40 +00:00
2020-03-23 17:38:39 +00:00
let state = State {
public_key,
private_key,
object_cache: Arc::new(RwLock::new(LruCache::new(
(1024 * 8).try_into().expect("nonzero"),
))),
2021-02-10 04:05:06 +00:00
node_cache: NodeCache::new(db.clone()),
breakers: Breakers::default(),
2021-02-10 04:05:06 +00:00
db,
2020-03-23 17:38:39 +00:00
};
Ok(state)
}
2020-03-15 02:05:40 +00:00
}