Initial Prototype
This commit is contained in:
commit
cfe1f0c6c7
8 changed files with 1468 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal 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
11
.idea/domainlink.iml
Normal 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
8
.idea/modules.xml
Normal 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
6
.idea/vcs.xml
Normal 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
1318
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
9
Cargo.toml
Normal file
9
Cargo.toml
Normal 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
107
src/main.rs
Normal 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")
|
||||
}
|
Loading…
Reference in a new issue