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,
|
|
|
|
error::MyError,
|
2020-12-23 18:06:15 +00:00
|
|
|
requests::{Breakers, Requests},
|
2020-03-20 00:55:11 +00:00
|
|
|
};
|
2020-09-07 21:51:02 +00:00
|
|
|
use activitystreams::url::Url;
|
2020-03-20 00:55:11 +00:00
|
|
|
use actix_web::web;
|
2020-12-29 17:27:14 +00:00
|
|
|
use async_rwlock::RwLock;
|
2021-02-10 04:05:06 +00:00
|
|
|
use log::info;
|
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;
|
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,
|
2020-03-20 00:55:11 +00:00
|
|
|
config: Config,
|
2021-02-10 04:05:06 +00:00
|
|
|
object_cache: Arc<RwLock<LruCache<Url, Url>>>,
|
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
|
|
|
}
|
|
|
|
|
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-02-10 04:05:06 +00:00
|
|
|
pub(crate) fn requests(&self) -> Requests {
|
2020-03-18 04:35:20 +00:00
|
|
|
Requests::new(
|
2020-06-03 17:37:36 +00:00
|
|
|
self.config.generate_url(UrlKind::MainKey).to_string(),
|
2020-03-20 00:55:11 +00:00
|
|
|
self.private_key.clone(),
|
|
|
|
format!(
|
2020-03-21 21:01:54 +00:00
|
|
|
"Actix Web 3.0.0-alpha.1 ({}/{}; +{})",
|
2020-03-20 00:55:11 +00:00
|
|
|
self.config.software_name(),
|
2020-03-21 21:01:54 +00:00
|
|
|
self.config.software_version(),
|
|
|
|
self.config.generate_url(UrlKind::Index),
|
2020-03-20 00:55:11 +00:00
|
|
|
),
|
2020-12-23 18:06:15 +00:00
|
|
|
self.breakers.clone(),
|
2020-03-18 04:35:20 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-10 04:05:06 +00:00
|
|
|
pub(crate) async fn inboxes_without(
|
|
|
|
&self,
|
|
|
|
existing_inbox: &Url,
|
|
|
|
domain: &str,
|
|
|
|
) -> Result<Vec<Url>, MyError> {
|
|
|
|
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| {
|
|
|
|
if let Some(dom) = inbox.domain() {
|
|
|
|
if inbox != existing_inbox && dom != domain {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-10 04:05:06 +00:00
|
|
|
pub(crate) async fn is_cached(&self, object_id: &Url) -> bool {
|
|
|
|
self.object_cache.read().await.contains(object_id)
|
2020-03-15 17:49:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 04:05:06 +00:00
|
|
|
pub(crate) async fn cache(&self, object_id: Url, actor_id: Url) {
|
|
|
|
self.object_cache.write().await.put(object_id, actor_id);
|
2020-03-15 17:49:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 04:05:06 +00:00
|
|
|
pub(crate) async fn build(config: Config, db: Db) -> Result<Self, MyError> {
|
|
|
|
let private_key = if let Ok(Some(key)) = db.private_key().await {
|
|
|
|
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??;
|
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,
|
|
|
|
config,
|
2021-02-10 04:05:06 +00:00
|
|
|
object_cache: Arc::new(RwLock::new(LruCache::new(1024 * 8))),
|
|
|
|
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
|
|
|
}
|