Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(services): tidy push, svc, app proxy and admin handling (#3880)

Hampusf4f69840df0c
diff --git a/Cargo.lock b/Cargo.lock
index 93318327c..5bdbb776f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1821,6 +1821,8 @@ dependencies = [
"fluxer_common",
"futures",
"hmac",
+ "hyper",
+ "hyper-util",
"p256",
"percent-encoding",
"rand 0.10.3",
@@ -1831,6 +1833,7 @@ dependencies = [
"sha2 0.11.1",
"thiserror",
"tokio",
+ "tower-service",
"tracing",
"url",
]
diff --git a/fluxer_admin/src/middleware/csrf.rs b/fluxer_admin/src/middleware/csrf.rs
index dbc54424d..bc83c9994 100644
--- a/fluxer_admin/src/middleware/csrf.rs
+++ b/fluxer_admin/src/middleware/csrf.rs
@@ -3,7 +3,7 @@
use axum::{
body::{Body, to_bytes},
extract::{Request, State},
- http::{HeaderValue, Method, StatusCode, header},
+ http::{HeaderMap, HeaderValue, Method, StatusCode, header},
middleware::Next,
response::{IntoResponse, Response},
};
@@ -49,7 +49,7 @@ pub async fn csrf_protection(
.iter()
.any(|suffix| path.ends_with(suffix));
if !is_ignored {
- if !is_same_site_request(&request, config.admin_origin().as_deref()) {
+ if !is_same_site_request(request.headers(), config.admin_origin().as_deref()) {
return StatusCode::FORBIDDEN.into_response();
}
let header_token = extract_csrf_header(&request);
@@ -166,16 +166,14 @@ async fn extract_csrf_from_form_body(
Ok((request, token))
}
-fn is_same_site_request(request: &Request, admin_origin: Option<&str>) -> bool {
- if let Some(site) = request
- .headers()
+pub(crate) fn is_same_site_request(headers: &HeaderMap, admin_origin: Option<&str>) -> bool {
+ if let Some(site) = headers
.get("sec-fetch-site")
.and_then(|value| value.to_str().ok())
{
return matches!(site, "same-origin" | "same-site" | "none");
}
- match request
- .headers()
+ match headers
.get(header::ORIGIN)
.and_then(|value| value.to_str().ok())
{
diff --git a/fluxer_admin/src/routes/auth.rs b/fluxer_admin/src/routes/auth.rs
index 8b137b954..bd83f3ed6 100644
--- a/fluxer_admin/src/routes/auth.rs
+++ b/fluxer_admin/src/routes/auth.rs
@@ -3,6 +3,7 @@
use crate::{
api::client::{AdminApiClient, ApiError},
config::AdminConfig,
+ middleware::csrf,
oauth2,
session::{
self, SESSION_COOKIE_NAME, SESSION_MAX_AGE, admin_cookie_path, append_cookies,
@@ -10,7 +11,7 @@ use crate::{
},
sso,
state::AppState,
- templates::pages::login,
+ templates::{layout_scripts::HISTORY_CACHE_CLEAR_SCRIPT, pages::login},
};
use axum::{
Json, Router,
@@ -375,6 +376,9 @@ async fn finish_admin_sign_in(state: &AppState, user_id: &str, access_token: &st
async fn logout(State(state): State<AppState>, headers: axum::http::HeaderMap) -> Response {
let config = state.config();
+ if !csrf::is_same_site_request(&headers, config.admin_origin().as_deref()) {
+ return StatusCode::FORBIDDEN.into_response();
+ }
if let Some(session_value) = extract_cookie(&headers, SESSION_COOKIE_NAME)
&& let Some(session) = session::parse_session(&session_value, &config.secret_key_base)
{
@@ -435,6 +439,7 @@ fn oauth_start_page(
<body>
<p>Starting authentication...</p>
<script>
+{HISTORY_CACHE_CLEAR_SCRIPT}
(function () {{
var flow = {{state: {state}, verifier: {verifier}, createdAt: Date.now()}};
try {{
diff --git a/fluxer_admin/src/routes/system_actions/branding_assets.rs b/fluxer_admin/src/routes/system_actions/branding_assets.rs
index 30341a39d..79bd82523 100644
--- a/fluxer_admin/src/routes/system_actions/branding_assets.rs
+++ b/fluxer_admin/src/routes/system_actions/branding_assets.rs
@@ -9,9 +9,10 @@ use crate::{
middleware::{auth::AuthContext, flash::FlashData, htmx},
state::AppState,
templates,
- utils::forms::MultiValueForm,
+ utils::forms::clean_string,
};
-use axum::{extract::Request, http::HeaderMap, response::Response};
+use axum::{body, extract::Request, http::HeaderMap, response::Response};
+use url::form_urlencoded;
const BRANDING_ASSET_FORM_BYTES: usize = BRANDING_ASSET_MAX_IMAGE_CHARS * 3 + 64 * 1024;
@@ -29,8 +30,11 @@ pub(super) async fn post(
request: Request,
) -> Response {
let config = state.config();
- let form = MultiValueForm::from_request_with_limit(request, BRANDING_ASSET_FORM_BYTES).await;
- let (kind, image) = match build_branding_asset_change(form.as_ref(), action) {
+ let form = body::to_bytes(request.into_body(), BRANDING_ASSET_FORM_BYTES)
+ .await
+ .ok()
+ .map(|bytes| BrandingAssetForm::parse(&bytes));
+ let (kind, image) = match build_branding_asset_change(form, action) {
Ok(change) => change,
Err(message) => {
return instance_config_response(config, headers, FlashData::error(message));
@@ -66,19 +70,49 @@ pub(super) async fn post(
instance_config_response(config, headers, flash)
}
+struct BrandingAssetForm {
+ kind: Option<String>,
+ image: Option<String>,
+}
+
+impl BrandingAssetForm {
+ fn parse(bytes: &[u8]) -> Self {
+ let mut form = Self {
+ kind: None,
+ image: None,
+ };
+ for (key, value) in form_urlencoded::parse(bytes) {
+ let slot = match key.as_ref() {
+ "kind" => &mut form.kind,
+ "image" => &mut form.image,
+ _ => continue,
+ };
+ if slot.is_none() {
+ *slot = Some(value.into_owned());
+ }
+ }
+ form
+ }
+}
+
fn build_branding_asset_change(
- form: Option<&MultiValueForm>,
+ form: Option<BrandingAssetForm>,
action: Action,
) -> Result<(BrandingAssetKind, Option<String>), &'static str> {
let form = form.ok_or("Invalid form data")?;
let kind = form
- .first("kind")
+ .kind
+ .as_deref()
.and_then(BrandingAssetKind::parse)
.ok_or("Unknown branding asset")?;
if matches!(action, Action::Remove) {
return Ok((kind, None));
}
- let image = form.clean("image").ok_or("Choose an image to upload")?;
+ let image = form
+ .image
+ .as_deref()
+ .and_then(clean_string)
+ .ok_or("Choose an image to upload")?;
Ok((kind, Some(image)))
}
diff --git a/fluxer_admin/src/templates/layout_scripts.rs b/fluxer_admin/src/templates/layout_scripts.rs
index 63651e93c..9f909b560 100644
--- a/fluxer_admin/src/templates/layout_scripts.rs
+++ b/fluxer_admin/src/templates/layout_scripts.rs
@@ -18,6 +18,9 @@ pub const THEME_INIT_SCRIPT: &str = r#"
})();
"#;
+pub const HISTORY_CACHE_CLEAR_SCRIPT: &str =
+ "try { localStorage.removeItem('htmx-history-cache'); } catch (e) {}";
+
pub const THEME_TOGGLE_SCRIPT: &str = r#"
(function () {
if (window.__fluxerAdminThemeToggle) return;
diff --git a/fluxer_admin/src/templates/pages/badges.rs b/fluxer_admin/src/templates/pages/badges.rs
index 2cec1dfaf..efedd8662 100644
--- a/fluxer_admin/src/templates/pages/badges.rs
+++ b/fluxer_admin/src/templates/pages/badges.rs
@@ -32,6 +32,7 @@ struct BadgePermissions {
}
pub const BADGE_PAGE_SIZE: u32 = 50;
+const MAX_BADGE_TRAIL: usize = 100;
#[derive(Clone, Debug, Default)]
pub struct BadgePageState {
@@ -64,12 +65,17 @@ impl BadgePageState {
.filter(|query| !query.is_empty())
.map(|query| query.chars().take(64).collect()),
after: after.and_then(cursor),
- trail: trail
- .unwrap_or("")
- .split(',')
- .filter(|entry| !entry.is_empty())
- .map(cursor)
- .collect(),
+ trail: {
+ let mut entries: Vec<Option<String>> = trail
+ .unwrap_or("")
+ .rsplit(',')
+ .filter(|entry| !entry.is_empty())
+ .take(MAX_BADGE_TRAIL)
+ .map(cursor)
+ .collect();
+ entries.reverse();
+ entries
+ },
}
}
diff --git a/fluxer_admin/src/templates/pages/guilds_list.rs b/fluxer_admin/src/templates/pages/guilds_list.rs
index 3d79a0055..b2d07686b 100644
--- a/fluxer_admin/src/templates/pages/guilds_list.rs
+++ b/fluxer_admin/src/templates/pages/guilds_list.rs
@@ -21,6 +21,8 @@ use crate::{
};
use maud::{Markup, html};
+const MAX_GUILD_ID_LOOKUP: usize = 200;
+
pub struct GuildsListParams {
pub q: String,
pub requested_ids: Vec<String>,
@@ -38,7 +40,7 @@ impl GuildsListParams {
let ids = ids.unwrap_or_default();
Self {
q: q.unwrap_or_default().trim().to_owned(),
- requested_ids: parse_comma_separated(&ids),
+ requested_ids: parse_guild_ids(&ids),
limit: match limit.unwrap_or(50) {
25 => 25,
100 => 100,
@@ -62,6 +64,19 @@ impl GuildsListParams {
}
}
+fn parse_guild_ids(ids: &str) -> Vec<String> {
+ let mut parsed: Vec<String> = Vec::new();
+ for id in parse_comma_separated(ids) {
+ if parsed.len() == MAX_GUILD_ID_LOOKUP {
+ break;
+ }
+ if id.chars().all(|ch| ch.is_ascii_digit()) && !parsed.contains(&id) {
+ parsed.push(id);
+ }
+ }
+ parsed
+}
+
pub struct GuildsListResults<'a> {
pub guilds: Option<&'a [GuildInfo]>,
pub total: Option<u64>,
diff --git a/fluxer_admin/src/templates/pages/login.rs b/fluxer_admin/src/templates/pages/login.rs
index b3b18dd23..3500618cc 100644
--- a/fluxer_admin/src/templates/pages/login.rs
+++ b/fluxer_admin/src/templates/pages/login.rs
@@ -5,7 +5,7 @@ use crate::{
config::AdminConfig,
templates::{
layout::{admin_title, favicon_links, product_name},
- layout_scripts::THEME_INIT_SCRIPT,
+ layout_scripts::{HISTORY_CACHE_CLEAR_SCRIPT, THEME_INIT_SCRIPT},
},
};
use maud::{DOCTYPE, Markup, PreEscaped, html};
@@ -29,6 +29,7 @@ pub fn login_page(
(favicon_links(config, branding))
}
body class="flex min-h-[100dvh] items-center justify-center bg-neutral-50 p-4" {
+ script { (PreEscaped(HISTORY_CACHE_CLEAR_SCRIPT)) }
main class="w-full max-w-sm" {
div class="flex flex-col gap-4 items-stretch" {
div class="rounded-lg bg-white transition-all border border-neutral-200 p-8" {
diff --git a/fluxer_admin/src/templates/pages/users_list.rs b/fluxer_admin/src/templates/pages/users_list.rs
index 63c306a8b..e122c9ed8 100644
--- a/fluxer_admin/src/templates/pages/users_list.rs
+++ b/fluxer_admin/src/templates/pages/users_list.rs
@@ -30,6 +30,7 @@ use crate::{
},
};
use maud::{Markup, html};
+use std::collections::HashSet;
pub struct UserListParams {
pub q: String,
@@ -156,14 +157,17 @@ pub fn users_list_page(config: &AdminConfig, auth: &AuthContext, view: UserListV
admin_layout(config, auth, "Users", "users", None, content)
}
+const MAX_ID_LOOKUP: usize = 500;
+
fn parse_ids_query(ids_query: &str) -> Vec<String> {
+ let mut seen = HashSet::new();
let mut ids = Vec::new();
for raw in ids_query.split(',') {
+ if ids.len() >= MAX_ID_LOOKUP {
+ break;
+ }
let id = raw.trim();
- if !id.is_empty()
- && id.chars().all(|ch| ch.is_ascii_digit())
- && !ids.iter().any(|v| v == id)
- {
+ if !id.is_empty() && id.chars().all(|ch| ch.is_ascii_digit()) && seen.insert(id) {
ids.push(id.to_owned());
}
}
diff --git a/fluxer_admin/src/utils/forms.rs b/fluxer_admin/src/utils/forms.rs
index fb01f8b8d..3584f7f07 100644
--- a/fluxer_admin/src/utils/forms.rs
+++ b/fluxer_admin/src/utils/forms.rs
@@ -33,11 +33,9 @@ pub struct MultiValueForm {
impl MultiValueForm {
pub async fn from_request(request: Request) -> Option<Self> {
- Self::from_request_with_limit(request, MAX_FORM_BYTES).await
- }
-
- pub async fn from_request_with_limit(request: Request, max_bytes: usize) -> Option<Self> {
- let bytes = body::to_bytes(request.into_body(), max_bytes).await.ok()?;
+ let bytes = body::to_bytes(request.into_body(), MAX_FORM_BYTES)
+ .await
+ .ok()?;
Some(Self::parse(bytes.as_ref()))
}
diff --git a/fluxer_app_proxy/src/routes/assets_proxy.rs b/fluxer_app_proxy/src/routes/assets_proxy.rs
index 951a562b4..4d8b93c8a 100644
--- a/fluxer_app_proxy/src/routes/assets_proxy.rs
+++ b/fluxer_app_proxy/src/routes/assets_proxy.rs
@@ -7,17 +7,21 @@ use crate::static_asset_policy::{
copy_asset_response_headers, guess_mime, is_font_mime,
};
use axum::{
- body::Body,
+ body::{Body, Bytes},
extract::{Path, State},
http::{HeaderMap, HeaderValue, StatusCode},
response::{IntoResponse, Response},
};
use std::time::Duration;
-use tokio::sync::{OwnedSemaphorePermit, TryAcquireError};
+use tokio::sync::{OwnedSemaphorePermit, TryAcquireError, mpsc};
+use tokio::time::Instant;
const ASSET_REQUEST_TIMEOUT: Duration = Duration::from_secs(15);
const MAX_ASSET_SIZE_BYTES: u64 = 100 * 1024 * 1024;
const BUILD_ASSET_HASH_LEN: usize = 16;
+const ASSET_BODY_BUFFERED_CHUNKS: usize = 4;
+
+type AssetBodyItem = Result<Option<Bytes>, axum::Error>;
pub async fn proxy_assets(
State(state): State<AppState>,
@@ -56,6 +60,7 @@ pub async fn proxy_assets(
}
};
+ let deadline = Instant::now() + ASSET_REQUEST_TIMEOUT;
let request_builder = state
.http_client
.get(&target_url)
@@ -92,61 +97,86 @@ pub async fn proxy_assets(
state.csp.asset_header(),
);
- let body = upstream_asset_body(upstream_response, upstream_slot);
+ let body = upstream_asset_body(upstream_response, upstream_slot, deadline);
let mut response = Response::new(body);
*response.status_mut() = status;
*response.headers_mut() = response_headers;
response
}
-struct UpstreamAssetReadState {
- response: reqwest::Response,
- _permit: OwnedSemaphorePermit,
- remaining_bytes: u64,
-}
-
fn upstream_asset_body(
upstream_response: reqwest::Response,
upstream_slot: OwnedSemaphorePermit,
+ deadline: Instant,
) -> Body {
- let state = UpstreamAssetReadState {
- response: upstream_response,
- _permit: upstream_slot,
- remaining_bytes: MAX_ASSET_SIZE_BYTES,
- };
- Body::from_stream(futures_util::stream::try_unfold(
- state,
- |mut state| async move {
- let Some(chunk) = state
- .response
- .chunk()
- .await
- .inspect_err(|err| {
- tracing::warn!(%err, "upstream asset body ended early");
- })
- .map_err(axum::Error::new)?
- else {
- return Ok(None);
- };
- let chunk_bytes = chunk.len() as u64;
- if chunk_bytes > state.remaining_bytes {
- tracing::warn!(
- maximum_bytes = MAX_ASSET_SIZE_BYTES,
- remaining_bytes = state.remaining_bytes,
- chunk_bytes,
- "upstream asset body exceeds size cap"
- );
- return Err(axum::Error::new(std::io::Error::new(
- std::io::ErrorKind::InvalidData,
- format!("upstream asset body exceeds {MAX_ASSET_SIZE_BYTES} bytes"),
- )));
+ let (sender, receiver) = mpsc::channel::<AssetBodyItem>(ASSET_BODY_BUFFERED_CHUNKS);
+ tokio::spawn(async move {
+ let _permit = upstream_slot;
+ if tokio::time::timeout_at(deadline, forward_upstream_asset(upstream_response, &sender))
+ .await
+ .is_err()
+ {
+ tracing::warn!("upstream asset body timed out");
+ }
+ });
+ Body::from_stream(futures_util::stream::unfold(
+ Some(receiver),
+ |receiver| async move {
+ let mut receiver = receiver?;
+ match receiver.recv().await {
+ Some(Ok(Some(chunk))) => Some((Ok(chunk), Some(receiver))),
+ Some(Ok(None)) => None,
+ Some(Err(err)) => Some((Err(err), None)),
+ None => Some((
+ Err(axum::Error::new(std::io::Error::new(
+ std::io::ErrorKind::UnexpectedEof,
+ "upstream asset body ended early",
+ ))),
+ None,
+ )),
}
- state.remaining_bytes -= chunk_bytes;
- Ok(Some((chunk, state)))
},
))
}
+async fn forward_upstream_asset(
+ mut response: reqwest::Response,
+ sender: &mpsc::Sender<AssetBodyItem>,
+) {
+ let mut remaining_bytes = MAX_ASSET_SIZE_BYTES;
+ loop {
+ let item = match response.chunk().await {
+ Ok(Some(chunk)) => {
+ let chunk_bytes = chunk.len() as u64;
+ if chunk_bytes > remaining_bytes {
+ tracing::warn!(
+ maximum_bytes = MAX_ASSET_SIZE_BYTES,
+ remaining_bytes,
+ chunk_bytes,
+ "upstream asset body exceeds size cap"
+ );
+ Err(axum::Error::new(std::io::Error::new(
+ std::io::ErrorKind::InvalidData,
+ format!("upstream asset body exceeds {MAX_ASSET_SIZE_BYTES} bytes"),
+ )))
+ } else {
+ remaining_bytes -= chunk_bytes;
+ Ok(Some(chunk))
+ }
+ }
+ Ok(None) => Ok(None),
+ Err(err) => {
+ tracing::warn!(%err, "upstream asset body ended early");
+ Err(axum::Error::new(err))
+ }
+ };
+ let finished = !matches!(item, Ok(Some(_)));
+ if sender.send(item).await.is_err() || finished {
+ return;
+ }
+ }
+}
+
fn is_build_asset_path(path: &str) -> bool {
let (module, file) = match path.split_once('/') {
Some((module, file)) => (Some(module), file),
diff --git a/fluxer_app_proxy/src/routes/spa_index.rs b/fluxer_app_proxy/src/routes/spa_index.rs
index cada948ec..5655ded72 100644
--- a/fluxer_app_proxy/src/routes/spa_index.rs
+++ b/fluxer_app_proxy/src/routes/spa_index.rs
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
use crate::branding_images::BrandingSnapshot;
-use crate::config::HttpEndpoint;
+use crate::config::{HttpEndpoint, HttpUrl};
use crate::csp::{CspHeaderValues, inline_script_hashes};
use crate::instance_brand_colors::{THEMES, instance_brand_variables};
use crate::state::{
@@ -14,8 +14,9 @@ use axum::{
http::{HeaderMap, HeaderName, HeaderValue, StatusCode, header},
response::{IntoResponse, Response},
};
+use futures_util::FutureExt;
use std::path::Path;
-use std::sync::Arc;
+use std::sync::{Arc, PoisonError};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use super::assets_proxy::serve_local_asset;
@@ -532,7 +533,7 @@ fn is_auth_entry_path(path: &str) -> bool {
#[allow(clippy::result_large_err)]
async fn load_spa_index_html(state: &AppState, request_path: &str) -> Result<String, Response> {
- let index_upstream_url = match &state.spa_index_source {
+ let upstream = match &state.spa_index_source {
SpaIndexSource::Bundled {
shell,
auth_entry_shell,
@@ -543,49 +544,79 @@ async fn load_spa_index_html(state: &AppState, request_path: &str) -> Result<Str
.unwrap_or(shell);
return Ok(shell.to_string());
}
- SpaIndexSource::Upstream(index_upstream_url) => index_upstream_url,
+ SpaIndexSource::Upstream(upstream) => upstream,
};
- let response = state
- .http_client
+ let fetch = {
+ let mut in_flight = upstream
+ .in_flight
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner);
+ match in_flight.as_ref().filter(|(_, task)| !task.is_finished()) {
+ Some((fetch, _)) => fetch.clone(),
+ None => {
+ let task = tokio::spawn(fetch_upstream_spa_index(
+ state.http_client.clone(),
+ upstream.url.clone(),
+ ));
+ let abort_handle = task.abort_handle();
+ let fetch = task
+ .map(|joined| joined.unwrap_or(Err(StatusCode::INTERNAL_SERVER_ERROR)))
+ .boxed()
+ .shared();
+ *in_flight = Some((fetch.clone(), abort_handle));
+ fetch
+ }
+ }
+ };
+ fetch
+ .await
+ .map(|html| html.to_string())
+ .map_err(IntoResponse::into_response)
+}
+
+async fn fetch_upstream_spa_index(
+ http_client: reqwest::Client,
+ index_upstream_url: Arc<HttpUrl>,
+) -> Result<Arc<str>, StatusCode> {
+ let mut response = http_client
.get(index_upstream_url.as_url().clone())
.timeout(Duration::from_secs(10))
.send()
.await
.map_err(|err| {
tracing::error!(url = %index_upstream_url, %err, "failed to fetch upstream index.html");
- StatusCode::BAD_GATEWAY.into_response()
+ StatusCode::BAD_GATEWAY
})?;
if !response.status().is_success() {
let status = response.status();
tracing::error!(url = %index_upstream_url, %status, "upstream index.html returned non-success status");
- return Err(StatusCode::BAD_GATEWAY.into_response());
+ return Err(StatusCode::BAD_GATEWAY);
}
if response
.content_length()
.is_some_and(|length| length > MAX_SPA_INDEX_BYTES as u64)
{
tracing::error!(url = %index_upstream_url, "upstream index.html exceeds the size limit");
- return Err(StatusCode::BAD_GATEWAY.into_response());
+ return Err(StatusCode::BAD_GATEWAY);
}
- let mut response = response;
let mut bytes: Vec<u8> = Vec::new();
loop {
let chunk = response.chunk().await.map_err(|err| {
tracing::error!(url = %index_upstream_url, %err, "failed to read upstream index.html body");
- StatusCode::BAD_GATEWAY.into_response()
+ StatusCode::BAD_GATEWAY
})?;
let Some(chunk) = chunk else {
break;
};
if chunk.len() > MAX_SPA_INDEX_BYTES - bytes.len() {
tracing::error!(url = %index_upstream_url, "upstream index.html exceeds the size limit");
- return Err(StatusCode::BAD_GATEWAY.into_response());
+ return Err(StatusCode::BAD_GATEWAY);
}
bytes.extend_from_slice(&chunk);
}
- String::from_utf8(bytes).map_err(|err| {
+ String::from_utf8(bytes).map(Arc::from).map_err(|err| {
tracing::error!(url = %index_upstream_url, %err, "upstream index.html is not valid UTF-8");
- StatusCode::BAD_GATEWAY.into_response()
+ StatusCode::BAD_GATEWAY
})
}
diff --git a/fluxer_app_proxy/src/state.rs b/fluxer_app_proxy/src/state.rs
index 08e2b1dc5..562b4e03a 100644
--- a/fluxer_app_proxy/src/state.rs
+++ b/fluxer_app_proxy/src/state.rs
@@ -5,10 +5,13 @@ use crate::config::{AppProxyConfig, HttpUrl};
use crate::csp::CompiledCspPolicy;
use crate::local_files::LocalFileStore;
use crate::routes::{bundles_app_assets, present_local_asset_prefixes};
-use std::sync::Arc;
+use axum::http::StatusCode;
+use futures_util::future::{BoxFuture, Shared};
+use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::io::AsyncReadExt;
use tokio::sync::Semaphore;
+use tokio::task::AbortHandle;
pub const MAX_SPA_INDEX_BYTES: usize = 4 * 1024 * 1024;
pub const MAX_RENDERED_SPA_INDEX_BYTES: usize = 8 * 1024 * 1024;
@@ -39,19 +42,30 @@ impl Default for AppProxyBudgets {
pub const AUTH_ENTRY_SHELL_FILE: &str = "auth-index.html";
+pub type UpstreamSpaIndexFetch = Shared<BoxFuture<'static, Result<Arc<str>, StatusCode>>>;
+
+#[derive(Clone)]
+pub struct UpstreamSpaIndex {
+ pub url: Arc<HttpUrl>,
+ pub in_flight: Arc<Mutex<Option<(UpstreamSpaIndexFetch, AbortHandle)>>>,
+}
+
#[derive(Clone)]
pub enum SpaIndexSource {
Bundled {
shell: Arc<str>,
auth_entry_shell: Option<Arc<str>>,
},
- Upstream(Arc<HttpUrl>),
+ Upstream(UpstreamSpaIndex),
}
impl SpaIndexSource {
pub async fn load(config: &AppProxyConfig) -> anyhow::Result<Self> {
if let Some(index_upstream_url) = &config.index_upstream_url {
- return Ok(Self::Upstream(Arc::new(index_upstream_url.clone())));
+ return Ok(Self::Upstream(UpstreamSpaIndex {
+ url: Arc::new(index_upstream_url.clone()),
+ in_flight: Arc::new(Mutex::new(None)),
+ }));
}
let static_dir = std::path::Path::new(&config.static_dir);
let shell = read_shell(&static_dir.join("index.html")).await?;
diff --git a/fluxer_push/Cargo.toml b/fluxer_push/Cargo.toml
index 2636eae26..59292e14d 100644
--- a/fluxer_push/Cargo.toml
+++ b/fluxer_push/Cargo.toml
@@ -24,6 +24,8 @@ base64 = "0.23.1"
clap = { version = "4.6.7", features = ["derive"] }
futures = "0.3.34"
hmac = "0.13.0"
+hyper = { version = "1.12.0", default-features = false, features = ["http1", "http2", "server"] }
+hyper-util = { version = "0.1.21", default-features = false, features = ["http1", "http2", "server-auto", "tokio"] }
percent-encoding = "2.3.2"
p256 = { version = "0.14.0", default-features = false, features = ["ecdh", "ecdsa", "pkcs8", "pem", "std"] }
rand = "0.10.3"
@@ -34,5 +36,6 @@ serde_json = "1.0.154"
sha2 = "0.11.1"
thiserror = "2.0.21"
tokio = { version = "1.54.1", features = ["macros", "net", "rt-multi-thread", "signal", "sync", "time"] }
+tower-service = "0.3.3"
tracing = "0.1.44"
url = "2.5.8"
diff --git a/fluxer_push/src/relay/mod.rs b/fluxer_push/src/relay/mod.rs
index ec736292a..56e913b34 100644
--- a/fluxer_push/src/relay/mod.rs
+++ b/fluxer_push/src/relay/mod.rs
@@ -313,7 +313,6 @@ async fn forward(
body: Body,
bytes: &mut usize,
) -> Result<(), Rejection> {
- let _permit = state.quota.admit()?;
let target = resolve(state, incoming)?;
if !device_token_is_shaped(incoming.leg, &incoming.device_token) {
return Err(Rejection::new(Reason::DeviceTokenInvalid));
@@ -322,9 +321,17 @@ async fn forward(
return Err(Rejection::new(Reason::BadRequest));
}
let delivery = Delivery::parse(header(headers, URGENCY_HEADER), header(headers, TTL_HEADER))?;
+ let lowered;
+ let quota_token: &str = match incoming.leg {
+ RelayLeg::Apns | RelayLeg::ApnsVoip => {
+ lowered = incoming.device_token.to_ascii_lowercase();
+ &lowered
+ }
+ RelayLeg::Fcm => &incoming.device_token,
+ };
state.quota.take(
&state.metrics,
- &incoming.device_token,
+ quota_token,
match incoming.leg {
RelayLeg::ApnsVoip => Urgency::Alert,
_ => delivery.urgency,
@@ -344,6 +351,7 @@ async fn forward(
if body.is_empty() {
return Err(Rejection::new(Reason::BadRequest));
}
+ let _permit = state.quota.admit()?;
let payload = envelope::encode_payload(&body);
match target {
diff --git a/fluxer_push/src/relay/quota.rs b/fluxer_push/src/relay/quota.rs
index 3be7e1f6b..50e47be37 100644
--- a/fluxer_push/src/relay/quota.rs
+++ b/fluxer_push/src/relay/quota.rs
@@ -17,6 +17,8 @@ const SECONDS_PER_MINUTE: f64 = 60.0;
const KEY_BYTES: usize = 16;
const IPV4_PREFIX_BYTES: usize = 3;
const IPV6_PREFIX_BYTES: usize = 8;
+const IPV4_FAMILY_TAG: u8 = 4;
+const IPV6_FAMILY_TAG: u8 = 6;
const MAX_GENERATION_AGE: Duration = Duration::from_secs(300);
type Key = [u8; KEY_BYTES];
@@ -112,10 +114,12 @@ pub fn source_key(ip: IpAddr) -> Key {
let mut key = [0u8; KEY_BYTES];
match ip.to_canonical() {
IpAddr::V4(ip) => {
- key[..IPV4_PREFIX_BYTES].copy_from_slice(&ip.octets()[..IPV4_PREFIX_BYTES])
+ key[0] = IPV4_FAMILY_TAG;
+ key[1..=IPV4_PREFIX_BYTES].copy_from_slice(&ip.octets()[..IPV4_PREFIX_BYTES]);
}
IpAddr::V6(ip) => {
- key[..IPV6_PREFIX_BYTES].copy_from_slice(&ip.octets()[..IPV6_PREFIX_BYTES])
+ key[0] = IPV6_FAMILY_TAG;
+ key[1..=IPV6_PREFIX_BYTES].copy_from_slice(&ip.octets()[..IPV6_PREFIX_BYTES]);
}
}
key
diff --git a/fluxer_push/src/server.rs b/fluxer_push/src/server.rs
index 67d309942..5347561fc 100644
--- a/fluxer_push/src/server.rs
+++ b/fluxer_push/src/server.rs
@@ -9,19 +9,32 @@ use crate::rpc::RpcClient;
use crate::tokens::TokenCache;
use crate::vendor;
use axum::Router;
+use axum::extract::ConnectInfo;
use fluxer_common::secret::SecretString;
use fluxer_svc::shutdown::{DEFAULT_DRAIN_TIMEOUT, drain_with_timeout, wait_for_shutdown};
use fluxer_svc::transport::NatsTransport;
+use hyper::body::Incoming;
+use hyper::service::service_fn;
+use hyper_util::rt::{TokioExecutor, TokioIo};
+use hyper_util::server::conn::auto::Builder;
+use std::io;
use std::net::SocketAddr;
+use std::pin::pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
-use tokio::net::TcpListener;
-use tokio::sync::{oneshot, watch};
+use tokio::net::{TcpListener, TcpStream};
+use tokio::sync::{Notify, oneshot, watch};
use tokio::task::JoinHandle;
-use tracing::info;
+use tokio::time::{Instant, sleep};
+use tower_service::Service;
+use tracing::{error, info};
const JOB_DRAIN_TIMEOUT: Duration = Duration::from_secs(25);
+const FIRST_REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
+const CONNECTION_IDLE_TIMEOUT: Duration = Duration::from_secs(120);
+const CONNECTION_CLOSE_GRACE: Duration = Duration::from_secs(15);
+const ACCEPT_ERROR_PAUSE: Duration = Duration::from_secs(1);
pub struct Sidecar {
metrics: Arc<Metrics>,
@@ -48,7 +61,7 @@ impl Sidecar {
pub struct Serving {
stop: oneshot::Sender<()>,
- served: JoinHandle<std::io::Result<()>>,
+ served: JoinHandle<()>,
}
impl Serving {
@@ -67,19 +80,101 @@ impl Serving {
pub async fn serve(addr: SocketAddr, router: Router) -> anyhow::Result<Serving> {
let listener = TcpListener::bind(addr).await?;
let (stop, stopped) = oneshot::channel::<()>();
- let served = tokio::spawn(async move {
- axum::serve(
- listener,
- router.into_make_service_with_connect_info::<SocketAddr>(),
- )
- .with_graceful_shutdown(async move {
- let _ = stopped.await;
- })
- .await
- });
+ let served = tokio::spawn(accept_connections(listener, router, stopped));
Ok(Serving { stop, served })
}
+async fn accept_connections(
+ listener: TcpListener,
+ router: Router,
+ mut stopped: oneshot::Receiver<()>,
+) {
+ let stopping = watch::Sender::new(false);
+ let (open_tx, open_rx) = watch::channel(());
+ loop {
+ let (stream, remote_addr) = tokio::select! {
+ accepted = listener.accept() => match accepted {
+ Ok(accepted) => accepted,
+ Err(err) => {
+ pause_after_accept_error(err).await;
+ continue;
+ }
+ },
+ _ = &mut stopped => break,
+ };
+ tokio::spawn(serve_connection(
+ stream,
+ remote_addr,
+ router.clone(),
+ stopping.subscribe(),
+ open_rx.clone(),
+ ));
+ }
+ stopping.send_replace(true);
+ drop(open_rx);
+ drop(listener);
+ open_tx.closed().await;
+}
+
+async fn pause_after_accept_error(err: io::Error) {
+ if matches!(
+ err.kind(),
+ io::ErrorKind::ConnectionRefused
+ | io::ErrorKind::ConnectionAborted
+ | io::ErrorKind::ConnectionReset
+ ) {
+ return;
+ }
+ error!(error = %err, "accept error");
+ sleep(ACCEPT_ERROR_PAUSE).await;
+}
+
+async fn serve_connection(
+ stream: TcpStream,
+ remote_addr: SocketAddr,
+ router: Router,
+ mut stopping: watch::Receiver<bool>,
+ _open: watch::Receiver<()>,
+) {
+ let activity = Arc::new(Notify::new());
+ let service = service_fn({
+ let activity = Arc::clone(&activity);
+ move |mut req: hyper::Request<Incoming>| {
+ activity.notify_one();
+ req.extensions_mut().insert(ConnectInfo(remote_addr));
+ router.clone().call(req)
+ }
+ });
+ let mut builder = Builder::new(TokioExecutor::new());
+ builder.http2().enable_connect_protocol();
+ let mut conn = pin!(builder.serve_connection_with_upgrades(TokioIo::new(stream), service));
+ let mut deadline = pin!(sleep(FIRST_REQUEST_TIMEOUT));
+ let mut served_request = false;
+ let mut closing = false;
+ loop {
+ tokio::select! {
+ _ = conn.as_mut() => break,
+ _ = activity.notified(), if !closing => {
+ served_request = true;
+ deadline.as_mut().reset(Instant::now() + CONNECTION_IDLE_TIMEOUT);
+ }
+ _ = stopping.wait_for(|stopping| *stopping), if !closing => {
+ conn.as_mut().graceful_shutdown();
+ closing = true;
+ deadline.as_mut().reset(Instant::now() + CONNECTION_CLOSE_GRACE);
+ }
+ _ = deadline.as_mut() => {
+ if closing || !served_request {
+ break;
+ }
+ conn.as_mut().graceful_shutdown();
+ closing = true;
+ deadline.as_mut().reset(Instant::now() + CONNECTION_CLOSE_GRACE);
+ }
+ }
+ }
+}
+
pub struct AppState {
pub(crate) cfg: DeliveryConfig,
pub(crate) metrics: Arc<Metrics>,
diff --git a/fluxer_svc/src/shard.rs b/fluxer_svc/src/shard.rs
index 6e9b76562..aefebcbfd 100644
--- a/fluxer_svc/src/shard.rs
+++ b/fluxer_svc/src/shard.rs
@@ -13,6 +13,7 @@ use tokio::task::JoinSet;
use tracing::{debug, info, warn};
const MAX_SHARD_REQUEST_BYTES: usize = 2 * 1024 * 1024;
+const MAX_IN_FLIGHT_REQUEST_BYTES: usize = 32 * 1024 * 1024;
const SHARD_DRAIN_LINGER: Duration = Duration::from_millis(500);
const SHARD_ANNOUNCE_TIMEOUT: Duration = Duration::from_secs(1);
const DRAIN_IDLE_UNSUBSCRIBE: Duration = Duration::from_millis(250);
@@ -237,6 +238,7 @@ where
let health_addr = config.listen_addr;
let is_serving = Arc::new(AtomicBool::new(false));
let request_permits = Arc::new(Semaphore::new(config.max_concurrent_requests));
+ let request_byte_budget = Arc::new(Semaphore::new(MAX_IN_FLIGHT_REQUEST_BYTES));
let mut tasks = JoinSet::new();
@@ -336,9 +338,26 @@ where
}
Err(TryAcquireError::Closed) => return anyhow::Ok(()),
};
+ let request_bytes =
+ msg.payload().len().clamp(1, MAX_SHARD_REQUEST_BYTES) as u32;
+ let byte_permit = match request_byte_budget
+ .clone()
+ .try_acquire_many_owned(request_bytes)
+ {
+ Ok(permit) => permit,
+ Err(TryAcquireError::NoPermits) => {
+ debug!(request_bytes, "shedding request, byte budget exhausted");
+ metrics.record_request();
+ metrics.record_request_error();
+ reply_json_error(&msg, &transport, "overloaded").await;
+ continue;
+ }
+ Err(TryAcquireError::Closed) => return anyhow::Ok(()),
+ };
let request = handle(msg);
requests.spawn(async move {
let _permit = permit;
+ let _byte_permit = byte_permit;
request.await;
});
}