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
|
use std::io::{self, IsTerminal, Write};
use std::net::{IpAddr, SocketAddr, TcpListener};
use std::thread;
use std::time::Duration;
use actix_files::NamedFile;
use actix_web::{
dev::{fn_service, ServiceRequest, ServiceResponse},
http::header::ContentType,
middleware, web, App, HttpRequest, HttpResponse, Responder,
};
use actix_web_httpauth::middleware::HttpAuthentication;
use anyhow::Result;
use clap::{crate_version, CommandFactory, Parser};
use colored::*;
use fast_qr::QRBuilder;
use log::{error, warn};
mod archive;
mod args;
mod auth;
mod config;
mod consts;
mod errors;
mod file_op;
mod file_utils;
mod listing;
mod pipe;
mod renderer;
use crate::config::MiniserveConfig;
use crate::errors::{RuntimeError, StartupError};
static STYLESHEET: &str = grass::include!("data/style.scss");
fn main() -> Result<()> {
let args = args::CliArgs::parse();
if let Some(shell) = args.print_completions {
let mut clap_app = args::CliArgs::command();
let app_name = clap_app.get_name().to_string();
clap_complete::generate(shell, &mut clap_app, app_name, &mut io::stdout());
return Ok(());
}
if args.print_manpage {
let clap_app = args::CliArgs::command();
let man = clap_mangen::Man::new(clap_app);
man.render(&mut io::stdout())?;
return Ok(());
}
let miniserve_config = MiniserveConfig::try_from_args(args)?;
run(miniserve_config).map_err(|e| {
errors::log_error_chain(e.to_string());
e
})?;
Ok(())
}
#[actix_web::main(miniserve)]
async fn run(miniserve_config: MiniserveConfig) -> Result<(), StartupError> {
let log_level = if miniserve_config.verbose {
simplelog::LevelFilter::Info
} else {
simplelog::LevelFilter::Warn
};
simplelog::TermLogger::init(
log_level,
simplelog::ConfigBuilder::new()
.set_time_format_rfc2822()
.build(),
simplelog::TerminalMode::Mixed,
if io::stdout().is_terminal() {
simplelog::ColorChoice::Auto
} else {
simplelog::ColorChoice::Never
},
)
.or_else(|_| simplelog::SimpleLogger::init(log_level, simplelog::Config::default()))
.expect("Couldn't initialize logger");
if miniserve_config.no_symlinks && miniserve_config.path.is_symlink() {
return Err(StartupError::NoSymlinksOptionWithSymlinkServePath(
miniserve_config.path.to_string_lossy().to_string(),
));
}
let inside_config = miniserve_config.clone();
let canon_path = miniserve_config
.path
.canonicalize()
.map_err(|e| StartupError::IoError("Failed to resolve path to be served".to_string(), e))?;
// warn if --index is specified but not found
if let Some(ref index) = miniserve_config.index {
if !canon_path.join(index).exists() {
warn!(
"The file '{}' provided for option --index could not be found.",
index.to_string_lossy(),
);
}
}
let path_string = canon_path.to_string_lossy();
println!(
"{name} v{version}",
name = "miniserve".bold(),
version = crate_version!()
);
if !miniserve_config.path_explicitly_chosen {
// If the path to serve has NOT been explicitly chosen and if this is NOT an interactive
// terminal, we should refuse to start for security reasons. This would be the case when
// running miniserve as a service but forgetting to set the path. This could be pretty
// dangerous if given with an undesired context path (for instance /root or /).
if !io::stdout().is_terminal() {
return Err(StartupError::NoExplicitPathAndNoTerminal);
}
warn!("miniserve has been invoked without an explicit path so it will serve the current directory after a short delay.");
warn!(
"Invoke with -h|--help to see options or invoke as `miniserve .` to hide this advice."
);
print!("Starting server in ");
io::stdout()
.flush()
.map_err(|e| StartupError::IoError("Failed to write data".to_string(), e))?;
for c in "3… 2… 1… \n".chars() {
print!("{c}");
io::stdout()
.flush()
.map_err(|e| StartupError::IoError("Failed to write data".to_string(), e))?;
thread::sleep(Duration::from_millis(500));
}
}
let display_urls = {
let (mut ifaces, wildcard): (Vec<_>, Vec<_>) = miniserve_config
.interfaces
.clone()
.into_iter()
.partition(|addr| !addr.is_unspecified());
// Replace wildcard addresses with local interface addresses
if !wildcard.is_empty() {
let all_ipv4 = wildcard.iter().any(|addr| addr.is_ipv4());
let all_ipv6 = wildcard.iter().any(|addr| addr.is_ipv6());
ifaces = if_addrs::get_if_addrs()
.unwrap_or_else(|e| {
error!("Failed to get local interface addresses: {}", e);
Default::default()
})
.into_iter()
.map(|iface| iface.ip())
.filter(|ip| (all_ipv4 && ip.is_ipv4()) || (all_ipv6 && ip.is_ipv6()))
.collect();
ifaces.sort();
}
ifaces
.into_iter()
.map(|addr| match addr {
IpAddr::V4(_) => format!("{}:{}", addr, miniserve_config.port),
IpAddr::V6(_) => format!("[{}]:{}", addr, miniserve_config.port),
})
.map(|addr| match miniserve_config.tls_rustls_config {
Some(_) => format!("https://{addr}"),
None => format!("http://{addr}"),
})
.map(|url| format!("{}{}", url, miniserve_config.route_prefix))
.collect::<Vec<_>>()
};
let socket_addresses = miniserve_config
.interfaces
.iter()
.map(|&interface| SocketAddr::new(interface, miniserve_config.port))
.collect::<Vec<_>>();
let display_sockets = socket_addresses
.iter()
.map(|sock| sock.to_string().green().bold().to_string())
.collect::<Vec<_>>();
let stylesheet = web::Data::new(
[
STYLESHEET,
inside_config.default_color_scheme.css(),
inside_config.default_color_scheme_dark.css_dark().as_str(),
]
.join("\n"),
);
let srv = actix_web::HttpServer::new(move || {
App::new()
.wrap(configure_header(&inside_config.clone()))
.app_data(inside_config.clone())
.app_data(stylesheet.clone())
.wrap_fn(errors::error_page_middleware)
.wrap(middleware::Logger::default())
.wrap(middleware::Condition::new(
miniserve_config.compress_response,
middleware::Compress::default(),
))
.route(&inside_config.favicon_route, web::get().to(favicon))
.route(&inside_config.css_route, web::get().to(css))
.service(
web::scope(&inside_config.route_prefix)
.wrap(middleware::Condition::new(
!inside_config.auth.is_empty(),
actix_web::middleware::Compat::new(HttpAuthentication::basic(
auth::handle_auth,
)),
))
.configure(|c| configure_app(c, &inside_config)),
)
.default_service(web::get().to(error_404))
});
let srv = socket_addresses.iter().try_fold(srv, |srv, addr| {
let listener = create_tcp_listener(*addr)
.map_err(|e| StartupError::IoError(format!("Failed to bind server to {addr}"), e))?;
#[cfg(feature = "tls")]
let srv = match &miniserve_config.tls_rustls_config {
Some(tls_config) => srv.listen_rustls(listener, tls_config.clone()),
None => srv.listen(listener),
};
#[cfg(not(feature = "tls"))]
let srv = srv.listen(listener);
srv.map_err(|e| StartupError::IoError(format!("Failed to bind server to {addr}"), e))
})?;
let srv = srv.shutdown_timeout(0).run();
println!("Bound to {}", display_sockets.join(", "));
println!("Serving path {}", path_string.yellow().bold());
println!(
"Available at (non-exhaustive list):\n {}\n",
display_urls
.iter()
.map(|url| url.green().bold().to_string())
.collect::<Vec<_>>()
.join("\n "),
);
// print QR code to terminal
if miniserve_config.show_qrcode && io::stdout().is_terminal() {
for url in display_urls
.iter()
.filter(|url| !url.contains("//127.0.0.1:") && !url.contains("//[::1]:"))
{
match QRBuilder::new(url.clone()).ecl(consts::QR_EC_LEVEL).build() {
Ok(qr) => {
println!("QR code for {}:", url.green().bold());
qr.print();
}
Err(e) => {
error!("Failed to render QR to terminal: {:?}", e);
}
};
}
}
if io::stdout().is_terminal() {
println!("Quit by pressing CTRL-C");
}
srv.await
.map_err(|e| StartupError::IoError("".to_owned(), e))
}
/// Allows us to set low-level socket options
///
/// This mainly used to set `set_only_v6` socket option
/// to get a consistent behavior across platforms.
/// see: https://github.com/svenstaro/miniserve/pull/500
fn create_tcp_listener(addr: SocketAddr) -> io::Result<TcpListener> {
use socket2::{Domain, Protocol, Socket, Type};
let socket = Socket::new(Domain::for_address(addr), Type::STREAM, Some(Protocol::TCP))?;
if addr.is_ipv6() {
socket.set_only_v6(true)?;
}
socket.set_reuse_address(true)?;
socket.bind(&addr.into())?;
socket.listen(1024 /* Default backlog */)?;
Ok(TcpListener::from(socket))
}
fn configure_header(conf: &MiniserveConfig) -> middleware::DefaultHeaders {
conf.header.iter().flatten().fold(
middleware::DefaultHeaders::new(),
|headers, (header_name, header_value)| headers.add((header_name, header_value)),
)
}
/// Configures the Actix application
///
/// This is where we configure the app to serve an index file, the file listing, or a single file.
fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) {
let dir_service = || {
let mut files = actix_files::Files::new("", &conf.path);
// Use specific index file if one was provided.
if let Some(ref index_file) = conf.index {
files = files.index_file(index_file.to_string_lossy());
// Handle SPA option.
//
// Note: --spa requires --index in clap.
if conf.spa {
files = files.default_handler(
NamedFile::open(conf.path.join(index_file))
.expect("Can't open SPA index file."),
);
}
}
// Handle --pretty-urls options.
//
// We rewrite the request to append ".html" to the path and serve the file. If the
// path ends with a `/`, we remove it before appending ".html".
//
// This is done to allow for pretty URLs, e.g. "/about" instead of "/about.html".
if conf.pretty_urls {
files = files.default_handler(fn_service(|req: ServiceRequest| async {
let (req, _) = req.into_parts();
let conf = req
.app_data::<MiniserveConfig>()
.expect("Could not get miniserve config");
let mut path_base = req.path()[1..].to_string();
if path_base.ends_with('/') {
path_base.pop();
}
if !path_base.ends_with("html") {
path_base = format!("{}.html", path_base);
}
let file = NamedFile::open_async(conf.path.join(path_base)).await?;
let res = file.into_response(&req);
Ok(ServiceResponse::new(req, res))
}));
}
if conf.show_hidden {
files = files.use_hidden_files();
}
let base_path = conf.path.clone();
let no_symlinks = conf.no_symlinks;
files
.show_files_listing()
.files_listing_renderer(listing::directory_listing)
.prefer_utf8(true)
.redirect_to_slash_directory()
.path_filter(move |path, _| {
// deny symlinks if conf.no_symlinks
!(no_symlinks && base_path.join(path).is_symlink())
})
};
if conf.path.is_file() {
// Handle single files
app.service(web::resource(["", "/"]).route(web::to(listing::file_handler)));
} else {
if conf.file_upload {
// Allow file upload
app.service(web::resource("/upload").route(web::post().to(file_op::upload_file)));
}
// Handle directories
app.service(dir_service());
}
}
async fn error_404(req: HttpRequest) -> Result<HttpResponse, RuntimeError> {
Err(RuntimeError::RouteNotFoundError(req.path().to_string()))
}
async fn favicon() -> impl Responder {
let logo = include_str!("../data/logo.svg");
HttpResponse::Ok()
.insert_header(ContentType(mime::IMAGE_SVG))
.body(logo)
}
async fn css(stylesheet: web::Data<String>) -> impl Responder {
HttpResponse::Ok()
.insert_header(ContentType(mime::TEXT_CSS))
.body(stylesheet.to_string())
}
|