feat: incoming rate limit, identity backup, v0.2.0
All checks were successful
ci / test (push) Successful in 4m11s
All checks were successful
ci / test (push) Successful in 4m11s
App-level token bucket on rend accepts (30/60s, burst 10), panic-safe TUI restore, sqlite WAL + integrity_check fail-closed. Encrypted owbak1 identity backup/restore. README and threat model cover profile, XMR sidecar, backup, mixed-version frames.
This commit is contained in:
parent
1a9b54d138
commit
e63203ff56
14 changed files with 809 additions and 26 deletions
296
src/tui.rs
296
src/tui.rs
|
|
@ -68,6 +68,8 @@ OnionWire keys\n\
|
|||
/profile edit name, bio, Monero address\n\
|
||||
/pay <xmr> [memo] invoice to receive\n\
|
||||
/tip <xmr> [memo] pay selected friend\n\
|
||||
/backup /path encrypted identity export\n\
|
||||
/restore /path overwrite self keys\n\
|
||||
? this help\n\
|
||||
Ctrl-Q quit\n\
|
||||
\n\
|
||||
|
|
@ -150,6 +152,8 @@ pub enum SlashCmd {
|
|||
Who,
|
||||
Pay { atomic: String, memo: String },
|
||||
Tip { atomic: String, memo: String },
|
||||
Backup { path: String },
|
||||
Restore { path: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
@ -261,9 +265,28 @@ pub fn parse_cmd(raw: &str) -> Option<SlashCmd> {
|
|||
{
|
||||
return parse_amount_cmd(rest).map(|(atomic, memo)| SlashCmd::Tip { atomic, memo });
|
||||
}
|
||||
if let Some(rest) = s.strip_prefix("/backup")
|
||||
&& (rest.is_empty() || rest.starts_with(char::is_whitespace))
|
||||
{
|
||||
return parse_path_cmd(rest).map(|path| SlashCmd::Backup { path });
|
||||
}
|
||||
if let Some(rest) = s.strip_prefix("/restore")
|
||||
&& (rest.is_empty() || rest.starts_with(char::is_whitespace))
|
||||
{
|
||||
return parse_path_cmd(rest).map(|path| SlashCmd::Restore { path });
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn parse_path_cmd(rest: &str) -> Option<String> {
|
||||
let path = rest.trim();
|
||||
if path.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(path.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_amount_cmd(rest: &str) -> Option<(String, String)> {
|
||||
let rest = rest.trim();
|
||||
if rest.is_empty() {
|
||||
|
|
@ -392,11 +415,96 @@ impl RotatePrompt {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum BackupKind {
|
||||
Backup,
|
||||
Restore,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum BackupDecision {
|
||||
Pending,
|
||||
Confirm,
|
||||
Cancel,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BackupPrompt {
|
||||
expected: &'static str,
|
||||
buf: String,
|
||||
}
|
||||
|
||||
impl BackupPrompt {
|
||||
pub fn backup() -> Self {
|
||||
Self {
|
||||
expected: "BACKUP",
|
||||
buf: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn restore() -> Self {
|
||||
Self {
|
||||
expected: "RESTORE",
|
||||
buf: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn on_esc(&self) -> BackupDecision {
|
||||
BackupDecision::Cancel
|
||||
}
|
||||
|
||||
pub fn on_char(&mut self, c: char) -> BackupDecision {
|
||||
if c == '\n' {
|
||||
return BackupDecision::Pending;
|
||||
}
|
||||
if c == '\u{8}' {
|
||||
self.buf.pop();
|
||||
return BackupDecision::Pending;
|
||||
}
|
||||
if !c.is_ascii_alphabetic() {
|
||||
return BackupDecision::Pending;
|
||||
}
|
||||
self.buf.push(c);
|
||||
if self.buf == self.expected {
|
||||
BackupDecision::Confirm
|
||||
} else {
|
||||
BackupDecision::Pending
|
||||
}
|
||||
}
|
||||
|
||||
pub fn typed(&self) -> &str {
|
||||
&self.buf
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backup_screen_text() -> &'static str {
|
||||
"Write encrypted identity backup?\n\
|
||||
This file is your identity secret. Treat it like the database.\n\
|
||||
Onion (locator) is not included.\n\
|
||||
Type BACKUP to confirm Esc to cancel"
|
||||
}
|
||||
|
||||
pub fn restore_screen_text() -> &'static str {
|
||||
"Restore identity from backup?\n\
|
||||
This overwrites your self keys. It does not rewrite the roster.\n\
|
||||
You may become a different person with old friends.\n\
|
||||
Type RESTORE to confirm Esc to cancel"
|
||||
}
|
||||
|
||||
pub fn restore_terminal() {
|
||||
ratatui::restore();
|
||||
}
|
||||
|
||||
pub fn run(node: Arc<Node>, rt: tokio::runtime::Handle) -> Result<AppExit, String> {
|
||||
let prev = std::panic::take_hook();
|
||||
std::panic::set_hook(Box::new(move |info| {
|
||||
restore_terminal();
|
||||
prev(info);
|
||||
}));
|
||||
let mut app = App::new(node, rt)?;
|
||||
let mut terminal = ratatui::init();
|
||||
let result = app.run(&mut terminal);
|
||||
ratatui::restore();
|
||||
restore_terminal();
|
||||
result
|
||||
}
|
||||
|
||||
|
|
@ -419,12 +527,38 @@ struct App {
|
|||
|
||||
enum Screen {
|
||||
Main,
|
||||
Share { art: String, payload: String },
|
||||
Paste { buf: String, err: Option<String> },
|
||||
Approve { payload: QrPayload },
|
||||
Rotate { prompt: RotatePrompt },
|
||||
Wipe { kind: WipeKind, prompt: WipePrompt },
|
||||
Profile { editor: ProfileEditor },
|
||||
Share {
|
||||
art: String,
|
||||
payload: String,
|
||||
},
|
||||
Paste {
|
||||
buf: String,
|
||||
err: Option<String>,
|
||||
},
|
||||
Approve {
|
||||
payload: QrPayload,
|
||||
},
|
||||
Rotate {
|
||||
prompt: RotatePrompt,
|
||||
},
|
||||
Wipe {
|
||||
kind: WipeKind,
|
||||
prompt: WipePrompt,
|
||||
},
|
||||
Profile {
|
||||
editor: ProfileEditor,
|
||||
},
|
||||
ConfirmKeys {
|
||||
kind: BackupKind,
|
||||
path: String,
|
||||
prompt: BackupPrompt,
|
||||
},
|
||||
Passphrase {
|
||||
kind: BackupKind,
|
||||
path: String,
|
||||
first: Option<String>,
|
||||
buf: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl App {
|
||||
|
|
@ -545,6 +679,22 @@ impl App {
|
|||
self.composer.clear();
|
||||
tip_cmd = Some((atomic, memo));
|
||||
}
|
||||
Some(SlashCmd::Backup { path }) => {
|
||||
self.composer.clear();
|
||||
self.screen = Screen::ConfirmKeys {
|
||||
kind: BackupKind::Backup,
|
||||
path,
|
||||
prompt: BackupPrompt::backup(),
|
||||
};
|
||||
}
|
||||
Some(SlashCmd::Restore { path }) => {
|
||||
self.composer.clear();
|
||||
self.screen = Screen::ConfirmKeys {
|
||||
kind: BackupKind::Restore,
|
||||
path,
|
||||
prompt: BackupPrompt::restore(),
|
||||
};
|
||||
}
|
||||
None => {}
|
||||
},
|
||||
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
|
|
@ -619,6 +769,48 @@ impl App {
|
|||
}
|
||||
_ => {}
|
||||
},
|
||||
Screen::ConfirmKeys { kind, path, prompt } => {
|
||||
let kind = *kind;
|
||||
match key.code {
|
||||
KeyCode::Esc => self.screen = Screen::Main,
|
||||
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
if prompt.on_char(c) == BackupDecision::Confirm {
|
||||
self.screen = Screen::Passphrase {
|
||||
kind,
|
||||
path: path.clone(),
|
||||
first: None,
|
||||
buf: String::new(),
|
||||
};
|
||||
}
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
prompt.on_char('\u{8}');
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Screen::Passphrase {
|
||||
kind,
|
||||
path,
|
||||
first,
|
||||
buf,
|
||||
} => match key.code {
|
||||
KeyCode::Esc => self.screen = Screen::Main,
|
||||
KeyCode::Backspace => {
|
||||
buf.pop();
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
let kind = *kind;
|
||||
let path = path.clone();
|
||||
let first = first.clone();
|
||||
let pass = buf.clone();
|
||||
self.submit_passphrase(kind, &path, first.as_deref(), &pass)?;
|
||||
}
|
||||
KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
buf.push(c);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
if let Some(kind) = wipe_confirm {
|
||||
return self.confirm_wipe(kind);
|
||||
|
|
@ -719,6 +911,55 @@ impl App {
|
|||
}
|
||||
}
|
||||
|
||||
fn submit_passphrase(
|
||||
&mut self,
|
||||
kind: BackupKind,
|
||||
path: &str,
|
||||
first: Option<&str>,
|
||||
pass: &str,
|
||||
) -> Result<(), String> {
|
||||
match kind {
|
||||
BackupKind::Backup => {
|
||||
if let Some(first) = first {
|
||||
if first != pass {
|
||||
self.alert = Some("passphrases do not match".into());
|
||||
self.screen = Screen::Main;
|
||||
return Ok(());
|
||||
}
|
||||
match self.node.write_backup(path, pass) {
|
||||
Ok(()) => {
|
||||
self.status_note = Some(format!("backup written {path}"));
|
||||
self.screen = Screen::Main;
|
||||
}
|
||||
Err(e) => {
|
||||
self.alert = Some(e);
|
||||
self.screen = Screen::Main;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.screen = Screen::Passphrase {
|
||||
kind,
|
||||
path: path.to_string(),
|
||||
first: Some(pass.to_string()),
|
||||
buf: String::new(),
|
||||
};
|
||||
}
|
||||
}
|
||||
BackupKind::Restore => match self.node.restore_backup(path, pass) {
|
||||
Ok(()) => {
|
||||
self.me_fp = to_hex(&self.node.identity_pk());
|
||||
self.status_note = Some("identity restored — roster unchanged".into());
|
||||
self.screen = Screen::Main;
|
||||
}
|
||||
Err(e) => {
|
||||
self.alert = Some(e);
|
||||
self.screen = Screen::Main;
|
||||
}
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn confirm_rotate(&mut self) -> Result<(), String> {
|
||||
let node = Arc::clone(&self.node);
|
||||
match self.rt.block_on(node.rotate()) {
|
||||
|
|
@ -855,6 +1096,10 @@ impl App {
|
|||
Screen::Rotate { prompt } => draw_rotate(f, prompt.typed()),
|
||||
Screen::Wipe { kind, prompt } => draw_wipe(f, *kind, prompt.typed()),
|
||||
Screen::Profile { editor } => draw_profile(f, editor),
|
||||
Screen::ConfirmKeys { kind, prompt, .. } => draw_confirm_keys(f, *kind, prompt.typed()),
|
||||
Screen::Passphrase {
|
||||
kind, first, buf, ..
|
||||
} => draw_passphrase(f, *kind, first.is_some(), buf),
|
||||
}
|
||||
if self.help_open {
|
||||
draw_text_overlay(f, "? help", help_overlay_text());
|
||||
|
|
@ -1094,6 +1339,11 @@ fn footer_hints(screen: &Screen) -> &'static str {
|
|||
WipeKind::All => "type WIPEALL Esc cancel ? help",
|
||||
},
|
||||
Screen::Profile { .. } => "Enter save Tab field Esc cancel ? help",
|
||||
Screen::ConfirmKeys { kind, .. } => match kind {
|
||||
BackupKind::Backup => "type BACKUP Esc cancel ? help",
|
||||
BackupKind::Restore => "type RESTORE Esc cancel ? help",
|
||||
},
|
||||
Screen::Passphrase { .. } => "Enter submit Esc cancel ? help",
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1194,6 +1444,38 @@ fn draw_profile(f: &mut Frame, editor: &ProfileEditor) {
|
|||
);
|
||||
}
|
||||
|
||||
fn draw_confirm_keys(f: &mut Frame, kind: BackupKind, typed: &str) {
|
||||
let (title, text) = match kind {
|
||||
BackupKind::Backup => ("(o) backup", backup_screen_text()),
|
||||
BackupKind::Restore => ("(o) restore", restore_screen_text()),
|
||||
};
|
||||
let body = format!("{text}\n\n{typed}");
|
||||
f.render_widget(
|
||||
Paragraph::new(body)
|
||||
.style(Style::default().fg(C_TEXT))
|
||||
.wrap(Wrap { trim: false })
|
||||
.block(themed_block(title)),
|
||||
f.area(),
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_passphrase(f: &mut Frame, kind: BackupKind, second: bool, buf: &str) {
|
||||
let stars: String = buf.chars().map(|_| '*').collect();
|
||||
let hint = match (kind, second) {
|
||||
(BackupKind::Backup, false) => "Enter passphrase",
|
||||
(BackupKind::Backup, true) => "Re-enter passphrase",
|
||||
(BackupKind::Restore, _) => "Enter passphrase",
|
||||
};
|
||||
let body = format!("{hint}\n\n{stars}\n\nEnter submit Esc cancel");
|
||||
f.render_widget(
|
||||
Paragraph::new(body)
|
||||
.style(Style::default().fg(C_TEXT))
|
||||
.wrap(Wrap { trim: false })
|
||||
.block(themed_block("(o) passphrase")),
|
||||
f.area(),
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_text_overlay(f: &mut Frame, title: &str, text: &str) {
|
||||
let area = f.area();
|
||||
let lines = text.lines().count() as u16 + 2;
|
||||
|
|
|
|||
Loading…
Reference in a new issue