- Code cleanup
- Index resolution without ad set by default
This commit is contained in:
Ivan Bushchik 2023-06-08 16:02:08 +03:00
parent df4c1b4fa6
commit 85eaa42ad8
No known key found for this signature in database
GPG key ID: 9F6DDABE11A2674D
3 changed files with 71 additions and 30 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "aliurl" name = "aliurl"
version = "0.3.1" version = "0.3.2"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
repository = "https://github.com/ivabus/aliurl" repository = "https://github.com/ivabus/aliurl"

View file

@ -1,3 +1,27 @@
/*
* MIT License
*
* Copyright (c) 2023 Ivan Bushchik
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
mod post; mod post;
#[macro_use] #[macro_use]
@ -16,7 +40,7 @@ static mut ACCESS_KEY_REQUIRED: bool = true;
const LEN_OF_GENERATIVE_ALIASES: usize = 6; 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 = false;
#[derive(Deserialize, Serialize, Clone)] #[derive(Deserialize, Serialize, Clone)]
struct Alias { struct Alias {

View file

@ -1,5 +1,30 @@
/*
* MIT License
*
* Copyright (c) 2023 Ivan Bushchik
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
use crate::*; use crate::*;
use rand::{distributions::Alphanumeric, Rng};
use rand::distributions::{Alphanumeric, DistString};
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;
@ -64,11 +89,8 @@ pub fn create_alias(data: &RawStr) -> (Status, RawJson<String>) {
None => { None => {
let mut gen: String; let mut gen: String;
'gen: loop { 'gen: loop {
gen = rand::thread_rng() gen =
.sample_iter(&Alphanumeric) Alphanumeric.sample_string(&mut rand::thread_rng(), LEN_OF_GENERATIVE_ALIASES);
.take(LEN_OF_GENERATIVE_ALIASES)
.map(char::from)
.collect();
for i in &aliases_list { for i in &aliases_list {
if i.alias == gen { if i.alias == gen {
continue 'gen; continue 'gen;
@ -86,34 +108,28 @@ pub fn create_alias(data: &RawStr) -> (Status, RawJson<String>) {
RawJson(json!({"Error": "Alias should not contain \"?\""}).to_string()), RawJson(json!({"Error": "Alias should not contain \"?\""}).to_string()),
); );
} }
let alia = if let Some(s) = data.redirect_with_ad { let alias = Alias {
if s.to_lowercase() == "true" { url: data.url,
Alias { alias,
url: data.url.clone(), redirect_with_ad: match data.redirect_with_ad {
alias: alias.clone(), Some(s) => {
redirect_with_ad: Some(true), if s.to_ascii_lowercase() == "true" {
Some(true)
} else {
None
}
} }
} else { None => None,
Alias { },
url: data.url.clone(),
alias: alias.clone(),
redirect_with_ad: None,
}
}
} else {
Alias {
url: data.url.clone(),
alias: alias.clone(),
redirect_with_ad: None,
}
}; };
aliases_list.push(alia.clone());
aliases_list.push(alias.clone());
aliases_list.dedup_by(|a, b| a.alias == b.alias); aliases_list.dedup_by(|a, b| a.alias == b.alias);
file.write_all(serde_json::to_string(&aliases_list).unwrap().as_bytes()).unwrap(); file.write_all(serde_json::to_string(&aliases_list).unwrap().as_bytes()).unwrap();
file.sync_all().unwrap(); file.sync_all().unwrap();
return (Status::Ok, RawJson(serde_json::to_string(&alia).unwrap()));
return (Status::Ok, RawJson(serde_json::to_string(&alias).unwrap()));
} }
#[post("/api/get_aliases", data = "<data>")] #[post("/api/get_aliases", data = "<data>")]
@ -124,6 +140,7 @@ pub fn get_aliases(data: &RawStr) -> (Status, RawJson<String>) {
return (Status::BadRequest, RawJson(json!({"Error": format!("{e}")}).to_string())) return (Status::BadRequest, RawJson(json!({"Error": format!("{e}")}).to_string()))
} }
}; };
if let Err(e) = check_access_key(data.access_key) { if let Err(e) = check_access_key(data.access_key) {
return e; return e;
} }