Merge branch 'initial'

This commit is contained in:
Neshura 2024-04-08 17:18:16 +02:00
commit e00eb5c276
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
8 changed files with 1468 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

8
.idea/.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

11
.idea/domainlink.iml Normal file
View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/domainlink.iml" filepath="$PROJECT_DIR$/.idea/domainlink.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

1318
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

9
Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "domainlink"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4"

107
src/main.rs Normal file
View file

@ -0,0 +1,107 @@
use std::fmt::{Display, format, Formatter};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use actix_web::{web, App, HttpResponse, HttpServer, get, Responder, HttpRequest};
use actix_web::web::Redirect;
#[derive(Clone)]
enum Protocol {
HTTP,
HTTPS
}
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://")
}
}
}
#[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<()> {
let config = Config {
redirects: vec![
DomainLinkConfig {
domain: "neshura.me".to_owned(),
protocol: Protocol::HTTPS,
target: "neshweb.net".to_owned(),
},
DomainLinkConfig {
domain: "lemmy.neshura.me".to_owned(),
protocol: Protocol::HTTPS,
target: "bookwormstory.social/u/neshura".to_owned()
},
DomainLinkConfig {
domain: "test2.neshura.me".to_owned(),
protocol: Protocol::HTTPS,
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 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 {
for port in &config.ports {
server = server.bind((address.clone(), port.clone()))?
}
}
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")
}