mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-05 22:36:23 +01:00
Proxy storage: flows table, bodies in blobs.sqlite, settings (#751)
This commit is contained in:
1 parent
694c0272cd
commit
b5508e6bb1
25 files changed
+942
-289
No files matched your search
@@ -16,47 +16,102 @@ pub struct ProxyHeader {
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
/// One proxied request and its response. The bodies are in `blobs.sqlite`, not the row.
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
#[ts(export, export_to = "gen_models.ts")]
|
||||
#[enum_def(table_name = "http_exchanges")]
|
||||
pub struct HttpExchange {
|
||||
#[enum_def(table_name = "flows")]
|
||||
pub struct Flow {
|
||||
pub id: String,
|
||||
pub created_at: NaiveDateTime,
|
||||
pub updated_at: NaiveDateTime,
|
||||
pub url: String,
|
||||
pub started_at: NaiveDateTime,
|
||||
pub method: String,
|
||||
pub req_headers: Vec<ProxyHeader>,
|
||||
pub req_body: Option<Vec<u8>>,
|
||||
pub res_status: Option<i32>,
|
||||
pub res_headers: Vec<ProxyHeader>,
|
||||
pub res_body: Option<Vec<u8>>,
|
||||
pub url: String,
|
||||
/// Version spoken by the client.
|
||||
pub client_http_version: String,
|
||||
/// Version spoken with the upstream server.
|
||||
pub upstream_http_version: Option<String>,
|
||||
pub remote_addr: Option<String>,
|
||||
pub status: Option<i32>,
|
||||
/// The response's Content-Type.
|
||||
pub content_type: Option<String>,
|
||||
pub request_headers: Vec<ProxyHeader>,
|
||||
pub response_headers: Vec<ProxyHeader>,
|
||||
/// Bytes of the request body on the wire, captured or not.
|
||||
#[ts(type = "number")]
|
||||
pub request_size: i64,
|
||||
/// Bytes of the response body on the wire, captured or not.
|
||||
#[ts(type = "number")]
|
||||
pub response_size: i64,
|
||||
/// Only the first `capture_body_limit` bytes of the body are stored.
|
||||
pub request_truncated: bool,
|
||||
/// Only the first `capture_body_limit` bytes of the body are stored.
|
||||
pub response_truncated: bool,
|
||||
/// Milliseconds from `started_at` to the response headers.
|
||||
pub elapsed_headers_ms: Option<i32>,
|
||||
/// Milliseconds from `started_at` to the end of the response.
|
||||
pub elapsed_ms: Option<i32>,
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum FlowBody {
|
||||
Request,
|
||||
Response,
|
||||
}
|
||||
|
||||
impl Flow {
|
||||
/// The blob store id of one of a flow's bodies.
|
||||
pub fn body_id(flow_id: &str, body: FlowBody) -> String {
|
||||
match body {
|
||||
FlowBody::Request => format!("{flow_id}.request"),
|
||||
FlowBody::Response => format!("{flow_id}.response"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn headers(&self, body: FlowBody) -> &[ProxyHeader] {
|
||||
match body {
|
||||
FlowBody::Request => &self.request_headers,
|
||||
FlowBody::Response => &self.response_headers,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn truncated(&self, body: FlowBody) -> bool {
|
||||
match body {
|
||||
FlowBody::Request => self.request_truncated,
|
||||
FlowBody::Response => self.response_truncated,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_header<'a>(headers: &'a [ProxyHeader], name: &str) -> Option<&'a str> {
|
||||
headers.iter().find(|h| h.name.eq_ignore_ascii_case(name)).map(|h| h.value.as_str())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(export, export_to = "gen_models.ts")]
|
||||
pub struct ModelPayload {
|
||||
pub model: HttpExchange,
|
||||
pub model: Flow,
|
||||
pub change: ModelChangeEvent,
|
||||
}
|
||||
|
||||
impl UpsertModelInfo for HttpExchange {
|
||||
impl UpsertModelInfo for Flow {
|
||||
fn table_name() -> impl IntoTableRef + IntoIden {
|
||||
HttpExchangeIden::Table
|
||||
FlowIden::Table
|
||||
}
|
||||
|
||||
fn id_column() -> impl IntoIden + Eq + Clone {
|
||||
HttpExchangeIden::Id
|
||||
FlowIden::Id
|
||||
}
|
||||
|
||||
fn generate_id() -> String {
|
||||
generate_prefixed_id("he")
|
||||
generate_prefixed_id("fl")
|
||||
}
|
||||
|
||||
fn order_by() -> (impl IntoColumnRef, Order) {
|
||||
(HttpExchangeIden::CreatedAt, Order::Desc)
|
||||
(FlowIden::StartedAt, Order::Desc)
|
||||
}
|
||||
|
||||
fn get_id(&self) -> String {
|
||||
@@ -67,32 +122,50 @@ impl UpsertModelInfo for HttpExchange {
|
||||
self,
|
||||
source: &UpdateSource,
|
||||
) -> DbResult<Vec<(impl IntoIden + Eq, impl Into<SimpleExpr>)>> {
|
||||
use HttpExchangeIden::*;
|
||||
use FlowIden::*;
|
||||
Ok(vec![
|
||||
(CreatedAt, upsert_date(source, self.created_at)),
|
||||
(UpdatedAt, upsert_date(source, self.updated_at)),
|
||||
(Url, self.url.into()),
|
||||
(StartedAt, self.started_at.into()),
|
||||
(Method, self.method.into()),
|
||||
(ReqHeaders, serde_json::to_string(&self.req_headers)?.into()),
|
||||
(ReqBody, self.req_body.into()),
|
||||
(ResStatus, self.res_status.into()),
|
||||
(ResHeaders, serde_json::to_string(&self.res_headers)?.into()),
|
||||
(ResBody, self.res_body.into()),
|
||||
(Url, self.url.into()),
|
||||
(ClientHttpVersion, self.client_http_version.into()),
|
||||
(UpstreamHttpVersion, self.upstream_http_version.into()),
|
||||
(RemoteAddr, self.remote_addr.into()),
|
||||
(Status, self.status.into()),
|
||||
(ContentType, self.content_type.into()),
|
||||
(RequestHeaders, serde_json::to_string(&self.request_headers)?.into()),
|
||||
(ResponseHeaders, serde_json::to_string(&self.response_headers)?.into()),
|
||||
(RequestSize, self.request_size.into()),
|
||||
(ResponseSize, self.response_size.into()),
|
||||
(RequestTruncated, self.request_truncated.into()),
|
||||
(ResponseTruncated, self.response_truncated.into()),
|
||||
(ElapsedHeadersMs, self.elapsed_headers_ms.into()),
|
||||
(ElapsedMs, self.elapsed_ms.into()),
|
||||
(Error, self.error.into()),
|
||||
])
|
||||
}
|
||||
|
||||
fn update_columns() -> Vec<impl IntoIden> {
|
||||
vec![
|
||||
HttpExchangeIden::UpdatedAt,
|
||||
HttpExchangeIden::Url,
|
||||
HttpExchangeIden::Method,
|
||||
HttpExchangeIden::ReqHeaders,
|
||||
HttpExchangeIden::ReqBody,
|
||||
HttpExchangeIden::ResStatus,
|
||||
HttpExchangeIden::ResHeaders,
|
||||
HttpExchangeIden::ResBody,
|
||||
HttpExchangeIden::Error,
|
||||
FlowIden::UpdatedAt,
|
||||
FlowIden::StartedAt,
|
||||
FlowIden::Method,
|
||||
FlowIden::Url,
|
||||
FlowIden::ClientHttpVersion,
|
||||
FlowIden::UpstreamHttpVersion,
|
||||
FlowIden::RemoteAddr,
|
||||
FlowIden::Status,
|
||||
FlowIden::ContentType,
|
||||
FlowIden::RequestHeaders,
|
||||
FlowIden::ResponseHeaders,
|
||||
FlowIden::RequestSize,
|
||||
FlowIden::ResponseSize,
|
||||
FlowIden::RequestTruncated,
|
||||
FlowIden::ResponseTruncated,
|
||||
FlowIden::ElapsedHeadersMs,
|
||||
FlowIden::ElapsedMs,
|
||||
FlowIden::Error,
|
||||
]
|
||||
}
|
||||
|
||||
@@ -100,19 +173,28 @@ impl UpsertModelInfo for HttpExchange {
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let req_headers: String = r.get("req_headers")?;
|
||||
let res_headers: String = r.get("res_headers")?;
|
||||
let request_headers: String = r.get("request_headers")?;
|
||||
let response_headers: String = r.get("response_headers")?;
|
||||
Ok(Self {
|
||||
id: r.get("id")?,
|
||||
created_at: r.get("created_at")?,
|
||||
updated_at: r.get("updated_at")?,
|
||||
url: r.get("url")?,
|
||||
started_at: r.get("started_at")?,
|
||||
method: r.get("method")?,
|
||||
req_headers: serde_json::from_str(&req_headers).unwrap_or_default(),
|
||||
req_body: r.get("req_body")?,
|
||||
res_status: r.get("res_status")?,
|
||||
res_headers: serde_json::from_str(&res_headers).unwrap_or_default(),
|
||||
res_body: r.get("res_body")?,
|
||||
url: r.get("url")?,
|
||||
client_http_version: r.get("client_http_version")?,
|
||||
upstream_http_version: r.get("upstream_http_version")?,
|
||||
remote_addr: r.get("remote_addr")?,
|
||||
status: r.get("status")?,
|
||||
content_type: r.get("content_type")?,
|
||||
request_headers: serde_json::from_str(&request_headers).unwrap_or_default(),
|
||||
response_headers: serde_json::from_str(&response_headers).unwrap_or_default(),
|
||||
request_size: r.get("request_size")?,
|
||||
response_size: r.get("response_size")?,
|
||||
request_truncated: r.get("request_truncated")?,
|
||||
response_truncated: r.get("response_truncated")?,
|
||||
elapsed_headers_ms: r.get("elapsed_headers_ms")?,
|
||||
elapsed_ms: r.get("elapsed_ms")?,
|
||||
error: r.get("error")?,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user