mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-05-13 19:30:29 +02:00
Merge main into proxy foundation
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
use crate::PluginContextExt;
|
||||
use crate::error::Result;
|
||||
use crate::error::{Error, Result};
|
||||
use crate::models_ext::QueryManagerExt;
|
||||
use log::info;
|
||||
use std::collections::BTreeMap;
|
||||
use std::fs::read_to_string;
|
||||
use std::io::ErrorKind;
|
||||
use tauri::{Manager, Runtime, WebviewWindow};
|
||||
use yaak_core::WorkspaceContext;
|
||||
use yaak_models::models::{
|
||||
@@ -18,8 +19,7 @@ pub(crate) async fn import_data<R: Runtime>(
|
||||
file_path: &str,
|
||||
) -> Result<BatchUpsertResult> {
|
||||
let plugin_manager = window.state::<PluginManager>();
|
||||
let file =
|
||||
read_to_string(file_path).unwrap_or_else(|_| panic!("Unable to read file {}", file_path));
|
||||
let file = read_import_file(file_path)?;
|
||||
let file_contents = file.as_str();
|
||||
let import_result = plugin_manager.import_data(&window.plugin_context(), file_contents).await?;
|
||||
|
||||
@@ -127,3 +127,41 @@ pub(crate) async fn import_data<R: Runtime>(
|
||||
|
||||
Ok(upserted)
|
||||
}
|
||||
|
||||
fn read_import_file(file_path: &str) -> Result<String> {
|
||||
read_to_string(file_path).map_err(|err| {
|
||||
if err.kind() == ErrorKind::InvalidData {
|
||||
Error::GenericError(format!(
|
||||
"Import file must be UTF-8 text; binary files are not supported: {file_path}"
|
||||
))
|
||||
} else {
|
||||
Error::GenericError(format!("Unable to read import file {file_path}: {err}"))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs::{remove_file, write};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
#[test]
|
||||
fn read_import_file_returns_error_for_binary_file() {
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"yaak-import-binary-{}.pftrace",
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("system time before unix epoch")
|
||||
.as_nanos()
|
||||
));
|
||||
write(&path, [0xff, 0xfe, 0xfd]).expect("write binary fixture");
|
||||
|
||||
let err = read_import_file(path.to_str().expect("temp path is utf-8"))
|
||||
.expect_err("binary import should return an error");
|
||||
|
||||
assert!(err.to_string().contains("binary files are not supported"));
|
||||
|
||||
remove_file(path).expect("remove binary fixture");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,8 +34,7 @@ use tokio::time;
|
||||
use yaak_common::command::new_checked_command;
|
||||
use yaak_crypto::manager::EncryptionManager;
|
||||
use yaak_grpc::manager::{GrpcConfig, GrpcHandle};
|
||||
use yaak_templates::strip_json_comments::strip_json_comments;
|
||||
use yaak_grpc::{Code, ServiceDefinition, serialize_message};
|
||||
use yaak_grpc::{Code, ServiceDefinition};
|
||||
use yaak_mac_window::AppHandleMacWindowExt;
|
||||
use yaak_models::models::{
|
||||
AnyModel, CookieJar, Environment, GrpcConnection, GrpcConnectionState, GrpcEvent,
|
||||
@@ -60,6 +59,7 @@ use yaak_plugins::template_callback::PluginTemplateCallback;
|
||||
use yaak_sse::sse::ServerSentEvent;
|
||||
use yaak_tauri_utils::window::WorkspaceWindowTrait;
|
||||
use yaak_templates::format_json::format_json;
|
||||
use yaak_templates::strip_json_comments::strip_json_comments;
|
||||
use yaak_templates::{RenderErrorBehavior, RenderOptions, Tokens, transform_args};
|
||||
use yaak_tls::find_client_certificate;
|
||||
|
||||
@@ -591,7 +591,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
&method,
|
||||
in_msg_stream,
|
||||
&metadata,
|
||||
client_cert,
|
||||
client_cert.clone(),
|
||||
on_message.clone(),
|
||||
)
|
||||
.await,
|
||||
@@ -607,7 +607,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
&method,
|
||||
in_msg_stream,
|
||||
&metadata,
|
||||
client_cert,
|
||||
client_cert.clone(),
|
||||
on_message.clone(),
|
||||
)
|
||||
.await,
|
||||
@@ -620,7 +620,9 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
(false, false) => (
|
||||
None,
|
||||
Some(
|
||||
connection.unary(&service, &method, &msg, &metadata, client_cert).await,
|
||||
connection
|
||||
.unary(&service, &method, &msg, &metadata, client_cert.clone())
|
||||
.await,
|
||||
),
|
||||
),
|
||||
};
|
||||
@@ -658,11 +660,34 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
&UpdateSource::from_window_label(window.label()),
|
||||
)
|
||||
.unwrap();
|
||||
let response_message = msg.into_inner();
|
||||
let content = match connection
|
||||
.serialize_message(&response_message, &metadata, client_cert.clone())
|
||||
.await
|
||||
{
|
||||
Ok(content) => content,
|
||||
Err(err) => {
|
||||
app_handle
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
content: "Failed to read response".to_string(),
|
||||
error: Some(err.to_string()),
|
||||
status: Some(Code::Internal as i32),
|
||||
event_type: GrpcEventType::ConnectionEnd,
|
||||
..base_event.clone()
|
||||
},
|
||||
&UpdateSource::from_window_label(window.label()),
|
||||
)
|
||||
.unwrap();
|
||||
return;
|
||||
}
|
||||
};
|
||||
app_handle
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
content: serialize_message(&msg.into_inner()).unwrap(),
|
||||
content,
|
||||
event_type: GrpcEventType::ServerMessage,
|
||||
..base_event.clone()
|
||||
},
|
||||
@@ -797,7 +822,28 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
loop {
|
||||
match stream.message().await {
|
||||
Ok(Some(msg)) => {
|
||||
let message = serialize_message(&msg).unwrap();
|
||||
let message = match connection
|
||||
.serialize_message(&msg, &metadata, client_cert.clone())
|
||||
.await
|
||||
{
|
||||
Ok(message) => message,
|
||||
Err(err) => {
|
||||
app_handle
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
content: "Failed to read response".to_string(),
|
||||
error: Some(err.to_string()),
|
||||
status: Some(Code::Internal as i32),
|
||||
event_type: GrpcEventType::ConnectionEnd,
|
||||
..base_event.clone()
|
||||
},
|
||||
&UpdateSource::from_window_label(window.label()),
|
||||
)
|
||||
.unwrap();
|
||||
break;
|
||||
}
|
||||
};
|
||||
app_handle
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
|
||||
@@ -19,6 +19,7 @@ use yaak::plugin_events::{
|
||||
GroupedPluginEvent, HostRequest, SharedPluginEventContext, handle_shared_plugin_event,
|
||||
};
|
||||
use yaak_crypto::manager::EncryptionManager;
|
||||
use yaak_http::cookies::get_cookie_value_from_jar;
|
||||
use yaak_models::models::{HttpResponse, Plugin};
|
||||
use yaak_models::queries::any_request::AnyRequest;
|
||||
use yaak_models::util::UpdateSource;
|
||||
@@ -422,12 +423,7 @@ async fn handle_host_plugin_request<R: Runtime>(
|
||||
let window = get_window_from_plugin_context(app_handle, plugin_context)?;
|
||||
let value = match cookie_jar_from_window(&window) {
|
||||
None => None,
|
||||
Some(j) => j.cookies.into_iter().find_map(|c| match Cookie::parse(c.raw_cookie) {
|
||||
Ok(c) if c.name().to_string().eq(&req.name) => {
|
||||
Some(c.value_trimmed().to_string())
|
||||
}
|
||||
_ => None,
|
||||
}),
|
||||
Some(j) => get_cookie_value_from_jar(j.cookies, &req.name, req.domain.as_deref()),
|
||||
};
|
||||
Ok(Some(InternalEventPayload::GetCookieValueResponse(GetCookieValueResponse { value })))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user