This commit is contained in:
JMARyA 2023-01-28 07:24:50 +01:00
parent 5895bb7d56
commit f2b24d33e1
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
7 changed files with 70 additions and 56 deletions

20
Cargo.lock generated
View file

@ -445,9 +445,9 @@ dependencies = [
[[package]] [[package]]
name = "cxx" name = "cxx"
version = "1.0.87" version = "1.0.88"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b61a7545f753a88bcbe0a70de1fcc0221e10bfc752f576754fa91e663db1622e" checksum = "322296e2f2e5af4270b54df9e85a02ff037e271af20ba3e7fe1575515dc840b8"
dependencies = [ dependencies = [
"cc", "cc",
"cxxbridge-flags", "cxxbridge-flags",
@ -457,9 +457,9 @@ dependencies = [
[[package]] [[package]]
name = "cxx-build" name = "cxx-build"
version = "1.0.87" version = "1.0.88"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f464457d494b5ed6905c63b0c4704842aba319084a0a3561cdc1359536b53200" checksum = "017a1385b05d631e7875b1f151c9f012d37b53491e2a87f65bff5c262b2111d8"
dependencies = [ dependencies = [
"cc", "cc",
"codespan-reporting", "codespan-reporting",
@ -472,15 +472,15 @@ dependencies = [
[[package]] [[package]]
name = "cxxbridge-flags" name = "cxxbridge-flags"
version = "1.0.87" version = "1.0.88"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43c7119ce3a3701ed81aca8410b9acf6fc399d2629d057b87e2efa4e63a3aaea" checksum = "c26bbb078acf09bc1ecda02d4223f03bdd28bd4874edcb0379138efc499ce971"
[[package]] [[package]]
name = "cxxbridge-macro" name = "cxxbridge-macro"
version = "1.0.87" version = "1.0.88"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65e07508b90551e610910fa648a1878991d367064997a596135b86df30daf07e" checksum = "357f40d1f06a24b60ae1fe122542c1fb05d28d32acb2aed064e84bc2ad1e252e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -644,7 +644,7 @@ dependencies = [
[[package]] [[package]]
name = "gnupg" name = "gnupg"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.hydrar.de/jmarya/gnupg-rs#3b57d7879b8bb676ffc860feac433611230118f6" source = "git+https://git.hydrar.de/jmarya/gnupg-rs#5e08b746122a6df58ca648bfe57c76bd40f7e5fd"
[[package]] [[package]]
name = "h2" name = "h2"
@ -1802,7 +1802,7 @@ checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
[[package]] [[package]]
name = "web-base" name = "web-base"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.hydrar.de/jmarya/web-base#0920a1fcbc15c4ec5bbf131103e98e44cb2a0968" source = "git+https://git.hydrar.de/jmarya/web-base#0c6e643cca39e7b18a7b3a870313752c6464c35b"
dependencies = [ dependencies = [
"actix-files", "actix-files",
"actix-web", "actix-web",

View file

@ -6,7 +6,7 @@ mod pages;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use actix_web::*; use actix_web::{web, App, HttpServer};
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {

View file

@ -1,17 +1,19 @@
use std::io::Write; use std::io::Write;
pub fn encrypt(msg: String) -> String { pub fn encrypt(msg: String) -> String {
let pgp = gnupg::GnuPG::new().unwrap(); let pgp = gnupg::GnuPG::new().expect("no gpg");
let pub_key = pgp let pub_key = pgp
.import_key(&std::fs::read_to_string("./config/pub.key").unwrap()) .import_key(&std::fs::read_to_string("./config/pub.key").expect("key could not be read"))
.unwrap(); .expect("key import error");
pgp.encrypt(&pub_key, &msg).unwrap() pgp.encrypt(&pub_key, &msg).expect("encryption failed")
} }
pub fn save_msg(msg: String, name: &str) { pub fn save_message(msg: String, name: &str) {
std::fs::create_dir_all("./data/messages").expect("couldn't create msg dir"); std::fs::create_dir_all("./data/messages").expect("couldn't create msg dir");
let time = chrono::offset::Utc::now(); let time = chrono::offset::Utc::now();
let time = time.format("%Y-%m-%d.%H-%M").to_string(); let time = time.format("%Y-%m-%d.%H-%M").to_string();
let mut f = std::fs::File::create(format!("./data/messages/{name}-{time}.asc")).unwrap(); let mut f = std::fs::File::create(format!("./data/messages/{name}-{time}.asc"))
f.write_all(encrypt(msg).as_bytes()).unwrap(); .expect("could not create file");
f.write_all(encrypt(msg).as_bytes())
.expect("file could not be written");
} }

View file

@ -1,7 +1,7 @@
use crate::config; use crate::config;
use crate::config::Config; use crate::config::Config;
use actix_web::web::Data; use actix_web::web::Data;
use log::*; use log::info;
pub async fn notify(msg: &str, title: &str, config: Data<Config>) { pub async fn notify(msg: &str, title: &str, config: Data<Config>) {
if let Some(gotify) = config.gotify_config() { if let Some(gotify) = config.gotify_config() {

View file

@ -1,5 +1,5 @@
use actix_files::NamedFile; use actix_files::NamedFile;
use actix_web::*; use actix_web::{get, Result};
// Assets // Assets

View file

@ -1,6 +1,6 @@
use crate::config::Config; use crate::config::Config;
use actix_web::web::Data; use actix_web::web::Data;
use actix_web::*; use actix_web::HttpResponse;
use maud::{html, PreEscaped}; use maud::{html, PreEscaped};
pub(crate) async fn build_site( pub(crate) async fn build_site(
@ -17,7 +17,7 @@ pub(crate) async fn build_site(
}; };
let mut c_class = "bg-dark text-white justify-content-center text-center".to_string(); let mut c_class = "bg-dark text-white justify-content-center text-center".to_string();
let mut c_style = "".to_string(); let mut c_style = String::new();
let mut g_style = "a {text-decoration: none; font-weight: bold; color: white}".to_string(); let mut g_style = "a {text-decoration: none; font-weight: bold; color: white}".to_string();
if !disable_color { if !disable_color {
if let (Some(fg), Some(bg)) = (config.fg_color(), config.bg_color()) { if let (Some(fg), Some(bg)) = (config.fg_color(), config.bg_color()) {
@ -49,5 +49,7 @@ pub(crate) async fn build_site(
}; };
}; };
HttpResponse::Ok().message_body(r.into_string()).unwrap() HttpResponse::Ok()
.message_body(r.into_string())
.expect("could not build page")
} }

View file

@ -1,7 +1,7 @@
use crate::{config, pages}; use crate::{config, pages};
use actix_web::http::header; use actix_web::http::header;
use actix_web::web::Form; use actix_web::web::Form;
use actix_web::*; use actix_web::{get, post, web, Error, HttpRequest, HttpResponse, Responder, Result};
use maud::{html, PreEscaped}; use maud::{html, PreEscaped};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -13,8 +13,8 @@ pub struct MessageForm {
#[post("/message")] #[post("/message")]
pub async fn message_post(r: HttpRequest, f: Form<MessageForm>) -> impl Responder { pub async fn message_post(r: HttpRequest, f: Form<MessageForm>) -> impl Responder {
let config: &web::Data<config::Config> = r.app_data().unwrap(); let config: &web::Data<config::Config> = r.app_data().expect("get config failed");
crate::msg::save_msg(f.message.clone(), &f.msg_name.to_string()); crate::msg::save_message(f.message.clone(), &f.msg_name.to_string());
crate::notification::notify( crate::notification::notify(
&format!("New Message from {}", f.msg_name), &format!("New Message from {}", f.msg_name),
"New Message", "New Message",
@ -26,7 +26,7 @@ pub async fn message_post(r: HttpRequest, f: Form<MessageForm>) -> impl Responde
#[get("/message")] #[get("/message")]
pub async fn message_page(r: HttpRequest) -> impl Responder { pub async fn message_page(r: HttpRequest) -> impl Responder {
let config: &web::Data<config::Config> = r.app_data().unwrap(); let config: &web::Data<config::Config> = r.app_data().expect("get config failed");
let resp = html! { let resp = html! {
div class="container" style="margin-top: 25px" { div class="container" style="margin-top: 25px" {
@ -45,10 +45,10 @@ pub async fn message_page(r: HttpRequest) -> impl Responder {
} }
#[get("/mirrors.txt")] #[get("/mirrors.txt")]
pub async fn mirrors(r: HttpRequest) -> impl Responder { pub async fn mirrors(r: HttpRequest) -> Result<impl Responder, Error> {
let config: &web::Data<config::Config> = r.app_data().unwrap(); let config: &web::Data<config::Config> = r.app_data().expect("get config failed");
if let Ok(mirror_file) = std::fs::File::open("./config/mirrors.txt") { if let Ok(mirror_file) = std::fs::File::open("./config/mirrors.txt") {
let content = std::io::read_to_string(mirror_file).unwrap(); let content = std::io::read_to_string(mirror_file).expect("could not read file");
if web_base::func::is_browser(&r) { if web_base::func::is_browser(&r) {
let resp = html! { let resp = html! {
div style="margin: 25px;" { div style="margin: 25px;" {
@ -57,26 +57,32 @@ pub async fn mirrors(r: HttpRequest) -> impl Responder {
}; };
} }
}; };
return pages::html_fn::build_site(resp.into_string(), "Mirrors", false, true, config) return Ok(pages::html_fn::build_site(
.await; resp.into_string(),
"Mirrors",
false,
true,
config,
)
.await);
} }
return HttpResponse::Ok().message_body(content).unwrap(); return HttpResponse::Ok().message_body(content);
} }
HttpResponse::NotFound() HttpResponse::NotFound().message_body(String::new())
.message_body("".to_string())
.unwrap()
} }
#[get("/public_key")] #[get("/public_key")]
pub async fn public_key(r: HttpRequest) -> impl Responder { pub async fn public_key(r: HttpRequest) -> Result<impl Responder, Error> {
if web_base::func::is_browser(&r) { if web_base::func::is_browser(&r) {
let config: &web::Data<config::Config> = r.app_data().unwrap(); let config: &web::Data<config::Config> = r.app_data().expect("get config failed");
let host = format!("http://{}", web_base::func::get_host(&r)); let host = format!("http://{}", web_base::func::get_host(&r));
let key = let key = std::io::read_to_string(
std::io::read_to_string(std::fs::File::open("./config/pub.key").unwrap()).unwrap(); std::fs::File::open("./config/pub.key").expect("key could not be opened"),
)
.expect("could not read key");
let pgp = gnupg::GnuPG::new().unwrap(); let pgp = gnupg::GnuPG::new().expect("no gpg");
let key_name = pgp.import_key(&key).unwrap().name; let key_name = pgp.import_key(&key).expect("key not valid").name;
let key = key.replace('\n', "<br>"); let key = key.replace('\n', "<br>");
let resp = html! { let resp = html! {
@ -92,25 +98,28 @@ pub async fn public_key(r: HttpRequest) -> impl Responder {
} }
}; };
return pages::html_fn::build_site(resp.into_string(), "Public Key", true, false, config) return Ok(pages::html_fn::build_site(
.await; resp.into_string(),
"Public Key",
true,
false,
config,
)
.await);
} }
if let Ok(key_f) = std::fs::File::open("./config/pub.key") { if let Ok(key_f) = std::fs::File::open("./config/pub.key") {
if let Ok(key_data) = std::io::read_to_string(key_f) { if let Ok(key_data) = std::io::read_to_string(key_f) {
return HttpResponse::Ok() return HttpResponse::Ok()
.insert_header(header::ContentType::plaintext()) .insert_header(header::ContentType::plaintext())
.message_body(key_data) .message_body(key_data);
.unwrap();
} }
} }
HttpResponse::NotFound() HttpResponse::NotFound().message_body(String::new())
.message_body("".to_string())
.unwrap()
} }
fn build_information_block(conf: &web::Data<config::Config>) -> String { fn build_information_block(conf: &web::Data<config::Config>) -> String {
let name = conf.name().unwrap(); let name = conf.name().expect("no name found");
html! { html! {
div class="container border-dark" style="margin-top: 20px" { div class="container border-dark" style="margin-top: 20px" {
img src="/assets/me" height="200" width="200" alt="Me" class="rounded"; img src="/assets/me" height="200" width="200" alt="Me" class="rounded";
@ -124,15 +133,16 @@ fn build_information_block(conf: &web::Data<config::Config>) -> String {
fn build_contact_block(conf: &web::Data<config::Config>) -> String { fn build_contact_block(conf: &web::Data<config::Config>) -> String {
if let Some(email) = conf.email() { if let Some(email) = conf.email() {
let pgp_key_message = match std::path::Path::new("./config/pub.key").exists() { let pgp_key_message = if std::path::Path::new("./config/pub.key").exists() {
true => html! { html! {
a href="/public_key" { "My PGP Key" }; a href="/public_key" { "My PGP Key" };
br; br;
a href="/message" { "Write a message" }; a href="/message" { "Write a message" };
br;br; br;br;
} }
.into_string(), .into_string()
false => "".to_string(), } else {
String::new()
}; };
html! { html! {
@ -149,7 +159,7 @@ fn build_contact_block(conf: &web::Data<config::Config>) -> String {
} }
.into_string() .into_string()
} else { } else {
"".to_string() String::new()
} }
} }
@ -170,7 +180,7 @@ fn build_donation_block(conf: &web::Data<config::Config>) -> String {
} }
.into_string() .into_string()
} else { } else {
"".to_string() String::new()
} }
} }