add security.txt

This commit is contained in:
Mylloon 2023-02-08 22:14:57 +01:00
parent 691c766312
commit d7ee3f903f
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
6 changed files with 65 additions and 22 deletions

View file

@ -1,9 +1,11 @@
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize, Clone)]
pub struct Config { pub struct Config {
pub scheme: String, pub scheme: String,
pub port: u16, pub port: u16,
pub mail: Option<String>,
pub lang: Option<String>,
} }
pub fn get_config(file_path: &str) -> Config { pub fn get_config(file_path: &str) -> Config {
@ -20,6 +22,8 @@ pub fn get_config(file_path: &str) -> Config {
Config { Config {
scheme: "http".to_string(), scheme: "http".to_string(),
port: 8080, port: 8080,
mail: None,
lang: None,
} }
} }
} }

View file

@ -1,10 +1,12 @@
use actix_web::{App, HttpServer}; use actix_web::{web, App, HttpServer};
mod config; mod config;
mod template;
#[path = "routes/index.rs"] #[path = "routes/index.rs"]
mod index; mod index;
#[path = "routes/agreements.rs"]
mod agreements;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
let config = config::get_config("config/config.toml"); let config = config::get_config("config/config.toml");
@ -12,8 +14,13 @@ async fn main() -> std::io::Result<()> {
let addr = ("127.0.0.1", config.port); let addr = ("127.0.0.1", config.port);
println!("Listening to {}://{}:{}", config.scheme, addr.0, addr.1); println!("Listening to {}://{}:{}", config.scheme, addr.0, addr.1);
HttpServer::new(|| App::new().service(index::page)) HttpServer::new(move || {
.bind(addr)? App::new()
.run() .app_data(web::Data::new(config.clone()))
.await .service(index::page)
.service(agreements::security)
})
.bind(addr)?
.run()
.await
} }

31
src/routes/agreements.rs Normal file
View file

@ -0,0 +1,31 @@
use std::net::SocketAddr;
use actix_web::{routes, web, HttpRequest, HttpResponse, Responder};
use askama::Template;
use crate::config::Config;
#[routes]
#[get("/.well-known/security.txt")]
#[get("/security.txt")]
pub async fn security(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(get_security(config.get_ref().clone(), req.peer_addr()))
}
#[derive(Template)]
#[template(path = "../templates/security.txt")]
struct SecurityTemplate {
contact: String,
pref_lang: String,
url: String,
}
pub fn get_security(config: Config, addr: Option<SocketAddr>) -> std::string::String {
let data = SecurityTemplate {
contact: config.mail.unwrap_or_default(),
pref_lang: config.lang.unwrap_or_default(),
url: format!("{}/.well-known/security.txt", addr.unwrap().ip()),
};
data.render().unwrap()
}

View file

@ -1,8 +1,19 @@
use crate::template::get_index;
use actix_web::{get, HttpResponse, Responder}; use actix_web::{get, HttpResponse, Responder};
use askama::Template;
#[get("/")] #[get("/")]
pub async fn page() -> impl Responder { pub async fn page() -> impl Responder {
HttpResponse::Ok().body(get_index()) HttpResponse::Ok().body(get_index())
} }
#[derive(Template)]
#[template(path = "../templates/index.html")]
struct IndexTemplate<'a> {
name: &'a str,
}
pub fn get_index() -> std::string::String {
let index = IndexTemplate { name: "world" }; // instantiate your struct
index.render().unwrap()
}

View file

@ -1,13 +0,0 @@
use askama::Template;
#[derive(Template)]
#[template(path = "../templates/index.html")]
struct HelloTemplate<'a> {
name: &'a str,
}
pub fn get_index() -> std::string::String {
let hello = HelloTemplate { name: "world" }; // instantiate your struct
hello.render().unwrap()
}

3
templates/security.txt Normal file
View file

@ -0,0 +1,3 @@
Contact: {{ contact }}
Preferred-Languages: {{ pref_lang }}
Canonical: {{ url }}