mirror of
https://framagit.org/ppom/reaction
synced 2026-03-14 20:55:47 +01:00
cluster: retrieve, generate and store iroh SecretKey
This commit is contained in:
parent
c918910453
commit
e3060d0404
3 changed files with 4058 additions and 893 deletions
3217
Cargo.lock
generated
3217
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -7,3 +7,7 @@ edition = "2024"
|
|||
tokio = { workspace = true, features = ["rt-multi-thread"] }
|
||||
remoc.workspace = true
|
||||
reaction-plugin.path = "../reaction-plugin"
|
||||
iroh = "0.94.0"
|
||||
base64 = "0.22.1"
|
||||
rand_core = { version = "0.9.3", features = ["os_rng"] }
|
||||
rand = "0.9.2"
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
use std::collections::BTreeSet;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use base64::{Engine, prelude::BASE64_STANDARD};
|
||||
use iroh::{SecretKey, defaults};
|
||||
use reaction_plugin::{
|
||||
ActionImpl, Hello, Manifest, PersistData, PluginInfo, RemoteResult, StreamImpl, Value,
|
||||
main_loop,
|
||||
};
|
||||
use remoc::rtc;
|
||||
use remoc::{chmux::SendError, rtc};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
|
@ -49,6 +51,8 @@ impl PluginInfo for Plugin {
|
|||
}
|
||||
|
||||
async fn finish_setup(&mut self) -> RemoteResult<()> {
|
||||
let data = self.data.as_mut().unwrap();
|
||||
let secret_key = secret_key(data).await;
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
|
@ -56,3 +60,47 @@ impl PluginInfo for Plugin {
|
|||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
async fn secret_key(data: &mut PersistData) -> SecretKey {
|
||||
if let Some(key) = get_secret_key(data) {
|
||||
key
|
||||
} else {
|
||||
let key = SecretKey::generate(&mut rand::rng());
|
||||
set_secret_key(data, &key).await;
|
||||
key
|
||||
}
|
||||
}
|
||||
|
||||
fn get_secret_key(data: &PersistData) -> Option<SecretKey> {
|
||||
match &data.persisted_data {
|
||||
Value::Object(map) => map.get("secret_key").and_then(|value| {
|
||||
if let Value::String(str) = value {
|
||||
let vec = BASE64_STANDARD.decode(str).ok()?;
|
||||
if vec.len() != 32 {
|
||||
return None;
|
||||
}
|
||||
let mut bytes = [0u8; 32];
|
||||
for i in 0..32 {
|
||||
bytes[i] = vec[i];
|
||||
}
|
||||
Some(SecretKey::from_bytes(&bytes))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
async fn set_secret_key(data: &mut PersistData, key: &SecretKey) {
|
||||
let mut current = match &data.persisted_data {
|
||||
Value::Object(map) => map.clone(),
|
||||
_ => BTreeMap::default(),
|
||||
};
|
||||
let base64 = BASE64_STANDARD.encode(key.to_bytes());
|
||||
current.insert("secret_key".into(), Value::String(base64));
|
||||
data.persist_data
|
||||
.send(Value::Object(current))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue