aerogramme/src/server.rs

158 lines
5 KiB
Rust
Raw Normal View History

2022-05-19 12:33:49 +00:00
use anyhow::{bail, Result};
use std::sync::Arc;
use rusoto_signature::Region;
use crate::config::*;
use crate::login::{ldap_provider::*, static_provider::*, *};
use crate::mailbox::Mailbox;
2022-06-01 16:00:56 +00:00
use boitalettres::proto::{Request, Response};
use boitalettres::server::accept::addr::{AddrIncoming, AddrStream};
use boitalettres::server::Server as ImapServer;
2022-06-02 15:59:29 +00:00
use std::pin::Pin;
2022-06-02 14:29:16 +00:00
use std::task::{Context, Poll};
use tower::Service;
2022-06-03 08:31:55 +00:00
use futures::future::BoxFuture;
2022-06-02 14:29:16 +00:00
2022-06-03 09:38:01 +00:00
pub struct Connection {
2022-06-03 09:58:42 +00:00
pub mailstore: Arc<Mailstore>,
}
impl Connection {
pub fn new(mailstore: Arc<Mailstore>) -> Self {
Self { mailstore }
}
2022-05-19 12:33:49 +00:00
}
2022-06-02 15:28:26 +00:00
impl Service<Request> for Connection {
2022-06-02 15:59:29 +00:00
type Response = Response;
2022-06-03 12:00:19 +00:00
type Error = boitalettres::errors::Error;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
2022-06-02 15:59:29 +00:00
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request) -> Self::Future {
tracing::debug!("Got request: {:#?}", req);
2022-06-03 12:00:19 +00:00
let mailstore = self.mailstore.clone();
2022-06-02 15:59:29 +00:00
Box::pin(async move {
use imap_codec::types::{
command::CommandBody,
response::{Capability, Data},
};
2022-06-02 14:29:16 +00:00
2022-06-02 15:59:29 +00:00
let r = match req.body {
CommandBody::Capability => {
let capabilities = vec![Capability::Imap4Rev1, Capability::Idle];
let body = vec![Data::Capability(capabilities)];
Response::ok(
"Pre-login capabilities listed, post-login capabilities have more.",
)?
.with_body(body)
}
CommandBody::Login {
2022-06-03 12:00:19 +00:00
username,
password,
} => {
let (u, p) = match (String::try_from(username), String::try_from(password)) {
(Ok(u), Ok(p)) => (u, p),
_ => { return Response::bad("Invalid characters") }
};
tracing::debug!(user = %u, "command.login");
let creds = match mailstore.login_provider.login(&u, &p).await {
Err(_) => { return Response::no("[AUTHENTICATIONFAILED] Authentication failed.") }
Ok(c) => c,
};
Response::ok("Logged in")?
}
2022-06-02 15:59:29 +00:00
_ => Response::bad("Error in IMAP command received by server.")?,
};
2022-06-02 14:29:16 +00:00
2022-06-02 15:59:29 +00:00
Ok(r)
})
}
2022-06-02 14:29:16 +00:00
}
2022-06-03 09:38:01 +00:00
pub struct Instance {
2022-06-03 09:58:42 +00:00
pub mailstore: Arc<Mailstore>
}
impl Instance {
pub fn new(mailstore: Arc<Mailstore>) -> Self {
Self { mailstore }
}
2022-06-03 09:38:01 +00:00
}
2022-06-02 15:28:26 +00:00
impl<'a> Service<&'a AddrStream> for Instance {
2022-06-02 15:59:29 +00:00
type Response = Connection;
type Error = anyhow::Error;
2022-06-03 08:31:55 +00:00
type Future = BoxFuture<'static, Result<Self::Response>>;
2022-06-02 15:12:58 +00:00
2022-06-02 15:59:29 +00:00
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
2022-06-02 15:12:58 +00:00
2022-06-02 15:59:29 +00:00
fn call(&mut self, addr: &'a AddrStream) -> Self::Future {
tracing::info!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept");
2022-06-03 09:58:42 +00:00
let ms = self.mailstore.clone();
Box::pin(async {
Ok(Connection::new(ms))
2022-06-03 09:38:01 +00:00
})
2022-06-02 15:59:29 +00:00
}
2022-06-02 15:12:58 +00:00
}
2022-06-03 09:58:42 +00:00
pub struct Mailstore {
pub login_provider: Box<dyn LoginProvider + Send + Sync>,
}
impl Mailstore {
pub fn new(config: Config) -> Result<Arc<Self>> {
2022-05-19 12:33:49 +00:00
let s3_region = Region::Custom {
2022-05-19 13:14:36 +00:00
name: config.aws_region.clone(),
2022-05-19 12:33:49 +00:00
endpoint: config.s3_endpoint,
};
let k2v_region = Region::Custom {
2022-05-19 13:14:36 +00:00
name: config.aws_region,
2022-05-19 12:33:49 +00:00
endpoint: config.k2v_endpoint,
};
2022-06-03 09:58:42 +00:00
let login_provider: Box<dyn LoginProvider + Send + Sync> = match (config.login_static, config.login_ldap)
2022-05-19 12:33:49 +00:00
{
2022-06-03 09:58:42 +00:00
(Some(st), None) => Box::new(StaticLoginProvider::new(st, k2v_region, s3_region)?),
(None, Some(ld)) => Box::new(LdapLoginProvider::new(ld, k2v_region, s3_region)?),
2022-05-19 12:33:49 +00:00
(Some(_), Some(_)) => bail!("A single login provider must be set up in config file"),
(None, None) => bail!("No login provider is set up in config file"),
};
2022-06-03 09:58:42 +00:00
Ok(Arc::new(Self { login_provider }))
2022-05-19 12:33:49 +00:00
}
2022-06-03 09:38:01 +00:00
}
2022-05-19 12:33:49 +00:00
2022-06-03 09:58:42 +00:00
2022-06-03 09:38:01 +00:00
pub struct Server {
pub incoming: AddrIncoming,
2022-06-03 09:58:42 +00:00
pub mailstore: Arc<Mailstore>,
2022-06-03 09:38:01 +00:00
}
impl Server {
pub async fn new(config: Config) -> Result<Self> {
Ok(Self {
incoming: AddrIncoming::new("127.0.0.1:4567").await?,
2022-06-03 09:58:42 +00:00
mailstore: Mailstore::new(config)?,
2022-06-03 09:38:01 +00:00
})
}
2022-06-01 16:00:56 +00:00
2022-06-03 09:38:01 +00:00
pub async fn run(self: Self) -> Result<()> {
2022-06-03 10:26:51 +00:00
tracing::info!("Starting server on {:#}", self.incoming.local_addr);
2022-06-03 09:58:42 +00:00
let server = ImapServer::new(self.incoming).serve(Instance::new(self.mailstore.clone()));
2022-06-01 16:00:56 +00:00
let _ = server.await?;
/*let creds = self.login_provider.login("quentin", "poupou").await?;
2022-05-19 13:14:36 +00:00
let mut mailbox = Mailbox::new(&creds, "TestMailbox".to_string()).await?;
2022-06-01 16:00:56 +00:00
mailbox.test().await?;*/
2022-05-19 12:33:49 +00:00
Ok(())
}
}