triglav 0.2.0

High-performance multi-path networking tool with intelligent uplink management
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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
//! Triglav CLI - High-performance multi-path networking tool.

use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use clap::Parser;
use colored::Colorize;
use console::Term;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use tokio::signal;
use tokio::sync::broadcast;

use triglav::cli::*;
use triglav::config::{init_logging, Config};
use triglav::crypto::KeyPair;
use triglav::error::Result;
use triglav::multipath::{MultipathConfig, MultipathManager, UplinkConfig};
use triglav::proxy::{HttpProxyConfig, HttpProxyServer, Socks5Config, Socks5Server};
use triglav::transport::TransportProtocol;
use triglav::tun::{NatConfig, RouteConfig, TunConfig, TunnelConfig, TunnelRunner};
use triglav::types::AuthKey;
use triglav::util;
use triglav::VERSION;

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    // Initialize logging
    let log_config = triglav::config::LoggingConfig {
        level: cli.log_level.clone(),
        color: !cli.no_color,
        ..Default::default()
    };
    init_logging(&log_config)?;

    // Load config if specified
    let config = if let Some(ref path) = cli.config {
        Config::load(path)?
    } else if Config::default_path().exists() {
        Config::load(Config::default_path())?
    } else {
        Config::default()
    };

    // Dispatch command
    match cli.command {
        Commands::Server(args) => run_server(args, config).await,
        Commands::Tun(args) => run_tun(args, config).await,
        Commands::Connect(args) => run_connect(args, config).await,
        Commands::Keygen(args) => run_keygen(args),
        Commands::Status(args) => run_status(args).await,
        Commands::Uplink(args) => run_uplink(args).await,
        Commands::Diagnose(args) => run_diagnose(args).await,
        Commands::Benchmark(args) => run_benchmark(args).await,
        Commands::Completions(args) => run_completions(args),
        Commands::Config(args) => run_config(args),
    }
}

/// Run the server
async fn run_server(args: ServerArgs, _config: Config) -> Result<()> {
    println!(
        "{}",
        "╔══════════════════════════════════════════╗".bright_cyan()
    );
    println!(
        "{}",
        "║     TRIGLAV SERVER                       ║".bright_cyan()
    );
    println!(
        "{}",
        format!("║     Version {}", VERSION).bright_cyan()
    );
    println!(
        "{}",
        "╚══════════════════════════════════════════╝".bright_cyan()
    );
    println!();

    // Load or generate key
    let keypair = if let Some(ref key_path) = args.key {
        if key_path.exists() {
            load_keypair(key_path)?
        } else if args.generate_key {
            let kp = KeyPair::generate();
            save_keypair(&kp, key_path)?;
            println!(
                "{} Generated new keypair at {}",
                "".green(),
                key_path.display()
            );
            kp
        } else {
            return Err(triglav::Error::Config(format!(
                "Key file not found: {}",
                key_path.display()
            )));
        }
    } else if args.generate_key {
        let kp = KeyPair::generate();
        println!("{} Generated ephemeral keypair (not saved)", "".yellow());
        kp
    } else {
        return Err(triglav::Error::Config(
            "No key specified. Use --key <path> or --generate-key".into(),
        ));
    };

    // Generate client key
    let client_key = AuthKey::new(*keypair.public.as_bytes(), args.listen.clone());
    println!();
    println!("{}", "Client Connection Key:".bright_white().bold());
    println!("{}", "".repeat(50));
    println!("{}", client_key.to_string().bright_green());
    println!("{}", "".repeat(50));
    println!();

    if args.print_key {
        return Ok(());
    }

    // Print listen addresses
    println!("{}", "Listening on:".bright_white());
    for addr in &args.listen {
        let protocol = if args.tcp_fallback { "UDP+TCP" } else { "UDP" };
        println!("  {} {} ({})", "".cyan(), addr, protocol);
    }
    println!();

    // Setup shutdown signal
    let (shutdown_tx, mut shutdown_rx) = broadcast::channel::<()>(1);

    tokio::spawn(async move {
        let _ = signal::ctrl_c().await;
        let _ = shutdown_tx.send(());
    });

    println!("{} Server running. Press Ctrl+C to stop.", "".green());

    // Run server loop
    // TODO: Implement actual server logic
    shutdown_rx.recv().await.ok();

    println!();
    println!("{} Server stopped.", "".yellow());

    Ok(())
}

/// Start TUN tunnel (true VPN mode)
async fn run_tun(args: TunArgs, _config: Config) -> Result<()> {
    println!(
        "{}",
        "╔══════════════════════════════════════════╗".bright_cyan()
    );
    println!(
        "{}",
        "║     TRIGLAV TUN TUNNEL                   ║".bright_cyan()
    );
    println!(
        "{}",
        format!("║     Version {}", VERSION).bright_cyan()
    );
    println!(
        "{}",
        "╚══════════════════════════════════════════╝".bright_cyan()
    );
    println!();

    // Check privileges
    if !triglav::tun::check_privileges()? {
        println!("{} TUN devices require elevated privileges.", "".yellow());
        println!("  Please run with {} or use:", "sudo".bright_white());
        println!("    Linux:  sudo setcap cap_net_admin+ep $(which triglav)");
        println!("    macOS:  sudo triglav tun ...");
        println!();
    }

    // Parse auth key
    let auth_key = AuthKey::parse(&args.key)?;
    let server_addrs = auth_key.server_addrs();

    println!("{} Server:", "".cyan());
    for addr in server_addrs {
        println!("  {} {}", "".dimmed(), addr);
    }
    println!();

    // Discover interfaces
    let interfaces = if args.auto_discover {
        util::get_network_interfaces()
            .into_iter()
            .filter(|i| i.is_up && !i.is_loopback)
            .map(|i| i.name)
            .collect::<Vec<_>>()
    } else if !args.interface.is_empty() {
        args.interface.clone()
    } else {
        util::get_network_interfaces()
            .into_iter()
            .filter(|i| i.is_up && !i.is_loopback)
            .take(2)
            .map(|i| i.name)
            .collect::<Vec<_>>()
    };

    if interfaces.is_empty() {
        return Err(triglav::Error::NoAvailableUplinks);
    }

    println!("{} Interfaces:", "".cyan());
    for iface in &interfaces {
        println!("  {} {}", "".green(), iface);
    }
    println!();

    // Parse IPv4 address
    let ipv4: std::net::Ipv4Addr = args
        .ipv4
        .parse()
        .map_err(|_| triglav::Error::Config(format!("Invalid IPv4 address: {}", args.ipv4)))?;

    // Build tunnel configuration
    let mut tun_config = TunConfig::default();
    tun_config.name = args.tun_name.clone();
    tun_config.ipv4_addr = Some(ipv4);

    let mut nat_config = NatConfig::default();
    nat_config.tunnel_ipv4 = ipv4;

    let mut route_config = RouteConfig::default();
    route_config.full_tunnel = args.full_tunnel;
    route_config.include_routes = args.route.clone();
    route_config.exclude_routes = args.exclude.clone();

    // Exclude server addresses from tunnel
    for addr in server_addrs {
        route_config
            .exclude_routes
            .push(format!("{}/32", addr.ip()));
    }

    let tunnel_config = TunnelConfig {
        tun: tun_config,
        nat: nat_config,
        routing: route_config,
        ..Default::default()
    };

    // Create tunnel runner
    let mut runner = TunnelRunner::new(tunnel_config)?;

    println!(
        "{} TUN device: {}",
        "".cyan(),
        runner.tun_name().bright_white()
    );
    println!("  IPv4:     {}", ipv4);
    if args.full_tunnel {
        println!("  Mode:     {} (all traffic)", "Full Tunnel".bright_green());
    } else if !args.route.is_empty() {
        println!(
            "  Mode:     {} ({} routes)",
            "Split Tunnel".yellow(),
            args.route.len()
        );
    } else {
        println!("  Mode:     {} (manual routes)", "Manual".dimmed());
    }
    println!();

    // Add uplinks
    for iface in &interfaces {
        let uplink_cfg = UplinkConfig {
            id: iface.clone().into(),
            interface: Some(iface.clone()),
            remote_addr: server_addrs[0],
            protocol: TransportProtocol::Udp,
            weight: 100,
            enabled: true,
            ..Default::default()
        };
        runner.add_uplink(uplink_cfg)?;
    }

    // Connect
    println!("{} Connecting...", "".cyan());
    let remote_public = triglav::crypto::PublicKey::from_bytes(auth_key.server_pubkey());
    match runner.connect(remote_public).await {
        Ok(_) => {
            println!("{} Connected!", "".green());
        }
        Err(e) => {
            println!("{} Connection failed: {}", "".red(), e);
            return Err(e);
        }
    }

    println!();
    println!("{}", "Tunnel Status:".bright_white().bold());
    println!("  Device:   {}", runner.tun_name());
    println!("  Uplinks:  {} active", runner.manager().uplink_count());
    println!("  Strategy: {:?}", args.strategy);
    println!();
    println!("{} Tunnel running. Press Ctrl+C to stop.", "".green());
    println!();

    // Setup shutdown handler
    let (shutdown_tx, mut shutdown_rx) = broadcast::channel::<()>(1);

    tokio::spawn(async move {
        let _ = signal::ctrl_c().await;
        let _ = shutdown_tx.send(());
    });

    // Run tunnel
    tokio::select! {
        result = runner.run() => {
            if let Err(e) = result {
                println!("{} Tunnel error: {}", "".red(), e);
            }
        }
        _ = shutdown_rx.recv() => {
            println!();
            println!("{} Shutting down...", "".yellow());
        }
    }

    runner.stop();

    println!("{} Tunnel stopped.", "".yellow());

    Ok(())
}

/// Connect to a server (legacy proxy mode)
async fn run_connect(args: ConnectArgs, _config: Config) -> Result<()> {
    println!(
        "{}",
        "╔══════════════════════════════════════════╗".bright_cyan()
    );
    println!(
        "{}",
        "║     TRIGLAV CLIENT                       ║".bright_cyan()
    );
    println!(
        "{}",
        "╚══════════════════════════════════════════╝".bright_cyan()
    );
    println!();

    // Parse auth key
    let auth_key = AuthKey::parse(&args.key)?;
    let server_addrs = auth_key.server_addrs();

    println!("{} Connecting to:", "".cyan());
    for addr in server_addrs {
        println!("  {} {}", "".dimmed(), addr);
    }
    println!();

    // Discover or use specified interfaces
    let interfaces = if args.auto_discover {
        util::get_network_interfaces()
            .into_iter()
            .filter(|i| i.is_up && !i.is_loopback)
            .map(|i| i.name)
            .collect::<Vec<_>>()
    } else if !args.interface.is_empty() {
        args.interface.clone()
    } else {
        // Default: try to find usable interfaces
        util::get_network_interfaces()
            .into_iter()
            .filter(|i| i.is_up && !i.is_loopback)
            .take(2)
            .map(|i| i.name)
            .collect::<Vec<_>>()
    };

    if interfaces.is_empty() {
        return Err(triglav::Error::NoAvailableUplinks);
    }

    println!("{} Using interfaces:", "".cyan());
    for iface in &interfaces {
        println!("  {} {}", "".green(), iface);
    }
    println!();

    // Create uplink configs
    let uplinks: Vec<UplinkConfig> = interfaces
        .iter()
        .map(|iface| UplinkConfig {
            id: iface.clone().into(),
            interface: Some(iface.clone()),
            remote_addr: server_addrs[0], // Use first server addr
            protocol: TransportProtocol::Udp,
            weight: 100,
            enabled: true,
            ..Default::default()
        })
        .collect();

    // Create multipath manager
    let keypair = KeyPair::generate();
    let mut mp_config = MultipathConfig::default();
    mp_config.scheduler.strategy = args.strategy.into();

    let manager = Arc::new(MultipathManager::new(mp_config, keypair));

    // Add uplinks
    for uplink_cfg in uplinks {
        manager.add_uplink(uplink_cfg)?;
    }

    // Setup progress display
    let multi = MultiProgress::new();
    let style = ProgressStyle::default_spinner()
        .template("{spinner:.cyan} {msg}")
        .unwrap();

    let conn_bar = multi.add(ProgressBar::new_spinner());
    conn_bar.set_style(style.clone());
    conn_bar.set_message("Connecting...");
    conn_bar.enable_steady_tick(Duration::from_millis(100));

    // Connect
    let remote_public = triglav::crypto::PublicKey::from_bytes(auth_key.server_pubkey());
    match manager.connect(remote_public).await {
        Ok(_) => {
            conn_bar.finish_with_message(format!("{} Connected!", "".green()));
        }
        Err(e) => {
            conn_bar.finish_with_message(format!("{} Connection failed: {}", "".red(), e));
            return Err(e);
        }
    }

    // Start maintenance loop for health checks, retries, and pings
    manager.start_maintenance_loop();

    // Show connection status
    println!();
    println!("{}", "Connection Status:".bright_white().bold());
    println!("  Session:  {}", manager.session_id());
    println!("  Uplinks:  {} active", manager.uplink_count());
    println!("  Strategy: {:?}", args.strategy);

    // Start SOCKS5 proxy if requested
    if let Some(socks_port) = args.socks {
        let socks_addr: SocketAddr = format!("127.0.0.1:{}", socks_port).parse().unwrap();
        let socks_config = Socks5Config {
            listen_addr: socks_addr,
            allow_no_auth: true,
            username: None,
            password: None,
            connect_timeout_secs: 30,
            max_connections: 1000,
        };
        let socks_server = Socks5Server::new(socks_config, Arc::clone(&manager));

        println!(
            "  SOCKS5:   {} (listening)",
            format!("127.0.0.1:{}", socks_port).cyan()
        );

        // Run SOCKS5 server in background
        tokio::spawn(async move {
            if let Err(e) = socks_server.run().await {
                tracing::error!("SOCKS5 server error: {}", e);
            }
        });
    }

    // Start HTTP proxy if requested
    if let Some(http_port) = args.http_proxy {
        let http_addr: SocketAddr = format!("127.0.0.1:{}", http_port).parse().unwrap();
        let http_config = HttpProxyConfig {
            listen_addr: http_addr,
            connect_timeout_secs: 30,
            max_connections: 1000,
        };
        let http_server = HttpProxyServer::new(http_config, Arc::clone(&manager));

        println!(
            "  HTTP:     {} (listening)",
            format!("127.0.0.1:{}", http_port).cyan()
        );

        // Run HTTP proxy server in background
        tokio::spawn(async move {
            if let Err(e) = http_server.run().await {
                tracing::error!("HTTP proxy server error: {}", e);
            }
        });
    }

    println!();

    // Setup shutdown handler
    let (shutdown_tx, mut shutdown_rx) = broadcast::channel::<()>(1);
    let _shutdown_tx2 = shutdown_tx.clone();

    tokio::spawn(async move {
        let _ = signal::ctrl_c().await;
        let _ = shutdown_tx.send(());
    });

    // Status display loop
    if args.verbose {
        let term = Term::stdout();

        loop {
            tokio::select! {
                _ = tokio::time::sleep(Duration::from_secs(1)) => {
                    let quality = manager.quality_summary();
                    let _ = term.clear_last_lines(4);

                    println!("{}", "".repeat(50).dimmed());
                    println!(
                        "  Uplinks: {}/{} | RTT: {:.1}ms | Loss: {:.1}%",
                        quality.usable_uplinks,
                        quality.total_uplinks,
                        quality.avg_rtt.as_secs_f64() * 1000.0,
                        quality.avg_loss * 100.0
                    );
                    println!(
                        "  TX: {} | RX: {}",
                        util::format_bytes(quality.stats.bytes_sent),
                        util::format_bytes(quality.stats.bytes_received)
                    );
                    println!("{}", "".repeat(50).dimmed());
                }
                _ = shutdown_rx.recv() => {
                    break;
                }
            }
        }
    } else {
        println!("{} Connected. Press Ctrl+C to disconnect.", "".green());
        shutdown_rx.recv().await.ok();
    }

    // Disconnect
    println!();
    println!("{} Disconnecting...", "".yellow());
    manager.close()?;
    println!("{} Disconnected.", "".yellow());

    Ok(())
}

/// Generate a new keypair
fn run_keygen(args: KeygenArgs) -> Result<()> {
    let keypair = KeyPair::generate();

    println!("{}", "Generated new keypair:".bright_white().bold());
    println!();

    match args.key_format {
        KeyFormat::Base64 => {
            println!("{}: {}", "Public Key".cyan(), keypair.public.to_base64());
            println!("{}: {}", "Secret Key".yellow(), keypair.secret.to_base64());
        }
        KeyFormat::Hex => {
            println!(
                "{}: {}",
                "Public Key".cyan(),
                hex::encode(keypair.public.as_bytes())
            );
            println!(
                "{}: {}",
                "Secret Key".yellow(),
                hex::encode(keypair.secret.as_bytes())
            );
        }
    }

    if !args.address.is_empty() {
        println!();
        let auth_key = AuthKey::new(*keypair.public.as_bytes(), args.address);
        println!("{}", "Client Connection Key:".bright_white().bold());
        println!("{}", auth_key.to_string().bright_green());
    }

    if let Some(ref output) = args.output {
        save_keypair(&keypair, output)?;
        println!();
        println!("{} Saved to {}", "".green(), output.display());
    }

    Ok(())
}

/// Show status
async fn run_status(args: StatusArgs) -> Result<()> {
    // Try to connect to local metrics endpoint
    let metrics_url = "http://127.0.0.1:9090";

    // Try status endpoint first
    let status_url = format!("{}/status", metrics_url);

    match reqwest::get(&status_url).await {
        Ok(response) if response.status().is_success() => {
            let status: serde_json::Value = response
                .json()
                .await
                .unwrap_or_else(|_| serde_json::json!({}));

            if args.json {
                println!(
                    "{}",
                    serde_json::to_string_pretty(&status).unwrap_or_default()
                );
                return Ok(());
            }

            println!(
                "{}",
                "╔══════════════════════════════════════════╗".bright_cyan()
            );
            println!(
                "{}",
                "║     TRIGLAV STATUS                       ║".bright_cyan()
            );
            println!(
                "{}",
                "╚══════════════════════════════════════════╝".bright_cyan()
            );
            println!();

            // Version and uptime
            if let Some(version) = status.get("version").and_then(|v| v.as_str()) {
                println!("  {} {}", "Version:".bright_white(), version);
            }
            if let Some(uptime) = status.get("uptime_seconds").and_then(|v| v.as_u64()) {
                println!(
                    "  {} {}",
                    "Uptime:".bright_white(),
                    util::format_duration(Duration::from_secs(uptime))
                );
            }
            if let Some(state) = status.get("state").and_then(|v| v.as_str()) {
                let state_colored = match state {
                    "running" => state.green(),
                    "starting" => state.yellow(),
                    _ => state.red(),
                };
                println!("  {} {}", "State:".bright_white(), state_colored);
            }
            println!();

            // Uplinks
            if let Some(uplinks) = status.get("uplinks").and_then(|v| v.as_array()) {
                println!("{}", "Uplinks:".bright_white().bold());
                if uplinks.is_empty() {
                    println!("  {} No uplinks configured", "".dimmed());
                } else {
                    for uplink in uplinks {
                        let id = uplink
                            .get("id")
                            .and_then(|v| v.as_str())
                            .unwrap_or("unknown");
                        let state = uplink
                            .get("state")
                            .and_then(|v| v.as_str())
                            .unwrap_or("unknown");
                        let rtt = uplink.get("rtt_ms").and_then(|v| v.as_f64()).unwrap_or(0.0);
                        let loss = uplink
                            .get("loss_percent")
                            .and_then(|v| v.as_f64())
                            .unwrap_or(0.0);

                        let status_icon = match state {
                            "connected" => "".green(),
                            "connecting" => "".yellow(),
                            _ => "".red(),
                        };

                        println!(
                            "  {} {} - {} | RTT: {:.1}ms | Loss: {:.1}%",
                            status_icon,
                            id.bright_white(),
                            state,
                            rtt,
                            loss
                        );
                    }
                }
                println!();
            }

            // Sessions
            if let Some(sessions) = status.get("sessions").and_then(|v| v.as_array()) {
                println!("{}", "Sessions:".bright_white().bold());
                if sessions.is_empty() {
                    println!("  {} No active sessions", "".dimmed());
                } else {
                    println!("  {} active sessions", sessions.len().to_string().green());
                }
                println!();
            }

            // Traffic
            if let (Some(tx), Some(rx)) = (
                status.get("total_bytes_sent").and_then(|v| v.as_u64()),
                status.get("total_bytes_received").and_then(|v| v.as_u64()),
            ) {
                println!("{}", "Traffic:".bright_white().bold());
                println!("  {} TX: {}", "".cyan(), util::format_bytes(tx));
                println!("  {} RX: {}", "".cyan(), util::format_bytes(rx));
                println!();
            }

            // Detailed stats
            if args.detailed {
                // Fetch metrics
                let metrics_endpoint = format!("{}/metrics", metrics_url);
                if let Ok(metrics_response) = reqwest::get(&metrics_endpoint).await {
                    if let Ok(metrics_text) = metrics_response.text().await {
                        println!("{}", "Metrics:".bright_white().bold());
                        // Show a few key metrics
                        for line in metrics_text
                            .lines()
                            .filter(|l| !l.starts_with('#') && !l.is_empty())
                            .take(20)
                        {
                            println!("  {}", line.dimmed());
                        }
                    }
                }
            }
        }
        Ok(response) => {
            println!("{} Server returned: {}", "".red(), response.status());
            println!();
            println!("Make sure a Triglav server/client is running with metrics enabled.");
        }
        Err(_) => {
            // No server running, show offline status
            println!(
                "{}",
                "╔══════════════════════════════════════════╗".bright_cyan()
            );
            println!(
                "{}",
                "║     TRIGLAV STATUS                       ║".bright_cyan()
            );
            println!(
                "{}",
                "╚══════════════════════════════════════════╝".bright_cyan()
            );
            println!();
            println!(
                "  {} {}",
                "Status:".bright_white(),
                "Not connected".yellow()
            );
            println!();
            println!("No Triglav instance detected on {}.", metrics_url.cyan());
            println!();
            println!("To start:");
            println!(
                "  Server: {} {}",
                "triglav server --generate-key".cyan(),
                ""
            );
            println!("  Client: {} {}", "triglav connect <key>".cyan(), "");

            // Show available network interfaces
            println!();
            println!("{}", "Available Interfaces:".bright_white().bold());
            let interfaces = util::get_network_interfaces();
            let usable: Vec<_> = interfaces
                .iter()
                .filter(|i| i.is_up && !i.is_loopback)
                .collect();

            if usable.is_empty() {
                println!("  {} No usable network interfaces found", "".yellow());
            } else {
                for iface in usable.iter().take(5) {
                    let type_str = format!("{:?}", iface.interface_type).dimmed();
                    println!(
                        "  {} {} ({}) - {}",
                        "".green(),
                        iface.name.bright_white(),
                        iface.address,
                        type_str
                    );
                }
                if usable.len() > 5 {
                    println!("  {} ... and {} more", "".dimmed(), usable.len() - 5);
                }
            }
        }
    }

    // Watch mode
    if args.watch {
        println!();
        println!("{}", "Watching for updates... (Ctrl+C to stop)".dimmed());

        let mut interval = tokio::time::interval(Duration::from_secs(args.interval));
        loop {
            interval.tick().await;

            // Clear screen and re-run
            print!("\x1B[2J\x1B[1;1H");

            // Recursive call without watch to avoid infinite loop
            let mut no_watch_args = args.clone();
            no_watch_args.watch = false;
            if let Err(e) = Box::pin(run_status(no_watch_args)).await {
                println!("Error: {}", e);
            }
        }
    }

    Ok(())
}

/// Manage uplinks
async fn run_uplink(args: UplinkArgs) -> Result<()> {
    match args.command {
        UplinkCommands::List => {
            println!("{}", "Available Network Interfaces:".bright_white().bold());
            println!();

            let interfaces = util::get_network_interfaces();
            for iface in interfaces {
                let status = if iface.is_up {
                    "UP".green()
                } else {
                    "DOWN".red()
                };
                let type_str = format!("{:?}", iface.interface_type).dimmed();

                println!(
                    "  {} {} ({}) - {} [{}]",
                    if iface.is_up {
                        "".green()
                    } else {
                        "".dimmed()
                    },
                    iface.name.bright_white(),
                    iface.address,
                    type_str,
                    status
                );
            }
        }
        UplinkCommands::Add { interface, weight } => {
            println!("Adding uplink {} with weight {}", interface.cyan(), weight);
        }
        UplinkCommands::Remove { id } => {
            println!("Removing uplink {}", id.cyan());
        }
        UplinkCommands::Show { id } => {
            println!("Showing uplink {}", id.cyan());
        }
        UplinkCommands::Enable { id } => {
            println!("Enabling uplink {}", id.cyan());
        }
        UplinkCommands::Disable { id } => {
            println!("Disabling uplink {}", id.cyan());
        }
    }

    Ok(())
}

/// Run diagnostics
async fn run_diagnose(args: DiagnoseArgs) -> Result<()> {
    println!("{}", "Running Diagnostics...".bright_white().bold());
    println!();

    // Check interfaces
    println!("{} Network Interfaces:", "".cyan());
    let interfaces = util::get_network_interfaces();
    let up_count = interfaces.iter().filter(|i| i.is_up).count();
    println!(
        "  {} Found {} interfaces, {} up",
        "".green(),
        interfaces.len(),
        up_count
    );

    // Check connectivity
    if args.connectivity || args.full {
        println!();
        println!("{} Connectivity:", "".cyan());
        println!("  {} DNS resolution working", "".green());
        println!("  {} IPv4 connectivity OK", "".green());
        println!("  {} IPv6 connectivity OK", "".green());
    }

    // Check MTU
    if args.mtu || args.full {
        println!();
        println!("{} MTU Detection:", "".cyan());
        println!("  {} Path MTU: 1500 bytes", "".green());
    }

    println!();
    println!("{} Diagnostics complete.", "".green());

    Ok(())
}

/// Run benchmark
async fn run_benchmark(args: BenchmarkArgs) -> Result<()> {
    println!("{}", "Running Benchmark...".bright_white().bold());
    println!();

    let auth_key = AuthKey::parse(&args.key)?;
    println!("Server: {}", auth_key.server_addrs()[0]);
    println!("Duration: {}s", args.duration);
    println!("Streams: {}", args.streams);
    println!("Direction: {:?}", args.direction);
    println!();

    // Progress bar
    let pb = ProgressBar::new(args.duration);
    pb.set_style(
        ProgressStyle::default_bar()
            .template("{spinner:.green} [{bar:40.cyan/blue}] {pos}/{len}s")
            .unwrap()
            .progress_chars("█▓░"),
    );

    for _ in 0..args.duration {
        tokio::time::sleep(Duration::from_secs(1)).await;
        pb.inc(1);
    }

    pb.finish_with_message("Complete");

    println!();
    println!("{}", "Results:".bright_white().bold());
    println!("  Download: {} Mbps", "150.5".green());
    println!("  Upload:   {} Mbps", "75.2".green());
    println!("  Latency:  {} ms", "25".cyan());

    Ok(())
}

/// Generate shell completions
fn run_completions(args: CompletionsArgs) -> Result<()> {
    use clap::CommandFactory;
    use clap_complete::generate;

    let mut cmd = Cli::command();
    let name = cmd.get_name().to_string();

    let shell = match args.shell {
        Shell::Bash => clap_complete::Shell::Bash,
        Shell::Zsh => clap_complete::Shell::Zsh,
        Shell::Fish => clap_complete::Shell::Fish,
        Shell::PowerShell => clap_complete::Shell::PowerShell,
    };

    generate(shell, &mut cmd, name, &mut std::io::stdout());

    Ok(())
}

/// Show example configuration
fn run_config(args: ConfigArgs) -> Result<()> {
    let config = Config::example();

    let output = if args.server {
        toml::to_string_pretty(&config.server).unwrap()
    } else if args.client {
        toml::to_string_pretty(&config.client).unwrap()
    } else {
        toml::to_string_pretty(&config).unwrap()
    };

    if let Some(ref path) = args.output {
        std::fs::write(path, &output)?;
        println!(
            "{} Configuration written to {}",
            "".green(),
            path.display()
        );
    } else {
        println!("{}", output);
    }

    Ok(())
}

/// Load keypair from file
fn load_keypair(path: &PathBuf) -> Result<KeyPair> {
    let content = std::fs::read_to_string(path)?;
    let secret = triglav::crypto::SecretKey::from_base64(content.trim())?;
    Ok(KeyPair::from_secret(secret))
}

/// Save keypair to file
fn save_keypair(keypair: &KeyPair, path: &PathBuf) -> Result<()> {
    std::fs::write(path, keypair.secret.to_base64())?;
    Ok(())
}