Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions iroh-relay/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::{fmt, future::Future, net::SocketAddr, num::NonZeroU32, pin::Pin, sync:

use derive_more::Debug;
use http::{
HeaderMap, Method, Request, Response, StatusCode, header::InvalidHeaderValue,
HeaderMap, HeaderValue, Method, Request, Response, StatusCode, header::InvalidHeaderValue,
response::Builder as ResponseBuilder,
};
use hyper::body::Incoming;
Expand Down Expand Up @@ -56,8 +56,11 @@ pub use self::{
resolver::{DEFAULT_CERT_RELOAD_INTERVAL, ReloadingResolver},
};

const NO_CONTENT_CHALLENGE_HEADER: &str = "X-Tailscale-Challenge";
const NO_CONTENT_RESPONSE_HEADER: &str = "X-Tailscale-Response";
// TODO: remove before 1.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please link to an issue linked to the milestone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const NO_CONTENT_CHALLENGE_HEADER_LEGACY: &str = "X-Tailscale-Challenge";
const NO_CONTENT_CHALLENGE_HEADER: &str = "X-Iroh-Challenge";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing "Iroh" capitalised is jarring 😉

const NO_CONTENT_RESPONSE_HEADER_LEGACY: &str = "X-Tailscale-Response";
const NO_CONTENT_RESPONSE_HEADER: &str = "X-Iroh-Response";
const NOTFOUND: &[u8] = b"Not Found";
const ROBOTS_TXT: &[u8] = b"User-agent: *\nDisallow: /\n";
const INDEX: &[u8] = br#"<html><body>
Expand Down Expand Up @@ -640,21 +643,28 @@ fn serve_no_content_handler<B: hyper::body::Body>(
r: Request<B>,
mut response: ResponseBuilder,
) -> HyperResult<Response<BytesBody>> {
let check = |c: &HeaderValue| {
!c.is_empty() && c.len() < 64 && c.as_bytes().iter().all(|c| is_challenge_char(*c as char))
};

if let Some(challenge) = r.headers().get(NO_CONTENT_CHALLENGE_HEADER) {
if !challenge.is_empty()
&& challenge.len() < 64
&& challenge
.as_bytes()
.iter()
.all(|c| is_challenge_char(*c as char))
{
if check(challenge) {
response = response.header(
NO_CONTENT_RESPONSE_HEADER,
format!("response {}", challenge.to_str()?),
);
}
}

if let Some(challenge) = r.headers().get(NO_CONTENT_CHALLENGE_HEADER_LEGACY) {
if check(challenge) {
response = response.header(
NO_CONTENT_RESPONSE_HEADER_LEGACY,
format!("response {}", challenge.to_str()?),
);
}
}

response
.status(StatusCode::NO_CONTENT)
.body(body_empty())
Expand Down
4 changes: 2 additions & 2 deletions iroh/src/net_report/reportgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,15 +606,15 @@ async fn check_captive_portal(
let portal_url = format!("http://{host_name}/generate_204");
let res = client
.request(reqwest::Method::GET, portal_url)
.header("X-Tailscale-Challenge", &challenge)
.header("X-Iroh-Challenge", &challenge)
.send()
.await
.context(captive_portal_error::HttpRequestSnafu)?;

let expected_response = format!("response {challenge}");
let is_valid_response = res
.headers()
.get("X-Tailscale-Response")
.get("X-Iroh-Response")
.map(|s| s.to_str().unwrap_or_default())
== Some(&expected_response);

Expand Down
Loading