0.3.1: Generate random strings instead of UUIDs in create_alias, if alias is not provided

This commit is contained in:
Ivan Bushchik 2023-06-08 09:11:05 +03:00
parent 5042f009cb
commit df4c1b4fa6
No known key found for this signature in database
GPG key ID: 9F6DDABE11A2674D
5 changed files with 32 additions and 11 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "aliurl" name = "aliurl"
version = "0.3.0" version = "0.3.1"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/ivabus/aliurl" repository = "https://github.com/ivabus/aliurl"
@ -12,4 +12,4 @@ rocket = "0.5.0-rc.3"
serde = { version = "1.0.163", features = ["derive"] } serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96" serde_json = "1.0.96"
url-escape = "0.1.1" url-escape = "0.1.1"
uuid = { version = "1.3.3", features = ["v4"] } rand = "0.8.5"

View file

@ -39,7 +39,7 @@ POST /api/create_alias HTTP/1.1
```json ```json
{ {
"url": "<URL_TO_BE_ALIASED>", "url": "<URL_TO_BE_ALIASED>",
"alias": "<ALIAS_URI>", // If not provided, UUID will be generated "alias": "<ALIAS_URI>", // If not provided, random string will be generated
"access_key": "<ACCESS_KEY>" // May not be provided, if no ./access_keys file "access_key": "<ACCESS_KEY>" // May not be provided, if no ./access_keys file
"redirect_with_ad": "<BOOL>" //May not be provided, if provided will use ./redirect.html "redirect_with_ad": "<BOOL>" //May not be provided, if provided will use ./redirect.html
} }

View file

@ -1,11 +1,9 @@
[release] [default]
address = "127.0.0.1" address = "127.0.0.1"
port = 8080 port = 8080
workers = 8 keep_alive = 0
keep_alive = 5 ident = "aliurl via Rocket"
ident = "Rocket" ip_header = false
ip_header = "X-Real-IP" # set to `false` to disable
log_level = "normal" log_level = "normal"
temp_dir = "/tmp" temp_dir = "/tmp"
cli_colors = true cli_colors = true
ctrlc = false

View file

@ -12,6 +12,9 @@ use rocket::response::Redirect;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
static mut ACCESS_KEY_REQUIRED: bool = true; static mut ACCESS_KEY_REQUIRED: bool = true;
const LEN_OF_GENERATIVE_ALIASES: usize = 6;
const INDEX_REDIRECT: &'static str = "https://ivabus.dev"; const INDEX_REDIRECT: &'static str = "https://ivabus.dev";
const INDEX_WITH_AD: bool = true; const INDEX_WITH_AD: bool = true;

View file

@ -1,4 +1,5 @@
use crate::*; use crate::*;
use rand::{distributions::Alphanumeric, Rng};
use rocket::http::{RawStr, Status}; use rocket::http::{RawStr, Status};
use rocket::response::content::RawJson; use rocket::response::content::RawJson;
use serde_json::json; use serde_json::json;
@ -60,11 +61,30 @@ pub fn create_alias(data: &RawStr) -> (Status, RawJson<String>) {
let mut aliases_list = read_aliases(); let mut aliases_list = read_aliases();
let mut file = std::fs::File::options().write(true).open("./alias.json").unwrap(); let mut file = std::fs::File::options().write(true).open("./alias.json").unwrap();
let alias = match data.alias { 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, Some(alias) => alias,
}; };
if alias.contains("?") { 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 { let alia = if let Some(s) = data.redirect_with_ad {
if s.to_lowercase() == "true" { if s.to_lowercase() == "true" {