Put Yaak Proxy's search on the shared query engine (#826)

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Gregory SchierandClaude Fable 5.1 authored and GitHub committed 2026-11-08 09:07:04 -07:00
1 parent 7453869ac6
commit 77226dbedb
58 files changed
+4754 -1594

No files matched your search

+6 -1
View File
@@ -554,7 +554,9 @@ async fn route(state: &ServerState, req: Request<Incoming>) -> Result<ControlRes
(&Method::GET, ["flows", id]) => {
found(ctx, id)?;
let limit = body_limit(&query)?;
json(&flows::detail(&ctx.db()?, &ctx.blobs()?, id, limit).await?)
let detail = flows::detail(&ctx.db()?, &ctx.blobs()?, id, limit).await?;
ctx.note_agent_read(id);
json(&detail)
}
(&Method::GET, ["flows", id, "request-body"]) => raw_body(ctx, id, FlowBody::Request).await,
(&Method::GET, ["flows", id, "response-body"]) => {
@@ -565,6 +567,7 @@ async fn route(state: &ServerState, req: Request<Incoming>) -> Result<ControlRes
let (after_seq, limit) = message_page(&query)?;
let messages = messages::list(&ctx.db()?, id, after_seq, limit)?;
let total = messages::count(&ctx.db()?, id)?;
ctx.note_agent_read(id);
json(&ListMessagesResponse { messages, total })
}
(&Method::GET, ["flows", id, "messages", seq]) => {
@@ -584,6 +587,7 @@ async fn route(state: &ServerState, req: Request<Incoming>) -> Result<ControlRes
(None, None)
};
let payload = BodyPreview::new(&payload, message.truncated);
ctx.note_agent_read(id);
json(&MessageDetail { message, payload, decoded, decode_error })
}
(&Method::GET, ["grpc-schemas"]) => json(&grpc::list(&ctx.db()?)?),
@@ -687,6 +691,7 @@ async fn raw_body(ctx: &ProxyCtx, id: &str, which: FlowBody) -> Result<ControlRe
let body =
bodies::read_body(&ctx.db()?, &ctx.blobs()?, &flow, which, ctx.capture_body_limit()?)
.await?;
ctx.note_agent_read(id);
let content_type =
find_header(flow.headers(which), "content-type").unwrap_or("application/octet-stream");
let mut resp = response(StatusCode::OK, content_type, body.data);
+17 -17
View File
@@ -320,17 +320,11 @@ pub fn is_key_unavailable(reason: &str) -> bool {
reason.starts_with(KEY_UNAVAILABLE)
}
/// The functions filters use: `sidebar_path(url)`, the app sidebar's place for a URL, and
/// `under_site(url, site)`, for filtering by its nodes; `folder_name(dir)`, for naming a program
/// by its folder; and `is_system(bundle_id, path)`, for macOS's own daemons.
/// The functions searches use: `folder_name(dir)`, for naming a program by its folder;
/// `is_system(bundle_id, path)`, for macOS's own daemons; and those the query compiler reads
/// flows' values and tests them with ([`crate::query::add_functions`]).
fn add_functions(conn: &mut rusqlite::Connection) -> rusqlite::Result<()> {
let flags = FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DETERMINISTIC;
conn.create_scalar_function("sidebar_path", 1, flags, |ctx| {
Ok(crate::flows::sidebar_path(&ctx.get::<String>(0)?))
})?;
conn.create_scalar_function("under_site", 2, flags, |ctx| {
Ok(crate::flows::under_site(&ctx.get::<String>(0)?, &ctx.get::<String>(1)?))
})?;
conn.create_scalar_function("folder_name", 1, flags, |ctx| {
let dir = ctx.get::<Option<String>>(0)?;
Ok(crate::flows::folder_name(dir.as_deref()).map(str::to_string))
@@ -339,7 +333,8 @@ fn add_functions(conn: &mut rusqlite::Connection) -> rusqlite::Result<()> {
let bundle_id = ctx.get::<Option<String>>(0)?;
let path = ctx.get::<Option<String>>(1)?;
Ok(crate::flows::is_system(bundle_id.as_deref(), path.as_deref()))
})
})?;
crate::query::add_functions(conn)
}
fn read_only_pool(path: &Path, key: Option<&DatabaseKey>) -> Result<SqlitePool> {
@@ -447,8 +442,7 @@ mod tests {
assert!(!flows[0].cancelled);
assert_eq!((flows[0].stream_kind, flows[0].message_count), (None, 0));
// Failed flows are found without the gRPC status column too.
let failed =
crate::flows::FlowFilter { status: Some("error".into()), ..Default::default() };
let failed = crate::flows::FlowFilter::search("@is:error");
assert_eq!(crate::flows::list(&db, &failed).unwrap().len(), 0);
// Filters by app work without the folder column, and count without the clear mark.
@@ -465,8 +459,7 @@ mod tests {
.unwrap();
drop(conn);
let db = ProxyQueryManager::open_read_only(&path, Some(&test_key())).unwrap();
let filter =
crate::flows::FlowFilter { app: Some(vec!["curl".into()]), ..Default::default() };
let filter = crate::flows::FlowFilter::search("@app:curl");
let (page, total, matching) = crate::flows::page(&db, &filter).unwrap();
assert_eq!((page.len(), total, matching), (1, 1, Some(1)));
assert_eq!(page[0].number, 1);
@@ -513,7 +506,8 @@ mod tests {
"flow_messages",
"flow_sweep_pending",
"flows",
"grpc_schemas"
"grpc_schemas",
"saved_queries"
]
);
@@ -532,6 +526,7 @@ mod tests {
"grpc_method",
"grpc_status",
"grpc_message",
"agent_read",
] {
assert!(flow_columns.contains(&column.to_string()), "{column} in {flow_columns:?}");
}
@@ -599,7 +594,12 @@ mod tests {
.unwrap()
.map(|v| v.unwrap())
.collect();
assert_eq!(&applied[6..], ["00000007", "00000008", "00000009", "00000010", "00000011"]);
assert_eq!(
&applied[6..],
[
"00000007", "00000008", "00000009", "00000010", "00000011", "00000012"
]
);
let mark_columns = columns(&conn, "flow_clear_mark");
assert_eq!(mark_columns, ["id", "max_rowid", "cleaner", "cleaner_at"]);
drop(conn);
@@ -718,7 +718,7 @@ mod tests {
};
db.with_tx(|c| c.upsert(&tunnel, &yaak_database::UpdateSource::Background).map(|_| ()))
.unwrap();
let filter = crate::flows::FlowFilter { tunnelled: Some(true), ..Default::default() };
let filter = crate::flows::FlowFilter::search("@is:tunnelled");
let listed = crate::flows::list(&db, &filter).unwrap();
assert_eq!(listed.iter().map(|f| f.id.as_str()).collect::<Vec<_>>(), ["fl_tunnel"]);
let old = crate::flows::find(&db, "fl_old").unwrap();
+175 -423
View File
@@ -1,6 +1,7 @@
use crate::bodies;
use crate::db::{ProxyQueryManager, Stores};
use crate::models::{Flow, FlowBody};
use crate::query::{FlowColumns, FlowQuery};
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use rusqlite::types::Value;
@@ -9,52 +10,19 @@ use ts_rs::TS;
use yaak_database::UpsertModelInfo;
use yaak_models::blob_manager::BlobManager;
/// Which flows a listing or a HAR export covers. Every field narrows it; none is all of them.
/// Which flows a listing or a HAR export covers: a search, and a page of it.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct FlowFilter {
/// Search terms, any case, each of which must match the URL (host, path or query), the
/// method, the status (`404`, `40`, `4xx`) or the content type: `stripe post 4xx`.
/// A search in the query language (`crate::query`): `stripe @status:4xx`,
/// `@host:*.github.com -@is:system`. None or blank covers every flow.
#[ts(optional)]
pub q: Option<String>,
/// Part of the host, any case: `stripe` matches `api.stripe.com`.
#[ts(optional)]
pub host: Option<String>,
/// Only flows under one of these hosts or paths, as the app's sidebar names them:
/// `api.stripe.com` or `api.stripe.com/v1/charges`, with any port after the host. A host
/// written `*.github.com` is a site: `github.com` and every host below it.
#[ts(optional)]
pub under: Option<Vec<String>>,
/// Only flows from one of these apps: by bundle id, by path for a process outside an app
/// bundle, by name in any case (`slack`), or, for a process outside an app, by the folder
/// it ran in (`fieldwork`).
#[ts(optional)]
pub app: Option<Vec<String>>,
/// Only flows from a process that ran in one of these working directories: the whole path,
/// or its folder's name in any case (`fieldwork`). With `app`, a program in a folder.
#[ts(optional)]
pub cwd: Option<Vec<String>>,
/// Only flows from macOS's own daemons (true), or none of them (false), as [`is_system`]
/// tells them by their executable.
#[ts(optional)]
pub system: Option<bool>,
/// Only HTTPS connections tunnelled undecrypted (true), or none of them (false).
#[ts(optional)]
pub tunnelled: Option<bool>,
/// Only flows one of these groups covers, each judged on its own: several sidebar rows
/// selected, `@app:Slack @host:a.com OR @app:Chrome @host:b.com`, are two groups and not every
/// pairing of their apps and hosts. A group's `after`, `before` and `limit` do not apply. Not
/// carried by a query string.
#[ts(optional)]
pub any: Option<Vec<FlowFilter>>,
/// The method, any case.
#[ts(optional)]
pub method: Option<String>,
/// A status (`404`), a class of them (`4xx`), or `error` for flows that failed, gRPC calls
/// that ended with a status other than OK among them.
#[ts(optional)]
pub status: Option<String>,
pub query: Option<String>,
/// Leaves out macOS's own daemons, as hiding System does, unless the query says
/// `@is:system` itself.
#[ts(optional, as = "Option<bool>")]
pub hide_system: bool,
/// At most this many flows, the newest.
#[ts(optional, type = "number")]
pub limit: Option<usize>,
@@ -69,30 +37,24 @@ pub struct FlowFilter {
}
impl FlowFilter {
/// The filter from a query string's `q`, `host`, `under`, `app`, `cwd`, `system`, `method`,
/// `status`, `limit`, `after` and `before`. `under`, `app` and `cwd` may repeat.
/// A filter for a search alone.
pub fn search(query: impl Into<String>) -> Self {
Self { query: Some(query.into()), ..Self::default() }
}
/// The filter from a query string's `query`, `hide_system`, `limit`, `after` and `before`.
pub fn from_query(query: &str) -> Result<Self, String> {
let mut filter = Self::default();
for (key, value) in url::form_urlencoded::parse(query.as_bytes()) {
let value = Some(value.into_owned()).filter(|v| !v.is_empty());
match key.as_ref() {
"q" => filter.q = value,
"host" => filter.host = value,
"under" => filter.under.get_or_insert_default().extend(value),
"app" => filter.app.get_or_insert_default().extend(value),
"cwd" => filter.cwd.get_or_insert_default().extend(value),
"system" => {
filter.system = value
.map(|v| v.parse().map_err(|_| format!("Invalid system {v:?}")))
"query" => filter.query = value,
"hide_system" => {
filter.hide_system = value
.map(|v| v.parse().map_err(|_| format!("Invalid hide_system {v:?}")))
.transpose()?
.unwrap_or_default()
}
"tunnelled" => {
filter.tunnelled = value
.map(|v| v.parse().map_err(|_| format!("Invalid tunnelled {v:?}")))
.transpose()?
}
"method" => filter.method = value,
"status" => filter.status = value,
"after" => filter.after = value,
"before" => filter.before = value,
"limit" => {
@@ -109,30 +71,10 @@ impl FlowFilter {
pub fn to_query(&self) -> String {
let mut query = url::form_urlencoded::Serializer::new(String::new());
let limit = self.limit.map(|l| l.to_string());
let system = self.system.map(|s| s.to_string());
let tunnelled = self.tunnelled.map(|t| t.to_string());
let hide_system = self.hide_system.then(|| "true".to_string());
for (key, value) in [
("q", &self.q),
("host", &self.host),
("system", &system),
("tunnelled", &tunnelled),
] {
if let Some(value) = value {
query.append_pair(key, value);
}
}
for (key, values) in [
("under", &self.under),
("app", &self.app),
("cwd", &self.cwd),
] {
for value in values.iter().flatten() {
query.append_pair(key, value);
}
}
for (key, value) in [
("method", &self.method),
("status", &self.status),
("query", &self.query),
("hide_system", &hide_system),
("limit", &limit),
("after", &self.after),
("before", &self.before),
@@ -144,166 +86,21 @@ impl FlowFilter {
query.finish()
}
/// The search, prepared to run now.
pub fn flow_query(&self) -> FlowQuery {
FlowQuery::new(self.query.as_deref().unwrap_or_default(), self.hide_system)
}
/// Narrows more than `after`, `before` and `limit` do, so it covers fewer than all flows.
pub fn narrows(&self) -> bool {
let given = |v: &Option<String>| v.as_deref().is_some_and(|v| !v.trim().is_empty());
given(&self.q)
|| given(&self.host)
|| given(&self.method)
|| given(&self.status)
|| self.under.is_some()
|| self.app.is_some()
|| self.cwd.is_some()
|| self.system.is_some()
|| self.tunnelled.is_some()
|| self.any.as_ref().is_some_and(|groups| groups.iter().any(FlowFilter::narrows))
!self.flow_query().is_empty()
}
/// The SQL condition for every field but `after`, `before` and `limit`, and its parameters,
/// for the database `conn` reads, which may be from before the folder or gRPC columns, when
/// read without migrating.
/// The SQL condition for the search, and its parameters, for the database `conn` reads,
/// which may be from before later columns, when read without migrating.
fn condition_in(&self, conn: &rusqlite::Connection) -> (String, Vec<Value>) {
let columns = Columns {
cwd: has_column(conn, "client_cwd"),
grpc: has_column(conn, "grpc_status"),
tunnelled: has_column(conn, "tunnelled"),
};
self.condition(columns)
self.flow_query().sql(&FlowColumns::of(conn))
}
/// [`Self::condition_in`], told which of the later columns the database has.
fn condition(&self, columns: Columns) -> (String, Vec<Value>) {
let Columns { cwd: has_cwd, grpc, tunnelled } = columns;
let mut sql = String::from("1");
let mut params: Vec<Value> = Vec::new();
for term in self.q.iter().flat_map(|q| q.split_whitespace()) {
let term = term.to_lowercase();
sql.push_str(
" AND (instr(lower(url), ?) > 0 OR lower(method) = ? \
OR instr(lower(content_type), ?) > 0",
);
params.extend([
term.clone().into(),
term.clone().into(),
term.clone().into(),
]);
if let Some(status) = status_condition(&term, &mut params) {
sql.push_str(" OR ");
sql.push_str(&status);
}
sql.push(')');
}
if let Some(host) = self.host.as_deref().filter(|h| !h.is_empty()) {
sql.push_str(&format!(" AND instr(lower({HOST_SQL}), ?) > 0"));
params.push(host.to_lowercase().into());
}
if let Some(under) = &self.under {
// The node itself, or anything below it, by the sidebar's rules.
let mut any = Vec::new();
for node in under {
if let Some(site) = site_of_node(node) {
any.push("under_site(url, ?)");
params.push(site.into());
continue;
}
let node = normalize_node(node);
any.push("(sidebar_path(url) = ? OR substr(sidebar_path(url), 1, ?) = ?)");
let below = format!("{node}/");
params.extend([
node.into(),
(below.chars().count() as i64).into(),
below.into(),
]);
}
let any = if any.is_empty() { "0".to_string() } else { any.join(" OR ") };
sql.push_str(&format!(" AND ({any})"));
}
if let Some(apps) = &self.app {
let marks = vec!["?"; apps.len()].join(", ");
sql.push_str(&format!(
" AND (coalesce(client_bundle_id, client_path) IN ({marks}) \
OR lower(client_name) IN ({marks})"
));
params.extend(apps.iter().map(|a| a.clone().into()));
params.extend(apps.iter().map(|a| a.to_lowercase().into()));
if has_cwd {
sql.push_str(&format!(
" OR (client_bundle_id IS NULL AND lower(folder_name(client_cwd)) IN ({marks}))"
));
params.extend(apps.iter().map(|a| a.to_lowercase().into()));
}
sql.push(')');
}
if let Some(cwds) = &self.cwd {
if has_cwd && !cwds.is_empty() {
let marks = vec!["?"; cwds.len()].join(", ");
sql.push_str(&format!(
" AND (client_cwd IN ({marks}) OR lower(folder_name(client_cwd)) IN ({marks}))"
));
params.extend(cwds.iter().map(|c| c.clone().into()));
params.extend(cwds.iter().map(|c| c.to_lowercase().into()));
} else {
// None, or a database from before the column, which has no folders.
sql.push_str(" AND 0");
}
}
match self.system {
Some(true) => sql.push_str(" AND is_system(client_bundle_id, client_path)"),
Some(false) => sql.push_str(" AND NOT is_system(client_bundle_id, client_path)"),
None => {}
}
match (self.tunnelled, tunnelled) {
(Some(true), true) => sql.push_str(" AND tunnelled"),
(Some(false), true) => sql.push_str(" AND NOT tunnelled"),
// A database from before tunnelled flows has none.
(Some(true), false) => sql.push_str(" AND 0"),
_ => {}
}
// Each group whole, then any of them.
if let Some(groups) = self.any.as_ref().filter(|g| !g.is_empty()) {
let mut any = Vec::new();
for group in groups {
let (condition, group_params) = group.condition(columns);
any.push(format!("({condition})"));
params.extend(group_params);
}
sql.push_str(&format!(" AND ({})", any.join(" OR ")));
}
if let Some(method) = self.method.as_deref().filter(|m| !m.is_empty()) {
sql.push_str(" AND lower(method) = ?");
params.push(method.to_lowercase().into());
}
match self.status.as_deref().map(str::trim) {
None | Some("") => {}
// A gRPC call that ended with a status other than OK failed too.
Some(s) if s.eq_ignore_ascii_case("error") && grpc => {
sql.push_str(" AND (error IS NOT NULL OR coalesce(grpc_status, 0) != 0)")
}
Some(s) if s.eq_ignore_ascii_case("error") => sql.push_str(" AND error IS NOT NULL"),
// A whole status or a class, not the start of one as a search term may be.
Some(s) if s.len() == 3 => match status_condition(&s.to_ascii_lowercase(), &mut params)
{
Some(status) => {
sql.push_str(" AND ");
sql.push_str(&status);
}
None => sql.push_str(" AND 0"),
},
Some(_) => sql.push_str(" AND 0"),
}
(sql, params)
}
}
/// Which of the columns later migrations added a database has, as one opened read-only before
/// they ran may not.
#[derive(Clone, Copy)]
struct Columns {
/// The folder a program ran in.
cwd: bool,
/// gRPC statuses.
grpc: bool,
tunnelled: bool,
}
/// The host and port of `url`, in SQL: what follows `://` up to the first `/`, `?` or `#`.
@@ -341,38 +138,6 @@ pub fn sidebar_path(url: &str) -> String {
key
}
/// A sidebar node as `sidebar_path` writes it, its host lowercased.
fn normalize_node(node: &str) -> String {
match node.split_once('/') {
Some((host, path)) => format!("{}/{path}", host.to_ascii_lowercase()),
None => node.to_ascii_lowercase(),
}
}
/// The site a sidebar node names, `github.com` for `*.github.com`, lowercased. None for a host or
/// a path.
fn site_of_node(node: &str) -> Option<String> {
let site = node.strip_prefix("*.")?;
(!site.is_empty() && !site.contains('/')).then(|| site.to_ascii_lowercase())
}
/// `url`'s host, without its port, is `site` or a host below it: `api.github.com` is under
/// `github.com`.
pub fn under_site(url: &str, site: &str) -> bool {
let path = sidebar_path(url);
let host = path.split('/').next().unwrap_or_default();
let host = strip_port(host);
host == site || host.strip_suffix(site).is_some_and(|rest| rest.ends_with('.'))
}
/// `host` without a `:port` after it, keeping an IPv6 address's brackets.
fn strip_port(host: &str) -> &str {
if host.starts_with('[') {
return host.find(']').map_or(host, |end| &host[..=end]);
}
host.rsplit_once(':').map_or(host, |(h, _)| h)
}
/// The last part of a directory's path, `fieldwork` for `/Users/me/src/fieldwork`. None for the
/// root, where daemons run, or no path.
pub fn folder_name(path: Option<&str>) -> Option<&str> {
@@ -418,26 +183,6 @@ pub fn is_system(bundle_id: Option<&str>, path: Option<&str>) -> bool {
SYSTEM_DIRS.iter().any(|dir| path.starts_with(dir)) || SYSTEM_PROGRAMS.contains(&name)
}
/// The SQL condition for a status term: `404` exactly, `40` as the start of one, or a class
/// such as `4xx`. None when the term is not one.
fn status_condition(term: &str, params: &mut Vec<Value>) -> Option<String> {
if let Some(class) = term.strip_suffix("xx")
&& let Ok(class @ 1..=9) = class.parse::<i64>()
{
params.push(class.into());
return Some("status / 100 = ?".into());
}
if term.is_empty() || term.len() > 3 || !term.chars().all(|c| c.is_ascii_digit()) {
return None;
}
if term.len() == 3 {
params.push(term.parse::<i64>().ok()?.into());
return Some("status = ?".into());
}
params.push(format!("{term}%").into());
Some("CAST(status AS TEXT) LIKE ?".into())
}
/// The flows `filter` covers, newest first: by start, then by id. Reads the table in that order
/// through its index and stops at the limit, so a page of a common match costs the same however
/// many flows there are.
@@ -462,13 +207,6 @@ pub fn page(
})
}
/// The database has the gRPC status column, added by a later migration.
/// The flows table has `column`: a database opened read-only before its migrations have run may
/// not.
fn has_column(conn: &rusqlite::Connection, column: &str) -> bool {
conn.prepare(&format!("SELECT {column} FROM flows LIMIT 0")).is_ok()
}
fn list_in(conn: &rusqlite::Connection, filter: &FlowFilter) -> Result<Vec<Flow>, String> {
let (condition, mut params) = filter.condition_in(conn);
// The rowid, for the flow's number.
@@ -511,6 +249,40 @@ fn count_matching(conn: &rusqlite::Connection, filter: &FlowFilter) -> Result<us
.map_err(|e| e.to_string())
}
/// How many flows each filter's search covers, whatever its limit, read from one snapshot.
pub fn count_each(db: &ProxyQueryManager, filters: &[FlowFilter]) -> Result<Vec<usize>, String> {
db.with_conn(|c| {
let tx = c.conn().resolve().unchecked_transaction().map_err(|e| e.to_string())?;
let counts = filters.iter().map(|f| count_matching(&tx, f)).collect::<Result<_, _>>()?;
tx.commit().map_err(|e| e.to_string())?;
Ok(counts)
})
}
/// The flows of `ids` that `filter`'s search covers, in any order. Cleared ones are left out.
pub fn matching_ids(
db: &ProxyQueryManager,
filter: &FlowFilter,
ids: &[String],
) -> Result<Vec<String>, String> {
if ids.is_empty() {
return Ok(Vec::new());
}
db.with_conn(|c| {
let conn = c.conn().resolve();
let (condition, mut params) = filter.condition_in(conn);
let marks = vec!["?"; ids.len()].join(", ");
let mut sql = format!("SELECT id FROM flows WHERE ({condition}) AND id IN ({marks})");
params.extend(ids.iter().map(|id| Value::from(id.clone())));
push_clear_mark(conn, &mut sql, &mut params);
let mut stmt = conn.prepare(&sql).map_err(|e| e.to_string())?;
let rows = stmt
.query_map(rusqlite::params_from_iter(params), |r| r.get(0))
.map_err(|e| e.to_string())?;
rows.collect::<Result<Vec<String>, _>>().map_err(|e| e.to_string())
})
}
/// Leaves out cleared flows, when a clear is pending; otherwise adds nothing, so SQLite keeps its
/// cheapest plan.
fn push_clear_mark(conn: &rusqlite::Connection, sql: &mut String, params: &mut Vec<Value>) {
@@ -1031,6 +803,13 @@ mod tests {
list(db, &filter).unwrap().into_iter().map(|f| f.url).collect()
}
/// The urls a search covers, sorted.
fn found(db: &ProxyQueryManager, query: &str) -> Vec<String> {
let mut found = urls(db, FlowFilter::search(query));
found.sort();
found
}
#[test]
fn filters() {
let (_dir, db) = db_of(vec![
@@ -1038,18 +817,20 @@ mod tests {
flow("http://example.com/missing", "GET", Some(404), None),
flow("https://down.example.com/", "GET", None, Some("refused")),
]);
let by = |filter: FlowFilter| urls(&db, filter).len();
assert_eq!(by(FlowFilter::default()), 3);
assert_eq!(by(FlowFilter { host: Some("STRIPE".into()), ..Default::default() }), 1);
assert_eq!(by(FlowFilter { host: Some("example.com".into()), ..Default::default() }), 2);
assert_eq!(by(FlowFilter { host: Some("charges".into()), ..Default::default() }), 0);
assert_eq!(by(FlowFilter { host: Some("8443".into()), ..Default::default() }), 1);
assert_eq!(by(FlowFilter { method: Some("get".into()), ..Default::default() }), 2);
assert_eq!(by(FlowFilter { status: Some("404".into()), ..Default::default() }), 1);
assert_eq!(by(FlowFilter { status: Some("2xx".into()), ..Default::default() }), 1);
assert_eq!(by(FlowFilter { status: Some("5XX".into()), ..Default::default() }), 0);
assert_eq!(by(FlowFilter { status: Some("40".into()), ..Default::default() }), 0);
assert_eq!(by(FlowFilter { status: Some("error".into()), ..Default::default() }), 1);
let by = |query: &str| urls(&db, FlowFilter::search(query)).len();
assert_eq!(urls(&db, FlowFilter::default()).len(), 3);
// A host whole, with or without its port; a part of one with wildcards.
assert_eq!(by("@host:API.STRIPE.COM"), 1);
assert_eq!(by("@host:api.stripe.com:8443"), 1);
assert_eq!(by("@host:stripe"), 0);
assert_eq!(by("@host:*stripe*"), 1);
assert_eq!(by("@host:*.example.com"), 2);
assert_eq!(by("@method:get"), 2);
assert_eq!(by("@status:404"), 1);
assert_eq!(by("@status:2xx"), 1);
assert_eq!(by("@status:5XX"), 0);
assert_eq!(by("@status:40"), 0);
assert_eq!(by("@is:error"), 1);
}
#[test]
@@ -1067,12 +848,7 @@ mod tests {
},
flow("http://localhost:3000/health", "HEAD", Some(503), None),
]);
let search = |q: &str| {
let filter = FlowFilter { q: Some(q.into()), ..Default::default() };
let mut found = urls(&db, filter);
found.sort();
found
};
let search = |q: &str| found(&db, q);
let stripe = "https://api.stripe.com/v1/charges?customer=cus_9";
let github = "https://api.github.com/repos/yaak";
let logo = "https://images.ctfassets.net/logo.png";
@@ -1081,28 +857,28 @@ mod tests {
assert_eq!(search("STRIPE"), vec![stripe]);
assert_eq!(search("/repos/"), vec![github]);
assert_eq!(search("cus_9"), vec![stripe]);
// The method, whole.
// The method and the status, any part of them.
assert_eq!(search("head"), vec![health]);
assert_eq!(search("ead"), Vec::<&str>::new());
// A status, its start, or its class.
assert_eq!(search("ead"), vec![health]);
assert_eq!(search("404"), vec![github]);
assert_eq!(search("50"), vec![health]);
assert_eq!(search("2xx"), vec![stripe, logo]);
// The content type.
assert_eq!(search("png"), vec![logo]);
assert_eq!(search("json"), vec![github, stripe]);
// Every term must match, each anywhere.
assert_eq!(search("json get"), vec![github]);
assert_eq!(search(" json 2xx "), vec![stripe]);
assert_eq!(search("json 5xx"), Vec::<&str>::new());
assert_eq!(search(" json @status:2xx "), vec![stripe]);
assert_eq!(search("json @status:5xx"), Vec::<&str>::new());
// `%` and `_` are plain characters, not patterns.
assert_eq!(search("%"), Vec::<&str>::new());
assert_eq!(search("cus_"), vec![stripe]);
assert_eq!(search(""), vec![health, github, stripe, logo]);
}
/// The terms a sidebar row writes cover what is under it: a host, with any port, and a
/// path with what is below it.
#[test]
fn under_matches_a_host_or_a_path_as_the_sidebar_does() {
fn hosts_and_paths_as_the_sidebar_writes_them() {
let (_dir, db) = db_of(vec![
flow("https://api.stripe.com/v1/charges", "GET", Some(200), None),
flow("https://api.stripe.com/v1/charges/ch_1?expand=x", "GET", Some(200), None),
@@ -1110,29 +886,38 @@ mod tests {
flow("https://api.stripe.com:8443/v1/charges", "GET", Some(200), None),
flow("http://api.stripe.com", "GET", Some(200), None),
flow("https://api.stripe.community/", "GET", Some(200), None),
flow("https://example.com/v1//users", "GET", Some(200), None),
flow("http://x.com:80/a", "GET", Some(200), None),
flow("https://x.com:443/a", "GET", Some(200), None),
flow("https://example.com/v1//users", "GET", Some(200), None),
]);
let under = |nodes: &[&str]| {
let nodes = nodes.iter().map(|n| n.to_string()).collect();
urls(&db, FlowFilter { under: Some(nodes), ..Default::default() }).len()
};
// A host on another port is a node of its own, as in the sidebar.
assert_eq!(under(&["api.stripe.com"]), 4);
assert_eq!(under(&["api.stripe.com:8443"]), 1);
assert_eq!(under(&["api.stripe.com/v1/charges"]), 2);
// Empty segments and default ports are left out, as the sidebar leaves them.
assert_eq!(under(&["example.com/v1/users"]), 1);
assert_eq!(under(&["x.com/a"]), 2);
assert_eq!(under(&["api.stripe.com/v1/charges", "example.com"]), 3);
assert_eq!(under(&[]), 0);
// The sidebar lowercases hosts; paths keep their case.
assert_eq!(under(&["API.stripe.com/v1/charges"]), 2);
assert_eq!(under(&["api.stripe.com/V1/charges"]), 0);
let count = |query: &str| found(&db, query).len();
assert_eq!(count("@host:api.stripe.com"), 5);
assert_eq!(count("@host:api.stripe.com:8443"), 1);
assert_eq!(count("@host:api.stripe.com @path:/v1/charges/*"), 3);
assert_eq!(count("@host:x.com @path:/a"), 2);
assert_eq!(count("@host:api.stripe.com @path:/v1/charges/* OR @host:x.com"), 5);
assert_eq!(count("@host:API.stripe.com @path:/V1/charges/*"), 3);
// A path with empty segments is found by the sidebar's row for it.
assert_eq!(count("@host:example.com @path:/v1/users/*"), 1);
}
/// The cases `lib/search.test.ts` checks `sidebarPath` against, so the two agree.
/// A search with `OR` at its top still pages and leaves out cleared flows.
#[test]
fn a_top_level_or_keeps_to_the_page_and_the_clear() {
let (_dir, db) = db_of(vec![
flow("https://a.com/1", "GET", Some(200), None),
flow("https://a.com/2", "GET", Some(404), None),
flow("https://a.com/3", "GET", Some(200), None),
]);
let query = "@status:200 OR @status:404";
let before = FlowFilter { before: Some("fl_001".into()), ..FlowFilter::search(query) };
assert_eq!(urls(&db, before), vec!["https://a.com/1"]);
clear(&db).unwrap();
assert!(urls(&db, FlowFilter::search(query)).is_empty());
assert_eq!(page(&db, &FlowFilter::search(query)).unwrap().2, Some(0));
}
#[test]
fn sidebar_path_rules() {
for (url, path) in [
@@ -1163,18 +948,15 @@ mod tests {
from(None, "/usr/bin/curl"),
flow("https://example.com/", "GET", Some(200), None),
]);
let app = |apps: &[&str]| {
let apps = apps.iter().map(|a| a.to_string()).collect();
urls(&db, FlowFilter { app: Some(apps), ..Default::default() }).len()
};
assert_eq!(app(&["com.google.Chrome"]), 2);
assert_eq!(app(&["/usr/bin/curl"]), 1);
assert_eq!(app(&["/Applications/Google Chrome.app"]), 0);
assert_eq!(app(&["com.google.Chrome", "/usr/bin/curl"]), 3);
let app = |query: &str| found(&db, query).len();
assert_eq!(app("@app:com.google.Chrome"), 2);
assert_eq!(app("@app:/usr/bin/curl"), 1);
assert_eq!(app("@app:\"/Applications/Google Chrome.app\""), 0);
assert_eq!(app("@app:com.google.Chrome OR @app:/usr/bin/curl"), 3);
}
/// A program in a folder: its working directory by the whole path, or its folder's name in
/// any case, along with whatever else the filter asks.
/// A program in a folder: its working directory by the whole path, or its folder's name,
/// any case, along with whatever else the search asks.
#[test]
fn cwd_matches_a_path_or_a_folder_name() {
let node_in = |cwd: Option<&str>| Flow {
@@ -1191,22 +973,14 @@ mod tests {
Flow { client_cwd: Some("/tmp/fieldwork".into()), ..node_in(None) },
Flow { client_path: Some("/usr/bin/curl".into()), ..node_in(Some("/tmp/ledger")) },
]);
let count = |app: Option<&str>, cwds: &[&str]| {
let cwd = Some(cwds.iter().map(|c| c.to_string()).collect());
let app = app.map(|a| vec![a.to_string()]);
urls(&db, FlowFilter { app, cwd, ..Default::default() }).len()
};
assert_eq!(count(None, &["/Users/ada/Workspace/fieldwork"]), 2, "the whole path");
assert_eq!(count(None, &["/users/ada/workspace/fieldwork"]), 0, "a path exactly");
assert_eq!(count(None, &["FieldWork"]), 3, "a folder name, any case");
assert_eq!(count(None, &["fieldwork", "ledger"]), 5, "any of them");
assert_eq!(count(Some("/usr/local/bin/node"), &["ledger"]), 1, "and the program");
assert_eq!(count(Some("/usr/bin/curl"), &["ledger"]), 1);
assert_eq!(count(None, &[]), 0);
let query = FlowFilter { cwd: Some(vec!["a".into(), "/b".into()]), ..Default::default() };
assert_eq!(query.to_query(), "cwd=a&cwd=%2Fb");
assert_eq!(FlowFilter::from_query(&query.to_query()).unwrap(), query);
assert!(query.narrows());
let count = |query: &str| found(&db, query).len();
assert_eq!(count("@cwd:/Users/ada/Workspace/fieldwork"), 2, "the whole path");
assert_eq!(count("@cwd:/users/ada/workspace/fieldwork"), 2, "any case");
assert_eq!(count("@cwd:FieldWork"), 3, "a folder name");
assert_eq!(count("@cwd:fieldwork OR @cwd:ledger"), 5, "any of them");
assert_eq!(count("@app:/usr/local/bin/node @cwd:ledger"), 1, "and the program");
assert_eq!(count("@app:/usr/bin/curl @cwd:ledger"), 1);
assert_eq!(count("@cwd:work"), 0, "whole");
}
#[test]
@@ -1221,7 +995,7 @@ mod tests {
.collect(),
);
let search = |before: Option<String>| FlowFilter {
q: Some("post".into()),
query: Some("post".into()),
before,
limit: Some(4),
..Default::default()
@@ -1245,8 +1019,7 @@ mod tests {
let (_dir, db) = db_of(vec![]);
let plan: Vec<String> = db.with_conn(|c| {
let conn = c.conn().resolve();
let (condition, params) =
FlowFilter { q: Some("stripe".into()), ..Default::default() }.condition_in(conn);
let (condition, params) = FlowFilter::search("stripe @status:>=400").condition_in(conn);
let sql = format!(
"EXPLAIN QUERY PLAN SELECT * FROM flows WHERE {condition} \
ORDER BY started_at DESC, id DESC LIMIT 500"
@@ -1322,7 +1095,7 @@ mod tests {
// The limit counts flows the filter keeps.
let ok = FlowFilter {
before: Some("fl_e".into()),
status: Some("2xx".into()),
query: Some("@status:2xx".into()),
limit: Some(2),
..Default::default()
};
@@ -1380,7 +1153,7 @@ mod tests {
write_flow("fl_d");
assert_eq!(ids(&FlowFilter::default()), vec!["fl_d"]);
assert_eq!(find(&db, "fl_d").map(|f| f.number), Some(4));
let narrowed = FlowFilter { q: Some("example".into()), ..Default::default() };
let narrowed = FlowFilter::search("example");
assert_eq!(page(&db, &narrowed).unwrap().1, 1);
// Bodies that fail to delete keep their flows, to delete again.
@@ -1540,49 +1313,39 @@ mod tests {
ids.sort();
ids
};
let app = |a: &str| FlowFilter { app: Some(vec![a.into()]), ..Default::default() };
assert_eq!(ids(app("slack")), vec!["fl_slack"]);
assert_eq!(ids(app("com.tinyspeck.slackmacgap")), vec!["fl_slack"]);
assert_eq!(ids(app("Fieldwork")), vec!["fl_node"]);
assert_eq!(ids(app("/opt/homebrew/bin/node")), vec!["fl_node"]);
let under = |n: &str| FlowFilter { under: Some(vec![n.into()]), ..Default::default() };
assert_eq!(ids(under("*.github.com")), vec!["fl_daemon", "fl_node"]);
assert_eq!(ids(under("api.github.com")), vec!["fl_node"]);
let system = |s| FlowFilter { system: Some(s), ..Default::default() };
assert_eq!(ids(system(true)), vec!["fl_daemon"]);
assert_eq!(ids(system(false)), vec!["fl_node", "fl_none", "fl_slack"]);
let ids = |query: &str| ids(FlowFilter::search(query));
assert_eq!(ids("@app:slack"), vec!["fl_slack"]);
assert_eq!(ids("@app:com.tinyspeck.slackmacgap"), vec!["fl_slack"]);
assert_eq!(ids("@app:Fieldwork"), vec!["fl_node"]);
assert_eq!(ids("@app:/opt/homebrew/bin/node"), vec!["fl_node"]);
assert_eq!(ids("@host:*.github.com"), vec!["fl_daemon", "fl_node"]);
assert_eq!(ids("@host:api.github.com"), vec!["fl_node"]);
assert_eq!(ids("@is:system"), vec!["fl_daemon"]);
assert_eq!(ids("-@is:system"), vec!["fl_node", "fl_none", "fl_slack"]);
// Groups, as two sidebar rows under different apps write them, each judged whole:
// flattened, the apps and hosts would pair across rows and match both flows.
let group = |a: &str, n: &str| FlowFilter {
app: Some(vec![a.into()]),
under: Some(vec![n.into()]),
..Default::default()
};
let crossed = FlowFilter {
any: Some(vec![
group("fieldwork", "github.com"),
group("nsurlsessiond", "api.github.com"),
]),
..Default::default()
};
assert_eq!(ids(crossed.clone()), Vec::<String>::new());
assert_eq!(page(&db, &crossed).unwrap().2, Some(0));
let flattened = FlowFilter {
app: Some(vec!["fieldwork".into(), "nsurlsessiond".into()]),
under: Some(vec!["github.com".into(), "api.github.com".into()]),
..Default::default()
};
let crossed = "@app:fieldwork @host:github.com OR @app:nsurlsessiond @host:api.github.com";
assert_eq!(ids(crossed), Vec::<String>::new());
assert_eq!(page(&db, &FlowFilter::search(crossed)).unwrap().2, Some(0));
let flattened = "(@app:fieldwork OR @app:nsurlsessiond) \
(@host:github.com OR @host:api.github.com)";
assert_eq!(ids(flattened), vec!["fl_daemon", "fl_node"]);
let either = FlowFilter {
any: Some(vec![
group("fieldwork", "api.github.com"),
FlowFilter { app: Some(vec!["slack".into()]), ..Default::default() },
]),
q: Some("https".into()),
..Default::default()
};
let either = "(@app:fieldwork @host:api.github.com OR @app:slack) https";
assert_eq!(ids(either), vec!["fl_node", "fl_slack"]);
// Hiding System leaves out its daemons, unless the search names them.
let hidden = |query: &str| {
let filter = FlowFilter { hide_system: true, ..FlowFilter::search(query) };
let mut ids: Vec<String> =
list(&db, &filter).unwrap().into_iter().map(|f| f.id).collect();
ids.sort();
ids
};
assert_eq!(hidden(""), vec!["fl_node", "fl_none", "fl_slack"]);
assert_eq!(hidden("@host:*.github.com"), vec!["fl_node"]);
assert_eq!(hidden("@is:system"), vec!["fl_daemon"]);
assert_eq!(hidden("@host:*.github.com OR @is:SYSTEM"), vec!["fl_daemon", "fl_node"]);
}
#[test]
@@ -1603,35 +1366,24 @@ mod tests {
assert!(!is_system(None, None));
assert_eq!(folder_name(Some("/Users/me/src/fieldwork/")), Some("fieldwork"));
assert_eq!(folder_name(Some("/")), None);
assert!(under_site("https://api.github.com:8443/x", "github.com"));
assert!(!under_site("https://notgithub.com/", "github.com"));
}
#[test]
fn query_round_trip() {
let filter = FlowFilter {
q: Some("post json".into()),
host: Some("api.stripe.com".into()),
under: Some(vec!["api.stripe.com/v1".into(), "example.com".into()]),
app: Some(vec!["com.google.Chrome".into()]),
status: Some("4xx".into()),
query: Some("post @status:4xx".into()),
hide_system: true,
limit: Some(20),
..Default::default()
};
assert_eq!(
filter.to_query(),
"q=post+json&host=api.stripe.com&under=api.stripe.com%2Fv1&under=example.com\
&app=com.google.Chrome&status=4xx&limit=20"
);
assert_eq!(filter.to_query(), "query=post+%40status%3A4xx&hide_system=true&limit=20");
assert_eq!(FlowFilter::from_query(&filter.to_query()).unwrap(), filter);
assert_eq!(FlowFilter::from_query("").unwrap(), FlowFilter::default());
assert!(FlowFilter::from_query("limit=lots").is_err());
let tunnelled = FlowFilter { tunnelled: Some(true), ..Default::default() };
assert_eq!(tunnelled.to_query(), "tunnelled=true");
assert_eq!(FlowFilter::from_query("tunnelled=true").unwrap(), tunnelled);
assert!(tunnelled.narrows());
assert!(FlowFilter::from_query("tunnelled=maybe").is_err());
assert!(FlowFilter::from_query("hide_system=maybe").is_err());
assert!(filter.narrows());
assert!(FlowFilter { hide_system: true, ..Default::default() }.narrows());
assert!(!FlowFilter::search(" ").narrows());
}
#[test]
+200 -9
View File
@@ -14,7 +14,9 @@ pub mod helper;
pub mod macos;
pub mod messages;
pub mod models;
pub mod query;
pub mod save;
pub mod saved_queries;
pub mod settings;
pub mod terminal;
@@ -47,7 +49,7 @@ use std::time::{Duration, SystemTime};
use ts_rs::TS;
pub use yaak_crypto::MasterKeySource;
use yaak_crypto::data_key::DataKey;
use yaak_database::{ModelChangeEvent, UpdateSource, UpsertModelInfo};
use yaak_database::{UpdateSource, UpsertModelInfo};
pub use yaak_grpc::GrpcConfig;
use yaak_models::blob_manager::{BlobManager, BodyChunkWriter};
use yaak_proxy::{CaStore, KeyCipher, ProxyConfig, ProxyEvent, ProxyHandle};
@@ -107,6 +109,8 @@ pub struct ProxyCtx {
pub events: RpcEventEmitter,
/// gRPC schemas as decoded, and the protoc that compiles attached protos.
pub grpc: GrpcState,
/// The search the app shows, which written flows' events are matched against.
live_search: Arc<query::LiveSearch>,
}
impl ProxyCtx {
@@ -148,6 +152,7 @@ impl ProxyCtx {
sweeper: Default::default(),
events,
grpc: GrpcState::default(),
live_search: Default::default(),
};
if db::needs_encrypting(data_dir) {
let unlocking = ctx.clone();
@@ -418,6 +423,7 @@ impl ProxyCtx {
db: self.db()?,
blobs: self.blobs()?,
events: self.events.clone(),
live_search: self.live_search.clone(),
attribution,
recording: self.recording.clone(),
finished: Default::default(),
@@ -1118,16 +1124,38 @@ impl ProxyCtx {
}
let (flow, created) = tx.upsert(&flow, &UpdateSource::Background)?;
// Sent before the commit, so it cannot reach the app after a clear that follows.
self.events.emit(
"model_write",
&ModelPayload { model: flow, change: ModelChangeEvent::Upsert { created } },
);
self.events.emit("model_write", &self.live_search.payload(flow, created));
Ok::<_, yaak_database::Error>(())
})
.map_err(|e| format!("Failed to record the saved request on the flow: {e}"))?;
Ok(saved)
}
/// Records that an agent read the flow through the control API, for `@is:read`, and tells
/// the app. Only the first read writes anything.
pub fn note_agent_read(&self, flow_id: &str) {
let noted = self.db().map_err(String::from).and_then(|db| {
db.with_tx(|tx| {
let conn = tx.conn().resolve();
let changed = conn.execute(
"UPDATE flows SET agent_read = TRUE WHERE id = ? AND NOT agent_read",
[flow_id],
)?;
// Sent before the commit, as every flow's event is.
if changed > 0
&& let Some(flow) = flows::find_in(conn, flow_id)
{
self.events.emit("model_write", &self.live_search.payload(flow, false));
}
Ok::<_, yaak_database::Error>(())
})
.map_err(|e| e.to_string())
});
if let Err(e) = noted {
warn!("Failed to note that an agent read flow {flow_id}: {e}");
}
}
/// Clears every flow at once and tells the app so its list empties, then deletes them and
/// their bodies in the background. Returns how many there were.
pub fn clear_flows(&self) -> Result<usize, String> {
@@ -1385,6 +1413,91 @@ pub struct ListFlowsResponse {
pub matching: Option<usize>,
}
#[derive(Deserialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct QuerySchemaRequest {}
/// The search's fields, for the search box's highlighting and completion.
#[derive(Serialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct QuerySchemaResponse {
#[ts(type = "import(\"@yaakapp-internal/query\").Schema")]
pub schema: serde_json::Value,
}
#[derive(Deserialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct ValidateQueryRequest {
pub query: String,
}
#[derive(Deserialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct QueryValuesRequest {
/// A field's path: `host`, `header` for header names, `header.accept` for a header's values.
pub path: String,
}
/// The search the app shows, which flows written from now on are matched against, their events
/// naming `key`. No query stops it.
#[derive(Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct WatchQueryRequest {
pub key: String,
#[ts(optional)]
pub query: Option<String>,
#[serde(default)]
#[ts(optional, as = "Option<bool>")]
pub hide_system: bool,
}
/// Which of some flows a search covers, for flows whose events were matched against another.
#[derive(Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct MatchFlowsRequest {
pub filter: flows::FlowFilter,
pub ids: Vec<String>,
}
#[derive(Deserialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct ListSavedQueriesRequest {}
#[derive(Deserialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct CreateSavedQueryRequest {
pub name: String,
pub query: String,
}
#[derive(Deserialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct RenameSavedQueryRequest {
pub id: String,
pub name: String,
}
#[derive(Deserialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct DeleteSavedQueryRequest {
pub id: String,
}
#[derive(Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct CountSavedQueriesRequest {
#[serde(default)]
#[ts(optional, as = "Option<bool>")]
pub hide_system: bool,
}
#[derive(Serialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct SavedQueriesChangedPayload {}
#[derive(Deserialize, TS)]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct ClearFlowsRequest {}
@@ -2326,6 +2439,73 @@ async fn clear_flows(ctx: ProxyCtx, _req: ClearFlowsRequest) -> Result<usize, Rp
blocking(move || ctx.clear_flows()).await
}
fn query_schema(
_ctx: &ProxyCtx,
_req: QuerySchemaRequest,
) -> Result<QuerySchemaResponse, RpcError> {
let schema = serde_json::to_value(query::schema()).map_err(rpc_error)?;
Ok(QuerySchemaResponse { schema })
}
fn validate_query(
_ctx: &ProxyCtx,
req: ValidateQueryRequest,
) -> Result<Vec<query::QueryDiagnostic>, RpcError> {
Ok(query::diagnostics(&req.query))
}
async fn query_values(ctx: ProxyCtx, req: QueryValuesRequest) -> Result<Vec<String>, RpcError> {
blocking(move || query::seen_values(&ctx.db()?, &req.path)).await
}
fn watch_query(ctx: &ProxyCtx, req: WatchQueryRequest) -> Result<bool, RpcError> {
let watched = req.query.filter(|q| !q.trim().is_empty() || req.hide_system);
ctx.live_search.watch(watched.map(|q| (req.key, q, req.hide_system)));
Ok(true)
}
async fn match_flows(ctx: ProxyCtx, req: MatchFlowsRequest) -> Result<Vec<String>, RpcError> {
blocking(move || flows::matching_ids(&ctx.db()?, &req.filter, &req.ids)).await
}
fn list_saved_queries(
ctx: &ProxyCtx,
_req: ListSavedQueriesRequest,
) -> Result<Vec<saved_queries::SavedQuery>, RpcError> {
saved_queries::list(&ctx.db()?).map_err(rpc_error)
}
fn create_saved_query(
ctx: &ProxyCtx,
req: CreateSavedQueryRequest,
) -> Result<saved_queries::SavedQuery, RpcError> {
let saved = saved_queries::create(&ctx.db()?, &req.name, &req.query).map_err(rpc_error)?;
ctx.events.emit("saved_queries_changed", &SavedQueriesChangedPayload {});
Ok(saved)
}
fn rename_saved_query(
ctx: &ProxyCtx,
req: RenameSavedQueryRequest,
) -> Result<saved_queries::SavedQuery, RpcError> {
let saved = saved_queries::rename(&ctx.db()?, &req.id, &req.name).map_err(rpc_error)?;
ctx.events.emit("saved_queries_changed", &SavedQueriesChangedPayload {});
Ok(saved)
}
fn delete_saved_query(ctx: &ProxyCtx, req: DeleteSavedQueryRequest) -> Result<bool, RpcError> {
let deleted = saved_queries::delete(&ctx.db()?, &req.id).map_err(rpc_error)?;
ctx.events.emit("saved_queries_changed", &SavedQueriesChangedPayload {});
Ok(deleted)
}
async fn count_saved_queries(
ctx: ProxyCtx,
req: CountSavedQueriesRequest,
) -> Result<Vec<saved_queries::SavedQueryCount>, RpcError> {
blocking(move || saved_queries::counts(&ctx.db()?, req.hide_system)).await
}
// -- Event loop --
/// Why the engine cannot be taken over from another app.
@@ -2429,6 +2609,8 @@ struct FlowSink {
db: ProxyQueryManager,
blobs: BlobManager,
events: RpcEventEmitter,
/// The search the app shows, which each flow written is matched against.
live_search: Arc<query::LiveSearch>,
attribution: Attribution,
recording: Arc<Mutex<RecordingLog>>,
/// Flows that have ended, waiting to be written together.
@@ -3076,10 +3258,7 @@ impl FlowSink {
let (saved, created) = ctx.upsert(&flow, &UpdateSource::Background)?;
// Sent while this holds the writer, before the commit, so a clear's event, sent after
// its own commit, always comes after it.
self.events.emit(
"model_write",
&ModelPayload { model: saved, change: ModelChangeEvent::Upsert { created } },
);
self.events.emit("model_write", &self.live_search.payload(saved, created));
Ok(true)
}
@@ -3142,6 +3321,13 @@ define_rpc! {
get_message_payload(GetMessagePayloadRequest) -> GetMessagePayloadResponse,
list_actions(ListActionsRequest) -> ListActionsResponse,
list_flows(flows::FlowFilter) -> ListFlowsResponse,
query_schema(QuerySchemaRequest) -> QuerySchemaResponse,
validate_query(ValidateQueryRequest) -> Vec<query::QueryDiagnostic>,
watch_query(WatchQueryRequest) -> bool,
list_saved_queries(ListSavedQueriesRequest) -> Vec<saved_queries::SavedQuery>,
create_saved_query(CreateSavedQueryRequest) -> saved_queries::SavedQuery,
rename_saved_query(RenameSavedQueryRequest) -> saved_queries::SavedQuery,
delete_saved_query(DeleteSavedQueryRequest) -> bool,
list_grpc_schemas(ListGrpcSchemasRequest) -> Vec<GrpcSchema>,
list_messages(ListMessagesRequest) -> ListMessagesResponse,
remove_grpc_schema(RemoveGrpcSchemaRequest) -> bool,
@@ -3179,6 +3365,9 @@ define_rpc! {
get_storage_size(GetStorageSizeRequest) -> GetStorageSizeResponse,
reset_captured_data(ResetCapturedDataRequest) -> bool,
open_terminal(OpenTerminalRequest) -> bool,
query_values(QueryValuesRequest) -> Vec<String>,
match_flows(MatchFlowsRequest) -> Vec<String>,
count_saved_queries(CountSavedQueriesRequest) -> Vec<saved_queries::SavedQueryCount>,
}
events {
decrypt_list_changed(DecryptListChangedPayload),
@@ -3187,6 +3376,7 @@ define_rpc! {
message_write(MessagePayload),
model_write(ModelPayload),
proxy_state_changed(ProxyStatePayload),
saved_queries_changed(SavedQueriesChangedPayload),
}
}
@@ -3256,6 +3446,7 @@ mod tests {
db: ctx.db().unwrap(),
blobs: ctx.blobs().unwrap(),
events: ctx.events.clone(),
live_search: ctx.live_search.clone(),
attribution: Attribution::new(0),
recording: ctx.recording.clone(),
finished: Default::default(),
+19
View File
@@ -107,6 +107,9 @@ pub struct Flow {
/// client rejected the proxy's certificate: method `CONNECT`, its URL the host, its sizes
/// the bytes each way, and no headers or bodies. Open until `elapsedMs` or `error` is set.
pub tunnelled: bool,
/// An agent read the flow through the control API: its detail, a body or its messages.
/// Never written by an upsert, which keeps what is stored.
pub agent_read: bool,
}
/// The streaming protocol a flow carries.
@@ -194,6 +197,21 @@ pub fn find_header<'a>(headers: &'a [ProxyHeader], name: &str) -> Option<&'a str
pub struct ModelPayload {
pub model: Flow,
pub change: ModelChangeEvent,
/// Whether the flow belongs in the list of the search the app shows, decided as the event
/// is sent. None while no search is shown.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub search: Option<SearchMatch>,
}
/// A written flow, matched against the search the app shows.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "gen_models.ts")]
pub struct SearchMatch {
/// The search's key, as the app gave it with the search.
pub key: String,
pub matches: bool,
}
impl UpsertModelInfo for Flow {
@@ -357,6 +375,7 @@ impl UpsertModelInfo for Flow {
grpc_status: added_column(r, "grpc_status")?,
grpc_message: added_column(r, "grpc_message")?,
tunnelled: added_column(r, "tunnelled")?.unwrap_or_default(),
agent_read: added_column(r, "agent_read")?.unwrap_or_default(),
})
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,181 @@
//! Searches saved by name, which the sidebar lists with a count each.
use crate::db::ProxyQueryManager;
use crate::flows::{self, FlowFilter};
use serde::{Deserialize, Serialize};
use ts_rs::TS;
use yaak_database::generate_prefixed_id;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "gen_models.ts")]
pub struct SavedQuery {
pub id: String,
pub name: String,
/// The search, as the search box takes it.
pub query: String,
/// Where it sits in the list, lowest first.
#[ts(type = "number")]
pub sort_order: i64,
}
/// How many flows a saved query covers.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export, export_to = "gen_rpc.ts")]
pub struct SavedQueryCount {
pub id: String,
#[ts(type = "number")]
pub count: usize,
}
fn failed(what: &str, e: impl std::fmt::Display) -> String {
format!("Failed to {what} the saved search: {e}")
}
fn read(row: &rusqlite::Row) -> rusqlite::Result<SavedQuery> {
Ok(SavedQuery {
id: row.get("id")?,
name: row.get("name")?,
query: row.get("query")?,
sort_order: row.get("sort_order")?,
})
}
/// Every saved query, in order. None for a database from before them, read without migrating.
pub fn list(db: &ProxyQueryManager) -> Result<Vec<SavedQuery>, String> {
db.with_conn(|c| {
let conn = c.conn().resolve();
let Ok(mut stmt) =
conn.prepare("SELECT * FROM saved_queries ORDER BY sort_order, created_at, id")
else {
return Ok(Vec::new());
};
stmt.query_map([], read)
.and_then(|rows| rows.collect::<rusqlite::Result<Vec<_>>>())
.map_err(|e| failed("list", e))
})
}
fn name_of(name: &str) -> Result<String, String> {
let name = name.trim();
if name.is_empty() {
return Err("A saved search needs a name".into());
}
Ok(name.to_string())
}
/// Saves `query` as `name`, after the others.
pub fn create(db: &ProxyQueryManager, name: &str, query: &str) -> Result<SavedQuery, String> {
let name = name_of(name)?;
let query = query.trim();
if query.is_empty() {
return Err("Nothing to save".into());
}
let id = generate_prefixed_id("sq");
db.with_tx(|c| {
let conn = c.conn().resolve();
conn.query_row(
"INSERT INTO saved_queries (id, name, query, sort_order) \
VALUES (?1, ?2, ?3, (SELECT coalesce(max(sort_order), -1) + 1 FROM saved_queries)) \
RETURNING *",
rusqlite::params![id, name, query],
read,
)
.map_err(yaak_database::Error::from)
})
.map_err(|e| failed("save", e))
}
pub fn rename(db: &ProxyQueryManager, id: &str, name: &str) -> Result<SavedQuery, String> {
let name = name_of(name)?;
db.with_tx(|c| {
c.conn()
.resolve()
.query_row(
"UPDATE saved_queries SET name = ?2, updated_at = CURRENT_TIMESTAMP WHERE id = ?1 \
RETURNING *",
rusqlite::params![id, name],
read,
)
.map_err(yaak_database::Error::from)
})
.map_err(|e| failed("rename", e))
}
/// Deletes one. False when there was none.
pub fn delete(db: &ProxyQueryManager, id: &str) -> Result<bool, String> {
db.with_tx(|c| {
c.conn()
.resolve()
.execute("DELETE FROM saved_queries WHERE id = ?", [id])
.map(|n| n > 0)
.map_err(yaak_database::Error::from)
})
.map_err(|e| failed("delete", e))
}
/// Each saved query's count of flows, with System's left out when `hide_system`, as the list
/// would show it, read from one snapshot.
pub fn counts(db: &ProxyQueryManager, hide_system: bool) -> Result<Vec<SavedQueryCount>, String> {
let saved = list(db)?;
let filters: Vec<FlowFilter> = saved
.iter()
.map(|s| FlowFilter { hide_system, ..FlowFilter::search(s.query.clone()) })
.collect();
let counts = flows::count_each(db, &filters)?;
Ok(saved
.into_iter()
.zip(counts)
.map(|(s, count)| SavedQueryCount { id: s.id, count })
.collect())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn saves_renames_counts_and_deletes() {
use crate::models::Flow;
use yaak_database::UpdateSource;
let dir = tempfile::tempdir().unwrap();
let db =
ProxyQueryManager::new(&dir.path().join("proxy.db"), &crate::db::test_key()).unwrap();
for (i, status) in [200, 404, 500].into_iter().enumerate() {
let flow = Flow {
id: format!("fl_{i}"),
url: format!("https://example.com/{i}"),
status: Some(status),
..Default::default()
};
db.with_tx(|tx| tx.upsert(&flow, &UpdateSource::Background)).unwrap();
}
let errors = create(&db, " Errors ", "@status:>=400").unwrap();
let ok = create(&db, "OK", "@status:2xx").unwrap();
assert_eq!((errors.name.as_str(), errors.sort_order, ok.sort_order), ("Errors", 0, 1));
assert!(create(&db, " ", "x").is_err());
assert!(create(&db, "x", " ").is_err());
let renamed = rename(&db, &ok.id, "Fine").unwrap();
assert_eq!(renamed.name, "Fine");
assert_eq!(
list(&db).unwrap().iter().map(|s| s.name.as_str()).collect::<Vec<_>>(),
["Errors", "Fine"]
);
let counts = counts(&db, false).unwrap();
assert_eq!(
counts,
vec![
SavedQueryCount { id: errors.id.clone(), count: 2 },
SavedQueryCount { id: ok.id.clone(), count: 1 },
]
);
assert!(delete(&db, &errors.id).unwrap());
assert!(!delete(&db, &errors.id).unwrap());
assert_eq!(list(&db).unwrap(), vec![renamed]);
}
}