rns-cli 0.2.2

CLI tools for the Reticulum Network Stack
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
//! rnpath - Display and manage Reticulum path table
//!
//! Connects to a running rnsd via RPC to query/modify the path table.

use std::path::Path;
use std::process;

use rns_cli::args::Args;
use rns_cli::format::{prettyfrequency, prettyhexrep, prettytime};
use rns_net::config;
use rns_net::pickle::PickleValue;
use rns_net::rpc::derive_auth_key;
use rns_net::storage;
use rns_net::{RpcAddr, RpcClient};

const VERSION: &str = env!("FULL_VERSION");

fn main() {
    let args = Args::parse();

    if args.has("version") {
        println!("rnpath {}", VERSION);
        return;
    }

    if args.has("help") || args.has("h") {
        print_usage();
        return;
    }

    env_logger::Builder::new()
        .filter_level(match args.verbosity {
            0 => log::LevelFilter::Warn,
            1 => log::LevelFilter::Info,
            _ => log::LevelFilter::Debug,
        })
        .format_timestamp_secs()
        .init();

    let config_path = args.config_path().map(|s| s.to_string());
    let show_table = args.has("t");
    let show_rates = args.has("r");
    let drop_hash = args.get("d").map(|s| s.to_string());
    let drop_via = args.get("x").map(|s| s.to_string());
    let drop_queues = args.has("D");
    let json_output = args.has("j");
    let max_hops: Option<u8> = args.get("m").and_then(|s| s.parse().ok());
    let show_blackholed = args.has("blackholed") || args.has("b");
    let blackhole_hash = args.get("B").map(|s| s.to_string());
    let unblackhole_hash = args.get("U").map(|s| s.to_string());
    let duration_hours: Option<f64> = args.get("duration").and_then(|s| s.parse().ok());
    let reason = args.get("reason").map(|s| s.to_string());
    let remote_hash = args.get("R").map(|s| s.to_string());

    // Remote management query via -R flag
    if let Some(ref hash_str) = remote_hash {
        remote_path(hash_str, config_path.as_deref());
        return;
    }

    // Load config
    let config_dir =
        storage::resolve_config_dir(config_path.as_ref().map(|s| Path::new(s.as_str())));
    let config_file = config_dir.join("config");
    let rns_config = if config_file.exists() {
        match config::parse_file(&config_file) {
            Ok(c) => c,
            Err(e) => {
                eprintln!("Error reading config: {}", e);
                process::exit(1);
            }
        }
    } else {
        match config::parse("") {
            Ok(c) => c,
            Err(e) => {
                eprintln!("Error: {}", e);
                process::exit(1);
            }
        }
    };

    let paths = match storage::ensure_storage_dirs(&config_dir) {
        Ok(p) => p,
        Err(e) => {
            eprintln!("Error: {}", e);
            process::exit(1);
        }
    };

    let identity = match storage::load_or_create_identity(&paths.identities) {
        Ok(id) => id,
        Err(e) => {
            eprintln!("Error loading identity: {}", e);
            process::exit(1);
        }
    };

    let auth_key = derive_auth_key(&identity.get_private_key().unwrap_or([0u8; 64]));

    let rpc_port = rns_config.reticulum.instance_control_port;
    let rpc_addr = RpcAddr::Tcp("127.0.0.1".into(), rpc_port);

    let mut client = match RpcClient::connect(&rpc_addr, &auth_key) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("Could not connect to rnsd: {}", e);
            process::exit(1);
        }
    };

    if show_table {
        show_path_table(&mut client, json_output, max_hops);
    } else if show_rates {
        show_rate_table(&mut client, json_output);
    } else if let Some(hash_str) = blackhole_hash {
        do_blackhole(&mut client, &hash_str, duration_hours, reason);
    } else if let Some(hash_str) = unblackhole_hash {
        do_unblackhole(&mut client, &hash_str);
    } else if show_blackholed {
        show_blackholed_list(&mut client);
    } else if let Some(hash_str) = drop_hash {
        drop_path(&mut client, &hash_str);
    } else if let Some(hash_str) = drop_via {
        drop_all_via(&mut client, &hash_str);
    } else if drop_queues {
        drop_announce_queues(&mut client);
    } else if let Some(hash_str) = args.positional.first() {
        lookup_path(&mut client, hash_str);
    } else {
        print_usage();
    }
}

fn parse_hex_hash(s: &str) -> Option<Vec<u8>> {
    let s = s.trim();
    if s.len() % 2 != 0 {
        return None;
    }
    let mut bytes = Vec::with_capacity(s.len() / 2);
    for i in (0..s.len()).step_by(2) {
        match u8::from_str_radix(&s[i..i + 2], 16) {
            Ok(b) => bytes.push(b),
            Err(_) => return None,
        }
    }
    Some(bytes)
}

fn show_path_table(client: &mut RpcClient, _json_output: bool, max_hops: Option<u8>) {
    let max_hops_val = match max_hops {
        Some(h) => PickleValue::Int(h as i64),
        None => PickleValue::None,
    };

    let response = match client.call(&PickleValue::Dict(vec![
        (
            PickleValue::String("get".into()),
            PickleValue::String("path_table".into()),
        ),
        (PickleValue::String("max_hops".into()), max_hops_val),
    ])) {
        Ok(r) => r,
        Err(e) => {
            eprintln!("RPC error: {}", e);
            process::exit(1);
        }
    };

    if let Some(entries) = response.as_list() {
        if entries.is_empty() {
            println!("Path table is empty");
            return;
        }
        println!(
            "{:<34} {:>6} {:<34} {:<10} {}",
            "Destination", "Hops", "Via", "Expires", "Interface"
        );
        println!("{}", "-".repeat(100));
        for entry in entries {
            let hash = entry
                .get("hash")
                .and_then(|v| v.as_bytes())
                .map(prettyhexrep)
                .unwrap_or_default();
            let hops = entry.get("hops").and_then(|v| v.as_int()).unwrap_or(0);
            let via = entry
                .get("via")
                .and_then(|v| v.as_bytes())
                .map(prettyhexrep)
                .unwrap_or_default();
            let expires = entry
                .get("expires")
                .and_then(|v| v.as_float())
                .map(|e| {
                    let remaining = e - rns_net::time::now();
                    if remaining > 0.0 {
                        prettytime(remaining)
                    } else {
                        "expired".into()
                    }
                })
                .unwrap_or_default();
            let interface = entry
                .get("interface")
                .and_then(|v| v.as_str())
                .unwrap_or("");

            println!(
                "{:<34} {:>6} {:<34} {:<10} {}",
                &hash[..hash.len().min(32)],
                hops,
                &via[..via.len().min(32)],
                expires,
                interface,
            );
        }
    } else {
        eprintln!("Unexpected response format");
    }
}

fn show_rate_table(client: &mut RpcClient, _json_output: bool) {
    let response = match client.call(&PickleValue::Dict(vec![(
        PickleValue::String("get".into()),
        PickleValue::String("rate_table".into()),
    )])) {
        Ok(r) => r,
        Err(e) => {
            eprintln!("RPC error: {}", e);
            process::exit(1);
        }
    };

    if let Some(entries) = response.as_list() {
        if entries.is_empty() {
            println!("Rate table is empty");
            return;
        }
        println!(
            "{:<34} {:>12} {:>12} {:>16}",
            "Destination", "Violations", "Frequency", "Blocked Until"
        );
        println!("{}", "-".repeat(78));
        for entry in entries {
            let hash = entry
                .get("hash")
                .and_then(|v| v.as_bytes())
                .map(prettyhexrep)
                .unwrap_or_default();
            let violations = entry
                .get("rate_violations")
                .and_then(|v| v.as_int())
                .unwrap_or(0);
            let blocked = entry
                .get("blocked_until")
                .and_then(|v| v.as_float())
                .map(|b| {
                    let remaining = b - rns_net::time::now();
                    if remaining > 0.0 {
                        prettytime(remaining)
                    } else {
                        "not blocked".into()
                    }
                })
                .unwrap_or_default();

            // Compute hourly frequency from timestamps
            let freq_str =
                if let Some(timestamps) = entry.get("timestamps").and_then(|v| v.as_list()) {
                    let ts: Vec<f64> = timestamps.iter().filter_map(|v| v.as_float()).collect();
                    if ts.len() >= 2 {
                        let span = ts[ts.len() - 1] - ts[0];
                        if span > 0.0 {
                            let freq_per_sec = (ts.len() - 1) as f64 / span;
                            prettyfrequency(freq_per_sec)
                        } else {
                            "none".into()
                        }
                    } else {
                        "none".into()
                    }
                } else {
                    "none".into()
                };

            println!(
                "{:<34} {:>12} {:>12} {:>16}",
                &hash[..hash.len().min(32)],
                violations,
                freq_str,
                blocked,
            );
        }
    }
}

fn show_blackholed_list(client: &mut RpcClient) {
    let response = match client.call(&PickleValue::Dict(vec![(
        PickleValue::String("get".into()),
        PickleValue::String("blackholed".into()),
    )])) {
        Ok(r) => r,
        Err(e) => {
            eprintln!("RPC error: {}", e);
            process::exit(1);
        }
    };

    if let Some(entries) = response.as_list() {
        if entries.is_empty() {
            println!("Blackhole list is empty");
            return;
        }
        println!("{:<34} {:<16} {}", "Identity Hash", "Expires", "Reason");
        println!("{}", "-".repeat(70));
        for entry in entries {
            let hash = entry
                .get("identity_hash")
                .and_then(|v| v.as_bytes())
                .map(prettyhexrep)
                .unwrap_or_default();
            let expires = entry
                .get("expires")
                .and_then(|v| v.as_float())
                .map(|e| {
                    if e == 0.0 {
                        "never".into()
                    } else {
                        let remaining = e - rns_net::time::now();
                        if remaining > 0.0 {
                            prettytime(remaining)
                        } else {
                            "expired".into()
                        }
                    }
                })
                .unwrap_or_default();
            let reason = entry.get("reason").and_then(|v| v.as_str()).unwrap_or("-");

            println!(
                "{:<34} {:<16} {}",
                &hash[..hash.len().min(32)],
                expires,
                reason,
            );
        }
    } else {
        eprintln!("Unexpected response format");
    }
}

fn do_blackhole(
    client: &mut RpcClient,
    hash_str: &str,
    duration_hours: Option<f64>,
    reason: Option<String>,
) {
    let hash_bytes = match parse_hex_hash(hash_str) {
        Some(b) if b.len() >= 16 => b,
        _ => {
            eprintln!("Invalid identity hash: {}", hash_str);
            process::exit(1);
        }
    };

    let mut dict = vec![(
        PickleValue::String("blackhole".into()),
        PickleValue::Bytes(hash_bytes[..16].to_vec()),
    )];
    if let Some(d) = duration_hours {
        dict.push((
            PickleValue::String("duration".into()),
            PickleValue::Float(d),
        ));
    }
    if let Some(r) = reason {
        dict.push((PickleValue::String("reason".into()), PickleValue::String(r)));
    }

    match client.call(&PickleValue::Dict(dict)) {
        Ok(r) => {
            if r.as_bool() == Some(true) {
                println!("Blackholed identity {}", prettyhexrep(&hash_bytes[..16]));
            } else {
                eprintln!("Failed to blackhole identity");
            }
        }
        Err(e) => {
            eprintln!("RPC error: {}", e);
            process::exit(1);
        }
    }
}

fn do_unblackhole(client: &mut RpcClient, hash_str: &str) {
    let hash_bytes = match parse_hex_hash(hash_str) {
        Some(b) if b.len() >= 16 => b,
        _ => {
            eprintln!("Invalid identity hash: {}", hash_str);
            process::exit(1);
        }
    };

    match client.call(&PickleValue::Dict(vec![(
        PickleValue::String("unblackhole".into()),
        PickleValue::Bytes(hash_bytes[..16].to_vec()),
    )])) {
        Ok(r) => {
            if r.as_bool() == Some(true) {
                println!(
                    "Removed {} from blackhole list",
                    prettyhexrep(&hash_bytes[..16])
                );
            } else {
                println!(
                    "Identity {} was not blackholed",
                    prettyhexrep(&hash_bytes[..16])
                );
            }
        }
        Err(e) => {
            eprintln!("RPC error: {}", e);
            process::exit(1);
        }
    }
}

fn lookup_path(client: &mut RpcClient, hash_str: &str) {
    let hash_bytes = match parse_hex_hash(hash_str) {
        Some(b) if b.len() >= 16 => b,
        _ => {
            eprintln!("Invalid destination hash: {}", hash_str);
            process::exit(1);
        }
    };

    let mut dest_hash = [0u8; 16];
    dest_hash.copy_from_slice(&hash_bytes[..16]);

    // Query next hop
    let response = match client.call(&PickleValue::Dict(vec![
        (
            PickleValue::String("get".into()),
            PickleValue::String("next_hop".into()),
        ),
        (
            PickleValue::String("destination_hash".into()),
            PickleValue::Bytes(dest_hash.to_vec()),
        ),
    ])) {
        Ok(r) => r,
        Err(e) => {
            eprintln!("RPC error: {}", e);
            process::exit(1);
        }
    };

    if let Some(next_hop) = response.as_bytes() {
        println!("Path to {} found", prettyhexrep(&dest_hash));
        println!("  Next hop: {}", prettyhexrep(next_hop));
    } else {
        println!("No path found for {}", prettyhexrep(&dest_hash));
    }
}

fn drop_path(client: &mut RpcClient, hash_str: &str) {
    let hash_bytes = match parse_hex_hash(hash_str) {
        Some(b) if b.len() >= 16 => b,
        _ => {
            eprintln!("Invalid destination hash: {}", hash_str);
            process::exit(1);
        }
    };

    let mut dest_hash = [0u8; 16];
    dest_hash.copy_from_slice(&hash_bytes[..16]);

    let response = match client.call(&PickleValue::Dict(vec![
        (
            PickleValue::String("drop".into()),
            PickleValue::String("path".into()),
        ),
        (
            PickleValue::String("destination_hash".into()),
            PickleValue::Bytes(dest_hash.to_vec()),
        ),
    ])) {
        Ok(r) => r,
        Err(e) => {
            eprintln!("RPC error: {}", e);
            process::exit(1);
        }
    };

    if response.as_bool() == Some(true) {
        println!("Dropped path for {}", prettyhexrep(&dest_hash));
    } else {
        println!("No path found for {}", prettyhexrep(&dest_hash));
    }
}

fn drop_all_via(client: &mut RpcClient, hash_str: &str) {
    let hash_bytes = match parse_hex_hash(hash_str) {
        Some(b) if b.len() >= 16 => b,
        _ => {
            eprintln!("Invalid transport hash: {}", hash_str);
            process::exit(1);
        }
    };

    let mut transport_hash = [0u8; 16];
    transport_hash.copy_from_slice(&hash_bytes[..16]);

    let response = match client.call(&PickleValue::Dict(vec![
        (
            PickleValue::String("drop".into()),
            PickleValue::String("all_via".into()),
        ),
        (
            PickleValue::String("destination_hash".into()),
            PickleValue::Bytes(transport_hash.to_vec()),
        ),
    ])) {
        Ok(r) => r,
        Err(e) => {
            eprintln!("RPC error: {}", e);
            process::exit(1);
        }
    };

    if let Some(n) = response.as_int() {
        println!("Dropped {} paths via {}", n, prettyhexrep(&transport_hash));
    }
}

fn drop_announce_queues(client: &mut RpcClient) {
    match client.call(&PickleValue::Dict(vec![(
        PickleValue::String("drop".into()),
        PickleValue::String("announce_queues".into()),
    )])) {
        Ok(_) => println!("Announce queues dropped"),
        Err(e) => {
            eprintln!("RPC error: {}", e);
            process::exit(1);
        }
    }
}

fn remote_path(hash_str: &str, config_path: Option<&str>) {
    let dest_hash = match rns_cli::remote::parse_hex_hash(hash_str) {
        Some(h) => h,
        None => {
            eprintln!(
                "Invalid destination hash: {} (expected 32 hex chars)",
                hash_str
            );
            process::exit(1);
        }
    };

    eprintln!(
        "Remote management query to {} (not yet fully implemented)",
        prettyhexrep(&dest_hash),
    );
    eprintln!("Requires an active link to the remote management destination.");
    eprintln!("This feature will work once rnsd is running and the remote node is reachable.");

    let _ = (dest_hash, config_path);
}

fn print_usage() {
    println!("Usage: rnpath [OPTIONS] [DESTINATION_HASH]");
    println!();
    println!("Options:");
    println!("  --config PATH, -c PATH  Path to config directory");
    println!("  -t                      Show path table");
    println!("  -m HOPS                 Filter path table by max hops");
    println!("  -r                      Show rate table");
    println!("  -d HASH                 Drop path for destination");
    println!("  -x HASH                 Drop all paths via transport");
    println!("  -D                      Drop all announce queues");
    println!("  -b                      Show blackholed identities");
    println!("  -B HASH                 Blackhole an identity");
    println!("  -U HASH                 Remove identity from blackhole list");
    println!("  --duration HOURS        Blackhole duration (default: permanent)");
    println!("  --reason TEXT           Reason for blackholing");
    println!("  -R HASH                 Query remote node via management link");
    println!("  -j                      JSON output");
    println!("  -v                      Increase verbosity");
    println!("  --version               Print version and exit");
    println!("  --help, -h              Print this help");
}