Lev
3 years ago
6 changed files with 85 additions and 39 deletions
@ -1,8 +1,57 @@
|
||||
use ironforce::IronForce; |
||||
use ironforce::res::IFResult; |
||||
use ironforce::{IronForce, Message, MessageType, PublicKey}; |
||||
|
||||
fn main() -> IFResult<()> { |
||||
let ironforce = IronForce::new(); |
||||
let (thread, _) = ironforce.launch_main_loop(500); |
||||
thread.join().unwrap() |
||||
let ironforce = IronForce::from_file("".to_string())?; |
||||
let if_keys = ironforce.keys.clone(); |
||||
println!( |
||||
"Our public key: {}", |
||||
base64::encode(if_keys.get_public().to_vec().as_slice()) |
||||
); |
||||
let (_thread, if_mutex) = ironforce.launch_main_loop(100); |
||||
let stdin = std::io::stdin(); |
||||
let if_mutex_clone = if_mutex.clone(); |
||||
let if_keys_clone = if_keys.clone(); |
||||
std::thread::spawn(move || loop { |
||||
if let Some(msg) = if_mutex_clone.lock().unwrap().read_message() { |
||||
println!( |
||||
"New message: {}", |
||||
String::from_utf8(msg.get_decrypted(&if_keys_clone).unwrap()).unwrap() |
||||
); |
||||
} |
||||
std::thread::sleep(std::time::Duration::from_millis(300)) |
||||
}); |
||||
loop { |
||||
let mut buf = String::new(); |
||||
stdin.read_line(&mut buf)?; |
||||
let msg_base = if buf.starts_with('>') { |
||||
let target_base64 = buf |
||||
.split(')') |
||||
.next() |
||||
.unwrap() |
||||
.trim_start_matches(">(") |
||||
.to_string(); |
||||
let target = if let Ok(res) = base64::decode(target_base64) { |
||||
res |
||||
} else { |
||||
println!("Wrong b64."); |
||||
continue; |
||||
}; |
||||
buf = buf |
||||
.split(')') |
||||
.skip(1) |
||||
.map(|s| s.to_string()) |
||||
.collect::<Vec<String>>() |
||||
.join(")"); |
||||
Message::build() |
||||
.message_type(MessageType::SingleCast) |
||||
.recipient(&PublicKey::from_vec(target).unwrap()) |
||||
} else { |
||||
Message::build().message_type(MessageType::Broadcast) |
||||
}; |
||||
if_mutex |
||||
.lock() |
||||
.unwrap() |
||||
.send_to_all(msg_base.content(buf.into_bytes()).sign(&if_keys).build()?)?; |
||||
} |
||||
} |
Loading…
Reference in new issue