vortix 0.3.1

Terminal UI for WireGuard and OpenVPN with real-time telemetry and leak guarding
Documentation
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
use clap::Parser;
use cli::args::Args;
use color_eyre::Result;
use event::{Event, EventHandler};
use vortix::app::App;
use vortix::{cli, config, constants, event, ui};

#[allow(clippy::too_many_lines)] // main() carries the whole bootstrap sequence
fn main() -> Result<()> {
    // Initialize error handling first — color_eyre::install() sets its own
    // panic hook, so we must call it before installing ours.
    color_eyre::install()?;

    // Subprocess runner + tracing (plan 002). Both live behind env-driven
    // toggles so production startup is silent; `RUST_LOG=vortix::process=info`
    // surfaces every subprocess invocation as a structured event.
    init_tracing();
    vortix::vortix_process::set_global_runner(vortix::vortix_process::CommandRunner::real());

    // Platform aggregate (plan 003 U7). Detect the OS variants once at startup;
    // consumers reach for `crate::platform::current_platform()` instead of
    // branching on `cfg(target_os)`.
    vortix::platform::set_global_platform(vortix::platform::Platform::detect_current());

    // Settings (plan 006 U7) — figment-layered: defaults → user file →
    // VORTIX_* env. CLI overrides currently bypass settings.
    let settings = match vortix::vortix_config::Settings::load() {
        Ok(s) => s,
        Err(e) => {
            eprintln!("warning: failed to load settings ({e}); using defaults");
            vortix::vortix_config::Settings::default()
        }
    };

    // Journal (plan 005 U8 prep) — open the per-session JSONL writer using
    // the runner's own tokio runtime (writer task is spawn'd on it). We
    // borrow the runtime via Handle so the Journal stays alive after main()
    // exits to the TUI loop.
    let runtime_handle = vortix::vortix_process::global_runner()
        .as_real()
        .map(|r| r.runtime().handle().clone());
    if let Some(handle) = runtime_handle.clone() {
        let _guard = handle.enter();
        match vortix::vortix_core::journal::Journal::open(
            vortix::vortix_core::journal::JournalConfig {
                disk: settings.journal.disk,
                retention_days: settings.journal.retention_days,
                retention_count: settings.journal.retention_count,
                ..Default::default()
            },
        ) {
            Ok(journal) => {
                vortix::vortix_core::journal::set_global_journal(journal);
            }
            Err(e) => {
                eprintln!("warning: failed to open journal ({e}); diagnostics will be limited");
            }
        }
    }
    let _ = runtime_handle; // suppress unused warning when no real runner installed

    // Now capture color_eyre's hook and wrap it with terminal restoration
    // and recovery instructions. Drop glue on App will still run to release
    // kill switch rules and VPN processes.
    let default_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |info| {
        restore_terminal();
        eprintln!();
        eprintln!("Vortix crashed unexpectedly.");
        eprintln!("If your network is broken, run:  vortix release-killswitch");
        eprintln!();
        default_hook(info);
    }));

    // Parse arguments
    let args = Args::parse();

    // Determine how config_dir was provided (for `info` command)
    let config_dir_source = if args.config_dir.is_some() {
        if std::env::var("VORTIX_CONFIG_DIR").is_ok() {
            // When both CLI and env are set, clap prefers CLI.
            // We can't distinguish perfectly, but env-only is the common case.
            // Check if the value matches the env var to decide.
            let env_val = std::env::var("VORTIX_CONFIG_DIR").unwrap_or_default();
            let cli_val = args
                .config_dir
                .as_ref()
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_default();
            if cli_val == env_val {
                "from VORTIX_CONFIG_DIR"
            } else {
                "from --config-dir"
            }
        } else {
            "from --config-dir"
        }
    } else {
        "default"
    };

    // Resolve config directory (CLI flag > SUDO_USER > XDG > default)
    let explicit_override = args.config_dir.is_some();
    let mut config_dir = config::resolve_config_dir(args.config_dir.as_ref())
        .map_err(|e| color_eyre::eyre::eyre!("Failed to resolve config directory: {e}"))?;

    // Migration check -- only when using default resolution (not explicit --config-dir)
    if !explicit_override {
        if let Some(old_dir) = config::check_migration(&config_dir) {
            config_dir = prompt_migration(&old_dir, &config_dir);
        }
    }

    // Store the resolved config dir globally so all utility functions use it
    config::set_config_dir(config_dir.clone());

    // Plan 006 U4: backfill profile sidecars for `.conf` / `.ovpn` files
    // imported before the sidecar scheme existed. Idempotent — no-ops once
    // every profile has a `.meta.toml`. Failures are logged + non-fatal:
    // startup MUST never abort because of migration trouble.
    //
    // VORTIX_SKIP_MIGRATION=<anything> bypasses the startup backfill for
    // users who need to disable it (see docs/MIGRATION.md).
    let profiles_dir = config_dir.join(constants::PROFILES_DIR_NAME);
    if std::env::var_os("VORTIX_SKIP_MIGRATION").is_some() {
        eprintln!("VORTIX_SKIP_MIGRATION set — skipping startup sidecar backfill.");
    } else {
        match vortix::vortix_config::migrate_legacy_profiles(&profiles_dir) {
            Ok(stats) => {
                if stats.created > 0 {
                    eprintln!(
                        "Migrated {} profile(s) to the new sidecar scheme.",
                        stats.created
                    );
                }
                if stats.failed > 0 {
                    eprintln!(
                        "Warning: {} profile(s) failed to migrate; existing files untouched. Run `vortix migrate` to retry.",
                        stats.failed
                    );
                }
            }
            Err(e) => {
                eprintln!(
                    "Warning: profile sidecar migration skipped — {e}. Startup continues; run `vortix migrate` once the issue is resolved."
                );
            }
        }
    }

    // Plan 008 U5: orphan-daemon scan. If a previous vortix crashed
    // while a tunnel was up, the user's `wg-quick` / `openvpn` /
    // `wireguard-go` daemon is probably still running. Warn so they
    // know to clean up (no auto-adopt — adoption arrives with the
    // plan 010 IPC layer).
    let orphans = vortix::vortix_process::scan_orphans();
    if !orphans.is_empty() {
        eprintln!(
            "Warning: detected {} possible orphan VPN process(es) from a previous session:",
            orphans.len()
        );
        for o in &orphans {
            eprintln!("  - pid {} ({})", o.pid, o.command);
        }
        eprintln!(
            "  These may be leftovers from a previous vortix crash. Run `sudo kill <pid>` to clean up, or `sudo vortix down --force` to tear down via vortix."
        );
    }

    // Load config.toml (or use defaults)
    let app_config = match config::load_config(&config_dir) {
        Ok(cfg) => cfg,
        Err(e) => {
            eprintln!("Error: {e}");
            eprintln!();
            eprintln!("Fix the file or remove it to use defaults:");
            eprintln!("  nano {}/config.toml", config_dir.display());
            eprintln!("  rm {}/config.toml", config_dir.display());
            std::process::exit(1);
        }
    };

    // Determine output mode from global flags
    let output_mode = if args.json {
        cli::output::OutputMode::Json
    } else if args.quiet {
        cli::output::OutputMode::Quiet
    } else {
        cli::output::OutputMode::Human
    };

    // Handle CLI commands (import, update, info, status, up, down, etc.)
    if let Some(command) = &args.command {
        let exit_code = cli::commands::handle_command(
            command,
            &config_dir,
            config_dir_source,
            &app_config,
            output_mode,
        );
        std::process::exit(exit_code);
    }

    // Run the TUI application
    let terminal = init_terminal()?;
    let result = run_tui(terminal, app_config, config_dir);
    restore_terminal();

    result
}

/// Prompts the user to migrate data from an old config directory.
///
/// Returns the config directory to use for this session.
fn prompt_migration(old_dir: &std::path::Path, new_dir: &std::path::Path) -> std::path::PathBuf {
    use std::io::Write;

    eprintln!();
    eprintln!("  Old data found at: {}", old_dir.display());
    eprintln!("  New config dir:    {}", new_dir.display());
    eprintln!();
    eprintln!("  Vortix now stores config under your home directory instead of");
    eprintln!("  /root, so profiles are accessible without sudo.");
    eprintln!();
    eprintln!("  [Y] Move your existing profiles and settings to the new location.");
    eprintln!("      Files are copied first, then deleted from the old path.");
    eprintln!();
    eprintln!(
        "  [n] Start fresh. Your old data stays at {} but",
        old_dir.display()
    );
    eprintln!("      won't be used. You can import profiles again or copy manually.");
    eprintln!();
    eprint!("  Move data? [Y/n] ");
    // Flush stderr so the prompt appears before we block on stdin
    let _ = std::io::stderr().flush();

    let mut input = String::new();
    if std::io::stdin().read_line(&mut input).is_err() {
        eprintln!("  Could not read input. Starting fresh.\n");
        return new_dir.to_path_buf();
    }
    let input = input.trim().to_lowercase();

    if input.is_empty() || input == "y" || input == "yes" {
        eprintln!();
        match config::migrate_data(old_dir, new_dir) {
            Ok(()) => {
                // Verify profiles were actually migrated
                let profiles_exist = new_dir.join("profiles").is_dir()
                    && std::fs::read_dir(new_dir.join("profiles"))
                        .map(|mut d| d.next().is_some())
                        .unwrap_or(false);
                if profiles_exist {
                    eprintln!("  Done! Data moved to {}\n", new_dir.display());
                } else {
                    eprintln!(
                        "  Warning: Move completed but no profiles found at {}",
                        new_dir.join("profiles").display()
                    );
                    eprintln!(
                        "  Check if your profiles are still at {}\n",
                        old_dir.display()
                    );
                }
                new_dir.to_path_buf()
            }
            Err(e) => {
                eprintln!("  Move failed: {e}");
                eprintln!("  Your original data is untouched at {}", old_dir.display());
                eprintln!("  Starting fresh at {}\n", new_dir.display());
                new_dir.to_path_buf()
            }
        }
    } else {
        eprintln!();
        eprintln!("  Starting fresh at {}", new_dir.display());
        eprintln!("  Old data is still at {}.", old_dir.display());
        eprintln!("  This prompt will appear until you migrate or the old data is removed.");
        eprintln!("  To silence it: --config-dir {}\n", old_dir.display());
        new_dir.to_path_buf()
    }
}

/// Runs the main TUI event loop.
fn run_tui(
    mut terminal: ratatui::DefaultTerminal,
    config: config::AppConfig,
    config_dir: std::path::PathBuf,
) -> Result<()> {
    let tick_rate = config.tick_rate;
    let profiles_dir_for_resolver = config_dir.join(constants::PROFILES_DIR_NAME);
    let mut app = App::new(config, config_dir);

    // Attach an `EngineHandle` (plan 005 U5/U6). The handle wraps the
    // FSM and gets a per-profile tunnel factory so a single
    // `Engine<TunnelKind>` drives both WG and OVPN. The actor spawns on
    // the bundled tokio runtime. Failure is non-fatal.
    if let Some(runtime) = vortix::vortix_process::global_runner().as_real() {
        let _guard = runtime.runtime().handle().enter();
        if let Some(journal) = vortix::vortix_core::journal::global_journal().cloned() {
            use vortix::state::Protocol;
            use vortix::tunnel::{tunnel_for_with_secrets, TunnelKind};
            use vortix::vortix_config::profile_store::{FsProfileStore, ProfileStore};
            use vortix::vortix_core::engine::{Engine, EngineHandle};
            use vortix::vortix_core::profile::{ProfileId, ProtocolKind};
            use vortix::vortix_protocol_wireguard::WgTunnel;

            // Live profile resolver — reads sidecars via FsProfileStore so
            // any consumer calling `handle.execute(Connect{id})` sees the
            // user's actual profiles (post-migration).
            let resolver_dir = profiles_dir_for_resolver.clone();
            let resolver = move |id: &ProfileId| {
                let store = FsProfileStore::new(resolver_dir.clone());
                store.get(id).ok()
            };

            // Per-Connect tunnel factory — picks WG vs OVPN from the
            // resolved profile's protocol. Plan 006 U6's wire-up.
            let factory_config_dir = vortix::utils::get_app_config_dir()
                .unwrap_or_else(|_| std::path::PathBuf::from("/tmp"));
            let factory = move |profile: &vortix::vortix_core::profile::Profile| {
                let proto = match profile.protocol {
                    ProtocolKind::OpenVpn => Protocol::OpenVPN,
                    // Default to WireGuard for any future variants.
                    _ => Protocol::WireGuard,
                };
                tunnel_for_with_secrets(proto, &factory_config_dir, "3", 30)
            };

            let initial_tunnel = TunnelKind::WireGuard(WgTunnel::new());
            let engine = Engine::new(initial_tunnel, resolver).with_tunnel_factory(factory);
            let handle = EngineHandle::local(engine, journal);
            app = app.with_engine_handle(handle);

            // Plan 005 U7: spawn a journal-subscriber task that reacts
            // to engine events. Today it nudges the legacy telemetry
            // worker on `TunnelUp` so connect → IP-refresh happens
            // promptly. Future units route more flows through here.
            if let Some(j) = vortix::vortix_core::journal::global_journal() {
                let mut rx = j.subscribe();
                let nudge = app.engine.telemetry_nudge.clone();
                tokio::spawn(async move {
                    use vortix::vortix_core::engine::EngineEvent;
                    while let Ok(envelope) = rx.recv().await {
                        if matches!(envelope.event, EngineEvent::TunnelUp { .. }) {
                            if let Some(n) = &nudge {
                                let _ = n.send(());
                            }
                        }
                    }
                });
            }
        }
    }
    let events = EventHandler::new(tick_rate);
    let size = terminal.size()?;
    app.on_resize(size.width, size.height);

    // Initial draw
    app.process_external();
    terminal.draw(|frame| ui::render(frame, &mut app))?;

    while !app.should_quit {
        if app.has_active_animation() {
            while let Some(event) = events.try_next()? {
                match event {
                    Event::Key(key_event) => app.handle_key(key_event),
                    Event::Mouse(mouse_event) => app.handle_mouse(mouse_event),
                    Event::Tick => app.on_tick(),
                    Event::Resize(w, h) => app.on_resize(w, h),
                }
            }
            app.advance_animation();
        } else {
            match events.next()? {
                Event::Key(key_event) => app.handle_key(key_event),
                Event::Mouse(mouse_event) => app.handle_mouse(mouse_event),
                Event::Tick => app.on_tick(),
                Event::Resize(width, height) => app.on_resize(width, height),
            }
        }

        app.process_external();
        terminal.draw(|frame| ui::render(frame, &mut app))?;

        if app.has_active_animation() {
            std::thread::sleep(std::time::Duration::from_millis(
                constants::FLIP_ANIMATION_FRAME_MS,
            ));
        }
    }

    Ok(())
}

/// Initialise tracing-subscriber with an env-filter layer.
///
/// Silent by default; `RUST_LOG=vortix::process=info` enables the structured
/// subprocess events emitted by `RealRunner`. The TUI uses stderr for log
/// output since stdout drives the alternate-screen terminal.
fn init_tracing() {
    use tracing_subscriber::EnvFilter;
    let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("off"));
    // Best-effort init: ignore the error from double-init in tests.
    let _ = tracing_subscriber::fmt()
        .with_env_filter(filter)
        .with_writer(std::io::stderr)
        .with_target(true)
        .compact()
        .try_init();
}

fn init_terminal() -> Result<ratatui::DefaultTerminal> {
    let mut terminal = ratatui::init();
    crossterm::execute!(std::io::stdout(), crossterm::event::EnableMouseCapture)?;
    terminal.clear()?;
    Ok(terminal)
}

fn restore_terminal() {
    let _ = crossterm::execute!(std::io::stdout(), crossterm::event::DisableMouseCapture);
    ratatui::restore();
}