this repo has no description
1use crate::state::AppState; 2use axum::{ 3 body::Bytes, 4 extract::{Path, Query, State}, 5 http::{HeaderMap, Method, StatusCode}, 6 response::{IntoResponse, Response}, 7}; 8use crate::api::proxy_client::proxy_client; 9use std::collections::HashMap; 10use tracing::{error, info}; 11pub async fn proxy_handler( 12 State(state): State<AppState>, 13 Path(method): Path<String>, 14 method_verb: Method, 15 headers: HeaderMap, 16 Query(params): Query<HashMap<String, String>>, 17 body: Bytes, 18) -> Response { 19 let proxy_header = headers 20 .get("atproto-proxy") 21 .and_then(|h| h.to_str().ok()) 22 .map(|s| s.to_string()); 23 let appview_url = match &proxy_header { 24 Some(url) => url.clone(), 25 None => match std::env::var("APPVIEW_URL") { 26 Ok(url) => url, 27 Err(_) => { 28 return (StatusCode::BAD_GATEWAY, "No upstream AppView configured").into_response(); 29 } 30 }, 31 }; 32 let target_url = format!("{}/xrpc/{}", appview_url, method); 33 info!("Proxying {} request to {}", method_verb, target_url); 34 let client = proxy_client(); 35 let mut request_builder = client.request(method_verb, &target_url).query(&params); 36 let mut auth_header_val = headers.get("Authorization").map(|h| h.clone()); 37 if let Some(aud) = &proxy_header { 38 if let Some(token) = crate::auth::extract_bearer_token_from_header( 39 headers.get("Authorization").and_then(|h| h.to_str().ok()) 40 ) { 41 if let Ok(auth_user) = crate::auth::validate_bearer_token(&state.db, &token).await { 42 if let Some(key_bytes) = auth_user.key_bytes { 43 if let Ok(new_token) = 44 crate::auth::create_service_token(&auth_user.did, aud, &method, &key_bytes) 45 { 46 if let Ok(val) = 47 axum::http::HeaderValue::from_str(&format!("Bearer {}", new_token)) 48 { 49 auth_header_val = Some(val); 50 } 51 } 52 } 53 } 54 } 55 } 56 if let Some(val) = auth_header_val { 57 request_builder = request_builder.header("Authorization", val); 58 } 59 for (key, value) in headers.iter() { 60 if key != "host" && key != "content-length" && key != "authorization" { 61 request_builder = request_builder.header(key, value); 62 } 63 } 64 request_builder = request_builder.body(body); 65 match request_builder.send().await { 66 Ok(resp) => { 67 let status = resp.status(); 68 let headers = resp.headers().clone(); 69 let body = match resp.bytes().await { 70 Ok(b) => b, 71 Err(e) => { 72 error!("Error reading proxy response body: {:?}", e); 73 return (StatusCode::BAD_GATEWAY, "Error reading upstream response") 74 .into_response(); 75 } 76 }; 77 let mut response_builder = Response::builder().status(status); 78 for (key, value) in headers.iter() { 79 response_builder = response_builder.header(key, value); 80 } 81 match response_builder.body(axum::body::Body::from(body)) { 82 Ok(r) => r, 83 Err(e) => { 84 error!("Error building proxy response: {:?}", e); 85 (StatusCode::INTERNAL_SERVER_ERROR, "Internal Server Error").into_response() 86 } 87 } 88 } 89 Err(e) => { 90 error!("Error sending proxy request: {:?}", e); 91 if e.is_timeout() { 92 (StatusCode::GATEWAY_TIMEOUT, "Upstream Timeout").into_response() 93 } else { 94 (StatusCode::BAD_GATEWAY, "Upstream Error").into_response() 95 } 96 } 97 } 98}