Add is_domain_available endpoint

This commit is contained in:
notplants 2021-05-25 15:05:24 +02:00
parent 01cda6f21e
commit 43e3a64143
3 changed files with 28 additions and 115 deletions

0
deploy_prod.sh Normal file → Executable file
View File

View File

@ -1,23 +0,0 @@
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(
name = "peach-dyndns-host",
rename_all = "kebab-case",
long_about = "\nTODO",
raw(setting = "structopt::clap::AppSettings::ColoredHelp")
)]
pub struct CliArgs {
#[structopt(flatten)]
log: clap_log_flag::Log,
#[structopt(flatten)]
verbose: clap_verbosity_flag::Verbosity,
}
pub fn args() -> Result<CliArgs, Box<dyn std::error::Error>> {
let args = CliArgs::from_args();
args.log.log_all(Some(args.verbose.log_level()))?;
Ok(args)
}

View File

@ -1,9 +1,9 @@
/*
* LIST OF ROUTES
* /domain/register (add a new domain and get back the TSIG key for subsequent updating with nsupdate)
* /domain/check-available (check if given domain is available)
* /user/register sends an email verification to create a new account) NOT IMPLEMENTED
* /user/verify (for clicking the link in the email) NOT IMPLEMENTED
* LIST OF METHODS
* register_domain (add a new domain and get back the TSIG key for subsequent updating with nsupdate)
* is_domain_available (check if given domain is available and return boolean result)
* register_user (sends an email verification to create a new account) NOT IMPLEMENTED
* verify_user (for clicking the link in the email) NOT IMPLEMENTED
*/
mod errors;
mod generate_zone;
@ -24,6 +24,11 @@ pub struct RegisterDomainPost {
domain: String,
}
#[derive(Deserialize, Debug)]
pub struct IsDomainAvailablePost {
domain: String,
}
/// Create JSON-RPC I/O handler, add RPC methods and launch HTTP server.
pub fn run() -> Result<(), BoxError> {
info!("Starting up.");
@ -45,6 +50,24 @@ pub fn run() -> Result<(), BoxError> {
}
});
io.add_method("is_domain_available", move |params: Params| {
let d: Result<IsDomainAvailablePost, Error> = params.parse();
match d {
Ok(d) => {
// if the domain has an invalid format return an erro
if !validate_domain(&d.domain) {
Err(Error::from(PeachDynDnsError::InvalidDomain{ domain: d.domain.to_string() }))
}
// if it has a valid format, check if its available
else {
let result = check_domain_available(&d.domain);
Ok(Value::Bool(result))
}
},
Err(e) => Err(Error::from(PeachDynDnsError::MissingParams { e })),
}
});
let http_server =
env::var("PEACH_DYNDNS_SERVER").unwrap_or_else(|_| "127.0.0.1:3001".to_string());
@ -64,90 +87,3 @@ pub fn run() -> Result<(), BoxError> {
Ok(())
}
//
//#[get("/")]
//pub fn index() -> &'static str {
// "This is the peach-dyndns server."
//}
//
//#[derive(Serialize)]
//pub struct JsonResponse {
// pub status: String,
// #[serde(skip_serializing_if = "Option::is_none")]
// pub data: Option<JsonValue>,
// #[serde(skip_serializing_if = "Option::is_none")]
// pub msg: Option<String>,
//}
//
//// helper function to build a JsonResponse object
//pub fn build_json_response(
// status: String,
// data: Option<JsonValue>,
// msg: Option<String>,
//) -> JsonResponse {
// JsonResponse { status, data, msg }
//}
//
//#[derive(Deserialize, Debug)]
//pub struct RegisterDomainPost {
// domain: String,
//}
//
//#[post("/domain/register", data = "<data>")]
//pub async fn register_domain(data: Json<RegisterDomainPost>) -> Json<JsonResponse> {
// info!("++ post request to register new domain: {:?}", data);
// // TODO: grab/create a mutex, so that only one rocket thread is calling register_domain at a time
// // check if its a valid domain
// if !validate_domain(&data.domain) {
// let status = "error".to_string();
// let msg = "domain is not in a valid format".to_string();
// Json(build_json_response(status, None, Some(msg)))
// } else {
// // check if the domain is available
// let is_domain_available = check_domain_available(&data.domain);
// if !is_domain_available {
// let status = "error".to_string();
// let msg = "can't register a domain that is already registered".to_string();
// Json(build_json_response(status, None, Some(msg)))
// } else {
// // generate configs for the zone
// let result = generate_zone(&data.domain);
// match result {
// Ok(key_file_text) => {
// let status = "success".to_string();
// let msg = key_file_text.to_string();
// Json(build_json_response(status, None, Some(msg)))
// }
// Err(_err) => {
// let status = "error".to_string();
// let msg = "there was an error creating the zone file".to_string();
// Json(build_json_response(status, None, Some(msg)))
// }
// }
// }
// }
//}
//
//#[derive(Deserialize, Debug)]
//pub struct CheckAvailableDomainPost {
// domain: String,
//}
//
//// route which returns a msg of "true" if the domain is available and "false" if it is already taken
//#[post("/domain/check-available", data = "<data>")]
//pub async fn check_available(data: Json<CheckAvailableDomainPost>) -> Json<JsonResponse> {
// info!("post request to check if domain is available {:?}", data);
// if !validate_domain(&data.domain) {
// let status = "error".to_string();
// let msg = "domain is not in a valid format".to_string();
// Json(build_json_response(status, None, Some(msg)))
// } else {
// let status = "success".to_string();
// let is_available = check_domain_available(&data.domain);
// let msg = is_available.to_string();
// Json(build_json_response(status, None, Some(msg)))
// }
//}