2024-03-25 21:19:02 +01:00
|
|
|
use std::{str::Utf8Error, sync::Arc};
|
2024-03-16 03:30:04 +01:00
|
|
|
|
2024-03-25 21:19:02 +01:00
|
|
|
use openssl::rsa::Rsa;
|
|
|
|
use sea_orm::{DatabaseConnection, DbErr, EntityTrait, QuerySelect, SelectColumns};
|
2024-03-16 03:30:04 +01:00
|
|
|
|
2024-03-25 21:19:02 +01:00
|
|
|
use crate::{dispatcher::Dispatcher, fetcher::Fetcher, model};
|
2024-03-25 05:02:39 +01:00
|
|
|
|
2024-03-20 08:56:35 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Context(Arc<ContextInner>);
|
|
|
|
struct ContextInner {
|
|
|
|
db: DatabaseConnection,
|
|
|
|
domain: String,
|
2024-03-25 05:02:39 +01:00
|
|
|
protocol: String,
|
2024-03-25 21:19:02 +01:00
|
|
|
fetcher: Fetcher,
|
|
|
|
// TODO keep these pre-parsed
|
|
|
|
public_key: String,
|
|
|
|
private_key: String,
|
2024-03-20 08:56:35 +01:00
|
|
|
}
|
2024-03-20 09:19:31 +01:00
|
|
|
|
2024-03-20 09:42:25 +01:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! url {
|
|
|
|
($ctx:expr, $($args: tt)*) => {
|
|
|
|
format!("{}{}", $ctx.base(), format!($($args)*))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-25 21:19:02 +01:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum ContextError {
|
|
|
|
#[error("database error: {0}")]
|
|
|
|
Db(#[from] DbErr),
|
|
|
|
|
|
|
|
#[error("openssl error: {0}")]
|
|
|
|
OpenSSL(#[from] openssl::error::ErrorStack),
|
|
|
|
|
|
|
|
#[error("invalid UTF8 PEM key: {0}")]
|
|
|
|
UTF8Error(#[from] Utf8Error)
|
|
|
|
}
|
|
|
|
|
2024-03-20 08:56:35 +01:00
|
|
|
impl Context {
|
2024-03-25 21:19:02 +01:00
|
|
|
pub async fn new(db: DatabaseConnection, mut domain: String) -> Result<Self, ContextError> {
|
2024-03-25 05:02:39 +01:00
|
|
|
let protocol = if domain.starts_with("http://")
|
|
|
|
{ "http://" } else { "https://" }.to_string();
|
2024-03-20 08:56:35 +01:00
|
|
|
if domain.ends_with('/') {
|
|
|
|
domain.replace_range(domain.len()-1.., "");
|
|
|
|
}
|
2024-03-25 05:02:39 +01:00
|
|
|
if domain.starts_with("http") {
|
|
|
|
domain = domain.replace("https://", "").replace("http://", "");
|
|
|
|
}
|
|
|
|
for _ in 0..1 { // TODO customize delivery workers amount
|
|
|
|
Dispatcher::spawn(db.clone(), domain.clone(), 30); // TODO ew don't do it this deep and secretly!!
|
|
|
|
}
|
2024-03-25 21:19:02 +01:00
|
|
|
let (public_key, private_key) = match model::application::Entity::find()
|
|
|
|
.select_only()
|
|
|
|
.select_column(model::application::Column::PublicKey)
|
|
|
|
.select_column(model::application::Column::PrivateKey)
|
|
|
|
.one(&db)
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
Some(model) => (model.public_key, model.private_key),
|
|
|
|
None => {
|
|
|
|
tracing::info!("generating application keys");
|
|
|
|
let rsa = Rsa::generate(2048)?;
|
|
|
|
let privk = std::str::from_utf8(&rsa.private_key_to_pem()?)?.to_string();
|
|
|
|
let pubk = std::str::from_utf8(&rsa.public_key_to_pem()?)?.to_string();
|
|
|
|
let system = model::application::ActiveModel {
|
|
|
|
id: sea_orm::ActiveValue::NotSet,
|
|
|
|
private_key: sea_orm::ActiveValue::Set(privk.clone()),
|
|
|
|
public_key: sea_orm::ActiveValue::Set(pubk.clone()),
|
|
|
|
};
|
|
|
|
model::application::Entity::insert(system).exec(&db).await?;
|
|
|
|
(pubk, privk)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let fetcher = Fetcher::new(db.clone(), domain.clone(), private_key.clone());
|
|
|
|
|
|
|
|
Ok(Context(Arc::new(ContextInner {
|
|
|
|
db, domain, protocol, private_key, public_key, fetcher,
|
|
|
|
})))
|
2024-03-20 08:56:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn db(&self) -> &DatabaseConnection {
|
|
|
|
&self.0.db
|
|
|
|
}
|
|
|
|
|
2024-03-20 09:42:25 +01:00
|
|
|
pub fn base(&self) -> &str {
|
|
|
|
&self.0.domain
|
|
|
|
}
|
|
|
|
|
2024-03-20 08:56:35 +01:00
|
|
|
pub fn uri(&self, entity: &str, id: String) -> String {
|
|
|
|
if id.starts_with("http") { id } else {
|
2024-03-25 05:02:39 +01:00
|
|
|
format!("{}{}/{}/{}", self.0.protocol, self.0.domain, entity, id)
|
2024-03-20 08:56:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-25 21:19:02 +01:00
|
|
|
pub fn fetch(&self) -> &Fetcher {
|
|
|
|
&self.0.fetcher
|
|
|
|
}
|
|
|
|
|
2024-03-20 09:42:25 +01:00
|
|
|
/// get full user id uri
|
|
|
|
pub fn uid(&self, id: String) -> String {
|
2024-03-20 09:19:31 +01:00
|
|
|
self.uri("users", id)
|
|
|
|
}
|
|
|
|
|
2024-03-20 09:42:25 +01:00
|
|
|
/// get full object id uri
|
|
|
|
pub fn oid(&self, id: String) -> String {
|
2024-03-20 09:19:31 +01:00
|
|
|
self.uri("objects", id)
|
|
|
|
}
|
|
|
|
|
2024-03-20 09:42:25 +01:00
|
|
|
/// get full activity id uri
|
|
|
|
pub fn aid(&self, id: String) -> String {
|
2024-03-20 09:19:31 +01:00
|
|
|
self.uri("activities", id)
|
|
|
|
}
|
|
|
|
|
2024-03-25 02:26:47 +01:00
|
|
|
/// get bare id, usually an uuid but unspecified
|
2024-03-20 08:56:35 +01:00
|
|
|
pub fn id(&self, id: String) -> String {
|
|
|
|
if id.starts_with(&self.0.domain) {
|
2024-03-20 11:00:21 +01:00
|
|
|
id.split('/').last().unwrap_or("").to_string()
|
2024-03-20 08:56:35 +01:00
|
|
|
} else {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
2024-03-25 02:26:47 +01:00
|
|
|
|
|
|
|
pub fn server(id: &str) -> String {
|
|
|
|
id
|
|
|
|
.replace("https://", "")
|
|
|
|
.replace("http://", "")
|
|
|
|
.split('/')
|
|
|
|
.next()
|
|
|
|
.unwrap_or("")
|
|
|
|
.to_string()
|
|
|
|
}
|
2024-03-20 08:56:35 +01:00
|
|
|
}
|