backend: put_file + props for generate
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
use rocket::fs::NamedFile;
|
use rocket::fs::NamedFile;
|
||||||
|
use rocket::fs::TempFile;
|
||||||
use rocket::http::Method;
|
use rocket::http::Method;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::response::Redirect;
|
use rocket::response::Redirect;
|
||||||
use rocket::response::{Responder, status};
|
use rocket::response::{Responder, status};
|
||||||
use rocket::serde::{Deserialize, json::Json};
|
use rocket::serde::{Deserialize, json::Json};
|
||||||
use rocket::uri;
|
use rocket::uri;
|
||||||
use rocket::{self, get, launch, post, routes};
|
use rocket::{self, get, launch, post, put, routes};
|
||||||
use rocket_cors::{AllowedOrigins, CorsOptions};
|
use rocket_cors::{AllowedOrigins, CorsOptions};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
@@ -16,6 +17,12 @@ use std::path::PathBuf;
|
|||||||
struct GenerationRequest<'r> {
|
struct GenerationRequest<'r> {
|
||||||
directory: &'r str,
|
directory: &'r str,
|
||||||
common_name: &'r str,
|
common_name: &'r str,
|
||||||
|
#[serde(default)]
|
||||||
|
email: &'r str,
|
||||||
|
#[serde(default)]
|
||||||
|
days: u32,
|
||||||
|
#[serde(default)]
|
||||||
|
use_openssl: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Responder)]
|
#[derive(Responder)]
|
||||||
@@ -90,12 +97,11 @@ async fn list_directories() -> Result<Json<Vec<String>>, status::Custom<String>>
|
|||||||
let mut directories = Vec::new();
|
let mut directories = Vec::new();
|
||||||
while let Ok(Some(entry)) = reader.next_entry().await {
|
while let Ok(Some(entry)) = reader.next_entry().await {
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
if check_is_valid_directory(&path).await.is_ok() {
|
if check_is_valid_directory(&path).await.is_ok()
|
||||||
if let Some(name) = path.file_name() {
|
&& let Some(name) = path.file_name() {
|
||||||
directories.push(name.to_str().unwrap().to_string())
|
directories.push(name.to_str().unwrap().to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Json(directories))
|
Ok(Json(directories))
|
||||||
}
|
}
|
||||||
@@ -156,6 +162,37 @@ async fn get_file(directory: &str, file: &str) -> Result<NamedFile, status::NotF
|
|||||||
.map_err(|e| status::NotFound(e.to_string()))
|
.map_err(|e| status::NotFound(e.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[put("/put/<directory>/<name>", format = "plain", data = "<file>")]
|
||||||
|
async fn put_file(directory: &str, name: &str, mut file: TempFile<'_>) -> status::Custom<String> {
|
||||||
|
let dir = Path::new(&get_base_directory()).join(directory);
|
||||||
|
if check_is_valid_directory(&dir).await.is_err() {
|
||||||
|
return status::Custom(
|
||||||
|
Status::BadRequest,
|
||||||
|
"The specified directory is not valid".into(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let dir = dir.join("config");
|
||||||
|
let path = dir.join(name);
|
||||||
|
|
||||||
|
// check if the file exists
|
||||||
|
match tokio::fs::metadata(&path).await {
|
||||||
|
Ok(meta) if meta.is_file() => {}
|
||||||
|
_ => {
|
||||||
|
return status::Custom(Status::NotFound, "The specified file is not found".into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(msg) = file.persist_to(&path).await {
|
||||||
|
return status::Custom(
|
||||||
|
Status::InternalServerError,
|
||||||
|
format!("Failed to write file: {}", msg),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
status::Custom(Status::NoContent, "".into())
|
||||||
|
}
|
||||||
|
|
||||||
#[post("/generate", data = "<request>")]
|
#[post("/generate", data = "<request>")]
|
||||||
async fn generate(request: Json<GenerationRequest<'_>>) -> Result<NamedFile, GenerationError> {
|
async fn generate(request: Json<GenerationRequest<'_>>) -> Result<NamedFile, GenerationError> {
|
||||||
let dir = Path::new(&get_base_directory()).join(request.directory);
|
let dir = Path::new(&get_base_directory()).join(request.directory);
|
||||||
@@ -163,12 +200,25 @@ async fn generate(request: Json<GenerationRequest<'_>>) -> Result<NamedFile, Gen
|
|||||||
|
|
||||||
let generator_bin = env::var("GENERATOR_BIN").unwrap_or("peazyrsa".into());
|
let generator_bin = env::var("GENERATOR_BIN").unwrap_or("peazyrsa".into());
|
||||||
let mut cmd = tokio::process::Command::new(generator_bin);
|
let mut cmd = tokio::process::Command::new(generator_bin);
|
||||||
if env::var("USE_OPENSSL").unwrap_or("no".into()) == "yes" {
|
|
||||||
|
let mut use_openssl = env::var("USE_OPENSSL").unwrap_or("no".into()) == "yes";
|
||||||
|
if let Some(req_use_openssl) = request.use_openssl {
|
||||||
|
use_openssl = req_use_openssl;
|
||||||
|
}
|
||||||
|
if use_openssl {
|
||||||
let openssl_bin = env::var("OPENSSL_BIN").unwrap_or("openssl".into());
|
let openssl_bin = env::var("OPENSSL_BIN").unwrap_or("openssl".into());
|
||||||
cmd.arg("--with-openssl").arg(openssl_bin);
|
cmd.arg("--with-openssl").arg(openssl_bin);
|
||||||
}
|
}
|
||||||
cmd.arg("-d").arg(&dir).arg(request.common_name);
|
cmd.arg("-d").arg(&dir).arg(request.common_name);
|
||||||
|
|
||||||
|
if !request.email.is_empty() {
|
||||||
|
cmd.arg("--email").arg(request.email);
|
||||||
|
}
|
||||||
|
|
||||||
|
if request.days > 0 {
|
||||||
|
cmd.arg("--days").arg(request.days.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
// execute the command and check error code
|
// execute the command and check error code
|
||||||
let status = cmd
|
let status = cmd
|
||||||
.status()
|
.status()
|
||||||
@@ -225,7 +275,7 @@ fn rocket() -> _ {
|
|||||||
let cors = CorsOptions::default()
|
let cors = CorsOptions::default()
|
||||||
.allowed_origins(AllowedOrigins::all())
|
.allowed_origins(AllowedOrigins::all())
|
||||||
.allowed_methods(
|
.allowed_methods(
|
||||||
vec![Method::Get, Method::Post]
|
vec![Method::Get, Method::Post, Method::Put]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(From::from)
|
.map(From::from)
|
||||||
.collect(),
|
.collect(),
|
||||||
@@ -235,7 +285,13 @@ fn rocket() -> _ {
|
|||||||
rocket::build()
|
rocket::build()
|
||||||
.mount(
|
.mount(
|
||||||
"/api/v1",
|
"/api/v1",
|
||||||
routes![list_directories, list_directory, get_file, generate],
|
routes![
|
||||||
|
list_directories,
|
||||||
|
list_directory,
|
||||||
|
get_file,
|
||||||
|
put_file,
|
||||||
|
generate
|
||||||
|
],
|
||||||
)
|
)
|
||||||
.attach(cors.to_cors().unwrap())
|
.attach(cors.to_cors().unwrap())
|
||||||
.mount("/", routes![index_redirect, frontend])
|
.mount("/", routes![index_redirect, frontend])
|
||||||
|
|||||||
Reference in New Issue
Block a user