1use std::collections::HashMap;
7use std::net::SocketAddr;
8
9pub use lab_ops_lab_lib::TransportProtocol;
10use serde::Deserialize;
11use serde::Serialize;
12
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub struct DockerPortMapRequest {
16 pub host_addr: SocketAddr,
17 pub container_addr: SocketAddr,
18 pub proto: TransportProtocol,
19}
20
21impl DockerPortMapRequest {
22 pub fn is_ipv6(&self) -> bool {
24 self.host_addr.is_ipv6()
25 }
26}
27
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30pub struct DockerPortMap {
31 pub id: u64,
33 pub request: DockerPortMapRequest,
35 pub container_id: String,
37 pub container_name: String,
39 pub rule_comment: String,
41}
42
43impl DockerPortMap {
44 pub fn new(
48 id: u64,
49 request: DockerPortMapRequest,
50 container_id: String,
51 container_name: String,
52 ) -> Self {
53 let rule_comment = format!("natmap:{}:{}", container_id, request.host_addr.port());
54 Self {
55 id,
56 request,
57 container_id,
58 container_name,
59 rule_comment,
60 }
61 }
62}
63
64#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct DockerRemapRequest {
67 pub host_port: u16,
68 pub new_host_port: u16,
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize, Default)]
77pub struct DockerAddMapRequest {
78 #[serde(default = "default_host_ip")]
80 pub host_ip: String,
81 pub host_port: u16,
83 pub container_port: u16,
85 #[serde(default)]
88 pub target_ip: Option<String>,
89 #[serde(default = "default_proto")]
91 pub proto: TransportProtocol,
92}
93
94fn default_host_ip() -> String {
95 "0.0.0.0".to_string()
96}
97
98fn default_proto() -> TransportProtocol {
99 TransportProtocol::default()
100}
101
102#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
106pub struct DnatConfig {
107 pub ext_ip: String,
109 pub int_ip: String,
111 pub ports: String,
113 pub proto: TransportProtocol,
115 pub ext_if: Option<String>,
117 #[serde(default)]
118 pub no_masquerade: bool,
119}
120
121impl DnatConfig {
122 pub fn rule_comment(&self) -> String {
124 format!("natmap:dnat:{}:{}", self.ext_ip, self.ports)
125 }
126}
127
128#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
130pub struct SnatConfig {
131 pub int_ip: String,
133 pub ext_ip: String,
135 pub ext_if: String,
137}
138
139impl SnatConfig {
140 pub fn rule_comment(&self) -> String {
142 format!("natmap:snat:{}:{}", self.int_ip, self.ext_ip)
143 }
144}
145
146#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
148pub struct HairpinConfig {
149 pub ext_ip: String,
151 pub int_ip: String,
153 pub ports: String,
155 pub proto: TransportProtocol,
157}
158
159impl HairpinConfig {
160 pub fn rule_comment(&self) -> String {
162 format!(
163 "natmap:hairpin:{}:{}:{}",
164 self.ext_ip, self.int_ip, self.ports
165 )
166 }
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize)]
173pub struct DnatRequest {
174 pub ext_ip: String,
175 pub int_ip: String,
176 pub ports: String,
177 pub proto: TransportProtocol,
178 pub ext_if: Option<String>,
179 #[serde(default)]
180 pub no_masquerade: bool,
181}
182
183#[derive(Debug, Clone, Serialize, Deserialize)]
185pub struct SnatRequest {
186 pub int_ip: String,
187 pub ext_ip: String,
188 pub ext_if: String,
189}
190
191#[derive(Debug, Clone, Serialize, Deserialize)]
193pub struct HairpinRequest {
194 pub ext_ip: String,
195 pub int_ip: String,
196 pub ports: String,
197 pub proto: TransportProtocol,
198}
199
200#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
201pub struct PolicyRouteConfig {
202 pub src_ip: String,
203 pub via: String,
204 pub table: u32,
205}
206
207#[derive(Debug, Clone, Serialize, Deserialize)]
208pub struct PolicyRouteRequest {
209 pub src_ip: String,
210 pub via: String,
211 pub table: u32,
212}
213
214#[derive(Debug, Clone, Serialize, Deserialize, Default)]
218pub struct DaemonState {
219 pub mapping: HashMap<String, Vec<DockerPortMap>>,
221 pub dnats: Vec<DnatConfig>,
223 pub snats: Vec<SnatConfig>,
225 pub hairpins: Vec<HairpinConfig>,
227 #[serde(default)]
229 pub policy_routes: Vec<PolicyRouteConfig>,
230}
231
232#[derive(Debug, Clone, Serialize, Deserialize)]
234pub struct ListResponse {
235 pub docker: Vec<DockerPortMap>,
236 pub dnats: Vec<DnatConfig>,
237 pub snats: Vec<SnatConfig>,
238 pub hairpins: Vec<HairpinConfig>,
239 pub policy_routes: Vec<PolicyRouteConfig>,
240}