2020-03-20 00:55:11 +00:00
|
|
|
use crate::{
|
|
|
|
config::{Config, UrlKind},
|
2020-03-23 22:17:53 +00:00
|
|
|
data::NodeCache,
|
2020-03-20 00:55:11 +00:00
|
|
|
db::Db,
|
2021-09-18 17:55:39 +00:00
|
|
|
error::Error,
|
2020-12-23 18:06:15 +00:00
|
|
|
requests::{Breakers, Requests},
|
2020-03-20 00:55:11 +00:00
|
|
|
};
|
2022-01-17 22:54:45 +00:00
|
|
|
use activitystreams::iri_string::types::IriString;
|
2020-03-20 00:55:11 +00:00
|
|
|
use actix_web::web;
|
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;
|
2022-11-02 18:55:45 +00:00
|
|
|
use tokio::sync::RwLock;
|
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>>>,
|
2020-03-23 03:52:42 +00:00
|
|
|
node_cache: NodeCache,
|
2020-12-23 18:06:15 +00:00
|
|
|
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 {
|
2020-03-23 03:52:42 +00:00
|
|
|
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(),
|
2020-03-20 00:55:11 +00:00
|
|
|
self.private_key.clone(),
|
2021-09-21 18:26:31 +00:00
|
|
|
config.user_agent(),
|
2020-12-23 18:06:15 +00:00
|
|
|
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",
|
2022-11-01 20:57:33 +00:00
|
|
|
skip_all,
|
2021-09-21 19:32:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-01 20:57:33 +00:00
|
|
|
#[tracing::instrument(name = "Building state", skip_all)]
|
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 {
|
2022-11-02 18:55:45 +00:00
|
|
|
tracing::info!("Using existing key");
|
2021-02-10 04:05:06 +00:00
|
|
|
key
|
|
|
|
} else {
|
2022-11-02 18:55:45 +00:00
|
|
|
tracing::info!("Generating new keys");
|
2021-02-10 04:05:06 +00:00
|
|
|
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??;
|
2020-03-20 00:55:11 +00:00
|
|
|
|
2021-02-10 04:05:06 +00:00
|
|
|
db.update_private_key(&key).await?;
|
2020-03-20 00:55:11 +00:00
|
|
|
|
2021-02-10 04:05:06 +00:00
|
|
|
key
|
2020-03-20 00:55:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
2020-03-20 00:55:11 +00:00
|
|
|
public_key,
|
|
|
|
private_key,
|
2022-09-28 23:01:52 +00:00
|
|
|
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()),
|
2020-12-23 18:06:15 +00:00
|
|
|
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
|
|
|
}
|