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}
118
119impl DnatConfig {
120 pub fn rule_comment(&self) -> String {
122 format!("natmap:dnat:{}:{}", self.ext_ip, self.ports)
123 }
124}
125
126#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
128pub struct SnatConfig {
129 pub int_ip: String,
131 pub ext_ip: String,
133 pub ext_if: String,
135}
136
137impl SnatConfig {
138 pub fn rule_comment(&self) -> String {
140 format!("natmap:snat:{}:{}", self.int_ip, self.ext_ip)
141 }
142}
143
144#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
146pub struct HairpinConfig {
147 pub ext_ip: String,
149 pub int_ip: String,
151 pub ports: String,
153 pub proto: TransportProtocol,
155}
156
157impl HairpinConfig {
158 pub fn rule_comment(&self) -> String {
160 format!(
161 "natmap:hairpin:{}:{}:{}",
162 self.ext_ip, self.int_ip, self.ports
163 )
164 }
165}
166
167#[derive(Debug, Clone, Serialize, Deserialize)]
171pub struct DnatRequest {
172 pub ext_ip: String,
173 pub int_ip: String,
174 pub ports: String,
175 pub proto: TransportProtocol,
176 pub ext_if: Option<String>,
177}
178
179#[derive(Debug, Clone, Serialize, Deserialize)]
181pub struct SnatRequest {
182 pub int_ip: String,
183 pub ext_ip: String,
184 pub ext_if: String,
185}
186
187#[derive(Debug, Clone, Serialize, Deserialize)]
189pub struct HairpinRequest {
190 pub ext_ip: String,
191 pub int_ip: String,
192 pub ports: String,
193 pub proto: TransportProtocol,
194}
195
196#[derive(Debug, Clone, Serialize, Deserialize, Default)]
200pub struct DaemonState {
201 pub mapping: HashMap<String, Vec<DockerPortMap>>,
203 pub dnats: Vec<DnatConfig>,
205 pub snats: Vec<SnatConfig>,
207 pub hairpins: Vec<HairpinConfig>,
209}
210
211#[derive(Debug, Clone, Serialize, Deserialize)]
213pub struct ListResponse {
214 pub docker: Vec<DockerPortMap>,
215 pub dnats: Vec<DnatConfig>,
216 pub snats: Vec<SnatConfig>,
217 pub hairpins: Vec<HairpinConfig>,
218}