[WIP] MTP migration

This commit is contained in:
Alex Emmet 2026-07-03 20:17:21 +02:00
commit 69e6b8b70e
6 changed files with 57 additions and 32 deletions

3
.gitignore vendored
View file

@ -27,3 +27,6 @@ target
# Added by cargo # Added by cargo
/target /target
*.mk
*.mpkb

26
Cargo.lock generated
View file

@ -2280,12 +2280,13 @@ dependencies = [
[[package]] [[package]]
name = "mtp" name = "mtp"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d"
dependencies = [ dependencies = [
"mtp-client", "mtp-client",
"mtp-codec", "mtp-codec",
"mtp-common", "mtp-common",
"mtp-crypto", "mtp-crypto",
"mtp-files",
"mtp-host", "mtp-host",
"mtp-transport", "mtp-transport",
"mtp-type-map", "mtp-type-map",
@ -2294,7 +2295,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-client" name = "mtp-client"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d"
dependencies = [ dependencies = [
"mtp-codec", "mtp-codec",
"mtp-common", "mtp-common",
@ -2307,7 +2308,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-codec" name = "mtp-codec"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d"
dependencies = [ dependencies = [
"base64", "base64",
"byteorder", "byteorder",
@ -2320,7 +2321,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-common" name = "mtp-common"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d"
dependencies = [ dependencies = [
"quinn", "quinn",
"rustls", "rustls",
@ -2331,7 +2332,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-crypto" name = "mtp-crypto"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d"
dependencies = [ dependencies = [
"base64", "base64",
"chacha20poly1305", "chacha20poly1305",
@ -2347,10 +2348,19 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "mtp-files"
version = "0.1.0"
source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d"
dependencies = [
"mtp-crypto",
"thiserror 1.0.69",
]
[[package]] [[package]]
name = "mtp-host" name = "mtp-host"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d"
dependencies = [ dependencies = [
"mtp-codec", "mtp-codec",
"mtp-common", "mtp-common",
@ -2363,7 +2373,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-transport" name = "mtp-transport"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d"
dependencies = [ dependencies = [
"log", "log",
"mtp-codec", "mtp-codec",
@ -2377,7 +2387,7 @@ dependencies = [
[[package]] [[package]]
name = "mtp-type-map" name = "mtp-type-map"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.methanium.net/methanium/mtp#44ff1d8781b4645dbff3dce2406155e4d9d43a4c" source = "git+https://git.methanium.net/methanium/mtp#b96c072a0f87de7828e45cf3dcd44aef4a9e459d"
dependencies = [ dependencies = [
"serde", "serde",
"serde_yaml", "serde_yaml",

View file

@ -6,7 +6,8 @@ edition = "2024"
[dependencies] [dependencies]
mtp = { git = "https://git.methanium.net/methanium/mtp", features = [ mtp = { git = "https://git.methanium.net/methanium/mtp", features = [
"host", "host",
"crypto" "crypto",
"files",
] } ] }
actix-web = { version = "4.12.1", features = ["rustls-0_23"] } actix-web = { version = "4.12.1", features = ["rustls-0_23"] }
@ -39,4 +40,6 @@ uuid = { version = "1.19.0", features = ["v4"] }
x448 = "0.6.0" x448 = "0.6.0"
zip = "6.0.0" zip = "6.0.0"
thiserror = "2.0.18" thiserror = "2.0.18"
mtp-crypto = { git = "https://git.methanium.net/methanium/mtp", version = "0.1.0", features = ["pqc"] } mtp-crypto = { git = "https://git.methanium.net/methanium/mtp", version = "0.1.0", features = [
"pqc",
] }

View file

@ -9,23 +9,32 @@ use crate::transport::omikron_connection;
use crate::util::file_util::get_directory; use crate::util::file_util::get_directory;
use crate::util::logger::PrintType; use crate::util::logger::PrintType;
use crate::util::logger::startup; use crate::util::logger::startup;
use base64::Engine as _;
use dotenv::from_path; use dotenv::from_path;
use mtp_crypto::{Keyring, PublicKeyBundle}; use mtp::files::{load_keyring as load_keyring_file, save_keyring, save_public_key_bundle};
use mtp_crypto::Keyring;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use rustls::crypto::aws_lc_rs::default_provider; use rustls::crypto::aws_lc_rs::default_provider;
use std::env; use std::env;
use std::path::Path; use std::path::Path;
static KEYRING_ENV: Lazy<String> = Lazy::new(|| env::var("KEYRING").unwrap()); const KEYRING_PATH: &str = "./omega.mk";
fn load_keyring() -> Keyring {
let bytes = base64::engine::general_purpose::STANDARD static KEYRING: Lazy<Keyring> = Lazy::new(|| {
.decode(&*KEYRING_ENV) load_keyring_file(KEYRING_PATH).unwrap_or_else(|_| {
.expect("Invalid KEYRING env var: not valid base64"); let kr = Keyring::generate();
Keyring::from_bytes(&bytes).expect("Invalid KEYRING env var: failed to deserialize") save_keyring(&kr, KEYRING_PATH).expect("Failed to save generated keyring");
save_public_key_bundle(&kr.public_key_bundle(), KEYRING_PATH)
.expect("Failed to save generated public key bundle");
eprintln!("Generated new keyring at {}", KEYRING_PATH);
kr
})
});
pub fn get_keyring() -> &'static Keyring {
&KEYRING
} }
pub fn get_public_key_bundle() -> PublicKeyBundle { pub fn load_keyring() -> Keyring {
load_keyring().public_key_bundle() Keyring::from_bytes(&KEYRING.to_bytes()).unwrap()
} }
#[tokio::main] #[tokio::main]

View file

@ -1,4 +1,4 @@
use crate::get_public_key_bundle; use crate::load_keyring;
use crate::sql::sql; use crate::sql::sql;
use crate::sql::sql::{get_by_user_id, get_omikron_by_id}; use crate::sql::sql::{get_by_user_id, get_omikron_by_id};
use crate::sql::user_online_tracker::get_iota_primary_omikron_connection; use crate::sql::user_online_tracker::get_iota_primary_omikron_connection;
@ -192,7 +192,7 @@ pub async fn handle(path: &str, body_string: Option<String>) -> HttpResponse {
["api", "get", "public_key"] => { ["api", "get", "public_key"] => {
let mut res = JsonValue::new_object(); let mut res = JsonValue::new_object();
res["status"] = "success".into(); res["status"] = "success".into();
let bundle = get_public_key_bundle(); let bundle = load_keyring().public_key_bundle();
res["public_key"] = base64::engine::general_purpose::STANDARD res["public_key"] = base64::engine::general_purpose::STANDARD
.encode(bundle.as_bytes()) .encode(bundle.as_bytes())
.into(); .into();

View file

@ -71,8 +71,6 @@ pub struct WaitingTask {
pub struct OmikronConnection { pub struct OmikronConnection {
id: u64, id: u64,
sender: Mutex<Option<Sender>>, sender: Mutex<Option<Sender>>,
challenge: RwLock<String>,
pub_key: RwLock<Option<Vec<u8>>>,
pub ping: RwLock<i64>, pub ping: RwLock<i64>,
waiting_tasks: DashMap<u32, WaitingTask>, waiting_tasks: DashMap<u32, WaitingTask>,
cleanup_handle: std::sync::Mutex<Option<tokio::task::JoinHandle<()>>>, cleanup_handle: std::sync::Mutex<Option<tokio::task::JoinHandle<()>>>,
@ -91,12 +89,10 @@ impl OmikronConnection {
// Construction // Construction
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
pub fn new(sender: Sender) -> Arc<Self> { pub fn new(sender: Sender, id: u64) -> Arc<Self> {
let conn = Arc::new(Self { let conn = Arc::new(Self {
id: rand::random(), id,
sender: Mutex::new(Some(sender)), sender: Mutex::new(Some(sender)),
challenge: RwLock::new(String::new()),
pub_key: RwLock::new(None),
ping: RwLock::new(-1), ping: RwLock::new(-1),
waiting_tasks: DashMap::new(), waiting_tasks: DashMap::new(),
cleanup_handle: std::sync::Mutex::new(None), cleanup_handle: std::sync::Mutex::new(None),
@ -163,11 +159,14 @@ impl OmikronConnection {
return Ok(()); return Ok(());
} }
// Handle ping regardless of auth state // Handle ping regardless of message type
if cv.is_type(CommunicationType::Ping) { if cv.is_type(CommunicationType::Ping) {
return self.handle_ping(cv).await; return self.handle_ping(cv).await;
} }
return Ok(());
// Authentication is completed by the mtp host before this connection exists.
let omikron_id = self.id as i64;
self.clone().handle_authenticated(cv, omikron_id).await
} }
async fn handle_authenticated( async fn handle_authenticated(
@ -1054,7 +1053,7 @@ pub async fn get_by_omikron_id(
.ok() .ok()
.map(|(bundle, _ip_address)| bundle) .map(|(bundle, _ip_address)| bundle)
} }
pub async fn complete_register(pub_key: PublicKeyBundle, description: Option<String>) -> u64 { pub async fn complete_register(_pub_key: PublicKeyBundle, _description: Option<String>) -> u64 {
0 0
} }
@ -1097,7 +1096,8 @@ pub async fn start(port: u16) -> Result<(), Box<dyn std::error::Error>> {
while let Ok(Some(mut connection)) = host.accept().await { while let Ok(Some(mut connection)) = host.accept().await {
tokio::spawn(async move { tokio::spawn(async move {
let conn = OmikronConnection::new(connection.sender); let conn = OmikronConnection::new(connection.sender, connection.client_id);
omikron_manager::add_omikron(conn.clone()).await;
conn.handle(&mut connection.receiver).await; conn.handle(&mut connection.receiver).await;
}); });
} }