tunnet-core 0.7.0

Core mesh runtime for Tunnet (identity, routing, streams, sync)
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
//! Agent IPC v2 - newline-delimited JSON request/response over a local socket.
//!
//! Every `tunnet <subcommand>` that is not bootstrap (`enroll` / `reset`) talks
//! to the running agent through this protocol. The agent owns CoreNode; the CLI
//! process stays unprivileged and finishes in milliseconds.

use std::net::Ipv4Addr;

use serde::{Deserialize, Serialize};

/// Wire protocol version - bump when breaking request/response shapes.
pub const IPC_VERSION: u32 = 2;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum IpcRequest {
    /// Legacy: open a bidirectional byte stream to a peer:port (FD handoff).
    OpenStream {
        host: String,
        port: u16,
    },
    /// Legacy: list mesh peers.
    ListPeers,

    /// Agent + network health summary. When `peers` is true, include peer table.
    Status {
        #[serde(default)]
        peers: bool,
    },
    DnsStatus,
    RouteList,
    RouteAdd {
        cidr: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        description: Option<String>,
    },
    Ping {
        peer: String,
        #[serde(default = "default_ping_count")]
        count: u32,
        #[serde(default = "default_ping_interval_ms")]
        interval_ms: u64,
    },
    Diag,
    Netcheck,

    ServeStart {
        port: u16,
        #[serde(default = "default_https")]
        protocol: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        certificate_pem: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        private_key_pem: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        internal_hostname: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        serve_id: Option<String>,
    },
    ServeStatus,
    ServeOff {
        port: u16,
    },

    TunnelStart {
        port: u16,
        #[serde(default = "default_https")]
        protocol: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        relay: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        subdomain: Option<String>,
    },
    TunnelStatus,
    TunnelOff {
        port: u16,
    },

    /// List SSH sessions from the control plane.
    SshSessions {
        #[serde(default = "default_ssh_list_limit")]
        limit: u32,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        status: Option<String>,
    },
    /// List SSH recordings from the control plane.
    SshRecordings {
        #[serde(default = "default_ssh_list_limit")]
        limit: u32,
    },
    /// Fetch a recording cast by session id.
    SshPlay {
        session_id: String,
    },

    /// Poll a check-mode re-auth challenge for a proof token.
    SshAuthPoll {
        challenge_token: String,
    },

    /// Send a file or directory to a peer (hostname / IP / endpoint id / `tag:name`).
    SendFile {
        path: String,
        target: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        message: Option<String>,
    },
    SendAccept {
        transfer_id: String,
    },
    SendReject {
        transfer_id: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        reason: Option<String>,
    },
    SendList,
    SendHistory,
    SendConfig,
    SendSetConfig {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        consent: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        inbox_path: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        pin_blobs: Option<bool>,
    },

    /// Whether TUN + system DNS/routes are active.
    DataPlaneStatus,
    /// Bring TUN + DNS + routes up (daemon must already be running).
    DataPlaneUp,
    /// Tear down TUN + DNS + routes; keep mesh/docs/IPC alive.
    DataPlaneDown,

    /// Direct: create invite code (coordinator).
    DirectInvite {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
        #[serde(default)]
        reusable: bool,
        #[serde(default = "default_invite_expires")]
        expires: String,
    },
    DirectRequests {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
    },
    DirectAccept {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
        peer_id: String,
    },
    DirectDeny {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
        peer_id: String,
    },
    DirectKick {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
        peer_id: String,
    },
    DirectFirewallShow {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
    },
    DirectFirewallOff {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
    },
    DirectFirewallAdd {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
        direction: String,
        action: String,
        #[serde(default = "default_fw_protocol")]
        protocol: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        port: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        peer: Option<String>,
    },
    DirectFirewallRemove {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
        index: usize,
    },
    DirectFirewallReset {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
    },
    DirectFirewallFlushConntrack {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
    },
    DirectFirewallPending {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
    },
    DirectFirewallAcceptSuggestion {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
    },
    DirectFirewallRejectSuggestion {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
    },
    DirectPolicyShow {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
    },
    DirectPolicySet {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
        /// TOML contents of a policy file (global rules + optional per-hostname).
        toml: String,
    },
    DirectPolicyClear {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
    },
    DirectKeepAlive {
        hostname: String,
        #[serde(default = "default_true")]
        enable: bool,
    },
    /// Manual IP override for birthday collisions.
    DirectOverrideIp {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        network: Option<String>,
        peer: String,
        ip: String,
    },
    DirectConnect {
        contact_id: String,
    },
    DirectConnectAllow {
        contact_id: String,
    },
    DirectConnectPending,
    DirectConnectAccept {
        contact_id: String,
    },
    DirectConnectDeny {
        contact_id: String,
    },
    DirectConnectRotate,

    /// Reload firewall / DNS / logging / keep-alive from tunnet.toml without dropping connections.
    Reload,
}

fn default_invite_expires() -> String {
    "24h".into()
}

fn default_ssh_list_limit() -> u32 {
    50
}

fn default_ping_count() -> u32 {
    4
}
fn default_ping_interval_ms() -> u64 {
    1000
}
fn default_https() -> String {
    "https".into()
}

fn default_fw_protocol() -> String {
    "tcp".into()
}

fn default_true() -> bool {
    true
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[allow(clippy::large_enum_variant)]
pub enum IpcResponse {
    Ready,
    Peers {
        peers: Vec<PeerLite>,
    },
    Status(StatusInfo),
    DnsStatus(DnsStatusInfo),
    Routes(RoutesInfo),
    RouteAdded {
        cidr: String,
    },
    /// Streaming-style: one PingProbe per round, then PingSummary.
    PingProbe(PingProbe),
    PingSummary(PingSummary),
    Diag(DiagInfo),
    Netcheck(NetcheckInfo),
    Serve(ServeInfo),
    Serves {
        serves: Vec<ServeInfo>,
    },
    Tunnel(TunnelInfo),
    Tunnels {
        tunnels: Vec<TunnelInfo>,
    },
    SshSessions {
        sessions: Vec<SshSessionInfo>,
    },
    SshRecordings {
        recordings: Vec<SshRecordingInfo>,
    },
    SshCast {
        session_id: String,
        cast_text: String,
        content_sha256: String,
    },
    SshAuthPoll {
        status: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        proof_token: Option<String>,
    },
    Transfer(TransferInfo),
    Transfers {
        transfers: Vec<TransferInfo>,
    },
    SendConfig(SendConfigInfo),
    DataPlane {
        up: bool,
    },
    DirectInvite {
        code: String,
    },
    DirectPending {
        requests: Vec<DirectPendingInfo>,
    },
    DirectFirewall {
        enabled: bool,
        rules: Vec<DirectFirewallRuleInfo>,
        #[serde(default)]
        conntrack_entries: usize,
        #[serde(default)]
        packets_allowed: u64,
        #[serde(default)]
        packets_denied: u64,
        #[serde(default)]
        packets_rejected: u64,
        #[serde(default)]
        suggested_rules: usize,
    },
    DirectFirewallPending {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        pending: Option<String>,
    },
    DirectPolicy {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        json: Option<String>,
    },
    DirectConnectPending {
        requests: Vec<DirectConnectPendingInfo>,
    },
    DirectContact {
        contact_id: String,
    },
    Ok {
        message: String,
    },
    Error {
        message: String,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DirectConnectPendingInfo {
    pub contact_id: String,
    pub endpoint_id: String,
    pub hostname: String,
    pub received_at: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DirectPendingInfo {
    pub endpoint_id: String,
    pub hostname: String,
    pub ipv4: String,
    pub collision_index: u8,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DirectFirewallRuleInfo {
    pub index: usize,
    pub direction: String,
    pub action: String,
    pub protocol: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ports: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub peer: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SshSessionInfo {
    pub id: String,
    pub src_endpoint_id: String,
    pub dst_endpoint_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub src_hostname: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dst_hostname: Option<String>,
    pub target_user: String,
    pub status: String,
    pub recorded: bool,
    pub started_at: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub duration_ms: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SshRecordingInfo {
    pub session_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub src_hostname: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dst_hostname: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target_user: Option<String>,
    pub byte_size: u64,
    pub created_at: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content_sha256: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerLite {
    pub ip: String,
    pub hostname: String,
    pub endpoint_id: String,
    pub tags: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub online: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub latency_ms: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub os: Option<String>,
    /// connected | suspended | reconnecting
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub conn_state: Option<String>,
    /// direct | relay | unknown
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bytes_in: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bytes_out: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_seen_secs_ago: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub keep_alive: Option<bool>,
    /// OpenSSH public host key line when advertised by the peer.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ssh_host_key: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatusInfo {
    pub ip: String,
    pub hostname: String,
    pub network_name: String,
    pub network_id: String,
    pub organization_id: String,
    pub endpoint_id: String,
    pub peers_total: usize,
    pub peers_online: usize,
    pub relay_status: String,
    pub uptime_secs: u64,
    pub agent_version: String,
    pub snapshot_version: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub peers: Option<Vec<PeerLite>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mode: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub data_plane_up: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub keep_alive: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub firewall_drops: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub conntrack_entries: Option<usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub on_demand: Option<OnDemandStatusInfo>,
    /// RFC3339 timestamp when this machine is deleted after inactivity.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<String>,
    /// Seconds until `expires_at`, when auto-expiry is enabled.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expires_in_secs: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OnDemandStatusInfo {
    pub reconnect_attempts: u64,
    pub reconnect_success: u64,
    pub reconnect_fail: u64,
    pub packets_buffered: u64,
    pub packets_dropped_timeout: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DnsStatusInfo {
    pub suffix: String,
    pub upstream: Vec<String>,
    pub peer_dns_active: bool,
    pub cached_entries: usize,
    pub synthetic_base: String,
    pub magic_ip: String,
    pub bind: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutesInfo {
    pub subnet_routes: Vec<SubnetRouteInfo>,
    pub hostname_routes: Vec<HostnameRouteInfo>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub exit_node: Option<ExitNodeRouteInfo>,
    pub split_tunnel_mode: String,
    pub split_tunnel_cidrs: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubnetRouteInfo {
    pub cidr: String,
    pub via_hostname: String,
    pub via_ip: String,
    pub via_endpoint_id: String,
    pub advertised_by_self: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HostnameRouteInfo {
    pub hostname: String,
    pub is_wildcard: bool,
    pub via_hostname: String,
    pub via_ip: String,
    pub via_endpoint_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target_ip: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExitNodeRouteInfo {
    pub hostname: String,
    pub via_ip: String,
    pub endpoint_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PingProbe {
    pub seq: u32,
    pub peer: String,
    pub peer_ip: String,
    pub latency_ms: f64,
    pub path: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PingSummary {
    pub peer: String,
    pub peer_ip: String,
    pub transmitted: u32,
    pub received: u32,
    pub loss_pct: f64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min_ms: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub avg_ms: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_ms: Option<f64>,
    pub path: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiagInfo {
    pub nat_type: String,
    pub endpoint_id: String,
    pub endpoint_online: bool,
    pub relay_reachable: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub relay_rtt_ms: Option<f64>,
    pub direct_peers: usize,
    pub relayed_peers: usize,
    pub total_peers: usize,
    pub notes: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetcheckInfo {
    pub ok: bool,
    pub checks: Vec<NetcheckItem>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetcheckItem {
    pub name: String,
    pub pass: bool,
    pub detail: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServeInfo {
    pub id: String,
    pub port: u16,
    pub protocol: String,
    pub url: String,
    pub status: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TunnelInfo {
    pub id: String,
    pub port: u16,
    pub protocol: String,
    pub public_url: String,
    pub relay: String,
    pub status: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransferInfo {
    pub transfer_id: String,
    pub direction: String,
    pub peer_endpoint_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub peer_hostname: Option<String>,
    pub file_name: String,
    pub size: u64,
    pub hash: String,
    pub status: String,
    pub percent: f32,
    pub bytes_transferred: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub inbox_path: Option<String>,
    pub is_directory: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SendConfigInfo {
    pub consent: String,
    pub inbox_path: String,
    pub pin_blobs: bool,
}

/// Convenience: self IPv4 as string for status.
pub fn ip_str(ip: Ipv4Addr) -> String {
    ip.to_string()
}