Skip to content

Commit

Permalink
use x-forwarded-proto rather then x-forwarded-ssl
Browse files Browse the repository at this point in the history
  • Loading branch information
randomairborne committed Apr 11, 2024
1 parent efd016b commit a3974cf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
22 changes: 12 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub struct IndexPage {
root_dns_name: Arc<str>,
cb: Arc<BustDir>,
ip: IpAddr,
https: bool,
proto: String,
}

#[derive(Template)]
Expand All @@ -88,7 +88,7 @@ pub struct NotFoundPage {
#[allow(clippy::unused_async)]
async fn home(
IpAddress(ip): IpAddress,
XForwardedSsl(https): XForwardedSsl,
XForwardedProto(proto): XForwardedProto,
Accept(accept): Accept,
State(state): State<AppState>,
) -> Result<Result<IndexPage, String>, Error> {
Expand All @@ -97,7 +97,7 @@ async fn home(
root_dns_name: state.root_dns_name,
cb: state.cb,
ip,
https,
proto,
};
Ok(Ok(page))
} else {
Expand Down Expand Up @@ -223,19 +223,21 @@ impl FromRequestParts<AppState> for IpAddress {
}

#[derive(Clone, Debug)]
pub struct XForwardedSsl(bool);
pub struct XForwardedProto(pub String);

#[axum::async_trait]
impl<S> FromRequestParts<S> for XForwardedSsl {
impl<S> FromRequestParts<S> for XForwardedProto {
type Rejection = Error;

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
let header_bytes = parts
let proto = parts
.headers
.get("X-Forwarded-SSL")
.map(HeaderValue::as_bytes);
let has_ssl = matches!(header_bytes, Some(b"on"));
Ok(Self(has_ssl))
.get("X-Forwarded-Proto")
.map(HeaderValue::to_str)
.and_then(Result::ok)
.unwrap_or("http")
.to_owned();
Ok(Self(proto))
}
}

Expand Down
22 changes: 8 additions & 14 deletions templates/index.hbs
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@
<!doctype html>
{%- set scheme -%}
{%- if https -%}
{%- let scheme = "https" -%}
{%- else -%}
{%- let scheme = "http" -%}
{%- endif -%}
{%- let description = "A simple, fast website to return your IPv4 and IPv6 addresses. No logs are kept. Free and open to all." -%}
<html lang="en">
<head>
<meta charset="UTF-8" />
<link
rel="preload"
href="{{ scheme }}://{{ root_dns_name }}/main.js?cb={{ "/main.js"|bust_dir(cb) }}"
href="{{ proto }}://{{ root_dns_name }}/main.js?cb={{ "/main.js"|bust_dir(cb) }}"
as="script"
/>
{%- match ip -%}
{%- when IpAddr::V4 with (_) -%}
<link
rel="preload"
href="{{ scheme }}://v6.{{ root_dns_name }}/raw"
href="{{ proto }}://v6.{{ root_dns_name }}/raw"
as="fetch"
crossorigin="anonymous"
/>
{%- when IpAddr::V6 with (_) -%}
<link
rel="preload"
href="{{ scheme }}://v4.{{ root_dns_name }}/raw"
href="{{ proto }}://v4.{{ root_dns_name }}/raw"
as="fetch"
crossorigin="anonymous"
/>
{%- endmatch -%}
<link
rel="stylesheet"
href="{{ scheme }}://{{ root_dns_name }}/main.css?cb={{ "/main.css"|bust_dir(cb) }}"
href="{{ proto }}://{{ root_dns_name }}/main.css?cb={{ "/main.css"|bust_dir(cb) }}"
/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
name="root-dns-name"
id="root-dns-name"
data-dns-name="{{ root_dns_name }}"
/>
<meta name="scheme" id="scheme" data-scheme="{{ scheme }}" />
<meta name="scheme" id="scheme" data-scheme="{{ proto }}" />
<meta name="description" content="{{ description }}" />
<meta property="og:title" content="GiveIP" />
<meta property="og:url" content="{{ scheme }}://{{ root_dns_name }}/" />
<meta property="og:url" content="{{ proto }}://{{ root_dns_name }}/" />
<meta property="og:description" content="{{ description }}" />
<link rel="canonical" href="{{ scheme }}://{{ root_dns_name }}/" />
<link rel="canonical" href="https://{{ root_dns_name }}/" />
{%- match ip -%}
{%- when IpAddr::V4 with (ipv4) -%}
<title id="title">Your public IP is {{ ipv4 }}</title>
Expand All @@ -56,7 +50,7 @@

<body>
<script
src="{{ scheme }}://{{ root_dns_name }}/main.js?cb={{ "/main.js"|bust_dir(cb) }}"
src="{{ proto }}://{{ root_dns_name }}/main.js?cb={{ "/main.js"|bust_dir(cb) }}"
defer
></script>
<script
Expand Down

0 comments on commit a3974cf

Please sign in to comment.