-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.rs
334 lines (290 loc) · 10.4 KB
/
main.rs
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
use rust_embed::{EmbeddedFile, RustEmbed};
use std::{
collections::HashMap,
convert::Infallible,
env,
net::IpAddr,
process,
str::FromStr,
sync::{Arc, RwLock},
};
use tokio::sync::broadcast::{Receiver, Sender};
use tokio::time::Duration;
use tokio_graceful_shutdown::{SubsystemHandle, Toplevel};
use tracing::{event, Level};
use tracing_subscriber::{prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt};
use types::{MailMessage, MessageId};
use crate::web_server::http_server;
mod mail_server;
mod types;
mod web_server;
pub struct AppState {
rx: Receiver<MailMessage>,
storage: RwLock<HashMap<MessageId, MailMessage>>,
prefix: String,
index: Option<String>,
}
#[derive(RustEmbed)]
#[folder = "../frontend/dist"]
pub struct Asset;
/// get a configuration from the environment or return default value
fn parse_env_var<T: FromStr>(name: &'static str, default: T) -> T {
env::var(name)
.unwrap_or_default()
.parse::<T>()
.unwrap_or(default)
}
fn load_index(path_prefix: &str) -> Option<String> {
let index: EmbeddedFile = Asset::get("index.html")?;
let index = String::from_utf8(index.data.to_vec()).ok()?;
let path_prefix = if path_prefix == "/" { "" } else { path_prefix };
// add path prefix to asset includes
Some(
index
.replace("href=\"/", &format!("href=\"{path_prefix}/static/"))
.replace(
"'/mailcrab-frontend",
&format!("'{path_prefix}/static/mailcrab-frontend"),
),
)
}
async fn storage(
mut storage_rx: Receiver<MailMessage>,
state: Arc<AppState>,
handle: SubsystemHandle,
) -> Result<(), Infallible> {
let mut running = true;
while running {
tokio::select! {
incoming = storage_rx.recv() => {
if let Ok(message) = incoming {
if let Ok(mut storage) = state.storage.write() {
storage.insert(message.id, message);
}
}
},
_ = handle.on_shutdown_requested() => {
running = false;
}
}
}
Ok(())
}
async fn mail_server(
smtp_host: IpAddr,
smtp_port: u16,
tx: Sender<MailMessage>,
enable_tls_auth: bool,
handle: SubsystemHandle,
) -> Result<(), Infallible> {
let task = tokio::task::spawn_blocking(move || {
if let Err(e) = mail_server::smtp_listen((smtp_host, smtp_port), tx, enable_tls_auth) {
event!(Level::ERROR, "MailCrab mail server error {e}");
}
});
tokio::select! {
_ = task => {},
_ = handle.on_shutdown_requested() => {}
};
Ok(())
}
#[tokio::main]
async fn main() {
// initialize logging
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
std::env::var("RUST_LOG")
.unwrap_or_else(|_| "mailcrab_backend=info,tower_http=info".into()),
))
.with(tracing_subscriber::fmt::layer())
.init();
let smtp_host: IpAddr = parse_env_var("SMTP_HOST", [0, 0, 0, 0].into());
let http_host: IpAddr = parse_env_var("HTTP_HOST", [127, 0, 0, 1].into());
let smtp_port: u16 = parse_env_var("SMTP_PORT", 1025);
let http_port: u16 = parse_env_var("HTTP_PORT", 1080);
// Enable auth implicitly enable TLS
let enable_tls_auth: bool = std::env::var("ENABLE_TLS_AUTH").map_or_else(
|_| false,
|v| v.to_ascii_lowercase().parse().unwrap_or(false),
);
// construct path prefix
let prefix = std::env::var("MAILCRAB_PREFIX").unwrap_or_default();
let prefix = format!("/{}", prefix.trim_matches('/'));
event!(
Level::INFO,
"MailCrab HTTP server starting on {http_host}:{http_port} and SMTP server on {smtp_host}:{smtp_port}"
);
// initialize internal broadcast queue
let (tx, rx) = tokio::sync::broadcast::channel::<MailMessage>(16);
let storage_rx = rx.resubscribe();
let app_state = Arc::new(AppState {
rx,
storage: Default::default(),
index: load_index(&prefix),
prefix,
});
// store broadcasted messages in a key/value store
let state = app_state.clone();
let result = Toplevel::new()
.start("Storage server", move |h| storage(storage_rx, state, h))
.start("Mail server", move |h| {
mail_server(smtp_host, smtp_port, tx, enable_tls_auth, h)
})
.start("Web server", move |h| {
http_server(http_host, http_port, app_state, h)
})
.catch_signals()
.handle_shutdown_requests(Duration::from_millis(5000))
.await;
let exit_code = match result {
Err(e) => {
event!(Level::ERROR, "MailCrab error {e}");
// failure
1
}
_ => {
event!(Level::INFO, "Thank you for using MailCrab!");
// success
0
}
};
process::exit(exit_code);
}
#[cfg(test)]
mod test {
use crate::parse_env_var;
use crate::types::MailMessageMetadata;
use fake::faker::company::en::{Buzzword, CatchPhase};
use fake::faker::internet::en::SafeEmail;
use fake::faker::lorem::en::Paragraph;
use fake::faker::name::en::Name;
use fake::Fake;
use lettre::address::Envelope;
use lettre::message::header::ContentType;
use lettre::message::{Attachment, MultiPart, SinglePart};
use lettre::{Address, Message, SmtpTransport, Transport};
use std::process::{Command, Stdio};
use tokio::time::{sleep, Duration};
fn send_message(
with_html: bool,
with_plain: bool,
with_attachment: bool,
) -> Result<Message, Box<dyn std::error::Error>> {
let smtp_port: u16 = parse_env_var("SMTP_PORT", 1025);
let mailer = SmtpTransport::builder_dangerous("127.0.0.1".to_string())
.port(smtp_port)
.build();
let to: String = SafeEmail().fake();
let to_name: String = Name().fake();
let from: String = SafeEmail().fake();
let from_name: String = Name().fake();
let body: String = [
Paragraph(2..3).fake::<String>(),
Paragraph(2..3).fake::<String>(),
Paragraph(2..3).fake::<String>(),
]
.join("\n");
let html: String = format!(
"{}\n<p><a href=\"https://github.com/tweedegolf/mailcrab\">external link</a></p>",
body.replace("\n", "<br>\n")
);
let builder = Message::builder()
.from(format!("{from_name} <{from}>",).parse()?)
.to(format!("{to_name} <{to}>").parse()?)
.subject(CatchPhase().fake::<String>());
let mut multipart = MultiPart::mixed().build();
match (with_html, with_plain) {
(true, true) => {
multipart = multipart.multipart(
MultiPart::alternative()
.singlepart(SinglePart::plain(body))
.singlepart(SinglePart::html(html)),
);
}
(false, true) => {
multipart = multipart.singlepart(SinglePart::plain(body));
}
(true, false) => {
multipart = multipart.singlepart(SinglePart::html(html));
}
_ => panic!("Email should have html or plain body"),
};
if with_attachment {
let filebody = std::fs::read("blank.pdf")?;
let content_type = ContentType::parse("application/pdf")?;
let filename = format!("{}.pdf", Buzzword().fake::<&str>().to_ascii_lowercase());
let attachment = Attachment::new(filename).body(filebody.clone(), content_type.clone());
multipart = multipart.singlepart(attachment);
}
let email = builder.multipart(multipart)?;
mailer.send(&email)?;
Ok(email)
}
async fn test_receive_messages() -> Result<Vec<MailMessageMetadata>, Box<dyn std::error::Error>>
{
send_message(true, true, false)?;
sleep(Duration::from_millis(1500)).await;
send_message(true, false, false)?;
sleep(Duration::from_millis(1500)).await;
send_message(false, true, true)?;
let http_port: u16 = parse_env_var("HTTP_PORT", 1080);
let mails: Vec<MailMessageMetadata> =
reqwest::get(format!("http://127.0.0.1:{http_port}/api/messages"))
.await?
.json()
.await?;
Ok(mails)
}
#[tokio::test]
async fn receive_message() {
let mut cmd = Command::new("cargo")
.arg("run")
.stdout(Stdio::inherit())
.spawn()
.unwrap();
// wait for mailcrab to startup
sleep(Duration::from_millis(20_000)).await;
let messages = test_receive_messages().await;
cmd.kill().unwrap();
let messages = messages.unwrap();
assert_eq!(messages.len(), 3);
assert!(messages[0].has_html);
assert!(messages[0].has_plain);
assert!(messages[0].attachments.is_empty());
assert!(messages[1].has_html);
assert!(!messages[1].has_plain);
assert!(messages[1].attachments.is_empty());
assert!(!messages[2].has_html);
assert!(messages[2].has_plain);
assert_eq!(messages[2].attachments.len(), 1);
}
#[tokio::test]
#[ignore]
async fn send_sample_messages() {
let smtp_port: u16 = parse_env_var("SMTP_PORT", 1025);
let mut paths = std::fs::read_dir("../samples").unwrap();
let mailer = SmtpTransport::builder_dangerous("127.0.0.1".to_string())
.port(smtp_port)
.build();
while let Some(Ok(entry)) = paths.next() {
let message = std::fs::read_to_string(entry.path()).unwrap();
let mut lines = message.lines();
let sender = lines
.next()
.unwrap()
.trim_start_matches("Sender: ")
.parse::<Address>()
.unwrap();
let recipients = lines
.next()
.unwrap()
.trim_start_matches("Recipients: ")
.split(',')
.map(|r| r.trim().parse::<Address>().unwrap())
.collect::<Vec<Address>>();
let envelope = Envelope::new(Some(sender), recipients).unwrap();
let email = lines.collect::<Vec<&str>>().join("\n");
mailer.send_raw(&envelope, email.as_bytes()).unwrap();
}
}
}