feat: update server to Smol Mail protocol 1.1

This commit is contained in:
randogoth 2026-09-27 09:39:01 +03:00
parent 71ad7e04c0
commit 24fe701ca2
8 changed files with 311 additions and 52 deletions

View file

@ -18,10 +18,14 @@ pub struct ServeArgs {
pub port: u16,
pub max_envelope: usize,
pub quota: i64,
pub requests_quota: i64,
pub retention_days: i64,
pub requests_retention_days: i64,
pub max_tokens: u16,
pub invite_token: Option<String>,
pub rate_connections: u32,
pub rate_sends: u32,
pub rate_tokens: u32,
}
pub fn run(args: ServeArgs) -> anyhow::Result<()> {
@ -33,24 +37,29 @@ pub fn run(args: ServeArgs) -> anyhow::Result<()> {
static_key.len()
);
let key_array: [u8; KEY_LEN] = static_key.clone().try_into().unwrap();
let server_static = derive_public(&key_array);
let config = Arc::new(ServerConfig {
max_envelope: args.max_envelope,
quota: args.quota,
main_quota: args.quota,
requests_quota: args.requests_quota,
max_tokens: args.max_tokens,
invite_token: args.invite_token.map(String::into_bytes),
server_static,
conn_limiter: RateLimiter::new(args.rate_connections),
send_limiter: RateLimiter::new(args.rate_sends),
token_limiter: RateLimiter::new(args.rate_tokens),
});
let retention_secs = args.retention_days * 86400;
let main_retention_secs = args.retention_days * 86400;
let requests_retention_secs = args.requests_retention_days * 86400;
let purge_db_path = args.db_path.clone();
std::thread::spawn(move || purge_loop(purge_db_path, retention_secs));
std::thread::spawn(move || purge_loop(purge_db_path, main_retention_secs, requests_retention_secs));
let listener = std::net::TcpListener::bind((args.host.as_str(), args.port))?;
log::info!("listening on {}:{}", args.host, args.port);
let key_array: [u8; KEY_LEN] = static_key.clone().try_into().unwrap();
let public = derive_public(&key_array);
log::info!("server public key: {}", b32(&public));
log::info!("server public key: {}", b32(&server_static));
for incoming in listener.incoming() {
let stream = match incoming {
@ -140,7 +149,7 @@ fn is_eof_like(e: &anyhow::Error) -> bool {
false
}
fn purge_loop(db_path: String, retention_secs: i64) {
fn purge_loop(db_path: String, main_retention_secs: i64, requests_retention_secs: i64) {
let store = match Store::open(&db_path) {
Ok(s) => s,
Err(e) => {
@ -154,7 +163,7 @@ fn purge_loop(db_path: String, retention_secs: i64) {
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
match store.purge(now - retention_secs) {
match store.purge(now - main_retention_secs, now - requests_retention_secs) {
Ok(0) => {}
Ok(n) => log::info!("expired {n} message(s)"),
Err(e) => log::error!("purge failed: {e}"),