zc2 0.0.23

P2P compute broker with credit-based billing, WAL, and broker mesh support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Guided onboarding wizard — a full-screen, Claude-Code-style setup flow.
//!
//! Launched by `zc login` / `zc init` and by a plain `zc` when no API key is
//! set. Steps: choose environment → browser device-code sign-in (show a code,
//! open the dashboard, poll for approval) → join the mesh. No key pasting.
//!
//! The device-code HTTP/parse logic is factored into small pure-ish functions
//! so it is unit-testable without a terminal.

use std::io;
use std::time::{Duration, Instant};

use crossterm::{
    event::{self, Event, KeyCode, KeyEventKind, KeyModifiers},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
    backend::CrosstermBackend,
    layout::{Alignment, Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph},
    Frame, Terminal,
};

use crate::credentials;
use crate::init::{decide, Decision};

/// The dashboards a user can target, in menu order.
pub const ENVS: &[(&str, &str)] = &[
    ("Production", credentials::PROD_API_URL),
    ("Staging", credentials::STAGING_API_URL),
];

/// Parsed `/api/auth/cli/start` response.
pub struct DeviceStart {
    pub device_code: String,
    pub user_code: String,
    pub verification_uri: String,
    pub interval: u64,
    pub expires_in: u64,
}

/// Parse a `/api/auth/cli/start` body. Pure — unit-tested without the network.
pub fn parse_device_start(body: &str) -> Option<DeviceStart> {
    let v: serde_json::Value = serde_json::from_str(body).ok()?;
    Some(DeviceStart {
        device_code: v["device_code"].as_str()?.to_string(),
        user_code: v["user_code"].as_str()?.to_string(),
        verification_uri: v["verification_uri"].as_str()?.to_string(),
        interval: v["interval"].as_u64().unwrap_or(5).clamp(1, 30),
        expires_in: v["expires_in"].as_u64().unwrap_or(600),
    })
}

/// Begin a device-code sign-in against a dashboard.
pub fn device_start(api_url: &str) -> Result<DeviceStart, String> {
    let url = format!("{}/api/auth/cli/start", api_url.trim_end_matches('/'));
    let resp = ureq::post(&url)
        .config()
        .http_status_as_error(false)
        .timeout_global(Some(Duration::from_secs(15)))
        .build()
        .send("")
        .map_err(|e| format!("could not reach {api_url}: {e}"))?;
    let status = resp.status().as_u16();
    if status == 404 {
        return Err(format!("browser sign-in isn't available on {api_url}"));
    }
    if status != 200 {
        return Err(format!(
            "sign-in could not start ({api_url}: HTTP {status})"
        ));
    }
    let body = resp
        .into_body()
        .read_to_string()
        .map_err(|e| e.to_string())?;
    parse_device_start(&body).ok_or_else(|| "malformed start response".into())
}

/// Poll once for approval. Wraps the shared `init::decide` logic.
pub fn device_poll(api_url: &str, device_code: &str) -> Decision {
    let url = format!("{}/api/auth/cli/poll", api_url.trim_end_matches('/'));
    let payload = serde_json::json!({ "device_code": device_code }).to_string();
    match ureq::post(&url)
        .config()
        .http_status_as_error(false)
        .timeout_global(Some(Duration::from_secs(15)))
        .build()
        .header("Content-Type", "application/json")
        .send(payload.as_str())
    {
        Ok(r) => {
            let s = r.status().as_u16();
            let b = r.into_body().read_to_string().unwrap_or_default();
            decide(s, &b)
        }
        // Transient network error — treat as still-pending so we keep polling.
        Err(_) => Decision::Pending,
    }
}

/// Which screen the wizard is on.
enum Step {
    ChooseEnv,
    Await {
        user_code: String,
        device_code: String,
        verification_uri: String,
        deadline: Instant,
        next_poll: Instant,
    },
    Connected {
        summary: String,
    },
    Failed(String),
}

struct Wizard {
    step: Step,
    env_idx: usize,
    notice: Option<String>,
    pending: Option<Pending>,
    quit: bool,
    connected: bool,
}

impl Wizard {
    fn new() -> Self {
        Self {
            step: Step::ChooseEnv,
            env_idx: 0,
            notice: None,
            pending: None,
            quit: false,
            connected: false,
        }
    }
    fn api_url(&self) -> &'static str {
        ENVS[self.env_idx].1
    }
}

/// Run the wizard. Returns `true` when the user finished connected (so callers
/// can drop straight into the shell). Falls back to text guidance off a TTY.
pub fn run() -> bool {
    use std::io::IsTerminal;
    if !io::stdout().is_terminal() {
        crate::init::print_connect_guidance();
        return false;
    }
    match run_inner() {
        Ok(connected) => connected,
        Err(e) => {
            eprintln!("onboarding error: {e}");
            false
        }
    }
}

fn run_inner() -> io::Result<bool> {
    let default_panic = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |info| {
        let _ = disable_raw_mode();
        let _ = execute!(io::stdout(), LeaveAlternateScreen);
        default_panic(info);
    }));

    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let mut terminal = Terminal::new(CrosstermBackend::new(stdout))?;

    let mut w = Wizard::new();
    while !w.quit {
        terminal.draw(|f| draw(f, &w))?;

        // Run any queued blocking action (start / connect) between frames.
        if let Some(action) = w.pending.take() {
            action.run(&mut w);
            continue;
        }

        // While awaiting approval, poll on the server's interval.
        if let Step::Await {
            device_code,
            deadline,
            next_poll,
            ..
        } = &w.step
        {
            let now = Instant::now();
            if now >= *deadline {
                w.step = Step::Failed("the code expired — press Enter to try again".into());
                continue;
            }
            if now >= *next_poll {
                let url = w.api_url();
                let dc = device_code.clone();
                match device_poll(url, &dc) {
                    Decision::Pending => {
                        if let Step::Await { next_poll, .. } = &mut w.step {
                            *next_poll = Instant::now() + Duration::from_secs(5);
                        }
                    }
                    Decision::Approved(token) => {
                        let _ = credentials::save(&token, Some(url));
                        std::env::set_var("ZAKURO_API_KEY", &token);
                        std::env::set_var("ZAKURO_API_URL", url);
                        w.notice = Some("Approved — connecting to the mesh…".into());
                        w.pending = Some(Pending::Connect);
                    }
                    Decision::Failed(msg) => {
                        w.step = Step::Failed(msg);
                    }
                }
                continue;
            }
        }

        if !event::poll(Duration::from_millis(120))? {
            continue;
        }
        if let Event::Key(k) = event::read()? {
            if k.kind == KeyEventKind::Press {
                handle_key(&mut w, k.code, k.modifiers);
            }
        }
    }

    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    terminal.show_cursor()?;
    if w.connected {
        if let Step::Connected { summary } = &w.step {
            println!("  ✓ Signed in · {summary}");
        }
    }
    Ok(w.connected)
}

enum Pending {
    Start,
    Connect,
}

impl Pending {
    fn run(self, w: &mut Wizard) {
        match self {
            Pending::Start => match device_start(w.api_url()) {
                Ok(ds) => {
                    let _ = crate::init::open_browser(&ds.verification_uri);
                    w.notice = Some("Opened the dashboard — approve the code there.".into());
                    w.step = Step::Await {
                        user_code: ds.user_code,
                        device_code: ds.device_code,
                        verification_uri: ds.verification_uri,
                        deadline: Instant::now() + Duration::from_secs(ds.expires_in),
                        next_poll: Instant::now() + Duration::from_secs(ds.interval),
                    };
                }
                Err(e) => w.step = Step::Failed(e),
            },
            Pending::Connect => {
                match crate::vpn::connect(crate::vpn::connector::Preference::Auto) {
                    Ok(info) => {
                        w.connected = true;
                        w.step = Step::Connected {
                            summary: format!(
                                "mesh up — {} · {} peer(s)",
                                info.address,
                                info.peers.len()
                            ),
                        };
                        w.notice = None;
                    }
                    Err(e) => {
                        w.step = Step::Failed(format!("{e}"));
                        w.notice = None;
                    }
                }
            }
        }
    }
}

fn handle_key(w: &mut Wizard, code: KeyCode, mods: KeyModifiers) {
    if code == KeyCode::Esc || (code == KeyCode::Char('c') && mods.contains(KeyModifiers::CONTROL))
    {
        w.quit = true;
        return;
    }
    match &w.step {
        Step::ChooseEnv => match code {
            KeyCode::Up | KeyCode::Char('k') => {
                w.env_idx = (w.env_idx + ENVS.len() - 1) % ENVS.len()
            }
            KeyCode::Down | KeyCode::Char('j') => w.env_idx = (w.env_idx + 1) % ENVS.len(),
            KeyCode::Char('q') => w.quit = true,
            KeyCode::Enter => {
                w.notice = Some("Starting browser sign-in…".into());
                w.pending = Some(Pending::Start);
            }
            _ => {}
        },
        Step::Await {
            verification_uri, ..
        } => {
            if code == KeyCode::Char('o') {
                let _ = crate::init::open_browser(verification_uri);
            }
        }
        Step::Connected { .. } => w.quit = true,
        Step::Failed(_) => match code {
            KeyCode::Enter | KeyCode::Char('r') => {
                w.step = Step::ChooseEnv;
                w.notice = None;
            }
            _ => w.quit = true,
        },
    }
}

fn draw(f: &mut Frame, w: &Wizard) {
    let area = f.size();
    let accent = Style::default()
        .fg(Color::Cyan)
        .add_modifier(Modifier::BOLD);
    let dim = Style::default().fg(Color::DarkGray);
    let ok = Style::default().fg(Color::Green);
    let err = Style::default().fg(Color::Red);

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .margin(1)
        .constraints([
            Constraint::Length(3),
            Constraint::Min(6),
            Constraint::Length(2),
            Constraint::Length(1),
        ])
        .split(area);

    let step_no = match w.step {
        Step::ChooseEnv => "Step 1/2",
        Step::Await { .. } => "Step 2/2",
        Step::Connected { .. } => "Done",
        Step::Failed(_) => "Problem",
    };
    f.render_widget(
        Paragraph::new(Line::from(vec![
            Span::styled(" Connect to Zakuro ", accent),
            Span::styled(format!("· {step_no}"), dim),
        ]))
        .block(Block::default().borders(Borders::ALL).border_style(accent)),
        chunks[0],
    );

    let body: Vec<Line> = match &w.step {
        Step::ChooseEnv => {
            let mut lines = vec![Line::from("Choose your environment:"), Line::from("")];
            for (i, (name, url)) in ENVS.iter().enumerate() {
                let sel = i == w.env_idx;
                lines.push(Line::from(vec![
                    Span::styled(if sel { "" } else { "     " }, accent),
                    Span::styled(
                        format!("{name}  "),
                        if sel { accent } else { Style::default() },
                    ),
                    Span::styled(format!("({url})"), dim),
                ]));
            }
            lines
        }
        Step::Await {
            user_code,
            verification_uri,
            ..
        } => vec![
            Line::from(vec![
                Span::raw("Environment: "),
                Span::styled(ENVS[w.env_idx].0, accent),
            ]),
            Line::from(""),
            Line::from("In the browser tab that opened, enter this code and approve:"),
            Line::from(""),
            Line::from(Span::styled(format!("        {user_code}"), accent)),
            Line::from(""),
            Line::from(vec![
                Span::styled("  ", dim),
                Span::styled(verification_uri.clone(), dim),
            ]),
            Line::from(""),
            Line::from(Span::styled("  Waiting for approval…", dim)),
        ],
        Step::Connected { summary } => vec![
            Line::from(vec![Span::styled("", ok), Span::raw(summary.clone())]),
            Line::from(""),
            Line::from("You're on the mesh. Press any key to open the zc terminal."),
        ],
        Step::Failed(msg) => vec![
            Line::from(vec![Span::styled("", err), Span::raw(msg.clone())]),
            Line::from(""),
            Line::from("Press Enter to start over, or Esc to quit."),
        ],
    };
    f.render_widget(
        Paragraph::new(body).block(Block::default().borders(Borders::ALL)),
        chunks[1],
    );

    let notice = w.notice.clone().unwrap_or_default();
    f.render_widget(
        Paragraph::new(Line::from(Span::styled(notice, dim))),
        chunks[2],
    );

    let hint = match w.step {
        Step::ChooseEnv => "↑/↓ select · ⏎ sign in · q quit",
        Step::Await { .. } => "o re-open browser · Esc cancel",
        Step::Connected { .. } => "press any key to continue",
        Step::Failed(_) => "⏎ start over · Esc quit",
    };
    f.render_widget(
        Paragraph::new(Line::from(Span::styled(hint, dim))).alignment(Alignment::Center),
        chunks[3],
    );
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_a_full_start_response() {
        let body = r#"{"device_code":"abc","user_code":"WXYZ-1234","verification_uri":"https://x/activate","interval":5,"expires_in":600}"#;
        let ds = parse_device_start(body).expect("parse");
        assert_eq!(ds.user_code, "WXYZ-1234");
        assert_eq!(ds.device_code, "abc");
        assert_eq!(ds.interval, 5);
    }

    #[test]
    fn start_defaults_interval_and_expiry() {
        let body = r#"{"device_code":"d","user_code":"c","verification_uri":"u"}"#;
        let ds = parse_device_start(body).expect("parse");
        assert_eq!(ds.interval, 5);
        assert_eq!(ds.expires_in, 600);
    }

    #[test]
    fn start_none_on_missing_fields() {
        assert!(parse_device_start(r#"{"user_code":"c"}"#).is_none());
        assert!(parse_device_start("garbage").is_none());
    }

    #[test]
    fn envs_are_prod_then_staging() {
        assert_eq!(ENVS[0].1, credentials::PROD_API_URL);
        assert_eq!(ENVS[1].1, credentials::STAGING_API_URL);
    }
}