From 15fa31207e82f5afd18432ca48f800c392a4dc25 Mon Sep 17 00:00:00 2001 From: Ivan Bushchik Date: Mon, 5 Jun 2023 09:59:25 +0300 Subject: [PATCH] 0.2.0: Add support for redirecting via html --- .gitignore | 3 ++- Cargo.toml | 2 +- README.md | 11 ++++++++++- redirect.html.example | 12 ++++++++++++ src/main.rs | 38 +++++++++++++++++++++++++++++++------- 5 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 redirect.html.example diff --git a/.gitignore b/.gitignore index 255cb84..faf151a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ access_keys alias.json .idea *.DS_Store -Cargo.lock \ No newline at end of file +Cargo.lock +redirect.html \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 6d55d5c..b913610 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aliurl" -version = "0.1.0" +version = "0.2.0" edition = "2021" license = "MIT" repository = "https://github.com/ivabus/aliurl" diff --git a/README.md b/README.md index cd80c74..a6ad6e0 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ cargo b -r ### Configuration -Add your access_keys to `./access_keys` or don't add any, if you don't want to use authorization. +Add your access_keys (separating by newline) to `./access_keys` or don't add any, if you don't want to use authorization. Edit `Rocket.toml` to set port and ip. @@ -41,14 +41,19 @@ POST /post HTTP/1.1 "url": "", "alias": "", // If not provided, UUID 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 } ``` ### Use alias + +#### Request + ```http request GET / HTTP/1.1 ``` +#### Response ```http request HTTP/1.1 303 See Other @@ -59,6 +64,10 @@ location: Aliases for root is declared in `src/main.rs` file in `INDEX_REDIRECT` const. +## Redirect with "ad" + +See `./redirect.html.example` to understand what's going on. + ## License The project is licensed under the terms of the [MIT license](./LICENSE). \ No newline at end of file diff --git a/redirect.html.example b/redirect.html.example new file mode 100644 index 0000000..f54f00b --- /dev/null +++ b/redirect.html.example @@ -0,0 +1,12 @@ + + + + + + + Redirecting in 5 seconds + + +

Redirecting in 5 seconds...

+ + \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a90b8ce..9aacaa3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,14 @@ use std::io::BufReader; use rocket::http::RawStr; use rocket::http::Status; +use rocket::response::content::RawHtml; use rocket::response::Redirect; use serde::{Deserialize, Serialize}; #[derive(Debug, Deserialize)] struct CreateAliasRequest { url: String, + redirect_with_ad: Option, access_key: Option, alias: Option, } @@ -23,6 +25,8 @@ const INDEX_REDIRECT: &'static str = "https://ivabus.dev"; struct Alias { url: String, alias: String, + #[serde(skip_serializing_if = "Option::is_none")] + redirect_with_ad: Option, } fn read_alias() -> Vec { @@ -75,10 +79,22 @@ fn create_alias(data: &RawStr) -> (Status, String) { if alias.contains("?") { return (Status::BadRequest, format!("Error: alias should not contain '?'")); } - alias_list.push(Alias { - url: data.url.clone(), - alias: alias.clone(), - }); + if let Some(s) = data.redirect_with_ad { + if s.to_lowercase() == "true" { + alias_list.push(Alias { + url: data.url.clone(), + alias: alias.clone(), + redirect_with_ad: Some(true), + }); + } + } else { + alias_list.push(Alias { + url: data.url.clone(), + alias: alias.clone(), + redirect_with_ad: None, + }); + } + alias_list.dedup_by(|a, b| a.alias == b.alias); file.write_all(serde_json::to_string(&alias_list).unwrap().as_bytes()).unwrap(); @@ -93,16 +109,24 @@ fn not_found() -> Status { } #[get("/")] -async fn get_page(page: String) -> Redirect { +async fn get_page(page: String) -> Result> { let mut decoded_page = String::new(); url_escape::decode_to_string(page, &mut decoded_page); let alias_list = read_alias(); for i in alias_list { if i.alias == decoded_page { - return Redirect::to(i.url); + if let Some(red) = i.redirect_with_ad { + if red { + let mut redirect = String::new(); + let mut file = std::fs::File::open("./redirect.html").unwrap(); + file.read_to_string(&mut redirect).unwrap(); + return Err(RawHtml(redirect.replace("#REDIRECT#", i.url.as_str()))); + } + } + return Ok(Redirect::to(i.url)); } } - Redirect::to("/404") + Ok(Redirect::to("/404")) } #[get("/")]