1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-10-16 21:33:54 -05:00

Compare commits

..

7 commits

7 changed files with 91 additions and 48 deletions

14
actix/Cargo.lock generated
View file

@ -88,7 +88,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb"
dependencies = [ dependencies = [
"quote", "quote",
"syn 2.0.57", "syn 2.0.58",
] ]
[[package]] [[package]]
@ -217,7 +217,7 @@ dependencies = [
"actix-router", "actix-router",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.57", "syn 2.0.58",
] ]
[[package]] [[package]]
@ -475,7 +475,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "chhoto-url" name = "chhoto-url"
version = "5.2.2" version = "5.2.4"
dependencies = [ dependencies = [
"actix-files", "actix-files",
"actix-session", "actix-session",
@ -1239,7 +1239,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.57", "syn 2.0.58",
] ]
[[package]] [[package]]
@ -1340,9 +1340,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.57" version = "2.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11a6ae1e52eb25aab8f3fb9fca13be982a373b8f1157ca14b897a825ba4a2d35" checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1682,7 +1682,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.57", "syn 2.0.58",
] ]
[[package]] [[package]]

View file

@ -1,6 +1,6 @@
[package] [package]
name = "chhoto-url" name = "chhoto-url"
version = "5.2.2" version = "5.2.4"
edition = "2021" edition = "2021"
authors = ["Sayantan Santra <sayantan[dot]santra689[at]gmail[dot]com"] authors = ["Sayantan Santra <sayantan[dot]santra689[at]gmail[dot]com"]
license = "mit" license = "mit"

View file

@ -1,16 +1,21 @@
use actix_session::Session; use actix_session::Session;
use std::{env, time::SystemTime}; use std::{env, time::SystemTime};
// Validate a given password
pub fn validate(session: Session) -> bool { pub fn validate(session: Session) -> bool {
// If there's no password provided, just return true // If there's no password provided, just return true
if env::var("password").is_err() { if env::var("password").is_err() {
return true; return true;
} }
let token = session.get::<String>("session-token"); if let Ok(token) = session.get::<String>("chhoto-url-auth") {
token.is_ok() && check(token.unwrap()) check(token)
} else {
false
}
} }
// Check a token cryptographically
fn check(token: Option<String>) -> bool { fn check(token: Option<String>) -> bool {
if let Some(token_body) = token { if let Some(token_body) = token {
let token_parts: Vec<&str> = token_body.split(';').collect(); let token_parts: Vec<&str> = token_body.split(';').collect();
@ -23,15 +28,16 @@ fn check(token: Option<String>) -> bool {
.duration_since(SystemTime::UNIX_EPOCH) .duration_since(SystemTime::UNIX_EPOCH)
.expect("Time went backwards!") .expect("Time went backwards!")
.as_secs(); .as_secs();
token_text == "session-token" && time_now < token_time + 1209600 // There are 1209600 seconds in 14 days token_text == "chhoto-url-auth" && time_now < token_time + 1209600 // There are 1209600 seconds in 14 days
} }
} else { } else {
false false
} }
} }
// Generate a new cryptographic token
pub fn gen_token() -> String { pub fn gen_token() -> String {
let token_text = String::from("session-token"); let token_text = String::from("chhoto-url-auth");
let time = SystemTime::now() let time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH) .duration_since(SystemTime::UNIX_EPOCH)
.expect("Time went backwards!") .expect("Time went backwards!")

View file

@ -1,6 +1,7 @@
use rusqlite::Connection; use rusqlite::Connection;
use serde::Serialize; use serde::Serialize;
// Struct for encoding a DB row
#[derive(Serialize)] #[derive(Serialize)]
pub struct DBRow { pub struct DBRow {
shortlink: String, shortlink: String,
@ -8,27 +9,37 @@ pub struct DBRow {
hits: i64, hits: i64,
} }
// Find a single URL
pub fn find_url(shortlink: &str, db: &Connection) -> Option<String> { pub fn find_url(shortlink: &str, db: &Connection) -> Option<String> {
let mut statement = db let mut statement = db
.prepare_cached("SELECT long_url FROM urls WHERE short_url = ?1") .prepare_cached("SELECT long_url FROM urls WHERE short_url = ?1")
.unwrap(); .expect("Error preparing SQL statement for find_url.");
statement statement
.query_row([shortlink], |row| row.get("long_url")) .query_row([shortlink], |row| row.get("long_url"))
.ok() .ok()
} }
// Get all URLs in DB
pub fn getall(db: &Connection) -> Vec<DBRow> { pub fn getall(db: &Connection) -> Vec<DBRow> {
let mut statement = db.prepare_cached("SELECT * FROM urls").unwrap(); let mut statement = db
.prepare_cached("SELECT * FROM urls")
.expect("Error preparing SQL statement for getall.");
let mut data = statement.query([]).unwrap(); let mut data = statement
.query([])
.expect("Error executing query for getall.");
let mut links: Vec<DBRow> = Vec::new(); let mut links: Vec<DBRow> = Vec::new();
while let Some(row) = data.next().unwrap() { while let Some(row) = data.next().expect("Error reading fetched rows.") {
let row_struct = DBRow { let row_struct = DBRow {
shortlink: row.get("short_url").unwrap(), shortlink: row
longlink: row.get("long_url").unwrap(), .get("short_url")
hits: row.get("hits").unwrap(), .expect("Error reading shortlink from row."),
longlink: row
.get("long_url")
.expect("Error reading shortlink from row."),
hits: row.get("hits").expect("Error reading shortlink from row."),
}; };
links.push(row_struct); links.push(row_struct);
} }
@ -36,14 +47,16 @@ pub fn getall(db: &Connection) -> Vec<DBRow> {
links links
} }
// Add a hit when site is visited
pub fn add_hit(shortlink: &str, db: &Connection) { pub fn add_hit(shortlink: &str, db: &Connection) {
db.execute( db.execute(
"UPDATE urls SET hits = hits + 1 WHERE short_url = ?1", "UPDATE urls SET hits = hits + 1 WHERE short_url = ?1",
[shortlink], [shortlink],
) )
.unwrap(); .expect("Error updating hit count.");
} }
// Insert a new link
pub fn add_link(shortlink: String, longlink: String, db: &Connection) -> bool { pub fn add_link(shortlink: String, longlink: String, db: &Connection) -> bool {
db.execute( db.execute(
"INSERT INTO urls (long_url, short_url, hits) VALUES (?1, ?2, ?3)", "INSERT INTO urls (long_url, short_url, hits) VALUES (?1, ?2, ?3)",
@ -52,11 +65,16 @@ pub fn add_link(shortlink: String, longlink: String, db: &Connection) -> bool {
.is_ok() .is_ok()
} }
// Delete and existing link
pub fn delete_link(shortlink: String, db: &Connection) -> bool { pub fn delete_link(shortlink: String, db: &Connection) -> bool {
let out = db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink]); if let Ok(delta) = db.execute("DELETE FROM urls WHERE short_url = ?1", [shortlink]) {
out.is_ok() && (out.unwrap() > 0) delta > 0
} else {
false
}
} }
// Open the DB, and create schema if missing
pub fn open_db(path: String) -> Connection { pub fn open_db(path: String) -> Connection {
let db = Connection::open(path).expect("Unable to open database!"); let db = Connection::open(path).expect("Unable to open database!");
// Create table if it doesn't exist // Create table if it doesn't exist
@ -69,6 +87,6 @@ pub fn open_db(path: String) -> Connection {
)", )",
[], [],
) )
.unwrap(); .expect("Unable to initialize empty database.");
db db
} }

View file

@ -100,14 +100,17 @@ async fn link_handler(shortlink: web::Path<String>, data: web::Data<AppState>) -
// Handle login // Handle login
#[post("/api/login")] #[post("/api/login")]
async fn login(req: String, session: Session) -> HttpResponse { async fn login(req: String, session: Session) -> HttpResponse {
if req == env::var("password").unwrap_or(req.clone()) { if let Ok(password) = env::var("password") {
// If no password was provided, match any password if password != req {
session.insert("session-token", auth::gen_token()).unwrap();
HttpResponse::Ok().body("Correct password!")
} else {
eprintln!("Failed login attempt!"); eprintln!("Failed login attempt!");
HttpResponse::Forbidden().body("Wrong password!") return HttpResponse::Forbidden().body("Wrong password!");
} }
}
// Return Ok if no password was set on the server side
session
.insert("chhoto-url-auth", auth::gen_token())
.expect("Error inserting auth token.");
HttpResponse::Ok().body("Correct password!")
} }
// Delete a given shortlink // Delete a given shortlink
@ -145,6 +148,7 @@ async fn main() -> std::io::Result<()> {
App::new() App::new()
.wrap( .wrap(
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone()) SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
.cookie_same_site(actix_web::cookie::SameSite::Strict)
.cookie_secure(false) .cookie_secure(false)
.build(), .build(),
) )

View file

@ -7,12 +7,14 @@ use std::env;
use crate::database; use crate::database;
#[derive(Deserialize, Default, PartialEq)] // Struct for reading link pairs sent during API call
#[derive(Deserialize)]
struct URLPair { struct URLPair {
shortlink: String, shortlink: String,
longlink: String, longlink: String,
} }
// Request the DB for searching an URL
pub fn get_longurl(shortlink: String, db: &Connection) -> Option<String> { pub fn get_longurl(shortlink: String, db: &Connection) -> Option<String> {
if validate_link(&shortlink) { if validate_link(&shortlink) {
database::find_url(shortlink.as_str(), db) database::find_url(shortlink.as_str(), db)
@ -21,26 +23,33 @@ pub fn get_longurl(shortlink: String, db: &Connection) -> Option<String> {
} }
} }
// Only have a-z, 0-9, - and _ as valid characters in a shortlink
fn validate_link(link: &str) -> bool { fn validate_link(link: &str) -> bool {
let re = Regex::new("^[a-z0-9-_]+$").unwrap(); let re = Regex::new("^[a-z0-9-_]+$").expect("Regex generation failed.");
re.is_match(link) re.is_match(link)
} }
// Request the DB for all URLs
pub fn getall(db: &Connection) -> String { pub fn getall(db: &Connection) -> String {
let links = database::getall(db); let links = database::getall(db);
serde_json::to_string(&links).unwrap() serde_json::to_string(&links).expect("Failure during creation of json from db.")
} }
// Make checks and then request the DB to add a new URL entry
pub fn add_link(req: String, db: &Connection) -> (bool, String) { pub fn add_link(req: String, db: &Connection) -> (bool, String) {
let mut chunks: URLPair = serde_json::from_str(&req).unwrap_or_default(); let mut chunks: URLPair;
if let Ok(json) = serde_json::from_str(&req) {
if chunks == URLPair::default() { chunks = json;
} else {
// shorturl should always be supplied, even if empty
return (false, String::from("Invalid request!")); return (false, String::from("Invalid request!"));
} }
let style = env::var("slug_style").unwrap_or(String::from("Pair")); let style = env::var("slug_style").unwrap_or(String::from("Pair"));
let len_str = env::var("slug_length").unwrap_or(String::from("8")); let mut len = env::var("slug_style")
let mut len: usize = len_str.parse().unwrap_or(8); .ok()
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(8);
if len < 4 { if len < 4 {
len = 4; len = 4;
} }
@ -61,6 +70,7 @@ pub fn add_link(req: String, db: &Connection) -> (bool, String) {
} }
} }
// Check if link, and request DB to delete it if exists
pub fn delete_link(shortlink: String, db: &Connection) -> bool { pub fn delete_link(shortlink: String, db: &Connection) -> bool {
if validate_link(shortlink.as_str()) { if validate_link(shortlink.as_str()) {
database::delete_link(shortlink, db) database::delete_link(shortlink, db)
@ -69,6 +79,7 @@ pub fn delete_link(shortlink: String, db: &Connection) -> bool {
} }
} }
// Generate a random link using either adjective-name pair (default) of a slug or a-z, 0-9
fn gen_link(style: String, len: usize) -> String { fn gen_link(style: String, len: usize) -> String {
#[rustfmt::skip] #[rustfmt::skip]
static ADJECTIVES: [&str; 108] = ["admiring", "adoring", "affectionate", "agitated", "amazing", "angry", "awesome", "beautiful", static ADJECTIVES: [&str; 108] = ["admiring", "adoring", "affectionate", "agitated", "amazing", "angry", "awesome", "beautiful",
@ -110,8 +121,12 @@ fn gen_link(style: String, len: usize) -> String {
} else { } else {
format!( format!(
"{0}-{1}", "{0}-{1}",
ADJECTIVES.choose(&mut rand::thread_rng()).unwrap(), ADJECTIVES
NAMES.choose(&mut rand::thread_rng()).unwrap() .choose(&mut rand::thread_rng())
.expect("Error choosing random adjective."),
NAMES
.choose(&mut rand::thread_rng())
.expect("Error choosing random name.")
) )
} }
} }

View file

@ -13,7 +13,7 @@ const getSiteUrl = async () => {
return window.location.host.replace(/\/$/, ''); return window.location.host.replace(/\/$/, '');
} }
else { else {
return text.replace(/\/$/, '').replace(/^"/, '').replace(/"$/, ''); return url.replace(/\/$/, '').replace(/^"/, '').replace(/"$/, '');
} }
} }
@ -173,6 +173,7 @@ const submitForm = () => {
}; };
const url = prepSubdir("/api/new"); const url = prepSubdir("/api/new");
let ok = false;
fetch(url, { fetch(url, {
method: "POST", method: "POST",
@ -182,15 +183,14 @@ const submitForm = () => {
body: JSON.stringify(data), body: JSON.stringify(data),
}) })
.then(res => { .then(res => {
if (!res.ok) { ok = res.ok;
showAlert(res.text(), "red"); return res.text();
return "error"; })
.then(text => {
if (!ok) {
showAlert(text, "red");
} }
else { else {
return res.text();
}
}).then(text => {
if (text != "error") {
copyShortUrl(text); copyShortUrl(text);
longUrl.value = ""; longUrl.value = "";
shortUrl.value = ""; shortUrl.value = "";