Added curl and wget support

This commit is contained in:
Justine Pelletreau
2023-07-03 17:03:43 +02:00
parent 1621878e44
commit 56e7e0da17
5 changed files with 38 additions and 7 deletions

View File

@ -3,6 +3,7 @@ use std::fs;
use serde::{Serialize, Deserialize};
use serde_derive::{Serialize, Deserialize};
use actix_web::{get, post, web, App, HttpResponse, HttpServer, HttpRequest, Responder};
use actix_web::http::header::HeaderValue;
use std::fs::OpenOptions;
use std::io::prelude::*;
use chrono::Local;
@ -24,7 +25,6 @@ struct EncryptedPost {
fn decrypt(encrypted: String, pass: &str) -> Result<String, String> {
let mc = new_magic_crypt!(pass, 256);
println!("Trying with {} and pass {}", encrypted, pass);
let result = mc.decrypt_base64_to_string(&encrypted);
match result {
Ok(v) => {
@ -69,7 +69,6 @@ fn get_all_posts() -> (Vec<EncryptedPost>, String) {
#[post("/")]
async fn post_index(web::Form(form): web::Form<Post>, req: HttpRequest) -> impl Responder {
println!("{:?}", req);
let hostname = req.headers().get("Host").unwrap().to_str().unwrap();
let pass = random_password();
let encrypted = encrypt(&form.content, &pass);
@ -88,7 +87,7 @@ async fn post_index(web::Form(form): web::Form<Post>, req: HttpRequest) -> impl
fn random_password() -> String {
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789)(*&^%$#@!~";
0123456789";
let mut rng = rand::thread_rng();
let password: String = (0..64)
.map(|_| {
@ -100,13 +99,33 @@ fn random_password() -> String {
}
#[get("/decrypt/{givenid}/{password}")] // <- define path parameters
async fn show_post(path: web::Path<(u64, String)>) -> impl Responder {
async fn show_post(path: web::Path<(u64, String)>, req: HttpRequest) -> impl Responder {
let (givenid, password) = path.into_inner();
let (posts, _) = get_all_posts();
let mut ret_text = fs::read_to_string("./Files/header.html").unwrap();
let placeholder = HeaderValue::from_static("Please ignore");
let useragent = req.headers()
.get("user-agent")
.unwrap_or(&placeholder)
.to_str()
.unwrap_or("Please ignore");
for post in posts {
if givenid == post.id {
let _ = &mut ret_text.push_str(&format!("\n <b>This is the link to your post. Don't lose it !</b><br>\n><i>{} @{}</i><br><br>\n", post.name, post.date));
//Change the output for wget and curl
if useragent.starts_with("curl") || useragent.starts_with("Wget") {
let mut content = decrypt(post.crypt, &password)
.unwrap_or("Error decrypting".to_string());
content.push_str("\n");
return HttpResponse::Ok().body(content);
}
let _ = &mut ret_text.push_str(&format!("\n\
<b>This is the link to your post. Don't lose it !\
It can also be accessed raw using Curl or Wget.</b><br>\n\
><i>{} @{}</i><br><br>\n", post.name, post.date));
let _ = match decrypt(post.crypt, &password) {
Ok(v) => {
let _ = &mut ret_text.push_str(&v);