mirror of
https://github.com/svenstaro/miniserve
synced 2024-07-08 20:05:56 +00:00
Add option to generate random 6-hexdigit routes
This commit is contained in:
parent
bf3777290d
commit
5c35d35afb
36
Cargo.lock
generated
36
Cargo.lock
generated
|
@ -243,6 +243,11 @@ dependencies = [
|
|||
"iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytesize"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.28"
|
||||
|
@ -459,6 +464,11 @@ name = "fnv"
|
|||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-cprng"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-zircon"
|
||||
version = "0.3.3"
|
||||
|
@ -679,8 +689,10 @@ dependencies = [
|
|||
"actix 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"actix-web 0.7.18 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"bytesize 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"nanoid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"yansi 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -753,6 +765,14 @@ dependencies = [
|
|||
"ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nanoid"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "net2"
|
||||
version = "0.2.33"
|
||||
|
@ -887,6 +907,18 @@ dependencies = [
|
|||
"proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"fuchsia-cprng 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.5.5"
|
||||
|
@ -1703,6 +1735,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum build_const 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39092a32794787acd8525ee150305ff051b0aa6cc2abaf193924f5ab05425f39"
|
||||
"checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d"
|
||||
"checksum bytes 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "40ade3d27603c2cb345eb0912aec461a6dec7e06a4ae48589904e808335c7afa"
|
||||
"checksum bytesize 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "716960a18f978640f25101b5cbf1c6f6b0d3192fab36a2d98ca96f0ecbe41010"
|
||||
"checksum cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4a8b715cb4597106ea87c7c84b2f1d452c7492033765df7f32651e66fcf749"
|
||||
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
|
||||
"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878"
|
||||
|
@ -1728,6 +1761,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1"
|
||||
"checksum flate2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2291c165c8e703ee54ef3055ad6188e3d51108e2ded18e9f2476e774fc5ad3d4"
|
||||
"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
|
||||
"checksum fuchsia-cprng 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "81f7f8eb465745ea9b02e2704612a9946a59fa40572086c6fd49d6ddcf30bf31"
|
||||
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
|
||||
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
|
||||
"checksum futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)" = "49e7653e374fe0d0c12de4250f0bdb60680b8c80eed558c5c7538eec9c89e21b"
|
||||
|
@ -1762,6 +1796,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)" = "71646331f2619b1026cc302f87a2b8b648d5c6dd6937846a16cc8ce0f347f432"
|
||||
"checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125"
|
||||
"checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919"
|
||||
"checksum nanoid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef553a0f07a7a45c731f0c5d83cf9ef9caddf7407e413142731db416504bfe0f"
|
||||
"checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88"
|
||||
"checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945"
|
||||
"checksum nom 4.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9c349f68f25f596b9f44cf0e7c69752a5c633b0550c3ff849518bfba0233774a"
|
||||
|
@ -1779,6 +1814,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09"
|
||||
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
|
||||
"checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c"
|
||||
"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
|
||||
"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c"
|
||||
"checksum rand 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "3906503e80ac6cbcacb2c2973fa8e473f24d7e2747c8c92bb230c2441cad96b5"
|
||||
"checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef"
|
||||
|
|
|
@ -27,3 +27,4 @@ base64 = "0.10"
|
|||
percent-encoding = "1.0.1"
|
||||
htmlescape = "0.3.1"
|
||||
bytesize = "1.0.0"
|
||||
nanoid = "0.2.0"
|
57
src/main.rs
57
src/main.rs
|
@ -14,6 +14,10 @@ use std::thread;
|
|||
use std::time::Duration;
|
||||
use yansi::{Color, Paint};
|
||||
|
||||
const ROUTE_ALPHABET: [char; 16] = [
|
||||
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f',
|
||||
];
|
||||
|
||||
enum BasicAuthError {
|
||||
Base64DecodeError,
|
||||
InvalidUsernameFormat,
|
||||
|
@ -34,6 +38,7 @@ pub struct MiniserveConfig {
|
|||
auth: Option<BasicAuthParams>,
|
||||
path_explicitly_chosen: bool,
|
||||
no_symlinks: bool,
|
||||
random_route: Option<String>,
|
||||
}
|
||||
|
||||
/// Decode a HTTP basic auth string into a tuple of username and password.
|
||||
|
@ -130,6 +135,11 @@ pub fn parse_args() -> MiniserveConfig {
|
|||
.help("Set authentication (username:password)")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("random-route")
|
||||
.long("random-route")
|
||||
.help("Generate a random route"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("no-symlinks")
|
||||
.short("P")
|
||||
|
@ -164,6 +174,11 @@ pub fn parse_args() -> MiniserveConfig {
|
|||
None
|
||||
};
|
||||
|
||||
let random_route = match matches.is_present("random-route") {
|
||||
true => Some(nanoid::custom(6, &ROUTE_ALPHABET)),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
MiniserveConfig {
|
||||
verbose,
|
||||
path: PathBuf::from(path.unwrap_or(".")),
|
||||
|
@ -172,6 +187,7 @@ pub fn parse_args() -> MiniserveConfig {
|
|||
auth,
|
||||
path_explicitly_chosen: path.is_some(),
|
||||
no_symlinks,
|
||||
random_route,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -183,7 +199,8 @@ fn file_handler(req: &HttpRequest<MiniserveConfig>) -> Result<fs::NamedFile> {
|
|||
fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> {
|
||||
let s = {
|
||||
let path = &app.state().path;
|
||||
let no_symlinks = app.state().no_symlinks;
|
||||
let no_symlinks = app.state().no_symlinks.clone();
|
||||
let random_route = app.state().random_route.clone();
|
||||
if path.is_file() {
|
||||
None
|
||||
} else {
|
||||
|
@ -192,16 +209,19 @@ fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> {
|
|||
.expect("Couldn't create path")
|
||||
.show_files_listing()
|
||||
.files_listing_renderer(move |dir, req| {
|
||||
directory_listing(dir, req, no_symlinks)
|
||||
directory_listing(dir, req, no_symlinks, random_route.clone())
|
||||
}),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let random_route = app.state().random_route.clone().unwrap_or(String::new());
|
||||
let full_route = format!("/{}", random_route);
|
||||
|
||||
if let Some(s) = s {
|
||||
app.handler("/", s)
|
||||
app.handler(&full_route, s)
|
||||
} else {
|
||||
app.resource("/", |r| r.f(file_handler))
|
||||
app.resource(&full_route, |r| r.f(file_handler))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -349,6 +369,18 @@ fn main() {
|
|||
))
|
||||
.bold()
|
||||
));
|
||||
let random_route = miniserve_config.clone().random_route;
|
||||
if random_route.is_some() {
|
||||
addresses.push_str(&format!(
|
||||
"{}",
|
||||
Color::Green
|
||||
.paint(format!(
|
||||
"/{random_route}",
|
||||
random_route = random_route.unwrap(),
|
||||
))
|
||||
.bold()
|
||||
));
|
||||
}
|
||||
}
|
||||
println!(
|
||||
"Serving path {path} at {addresses}",
|
||||
|
@ -365,17 +397,22 @@ fn directory_listing<S>(
|
|||
dir: &fs::Directory,
|
||||
req: &HttpRequest<S>,
|
||||
skip_symlinks: bool,
|
||||
random_route: Option<String>,
|
||||
) -> Result<HttpResponse, io::Error> {
|
||||
let index_of = format!("Index of {}", req.path());
|
||||
let mut body = String::new();
|
||||
let base = Path::new(req.path());
|
||||
let random_route = format!("/{}", random_route.unwrap_or(String::new()));
|
||||
|
||||
if let Some(parent) = base.parent() {
|
||||
let _ = write!(
|
||||
body,
|
||||
"<tr><td><a class=\"{}\" href=\"{}\">..</a></td><td></td></tr>",
|
||||
"root",
|
||||
parent.display()
|
||||
);
|
||||
if req.path() != random_route {
|
||||
let _ = write!(
|
||||
body,
|
||||
"<tr><td><a class=\"{}\" href=\"{}\">..</a></td><td></td></tr>",
|
||||
"root",
|
||||
parent.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for entry in dir.path.read_dir()? {
|
||||
|
|
Loading…
Reference in New Issue
Block a user