mirror of
https://github.com/ivabus/aliurl
synced 2024-11-21 22:25:06 +03:00
0.2.0: Add support for redirecting via html
This commit is contained in:
parent
7a847fe028
commit
15fa31207e
5 changed files with 56 additions and 10 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -3,4 +3,5 @@ access_keys
|
||||||
alias.json
|
alias.json
|
||||||
.idea
|
.idea
|
||||||
*.DS_Store
|
*.DS_Store
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
redirect.html
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "aliurl"
|
name = "aliurl"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
repository = "https://github.com/ivabus/aliurl"
|
repository = "https://github.com/ivabus/aliurl"
|
||||||
|
|
11
README.md
11
README.md
|
@ -14,7 +14,7 @@ cargo b -r
|
||||||
|
|
||||||
### Configuration
|
### 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.
|
Edit `Rocket.toml` to set port and ip.
|
||||||
|
|
||||||
|
@ -41,14 +41,19 @@ POST /post HTTP/1.1
|
||||||
"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, UUID 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
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Use alias
|
### Use alias
|
||||||
|
|
||||||
|
|
||||||
|
#### Request
|
||||||
|
|
||||||
```http request
|
```http request
|
||||||
GET /<ALIAS> HTTP/1.1
|
GET /<ALIAS> HTTP/1.1
|
||||||
```
|
```
|
||||||
|
#### Response
|
||||||
|
|
||||||
```http request
|
```http request
|
||||||
HTTP/1.1 303 See Other
|
HTTP/1.1 303 See Other
|
||||||
|
@ -59,6 +64,10 @@ location: <URL>
|
||||||
|
|
||||||
Aliases for root is declared in `src/main.rs` file in `INDEX_REDIRECT` const.
|
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
|
## License
|
||||||
|
|
||||||
The project is licensed under the terms of the [MIT license](./LICENSE).
|
The project is licensed under the terms of the [MIT license](./LICENSE).
|
12
redirect.html.example
Normal file
12
redirect.html.example
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta content="5;url=#REDIRECT#" http-equiv="refresh"/>
|
||||||
|
<meta content="width=device-width" name="viewport">
|
||||||
|
<title>Redirecting in 5 seconds</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Redirecting in 5 seconds...</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
38
src/main.rs
38
src/main.rs
|
@ -6,12 +6,14 @@ use std::io::BufReader;
|
||||||
|
|
||||||
use rocket::http::RawStr;
|
use rocket::http::RawStr;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
|
use rocket::response::content::RawHtml;
|
||||||
use rocket::response::Redirect;
|
use rocket::response::Redirect;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct CreateAliasRequest {
|
struct CreateAliasRequest {
|
||||||
url: String,
|
url: String,
|
||||||
|
redirect_with_ad: Option<String>,
|
||||||
access_key: Option<String>,
|
access_key: Option<String>,
|
||||||
alias: Option<String>,
|
alias: Option<String>,
|
||||||
}
|
}
|
||||||
|
@ -23,6 +25,8 @@ const INDEX_REDIRECT: &'static str = "https://ivabus.dev";
|
||||||
struct Alias {
|
struct Alias {
|
||||||
url: String,
|
url: String,
|
||||||
alias: String,
|
alias: String,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
redirect_with_ad: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_alias() -> Vec<Alias> {
|
fn read_alias() -> Vec<Alias> {
|
||||||
|
@ -75,10 +79,22 @@ fn create_alias(data: &RawStr) -> (Status, String) {
|
||||||
if alias.contains("?") {
|
if alias.contains("?") {
|
||||||
return (Status::BadRequest, format!("Error: alias should not contain '?'"));
|
return (Status::BadRequest, format!("Error: alias should not contain '?'"));
|
||||||
}
|
}
|
||||||
alias_list.push(Alias {
|
if let Some(s) = data.redirect_with_ad {
|
||||||
url: data.url.clone(),
|
if s.to_lowercase() == "true" {
|
||||||
alias: alias.clone(),
|
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);
|
alias_list.dedup_by(|a, b| a.alias == b.alias);
|
||||||
|
|
||||||
file.write_all(serde_json::to_string(&alias_list).unwrap().as_bytes()).unwrap();
|
file.write_all(serde_json::to_string(&alias_list).unwrap().as_bytes()).unwrap();
|
||||||
|
@ -93,16 +109,24 @@ fn not_found() -> Status {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/<page>")]
|
#[get("/<page>")]
|
||||||
async fn get_page(page: String) -> Redirect {
|
async fn get_page(page: String) -> Result<Redirect, RawHtml<String>> {
|
||||||
let mut decoded_page = String::new();
|
let mut decoded_page = String::new();
|
||||||
url_escape::decode_to_string(page, &mut decoded_page);
|
url_escape::decode_to_string(page, &mut decoded_page);
|
||||||
let alias_list = read_alias();
|
let alias_list = read_alias();
|
||||||
for i in alias_list {
|
for i in alias_list {
|
||||||
if i.alias == decoded_page {
|
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("/")]
|
#[get("/")]
|
||||||
|
|
Loading…
Reference in a new issue