Browse Source

feat: add no-auth-read options

pull/8/head
sigoden 3 years ago
parent
commit
d9547ad00b
  1. 2
      README.md
  2. 18
      src/args.rs
  3. 3
      src/server.rs

2
README.md

@ -45,7 +45,7 @@ duf folder_name
Only serve static files, disable editing operations such as update or delete Only serve static files, disable editing operations such as update or delete
``` ```
duf --no-change duf --readonly
``` ```
Finally, run this command to see a list of all available option Finally, run this command to see a list of all available option

18
src/args.rs

@ -35,18 +35,23 @@ fn app() -> clap::Command<'static> {
.help("Path to a directory for serving files"), .help("Path to a directory for serving files"),
) )
.arg( .arg(
Arg::new("no-change") Arg::new("readonly")
.short('C') .short('r')
.long("no-change") .long("readonly")
.help("Disable change operations such as update or delete"), .help("Disable change operations such as update or delete"),
) )
.arg( .arg(
Arg::new("auth") Arg::new("auth")
.short('a') .short('a')
.long("auth") .long("auth")
.help("Authenticate with user and pass") .help("Use HTTP authentication for all operations")
.value_name("user:pass"), .value_name("user:pass"),
) )
.arg(
Arg::new("no-auth-read")
.long("no-auth-read")
.help("Do not authenticate read operations like static serving"),
)
.arg( .arg(
Arg::new("cors") Arg::new("cors")
.long("cors") .long("cors")
@ -65,6 +70,7 @@ pub struct Args {
pub path: PathBuf, pub path: PathBuf,
pub readonly: bool, pub readonly: bool,
pub auth: Option<String>, pub auth: Option<String>,
pub no_auth_read: bool,
pub cors: bool, pub cors: bool,
} }
@ -78,9 +84,10 @@ impl Args {
let port = matches.value_of_t::<u16>("port")?; let port = matches.value_of_t::<u16>("port")?;
let path = matches.value_of_os("path").unwrap_or_default(); let path = matches.value_of_os("path").unwrap_or_default();
let path = Args::parse_path(path)?; let path = Args::parse_path(path)?;
let readonly = matches.is_present("no-change"); let readonly = matches.is_present("readonly");
let cors = matches.is_present("cors"); let cors = matches.is_present("cors");
let auth = matches.value_of("auth").map(|v| v.to_owned()); let auth = matches.value_of("auth").map(|v| v.to_owned());
let no_auth_read = matches.is_present("no-auth-read");
Ok(Args { Ok(Args {
address, address,
@ -88,6 +95,7 @@ impl Args {
path, path,
readonly, readonly,
auth, auth,
no_auth_read,
cors, cors,
}) })
} }

3
src/server.rs

@ -319,6 +319,9 @@ impl InnerService {
let value = std::str::from_utf8(&value)?; let value = std::str::from_utf8(&value)?;
return Ok(value == auth); return Ok(value == auth);
} else { } else {
if self.args.no_auth_read && req.method() == Method::GET {
return Ok(true);
}
return Ok(false); return Ok(false);
} }
} }

Loading…
Cancel
Save