Skip to main content

zlayer_types/api/
tunnels.rs

1//! Tunnel management API DTOs.
2//!
3//! Wire types for the tunnel management endpoints.
4
5use serde::{Deserialize, Serialize};
6use utoipa::ToSchema;
7
8/// Request to create a new tunnel token
9#[derive(Debug, Deserialize, ToSchema)]
10pub struct CreateTunnelRequest {
11    /// Name for this tunnel (for identification)
12    pub name: String,
13    /// Optional list of services this tunnel is allowed to expose
14    #[serde(default)]
15    pub services: Vec<String>,
16    /// Time-to-live in seconds (default: 24 hours)
17    #[serde(default = "default_ttl")]
18    pub ttl_secs: u64,
19}
20
21fn default_ttl() -> u64 {
22    86400 // 24 hours
23}
24
25/// Response after creating a tunnel token
26#[derive(Debug, Serialize, Deserialize, ToSchema)]
27pub struct CreateTunnelResponse {
28    /// Unique tunnel identifier
29    pub id: String,
30    /// Name of the tunnel
31    pub name: String,
32    /// The tunnel token to use for authentication
33    pub token: String,
34    /// Services this tunnel can expose
35    pub services: Vec<String>,
36    /// When the token expires (Unix timestamp)
37    pub expires_at: u64,
38    /// When the tunnel was created (Unix timestamp)
39    pub created_at: u64,
40}
41
42/// Tunnel summary for list operations
43#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
44pub struct TunnelSummary {
45    /// Unique tunnel identifier
46    pub id: String,
47    /// Name of the tunnel
48    pub name: String,
49    /// Current status (active, disconnected, expired)
50    pub status: String,
51    /// Services this tunnel can expose
52    pub services: Vec<String>,
53    /// When the tunnel was created (Unix timestamp)
54    pub created_at: u64,
55    /// When the token expires (Unix timestamp)
56    pub expires_at: u64,
57    /// Last time the tunnel connected (Unix timestamp, if ever)
58    pub last_connected: Option<u64>,
59}
60
61/// Detailed tunnel status
62#[derive(Debug, Serialize, Deserialize, ToSchema)]
63pub struct TunnelStatus {
64    /// Unique tunnel identifier
65    pub id: String,
66    /// Name of the tunnel
67    pub name: String,
68    /// Current status (active, disconnected, expired)
69    pub status: String,
70    /// Services this tunnel can expose
71    pub allowed_services: Vec<String>,
72    /// Currently registered services
73    pub registered_services: Vec<RegisteredServiceInfo>,
74    /// When the tunnel was created (Unix timestamp)
75    pub created_at: u64,
76    /// When the token expires (Unix timestamp)
77    pub expires_at: u64,
78    /// Last time the tunnel connected (Unix timestamp, if ever)
79    pub last_connected: Option<u64>,
80    /// Client IP address (if connected)
81    pub client_addr: Option<String>,
82    /// Number of active connections
83    pub active_connections: u32,
84}
85
86/// Information about a registered service in a tunnel
87#[derive(Debug, Serialize, Deserialize, ToSchema)]
88pub struct RegisteredServiceInfo {
89    /// Service name
90    pub name: String,
91    /// Service ID
92    pub service_id: String,
93    /// Protocol (tcp or udp)
94    pub protocol: String,
95    /// Local port on the client
96    pub local_port: u16,
97    /// Remote port exposed on the server
98    pub remote_port: u16,
99    /// Current status
100    pub status: String,
101}
102
103/// Request to create a node-to-node tunnel
104#[derive(Debug, Deserialize, ToSchema)]
105pub struct CreateNodeTunnelRequest {
106    /// Name for this tunnel
107    pub name: String,
108    /// Source node ID
109    pub from_node: String,
110    /// Destination node ID
111    pub to_node: String,
112    /// Local port on the source node
113    pub local_port: u16,
114    /// Remote port on the destination node
115    pub remote_port: u16,
116    /// Exposure level (public, internal)
117    #[serde(default = "default_expose")]
118    pub expose: String,
119}
120
121fn default_expose() -> String {
122    "internal".to_string()
123}
124
125/// Response after creating a node-to-node tunnel
126#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
127pub struct CreateNodeTunnelResponse {
128    /// Tunnel name
129    pub name: String,
130    /// Source node ID
131    pub from_node: String,
132    /// Destination node ID
133    pub to_node: String,
134    /// Local port
135    pub local_port: u16,
136    /// Remote port
137    pub remote_port: u16,
138    /// Exposure level
139    pub expose: String,
140    /// Current status
141    pub status: String,
142}
143
144/// Generic success response
145#[derive(Debug, Serialize, ToSchema)]
146pub struct SuccessResponse {
147    /// Success message
148    pub message: String,
149}