diff --git a/Cargo.toml b/Cargo.toml index 13be0de..495a09c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aliurl" -version = "0.3.0" +version = "0.3.1" edition = "2021" license = "MIT" repository = "https://github.com/ivabus/aliurl" @@ -12,4 +12,4 @@ rocket = "0.5.0-rc.3" serde = { version = "1.0.163", features = ["derive"] } serde_json = "1.0.96" url-escape = "0.1.1" -uuid = { version = "1.3.3", features = ["v4"] } +rand = "0.8.5" \ No newline at end of file diff --git a/README.md b/README.md index cd4ed2a..edc5be4 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ POST /api/create_alias HTTP/1.1 ```json { "url": "", - "alias": "", // If not provided, UUID will be generated + "alias": "", // If not provided, random string will be generated "access_key": "" // May not be provided, if no ./access_keys file "redirect_with_ad": "" //May not be provided, if provided will use ./redirect.html } diff --git a/Rocket.toml b/Rocket.toml index cd6b578..b51bf8c 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -1,11 +1,9 @@ -[release] +[default] address = "127.0.0.1" port = 8080 -workers = 8 -keep_alive = 5 -ident = "Rocket" -ip_header = "X-Real-IP" # set to `false` to disable +keep_alive = 0 +ident = "aliurl via Rocket" +ip_header = false log_level = "normal" temp_dir = "/tmp" cli_colors = true -ctrlc = false diff --git a/src/main.rs b/src/main.rs index 9c9dc06..dd7f6fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,9 @@ use rocket::response::Redirect; use serde::{Deserialize, Serialize}; static mut ACCESS_KEY_REQUIRED: bool = true; + +const LEN_OF_GENERATIVE_ALIASES: usize = 6; + const INDEX_REDIRECT: &'static str = "https://ivabus.dev"; const INDEX_WITH_AD: bool = true; diff --git a/src/post.rs b/src/post.rs index 7e206b6..2a7714e 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,4 +1,5 @@ use crate::*; +use rand::{distributions::Alphanumeric, Rng}; use rocket::http::{RawStr, Status}; use rocket::response::content::RawJson; use serde_json::json; @@ -60,11 +61,30 @@ pub fn create_alias(data: &RawStr) -> (Status, RawJson) { let mut aliases_list = read_aliases(); let mut file = std::fs::File::options().write(true).open("./alias.json").unwrap(); let alias = match data.alias { - None => uuid::Uuid::new_v4().to_string(), + None => { + let mut gen: String; + 'gen: loop { + gen = rand::thread_rng() + .sample_iter(&Alphanumeric) + .take(LEN_OF_GENERATIVE_ALIASES) + .map(char::from) + .collect(); + for i in &aliases_list { + if i.alias == gen { + continue 'gen; + } + } + break 'gen; + } + gen + } Some(alias) => alias, }; if alias.contains("?") { - return (Status::BadRequest, RawJson(json!({"Error": "No access key"}).to_string())); + return ( + Status::BadRequest, + RawJson(json!({"Error": "Alias should not contain \"?\""}).to_string()), + ); } let alia = if let Some(s) = data.redirect_with_ad { if s.to_lowercase() == "true" {