domainlink/src/main.rs

146 lines
4.3 KiB
Rust
Raw Normal View History

use std::fmt::{Display, Formatter};
2024-04-08 15:17:25 +00:00
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use actix_web::{web, App, HttpResponse, HttpServer, get, Responder, HttpRequest};
use log::{LevelFilter};
use systemd_journal_logger::{connected_to_journal, JournalLog};
macro_rules! info {
($msg:tt) => {
match connected_to_journal() {
true => log::info!("[INFO] {}", $msg),
false => println!("[INFO] {}", $msg),
}
};
}
macro_rules! warn {
($msg:tt) => {
match connected_to_journal() {
true => log::warn!("[WARN] {}", $msg),
false => println!("[WARN] {}", $msg),
}
};
}
macro_rules! error {
($msg:tt) => {
match connected_to_journal() {
true => log::error!("[ERROR] {}", $msg),
false => eprintln!("[ERROR] {}", $msg),
}
};
}
2024-04-08 15:17:25 +00:00
#[derive(Clone)]
enum Protocol {
Http,
Https
2024-04-08 15:17:25 +00:00
}
impl Display for Protocol {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Protocol::Http => write!(f, "http://"),
Protocol::Https => write!(f, "https://")
2024-04-08 15:17:25 +00:00
}
}
}
#[derive(Clone)]
struct DomainLinkConfig {
domain: String,
protocol: Protocol,
target: String,
}
#[derive(Clone)]
struct Config {
redirects: Vec<DomainLinkConfig>,
ports: Vec<u16>,
addresses: Vec<IpAddr>,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
JournalLog::new().expect("Systemd-Logger crate error").install().expect("Systemd-Logger crate error");
log::set_max_level(LevelFilter::Info);
2024-04-08 15:17:25 +00:00
let config = Config {
redirects: vec![
DomainLinkConfig {
domain: "neshura.me".to_owned(),
protocol: Protocol::Https,
2024-04-08 15:17:25 +00:00
target: "neshweb.net".to_owned(),
},
DomainLinkConfig {
domain: "lemmy.neshura.me".to_owned(),
protocol: Protocol::Https,
2024-04-08 15:17:25 +00:00
target: "bookwormstory.social/u/neshura".to_owned()
},
DomainLinkConfig {
domain: "test2.neshura.me".to_owned(),
protocol: Protocol::Https,
2024-04-08 15:17:25 +00:00
target: "neshweb.net".to_owned()
}
],
ports: vec![8080, 8090],
addresses: vec![
IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)),
IpAddr::V4(Ipv4Addr::new(192, 168, 178, 11))
],
};
let msg = "This Build is not intended for production use!";
info!("Test Info");
warn!(msg);
error!(msg);
2024-04-08 15:17:25 +00:00
let mut server = HttpServer::new(move || {
App::new()
.app_data(web::Data::new(config.redirects.clone()))
.service(handle)
.service(dry_handle)
});
for address in config.addresses.iter() {
for port in config.ports.iter() {
server = server.bind((*address, *port))?
2024-04-08 15:17:25 +00:00
}
}
server.run().await
}
#[get("/")]
async fn handle(redirects: web::Data<Vec<DomainLinkConfig>>, request: HttpRequest) -> impl Responder {
if let Some(host_raw) = request.headers().get("host") {
let host = host_raw.to_str().expect("host conversion to string should never fail");
println!("{host}");
for redirect in redirects.iter() {
if redirect.domain == host {
return HttpResponse::PermanentRedirect().insert_header(("location", format!("{}{}", redirect.protocol, redirect.target).as_str())).finish();
}
}
let fail_msg = format!("No Redirect for {host} found");
return HttpResponse::NotFound().body(fail_msg)
}
HttpResponse::NotFound().body("Host not specified")
}
#[get("/dry")]
async fn dry_handle(redirects: web::Data<Vec<DomainLinkConfig>>, request: HttpRequest) -> impl Responder {
if let Some(host_raw) = request.headers().get("host") {
let host = host_raw.to_str().expect("host conversion to string should never fail");
println!("{host}");
for redirect in redirects.iter() {
if redirect.domain == host {
let body = format!("Redirecting: {} -> {}{}", host, redirect.protocol, redirect.target);
return HttpResponse::Ok().body(body);
}
}
let fail_msg = format!("No Redirect for {host} found");
return HttpResponse::NotFound().body(fail_msg)
}
HttpResponse::NotFound().body("Host not specified")
}